|
61 | 61 | import io.grpc.netty.shaded.io.netty.handler.codec.http.DefaultFullHttpResponse; |
62 | 62 | import io.grpc.netty.shaded.io.netty.handler.codec.http.FullHttpRequest; |
63 | 63 | import io.grpc.netty.shaded.io.netty.handler.codec.http.FullHttpResponse; |
| 64 | +import com.google.api.gax.rpc.FixedHeaderProvider; |
64 | 65 | import io.grpc.netty.shaded.io.netty.handler.codec.http.HttpResponseStatus; |
65 | 66 | import java.nio.ByteBuffer; |
66 | 67 | import java.nio.charset.StandardCharsets; |
@@ -136,6 +137,87 @@ 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 = req.headers().getAll("User-Agent").stream() |
| 145 | + .anyMatch(agent -> agent.contains("my-custom-agent")); |
| 146 | + assertThat(hasCustom).isTrue(); |
| 147 | + // check that it does not also contain the gcloud-java generated header |
| 148 | + boolean hasDefault = req.headers().getAll("User-Agent").stream() |
| 149 | + .anyMatch(agent -> agent.contains("gcloud-java/")); |
| 150 | + assertThat(hasDefault).isFalse(); |
| 151 | + |
| 152 | + CreateMultipartUploadResponse response = |
| 153 | + CreateMultipartUploadResponse.builder() |
| 154 | + .bucket("test-bucket") |
| 155 | + .key("test-key") |
| 156 | + .uploadId("test-upload-id") |
| 157 | + .build(); |
| 158 | + ByteBuf buf = Unpooled.wrappedBuffer(xmlMapper.writeValueAsBytes(response)); |
| 159 | + |
| 160 | + DefaultFullHttpResponse resp = |
| 161 | + new DefaultFullHttpResponse(req.protocolVersion(), OK, buf); |
| 162 | + resp.headers().set(CONTENT_TYPE, "application/xml; charset=utf-8"); |
| 163 | + return resp; |
| 164 | + }; |
| 165 | + |
| 166 | + try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) { |
| 167 | + HttpStorageOptions options = |
| 168 | + fakeHttpServer.getHttpStorageOptions().toBuilder() |
| 169 | + .setHeaderProvider(FixedHeaderProvider.create(ImmutableMap.of("User-Agent", "my-custom-agent"))) |
| 170 | + .build(); |
| 171 | + |
| 172 | + MultipartUploadHttpRequestManager multipartUploadHttpRequestManager = |
| 173 | + MultipartUploadHttpRequestManager.createFrom(options); |
| 174 | + CreateMultipartUploadRequest request = |
| 175 | + CreateMultipartUploadRequest.builder() |
| 176 | + .bucket("test-bucket") |
| 177 | + .key("test-key") |
| 178 | + .contentType("application/octet-stream") |
| 179 | + .build(); |
| 180 | + |
| 181 | + multipartUploadHttpRequestManager.sendCreateMultipartUploadRequest(request); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void createFrom_withoutExistingUserAgent() throws Exception { |
| 187 | + HttpRequestHandler handler = |
| 188 | + req -> { |
| 189 | + boolean hasDefault = req.headers().getAll("User-Agent").stream() |
| 190 | + .anyMatch(agent -> agent.startsWith("gcloud-java/")); |
| 191 | + assertThat(hasDefault).isTrue(); |
| 192 | + |
| 193 | + CreateMultipartUploadResponse response = |
| 194 | + CreateMultipartUploadResponse.builder() |
| 195 | + .bucket("test-bucket") |
| 196 | + .key("test-key") |
| 197 | + .uploadId("test-upload-id") |
| 198 | + .build(); |
| 199 | + ByteBuf buf = Unpooled.wrappedBuffer(xmlMapper.writeValueAsBytes(response)); |
| 200 | + |
| 201 | + DefaultFullHttpResponse resp = |
| 202 | + new DefaultFullHttpResponse(req.protocolVersion(), OK, buf); |
| 203 | + resp.headers().set(CONTENT_TYPE, "application/xml; charset=utf-8"); |
| 204 | + return resp; |
| 205 | + }; |
| 206 | + |
| 207 | + try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) { |
| 208 | + MultipartUploadHttpRequestManager multipartUploadHttpRequestManager = |
| 209 | + MultipartUploadHttpRequestManager.createFrom(fakeHttpServer.getHttpStorageOptions()); |
| 210 | + CreateMultipartUploadRequest request = |
| 211 | + CreateMultipartUploadRequest.builder() |
| 212 | + .bucket("test-bucket") |
| 213 | + .key("test-key") |
| 214 | + .contentType("application/octet-stream") |
| 215 | + .build(); |
| 216 | + |
| 217 | + multipartUploadHttpRequestManager.sendCreateMultipartUploadRequest(request); |
| 218 | + } |
| 219 | + } |
| 220 | + |
139 | 221 | @Test |
140 | 222 | public void sendCreateMultipartUploadRequest_error() throws Exception { |
141 | 223 | HttpRequestHandler handler = |
|
0 commit comments