|
30 | 30 | import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
31 | 31 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
32 | 32 | import com.google.api.client.http.HttpResponseException; |
| 33 | +import com.google.api.gax.rpc.FixedHeaderProvider; |
33 | 34 | import com.google.cloud.storage.FakeHttpServer.HttpRequestHandler; |
34 | 35 | import com.google.cloud.storage.it.ChecksummedTestContent; |
35 | 36 | import com.google.cloud.storage.it.runner.StorageITRunner; |
@@ -136,6 +137,91 @@ public void sendCreateMultipartUploadRequest_success() throws Exception { |
136 | 137 | } |
137 | 138 | } |
138 | 139 |
|
| 140 | + @Test |
| 141 | + public void createFrom_withExistingUserAgent() throws Exception { |
| 142 | + HttpRequestHandler handler = |
| 143 | + req -> { |
| 144 | + boolean hasCustom = |
| 145 | + req.headers().getAll("User-Agent").stream() |
| 146 | + .anyMatch(agent -> agent.contains("my-custom-agent")); |
| 147 | + assertThat(hasCustom).isTrue(); |
| 148 | + // check that it does not also contain the gcloud-java generated header |
| 149 | + boolean hasDefault = |
| 150 | + req.headers().getAll("User-Agent").stream() |
| 151 | + .anyMatch(agent -> agent.contains("gcloud-java/")); |
| 152 | + assertThat(hasDefault).isFalse(); |
| 153 | + |
| 154 | + CreateMultipartUploadResponse response = |
| 155 | + CreateMultipartUploadResponse.builder() |
| 156 | + .bucket("test-bucket") |
| 157 | + .key("test-key") |
| 158 | + .uploadId("test-upload-id") |
| 159 | + .build(); |
| 160 | + ByteBuf buf = Unpooled.wrappedBuffer(xmlMapper.writeValueAsBytes(response)); |
| 161 | + |
| 162 | + DefaultFullHttpResponse resp = |
| 163 | + new DefaultFullHttpResponse(req.protocolVersion(), OK, buf); |
| 164 | + resp.headers().set(CONTENT_TYPE, "application/xml; charset=utf-8"); |
| 165 | + return resp; |
| 166 | + }; |
| 167 | + |
| 168 | + try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) { |
| 169 | + HttpStorageOptions options = |
| 170 | + fakeHttpServer.getHttpStorageOptions().toBuilder() |
| 171 | + .setHeaderProvider( |
| 172 | + FixedHeaderProvider.create(ImmutableMap.of("User-Agent", "my-custom-agent"))) |
| 173 | + .build(); |
| 174 | + |
| 175 | + MultipartUploadHttpRequestManager multipartUploadHttpRequestManager = |
| 176 | + MultipartUploadHttpRequestManager.createFrom(options); |
| 177 | + CreateMultipartUploadRequest request = |
| 178 | + CreateMultipartUploadRequest.builder() |
| 179 | + .bucket("test-bucket") |
| 180 | + .key("test-key") |
| 181 | + .contentType("application/octet-stream") |
| 182 | + .build(); |
| 183 | + |
| 184 | + multipartUploadHttpRequestManager.sendCreateMultipartUploadRequest(request); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void createFrom_withoutExistingUserAgent() throws Exception { |
| 190 | + HttpRequestHandler handler = |
| 191 | + req -> { |
| 192 | + boolean hasDefault = |
| 193 | + req.headers().getAll("User-Agent").stream() |
| 194 | + .anyMatch(agent -> agent.startsWith("gcloud-java/")); |
| 195 | + assertThat(hasDefault).isTrue(); |
| 196 | + |
| 197 | + CreateMultipartUploadResponse response = |
| 198 | + CreateMultipartUploadResponse.builder() |
| 199 | + .bucket("test-bucket") |
| 200 | + .key("test-key") |
| 201 | + .uploadId("test-upload-id") |
| 202 | + .build(); |
| 203 | + ByteBuf buf = Unpooled.wrappedBuffer(xmlMapper.writeValueAsBytes(response)); |
| 204 | + |
| 205 | + DefaultFullHttpResponse resp = |
| 206 | + new DefaultFullHttpResponse(req.protocolVersion(), OK, buf); |
| 207 | + resp.headers().set(CONTENT_TYPE, "application/xml; charset=utf-8"); |
| 208 | + return resp; |
| 209 | + }; |
| 210 | + |
| 211 | + try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) { |
| 212 | + MultipartUploadHttpRequestManager multipartUploadHttpRequestManager = |
| 213 | + MultipartUploadHttpRequestManager.createFrom(fakeHttpServer.getHttpStorageOptions()); |
| 214 | + CreateMultipartUploadRequest request = |
| 215 | + CreateMultipartUploadRequest.builder() |
| 216 | + .bucket("test-bucket") |
| 217 | + .key("test-key") |
| 218 | + .contentType("application/octet-stream") |
| 219 | + .build(); |
| 220 | + |
| 221 | + multipartUploadHttpRequestManager.sendCreateMultipartUploadRequest(request); |
| 222 | + } |
| 223 | + } |
| 224 | + |
139 | 225 | @Test |
140 | 226 | public void sendCreateMultipartUploadRequest_error() throws Exception { |
141 | 227 | HttpRequestHandler handler = |
|
0 commit comments