|
29 | 29 | */ |
30 | 30 | package com.google.api.gax.httpjson; |
31 | 31 |
|
| 32 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
32 | 33 | import static org.mockito.Mockito.mock; |
33 | 34 |
|
| 35 | +import com.google.api.client.http.ByteArrayContent; |
34 | 36 | import com.google.api.client.http.EmptyContent; |
35 | 37 | import com.google.api.client.http.HttpRequest; |
36 | 38 | import com.google.api.client.testing.http.MockHttpTransport; |
37 | 39 | import com.google.api.gax.tracing.ApiTracer; |
| 40 | +import com.google.api.pathtemplate.PathTemplate; |
38 | 41 | import com.google.common.truth.Truth; |
39 | 42 | import com.google.longrunning.ListOperationsRequest; |
40 | 43 | import com.google.protobuf.Empty; |
|
44 | 47 | import java.io.IOException; |
45 | 48 | import java.nio.charset.StandardCharsets; |
46 | 49 | import java.util.Arrays; |
| 50 | +import java.util.Collections; |
47 | 51 | import java.util.HashMap; |
48 | 52 | import java.util.LinkedHashMap; |
49 | 53 | import java.util.List; |
@@ -326,4 +330,156 @@ void testUpdateRunnableTimeout_shouldUpdate() throws IOException { |
326 | 330 | Truth.assertThat(httpRequest.getReadTimeout()).isEqualTo(30000L); |
327 | 331 | Truth.assertThat(httpRequest.getConnectTimeout()).isEqualTo(30000L); |
328 | 332 | } |
| 333 | + |
| 334 | + @Test |
| 335 | + void testResumableUploadChunkRequestFormatter() throws IOException { |
| 336 | + byte[] rawPayload = "binary \0 raw \1 payload".getBytes(StandardCharsets.UTF_8); |
| 337 | + ResumableUploadChunkRequestFormatter<Field> binaryRequestFormatter = |
| 338 | + new ResumableUploadChunkRequestFormatter<Field>() { |
| 339 | + @Override |
| 340 | + public Map<String, List<String>> getQueryParamNames(Field apiMessage) { |
| 341 | + return Collections.emptyMap(); |
| 342 | + } |
| 343 | + |
| 344 | + @Override |
| 345 | + public byte[] getBinaryRequestBody(Field apiMessage) { |
| 346 | + return rawPayload; |
| 347 | + } |
| 348 | + |
| 349 | + @Override |
| 350 | + public String getPath(Field apiMessage) { |
| 351 | + return "/upload"; |
| 352 | + } |
| 353 | + |
| 354 | + @Override |
| 355 | + public PathTemplate getPathTemplate() { |
| 356 | + return PathTemplate.create("{+path}"); |
| 357 | + } |
| 358 | + }; |
| 359 | + |
| 360 | + ApiMethodDescriptor<Field, Empty> methodDescriptor = |
| 361 | + ApiMethodDescriptor.<Field, Empty>newBuilder() |
| 362 | + .setFullMethodName("upload.binary") |
| 363 | + .setHttpMethod("POST") |
| 364 | + .setRequestFormatter(binaryRequestFormatter) |
| 365 | + .setResponseParser(responseParser) |
| 366 | + .build(); |
| 367 | + |
| 368 | + HttpRequestRunnable<Field, Empty> httpRequestRunnable = |
| 369 | + new HttpRequestRunnable<>( |
| 370 | + requestMessage, |
| 371 | + methodDescriptor, |
| 372 | + ENDPOINT, |
| 373 | + HttpJsonCallOptions.newBuilder().build(), |
| 374 | + new MockHttpTransport(), |
| 375 | + HttpJsonMetadata.newBuilder().build(), |
| 376 | + result -> {}); |
| 377 | + |
| 378 | + HttpRequest httpRequest = httpRequestRunnable.createHttpRequest(); |
| 379 | + Truth.assertThat(httpRequest.getContent()).isInstanceOf(ByteArrayContent.class); |
| 380 | + Truth.assertThat(httpRequest.getContent().getType()).isEqualTo("application/octet-stream"); |
| 381 | + Truth.assertThat(httpRequest.getContent().getLength()).isEqualTo(rawPayload.length); |
| 382 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { |
| 383 | + httpRequest.getContent().writeTo(out); |
| 384 | + Truth.assertThat(out.toByteArray()).isEqualTo(rawPayload); |
| 385 | + } |
| 386 | + assertThrows( |
| 387 | + UnsupportedOperationException.class, |
| 388 | + () -> binaryRequestFormatter.getRequestBody(requestMessage)); |
| 389 | + } |
| 390 | + |
| 391 | + @Test |
| 392 | + void testResumableUploadChunkRequestFormatter_emptyPayload() throws IOException { |
| 393 | + ResumableUploadChunkRequestFormatter<Field> emptyRequestFormatter = |
| 394 | + new ResumableUploadChunkRequestFormatter<Field>() { |
| 395 | + @Override |
| 396 | + public Map<String, List<String>> getQueryParamNames(Field apiMessage) { |
| 397 | + return Collections.emptyMap(); |
| 398 | + } |
| 399 | + |
| 400 | + @Override |
| 401 | + public byte[] getBinaryRequestBody(Field apiMessage) { |
| 402 | + return new byte[0]; |
| 403 | + } |
| 404 | + |
| 405 | + @Override |
| 406 | + public String getPath(Field apiMessage) { |
| 407 | + return "/upload"; |
| 408 | + } |
| 409 | + |
| 410 | + @Override |
| 411 | + public PathTemplate getPathTemplate() { |
| 412 | + return PathTemplate.create("{+path}"); |
| 413 | + } |
| 414 | + }; |
| 415 | + |
| 416 | + ApiMethodDescriptor<Field, Empty> methodDescriptor = |
| 417 | + ApiMethodDescriptor.<Field, Empty>newBuilder() |
| 418 | + .setFullMethodName("upload.empty") |
| 419 | + .setHttpMethod("POST") |
| 420 | + .setRequestFormatter(emptyRequestFormatter) |
| 421 | + .setResponseParser(responseParser) |
| 422 | + .build(); |
| 423 | + |
| 424 | + HttpRequestRunnable<Field, Empty> httpRequestRunnable = |
| 425 | + new HttpRequestRunnable<>( |
| 426 | + requestMessage, |
| 427 | + methodDescriptor, |
| 428 | + ENDPOINT, |
| 429 | + HttpJsonCallOptions.newBuilder().build(), |
| 430 | + new MockHttpTransport(), |
| 431 | + HttpJsonMetadata.newBuilder().build(), |
| 432 | + result -> {}); |
| 433 | + |
| 434 | + HttpRequest httpRequest = httpRequestRunnable.createHttpRequest(); |
| 435 | + Truth.assertThat(httpRequest.getContent()).isInstanceOf(EmptyContent.class); |
| 436 | + } |
| 437 | + |
| 438 | + @Test |
| 439 | + void testAbsoluteUrlSupport() throws IOException { |
| 440 | + String absoluteUrl = "https://custom-upload-host.googleapis.com/upload/session/123?sid=abc"; |
| 441 | + HttpRequestFormatter<Field> absoluteUrlFormatter = |
| 442 | + new HttpRequestFormatter<Field>() { |
| 443 | + @Override |
| 444 | + public Map<String, List<String>> getQueryParamNames(Field apiMessage) { |
| 445 | + return Collections.emptyMap(); |
| 446 | + } |
| 447 | + |
| 448 | + @Override |
| 449 | + public String getRequestBody(Field apiMessage) { |
| 450 | + return ""; |
| 451 | + } |
| 452 | + |
| 453 | + @Override |
| 454 | + public String getPath(Field apiMessage) { |
| 455 | + return absoluteUrl; |
| 456 | + } |
| 457 | + |
| 458 | + @Override |
| 459 | + public PathTemplate getPathTemplate() { |
| 460 | + return PathTemplate.create("{+path}"); |
| 461 | + } |
| 462 | + }; |
| 463 | + |
| 464 | + ApiMethodDescriptor<Field, Empty> methodDescriptor = |
| 465 | + ApiMethodDescriptor.<Field, Empty>newBuilder() |
| 466 | + .setFullMethodName("upload.absolute") |
| 467 | + .setHttpMethod("POST") |
| 468 | + .setRequestFormatter(absoluteUrlFormatter) |
| 469 | + .setResponseParser(responseParser) |
| 470 | + .build(); |
| 471 | + |
| 472 | + HttpRequestRunnable<Field, Empty> httpRequestRunnable = |
| 473 | + new HttpRequestRunnable<>( |
| 474 | + requestMessage, |
| 475 | + methodDescriptor, |
| 476 | + ENDPOINT, |
| 477 | + HttpJsonCallOptions.newBuilder().build(), |
| 478 | + new MockHttpTransport(), |
| 479 | + HttpJsonMetadata.newBuilder().build(), |
| 480 | + result -> {}); |
| 481 | + |
| 482 | + HttpRequest httpRequest = httpRequestRunnable.createHttpRequest(); |
| 483 | + Truth.assertThat(httpRequest.getUrl().build()).isEqualTo(absoluteUrl); |
| 484 | + } |
329 | 485 | } |
0 commit comments