|
16 | 16 | package org.asynchttpclient; |
17 | 17 |
|
18 | 18 | import io.github.artsok.RepeatedIfExceptionsTest; |
| 19 | +import io.netty.handler.codec.http.DefaultHttpHeaders; |
| 20 | +import io.netty.handler.codec.http.HttpHeaders; |
19 | 21 | import io.netty.handler.codec.http.HttpMethod; |
20 | 22 | import io.netty.handler.codec.http.cookie.Cookie; |
21 | 23 | import io.netty.handler.codec.http.cookie.DefaultCookie; |
|
29 | 31 | import static java.nio.charset.StandardCharsets.UTF_8; |
30 | 32 | import static java.util.Collections.singletonList; |
31 | 33 | import static org.asynchttpclient.Dsl.get; |
| 34 | +import static org.asynchttpclient.Dsl.post; |
32 | 35 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
33 | 37 | import static org.junit.jupiter.api.Assertions.assertTrue; |
34 | 38 |
|
35 | 39 | public class RequestBuilderTest { |
@@ -220,4 +224,192 @@ public void testSettingHeadersUsingMapWithStringKeys() { |
220 | 224 | Request request = requestBuilder.build(); |
221 | 225 | assertEquals(request.getHeaders().get("X-Forwarded-For"), "10.0.0.1"); |
222 | 226 | } |
| 227 | + |
| 228 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 229 | + public void testUserSetTextPlainContentTypeShouldNotBeModified() { |
| 230 | + Request request = post("http://localhost/test") |
| 231 | + .setHeader("Content-Type", "text/plain") |
| 232 | + .setBody("Hello World") |
| 233 | + .build(); |
| 234 | + |
| 235 | + String contentType = request.getHeaders().get("Content-Type"); |
| 236 | + assertEquals("text/plain", contentType, "Content-Type should not be modified when user explicitly sets it"); |
| 237 | + assertFalse(contentType.contains("charset"), "Charset should not be added to user-specified Content-Type"); |
| 238 | + } |
| 239 | + |
| 240 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 241 | + public void testUserSetTextXmlContentTypeShouldNotBeModified() { |
| 242 | + Request request = post("http://localhost/test") |
| 243 | + .setHeader("Content-Type", "text/xml") |
| 244 | + .setBody("<test>Hello</test>") |
| 245 | + .build(); |
| 246 | + |
| 247 | + String contentType = request.getHeaders().get("Content-Type"); |
| 248 | + assertEquals("text/xml", contentType, "Content-Type should not be modified when user explicitly sets it"); |
| 249 | + } |
| 250 | + |
| 251 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 252 | + public void testUserSetTextHtmlContentTypeShouldNotBeModified() { |
| 253 | + Request request = post("http://localhost/test") |
| 254 | + .setHeader("Content-Type", "text/html") |
| 255 | + .setBody("<html></html>") |
| 256 | + .build(); |
| 257 | + |
| 258 | + String contentType = request.getHeaders().get("Content-Type"); |
| 259 | + assertEquals("text/html", contentType, "Content-Type should not be modified when user explicitly sets it"); |
| 260 | + } |
| 261 | + |
| 262 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 263 | + public void testUserSetContentTypeWithCharsetShouldBePreserved() { |
| 264 | + Request request = post("http://localhost/test") |
| 265 | + .setHeader("Content-Type", "text/xml; charset=ISO-8859-1") |
| 266 | + .setBody("<test>Hello</test>") |
| 267 | + .build(); |
| 268 | + |
| 269 | + String contentType = request.getHeaders().get("Content-Type"); |
| 270 | + assertEquals("text/xml; charset=ISO-8859-1", contentType, "User-specified charset should be preserved"); |
| 271 | + assertTrue(contentType.contains("ISO-8859-1"), "ISO-8859-1 charset should be preserved"); |
| 272 | + assertFalse(contentType.contains("UTF-8"), "UTF-8 should not be added"); |
| 273 | + } |
| 274 | + |
| 275 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 276 | + public void testApplicationJsonContentTypeShouldNotBeModified() { |
| 277 | + Request request = post("http://localhost/test") |
| 278 | + .setHeader("Content-Type", "application/json") |
| 279 | + .setBody("{\"key\": \"value\"}") |
| 280 | + .build(); |
| 281 | + |
| 282 | + String contentType = request.getHeaders().get("Content-Type"); |
| 283 | + assertEquals("application/json", contentType, "application/json should not be modified"); |
| 284 | + assertFalse(contentType.contains("charset"), "Charset should not be added to application/json"); |
| 285 | + } |
| 286 | + |
| 287 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 288 | + public void testAddHeaderContentTypeShouldNotBeModified() { |
| 289 | + Request request = post("http://localhost/test") |
| 290 | + .addHeader("Content-Type", "text/plain") |
| 291 | + .setBody("Hello World") |
| 292 | + .build(); |
| 293 | + |
| 294 | + String contentType = request.getHeaders().get("Content-Type"); |
| 295 | + assertEquals("text/plain", contentType, "Content-Type set via addHeader should not be modified"); |
| 296 | + } |
| 297 | + |
| 298 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 299 | + public void testSetHeadersWithHttpHeadersShouldLockContentType() { |
| 300 | + HttpHeaders httpHeaders = new DefaultHttpHeaders(); |
| 301 | + httpHeaders.set("Content-Type", "text/plain"); |
| 302 | + |
| 303 | + Request request = post("http://localhost/test") |
| 304 | + .setHeaders(httpHeaders) |
| 305 | + .setBody("Hello World") |
| 306 | + .build(); |
| 307 | + |
| 308 | + String contentType = request.getHeaders().get("Content-Type"); |
| 309 | + assertEquals("text/plain", contentType, "Content-Type set via setHeaders(HttpHeaders) should not be modified"); |
| 310 | + } |
| 311 | + |
| 312 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 313 | + public void testSetHeadersWithMapShouldLockContentType() { |
| 314 | + Map<String, List<String>> headerMap = new HashMap<>(); |
| 315 | + headerMap.put("Content-Type", singletonList("text/plain")); |
| 316 | + |
| 317 | + Request request = post("http://localhost/test") |
| 318 | + .setHeaders(headerMap) |
| 319 | + .setBody("Hello World") |
| 320 | + .build(); |
| 321 | + |
| 322 | + String contentType = request.getHeaders().get("Content-Type"); |
| 323 | + assertEquals("text/plain", contentType, "Content-Type set via setHeaders(Map) should not be modified"); |
| 324 | + } |
| 325 | + |
| 326 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 327 | + public void testSetSingleHeadersShouldLockContentType() { |
| 328 | + Map<String, String> headerMap = new HashMap<>(); |
| 329 | + headerMap.put("Content-Type", "text/plain"); |
| 330 | + |
| 331 | + Request request = post("http://localhost/test") |
| 332 | + .setSingleHeaders(headerMap) |
| 333 | + .setBody("Hello World") |
| 334 | + .build(); |
| 335 | + |
| 336 | + String contentType = request.getHeaders().get("Content-Type"); |
| 337 | + assertEquals("text/plain", contentType, "Content-Type set via setSingleHeaders should not be modified"); |
| 338 | + } |
| 339 | + |
| 340 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 341 | + public void testClearHeadersShouldResetContentTypeLock() { |
| 342 | + Request request = post("http://localhost/test") |
| 343 | + .setHeader("Content-Type", "text/plain") |
| 344 | + .clearHeaders() |
| 345 | + .setHeader("Content-Type", "text/xml") |
| 346 | + .setBody("<test></test>") |
| 347 | + .build(); |
| 348 | + |
| 349 | + String contentType = request.getHeaders().get("Content-Type"); |
| 350 | + assertEquals("text/xml", contentType, "Content-Type should still be preserved after clear and re-set"); |
| 351 | + } |
| 352 | + |
| 353 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 354 | + public void testPrototypeRequestShouldPreserveContentType() { |
| 355 | + Request original = post("http://localhost/test") |
| 356 | + .setHeader("Content-Type", "text/plain") |
| 357 | + .setBody("Hello") |
| 358 | + .build(); |
| 359 | + |
| 360 | + Request copy = post("http://localhost/test") |
| 361 | + .setUrl(original.getUri().toUrl()) |
| 362 | + .setHeaders(original.getHeaders()) |
| 363 | + .setBody("Hello") |
| 364 | + .build(); |
| 365 | + |
| 366 | + String contentType = copy.getHeaders().get("Content-Type"); |
| 367 | + assertEquals("text/plain", contentType, "Content-Type should be preserved from prototype"); |
| 368 | + } |
| 369 | + |
| 370 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 371 | + public void testRequestBuilderFromPrototypeShouldPreserveContentType() { |
| 372 | + Request original = post("http://localhost/test") |
| 373 | + .setHeader("Content-Type", "text/plain") |
| 374 | + .setBody("Hello") |
| 375 | + .build(); |
| 376 | + |
| 377 | + Request copy = new RequestBuilder(original).build(); |
| 378 | + |
| 379 | + String contentType = copy.getHeaders().get("Content-Type"); |
| 380 | + assertEquals("text/plain", contentType, "Content-Type should be preserved from prototype via RequestBuilder"); |
| 381 | + } |
| 382 | + |
| 383 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 384 | + public void testCaseInsensitiveContentTypeHeader() { |
| 385 | + Request request = post("http://localhost/test") |
| 386 | + .setHeader("content-type", "text/plain") |
| 387 | + .setBody("Hello World") |
| 388 | + .build(); |
| 389 | + |
| 390 | + String contentType = request.getHeaders().get("Content-Type"); |
| 391 | + assertEquals("text/plain", contentType, "Content-Type should be matched case-insensitively"); |
| 392 | + } |
| 393 | + |
| 394 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 395 | + public void testSetHeaderWithIterableShouldLockContentType() { |
| 396 | + Request request = post("http://localhost/test") |
| 397 | + .setHeader("Content-Type", singletonList("text/plain")) |
| 398 | + .setBody("Hello World") |
| 399 | + .build(); |
| 400 | + |
| 401 | + String contentType = request.getHeaders().get("Content-Type"); |
| 402 | + assertEquals("text/plain", contentType, "Content-Type set via setHeader(Iterable) should not be modified"); |
| 403 | + } |
| 404 | + |
| 405 | + @RepeatedIfExceptionsTest(repeats = 5) |
| 406 | + public void testAddHeaderWithIterableShouldLockContentType() { |
| 407 | + Request request = post("http://localhost/test") |
| 408 | + .addHeader("Content-Type", singletonList("text/plain")) |
| 409 | + .setBody("Hello World") |
| 410 | + .build(); |
| 411 | + |
| 412 | + String contentType = request.getHeaders().get("Content-Type"); |
| 413 | + assertEquals("text/plain", contentType, "Content-Type set via addHeader(Iterable) should not be modified"); |
| 414 | + } |
223 | 415 | } |
0 commit comments