|
3 | 3 | // license that can be found in the LICENSE file. |
4 | 4 | package com.deepl.api; |
5 | 5 |
|
| 6 | +import static org.mockito.Mockito.*; |
| 7 | + |
6 | 8 | import java.io.*; |
7 | 9 | import java.net.*; |
8 | 10 | import java.time.*; |
9 | 11 | import java.util.*; |
| 12 | +import java.util.stream.Stream; |
10 | 13 | import org.junit.jupiter.api.*; |
| 14 | +import org.junit.jupiter.params.ParameterizedTest; |
| 15 | +import org.junit.jupiter.params.provider.Arguments; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
| 17 | +import org.mockito.MockedConstruction; |
| 18 | +import org.mockito.Mockito; |
11 | 19 |
|
12 | 20 | class GeneralTest extends TestBase { |
13 | 21 |
|
@@ -237,4 +245,96 @@ void testUsageTeamDocumentLimit() throws Exception { |
237 | 245 | Assertions.assertNotNull(usage.getTeamDocument()); |
238 | 246 | Assertions.assertTrue(usage.getTeamDocument().limitReached()); |
239 | 247 | } |
| 248 | + |
| 249 | + @ParameterizedTest |
| 250 | + @MethodSource("provideUserAgentTestData") |
| 251 | + void testUserAgent( |
| 252 | + SessionOptions sessionOptions, |
| 253 | + TranslatorOptions translatorOptions, |
| 254 | + Iterable<String> requiredStrings, |
| 255 | + Iterable<String> blocklistedStrings) |
| 256 | + throws Exception { |
| 257 | + Map<String, String> headers = new HashMap<>(); |
| 258 | + HttpURLConnection con = Mockito.mock(HttpURLConnection.class); |
| 259 | + Mockito.doAnswer( |
| 260 | + invocation -> { |
| 261 | + String key = (String) invocation.getArgument(0); |
| 262 | + String value = (String) invocation.getArgument(1); |
| 263 | + headers.put(key, value); |
| 264 | + return null; |
| 265 | + }) |
| 266 | + .when(con) |
| 267 | + .setRequestProperty(Mockito.any(String.class), Mockito.any(String.class)); |
| 268 | + Mockito.when(con.getResponseCode()).thenReturn(200); |
| 269 | + try (MockedConstruction<URL> mockUrl = |
| 270 | + Mockito.mockConstruction( |
| 271 | + URL.class, |
| 272 | + (mock, context) -> { |
| 273 | + Mockito.when(mock.openConnection()).thenReturn(con); |
| 274 | + })) { |
| 275 | + Translator translator = createTranslator(sessionOptions, translatorOptions); |
| 276 | + Usage usage = translator.getUsage(); |
| 277 | + String userAgentHeader = headers.get("User-Agent"); |
| 278 | + for (String s : requiredStrings) { |
| 279 | + Assertions.assertTrue( |
| 280 | + userAgentHeader.contains(s), |
| 281 | + String.format( |
| 282 | + "Expected User-Agent header to contain %s\nActual:\n%s", s, userAgentHeader)); |
| 283 | + } |
| 284 | + for (String n : blocklistedStrings) { |
| 285 | + Assertions.assertFalse( |
| 286 | + userAgentHeader.contains(n), |
| 287 | + String.format( |
| 288 | + "Expected User-Agent header not to contain %s\nActual:\n%s", n, userAgentHeader)); |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + // Session options & Translator options: Used to construct the `Translator` |
| 294 | + // Next arg: List of Strings that must be contained in the user agent header |
| 295 | + // Last arg: List of Strings that must not be contained in the user agent header |
| 296 | + private static Stream<? extends Arguments> provideUserAgentTestData() { |
| 297 | + Map<String, String> testHeaders = new HashMap<>(); |
| 298 | + testHeaders.put("User-Agent", "my custom user agent"); |
| 299 | + Iterable<String> lightPlatformInfo = Arrays.asList("deepl-java/"); |
| 300 | + Iterable<String> lightPlatformInfoWithAppInfo = |
| 301 | + Arrays.asList("deepl", "my-java-translation-plugin/1.2.3"); |
| 302 | + Iterable<String> detailedPlatformInfo = Arrays.asList(" java/", "("); |
| 303 | + Iterable<String> detailedPlatformInfoWithAppInfo = |
| 304 | + Arrays.asList(" java/", "(", "my-java-translation-plugin/1.2.3"); |
| 305 | + Iterable<String> customUserAgent = Arrays.asList("my custom user agent"); |
| 306 | + Iterable<String> noStrings = new ArrayList<String>(); |
| 307 | + return Stream.of( |
| 308 | + Arguments.of( |
| 309 | + new SessionOptions(), new TranslatorOptions(), detailedPlatformInfo, noStrings), |
| 310 | + Arguments.of( |
| 311 | + new SessionOptions(), |
| 312 | + new TranslatorOptions().setSendPlatformInfo(false), |
| 313 | + lightPlatformInfo, |
| 314 | + detailedPlatformInfo), |
| 315 | + Arguments.of( |
| 316 | + new SessionOptions(), |
| 317 | + new TranslatorOptions().setHeaders(testHeaders), |
| 318 | + customUserAgent, |
| 319 | + detailedPlatformInfo), |
| 320 | + Arguments.of( |
| 321 | + new SessionOptions(), |
| 322 | + new TranslatorOptions().setAppInfo("my-java-translation-plugin", "1.2.3"), |
| 323 | + detailedPlatformInfoWithAppInfo, |
| 324 | + noStrings), |
| 325 | + Arguments.of( |
| 326 | + new SessionOptions(), |
| 327 | + new TranslatorOptions() |
| 328 | + .setSendPlatformInfo(false) |
| 329 | + .setAppInfo("my-java-translation-plugin", "1.2.3"), |
| 330 | + lightPlatformInfoWithAppInfo, |
| 331 | + detailedPlatformInfo), |
| 332 | + Arguments.of( |
| 333 | + new SessionOptions(), |
| 334 | + new TranslatorOptions() |
| 335 | + .setHeaders(testHeaders) |
| 336 | + .setAppInfo("my-java-translation-plugin", "1.2.3"), |
| 337 | + customUserAgent, |
| 338 | + detailedPlatformInfoWithAppInfo)); |
| 339 | + } |
240 | 340 | } |
0 commit comments