|
10 | 10 | import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
11 | 11 | import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; |
12 | 12 | import static com.github.tomakehurst.wiremock.client.WireMock.get; |
13 | | -import static com.github.tomakehurst.wiremock.client.WireMock.notMatching; |
| 13 | +import static com.github.tomakehurst.wiremock.client.WireMock.matching; |
14 | 14 | import static com.github.tomakehurst.wiremock.client.WireMock.okJson; |
15 | 15 | import static com.github.tomakehurst.wiremock.client.WireMock.post; |
16 | 16 | import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; |
|
35 | 35 | import java.nio.file.Path; |
36 | 36 | import java.time.Duration; |
37 | 37 | import java.util.Collections; |
| 38 | +import java.util.HashMap; |
38 | 39 | import java.util.List; |
| 40 | +import java.util.Map; |
39 | 41 | import org.junit.jupiter.api.Test; |
40 | 42 | import org.junit.jupiter.api.extension.RegisterExtension; |
41 | 43 | import org.junit.jupiter.api.io.TempDir; |
@@ -223,13 +225,13 @@ void throwsIllegalArgumentWhenTheImportFileIsInvalid() { |
223 | 225 | } |
224 | 226 |
|
225 | 227 | @Test |
226 | | - void uploadsTheFileBytesWithoutDeepLHeaders() throws Exception { |
| 228 | + void uploadsTheFileBytesWithTheUserAgentAndWithoutAuthorization() throws Exception { |
227 | 229 | byte[] content = new byte[] {0x3c, (byte) 0xff, 0x00, (byte) 0x80, 0x3e}; |
228 | 230 | wm.stubFor( |
229 | 231 | put("/storage") |
230 | 232 | .withHeader("Content-Type", equalTo("application/xml")) |
231 | 233 | .withHeader("Authorization", absent()) |
232 | | - .withHeader("User-Agent", notMatching("deepl-java.*")) |
| 234 | + .withHeader("User-Agent", matching("deepl-java/.*")) |
233 | 235 | .withRequestBody(binaryEqualTo(content)) |
234 | 236 | .willReturn(aResponse().withStatus(200))); |
235 | 237 | client.uploadTranslationMemoryFile(storageUrl(), content, "application/xml"); |
@@ -454,17 +456,42 @@ void sendsTheIdOfATranslationMemoryObjectWhenCreatingAnExport() throws Exception |
454 | 456 | } |
455 | 457 |
|
456 | 458 | @Test |
457 | | - void downloadsTheExportToAFileWithoutDeepLHeaders() throws Exception { |
| 459 | + void downloadsTheExportToAFileWithTheUserAgentAndWithoutAuthorization() throws Exception { |
458 | 460 | wm.stubFor( |
459 | 461 | get("/storage/export") |
460 | 462 | .withHeader("Authorization", absent()) |
461 | | - .withHeader("User-Agent", notMatching("deepl-java.*")) |
| 463 | + .withHeader("User-Agent", matching("deepl-java/.*")) |
462 | 464 | .willReturn(aResponse().withStatus(200).withBody("<tmx/>"))); |
463 | 465 | File output = tempDir.resolve("export.tmx").toFile(); |
464 | 466 | client.downloadTranslationMemoryExport(completedExport(), output); |
465 | 467 | assertEquals("<tmx/>", Fixtures.readFile(output)); |
466 | 468 | } |
467 | 469 |
|
| 470 | + @Test |
| 471 | + void sendsOnlyTheCallersUserAgentToStorage() throws Exception { |
| 472 | + Map<String, String> headers = new HashMap<>(); |
| 473 | + headers.put("user-agent", "my-app/1.0"); |
| 474 | + headers.put("X-Custom", "v"); |
| 475 | + headers.put("Authorization", "Bearer t"); |
| 476 | + DeepLClient customClient = |
| 477 | + OfflineClient.createClient( |
| 478 | + wm, (DeepLClientOptions) new DeepLClientOptions().setHeaders(headers)); |
| 479 | + wm.stubFor( |
| 480 | + put("/storage") |
| 481 | + .withHeader("User-Agent", equalTo("my-app/1.0")) |
| 482 | + .withHeader("X-Custom", absent()) |
| 483 | + .withHeader("Authorization", absent()) |
| 484 | + .willReturn(aResponse().withStatus(200))); |
| 485 | + wm.stubFor( |
| 486 | + get("/storage/export") |
| 487 | + .withHeader("User-Agent", equalTo("my-app/1.0")) |
| 488 | + .withHeader("X-Custom", absent()) |
| 489 | + .withHeader("Authorization", absent()) |
| 490 | + .willReturn(aResponse().withStatus(200).withBody("<tmx/>"))); |
| 491 | + customClient.uploadTranslationMemoryFile(storageUrl(), new byte[] {0x3c}, "application/xml"); |
| 492 | + customClient.downloadTranslationMemoryExport(completedExport(), new ByteArrayOutputStream()); |
| 493 | + } |
| 494 | + |
468 | 495 | @Test |
469 | 496 | void downloadsTheExportToAStream() throws Exception { |
470 | 497 | byte[] bytes = new byte[] {0x00, (byte) 0xff, (byte) 0x80}; |
|
0 commit comments