|
21 | 21 | import io.netty.util.CharsetUtil; |
22 | 22 | import io.netty.util.ReferenceCountUtil; |
23 | 23 | import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.function.Executable; |
24 | 25 |
|
25 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
26 | 27 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
@@ -227,6 +228,359 @@ public void testConnectionClosedAfterResponseWhenBothTransferEncodingAndContentL |
227 | 228 | assertFalse(ch.finishAndReleaseAll()); |
228 | 229 | } |
229 | 230 |
|
| 231 | + @Test |
| 232 | + public void testInterleavedRequestResponseAcrossOverflow() { |
| 233 | + // Test interleaved enqueue/dequeue that crosses the inline-to-overflow boundary. |
| 234 | + // Send some requests, process some responses, then send more requests to trigger overflow. |
| 235 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 236 | + |
| 237 | + // Send 30 GET requests (not yet filling the 32-slot inline queue). |
| 238 | + StringBuilder requests = new StringBuilder(); |
| 239 | + for (int i = 0; i < 30; i++) { |
| 240 | + requests.append("GET /").append(i).append(" HTTP/1.1\r\nHost: a\r\n\r\n"); |
| 241 | + } |
| 242 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer(requests.toString(), CharsetUtil.UTF_8))); |
| 243 | + |
| 244 | + // Drain inbound. |
| 245 | + for (;;) { |
| 246 | + Object msg = ch.readInbound(); |
| 247 | + if (msg == null) { |
| 248 | + break; |
| 249 | + } |
| 250 | + if (msg instanceof HttpContent) { |
| 251 | + ((HttpContent) msg).release(); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + // Respond to 10 of them (draining 10 from the queue, leaving 20). |
| 256 | + for (int i = 0; i < 10; i++) { |
| 257 | + FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 258 | + Unpooled.copiedBuffer("ok", CharsetUtil.UTF_8)); |
| 259 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 2); |
| 260 | + assertTrue(ch.writeOutbound(resp)); |
| 261 | + ByteBuf buf = ch.readOutbound(); |
| 262 | + buf.release(); |
| 263 | + } |
| 264 | + |
| 265 | + // Now send 15 more requests (20 remaining + 15 = 35 total, exceeding inline capacity of 32). |
| 266 | + // Put a HEAD request as the last one to verify overflow ordering. |
| 267 | + requests = new StringBuilder(); |
| 268 | + for (int i = 30; i < 44; i++) { |
| 269 | + requests.append("GET /").append(i).append(" HTTP/1.1\r\nHost: a\r\n\r\n"); |
| 270 | + } |
| 271 | + requests.append("HEAD /44 HTTP/1.1\r\nHost: a\r\n\r\n"); |
| 272 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer(requests.toString(), CharsetUtil.UTF_8))); |
| 273 | + |
| 274 | + // Drain inbound. |
| 275 | + for (;;) { |
| 276 | + Object msg = ch.readInbound(); |
| 277 | + if (msg == null) { |
| 278 | + break; |
| 279 | + } |
| 280 | + if (msg instanceof HttpContent) { |
| 281 | + ((HttpContent) msg).release(); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + // Respond to remaining 20 GET requests (inline queue). |
| 286 | + for (int i = 10; i < 30; i++) { |
| 287 | + FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 288 | + Unpooled.copiedBuffer("ok", CharsetUtil.UTF_8)); |
| 289 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 2); |
| 290 | + assertTrue(ch.writeOutbound(resp)); |
| 291 | + ByteBuf buf = ch.readOutbound(); |
| 292 | + buf.release(); |
| 293 | + } |
| 294 | + |
| 295 | + // Respond to the 14 GET requests that were added. |
| 296 | + for (int i = 30; i < 44; i++) { |
| 297 | + FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 298 | + Unpooled.copiedBuffer("ok", CharsetUtil.UTF_8)); |
| 299 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 2); |
| 300 | + assertTrue(ch.writeOutbound(resp)); |
| 301 | + ByteBuf buf = ch.readOutbound(); |
| 302 | + String encoded = buf.toString(CharsetUtil.US_ASCII); |
| 303 | + assertTrue(encoded.contains("ok"), "GET response at position " + i + " should contain body"); |
| 304 | + buf.release(); |
| 305 | + } |
| 306 | + |
| 307 | + // Respond to the HEAD request at position 44 — must be content-always-empty. |
| 308 | + HttpResponse headResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); |
| 309 | + headResp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 999); |
| 310 | + assertTrue(ch.writeOutbound(headResp)); |
| 311 | + assertTrue(ch.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT)); |
| 312 | + |
| 313 | + ByteBuf buf = ch.readOutbound(); |
| 314 | + String encoded = buf.toString(CharsetUtil.US_ASCII); |
| 315 | + assertTrue(encoded.contains("HTTP/1.1 200 OK"), "HEAD response should be 200 OK"); |
| 316 | + buf.release(); |
| 317 | + |
| 318 | + buf = ch.readOutbound(); |
| 319 | + assertFalse(buf.isReadable(), "HEAD response body should be empty in overflow scenario"); |
| 320 | + buf.release(); |
| 321 | + |
| 322 | + assertFalse(ch.finishAndReleaseAll()); |
| 323 | + } |
| 324 | + |
| 325 | + @Test |
| 326 | + public void testGetMethodHasNormalBody() { |
| 327 | + // Verify a simple GET request is treated as METHOD_FLAG_NONE and body is included. |
| 328 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 329 | + |
| 330 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer( |
| 331 | + "GET / HTTP/1.1\r\nHost: a\r\n\r\n", CharsetUtil.UTF_8))); |
| 332 | + |
| 333 | + HttpRequest request = ch.readInbound(); |
| 334 | + assertEquals(HttpMethod.GET, request.method()); |
| 335 | + LastHttpContent content = ch.readInbound(); |
| 336 | + content.release(); |
| 337 | + |
| 338 | + FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 339 | + Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8)); |
| 340 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 5); |
| 341 | + assertTrue(ch.writeOutbound(resp)); |
| 342 | + |
| 343 | + ByteBuf buf = ch.readOutbound(); |
| 344 | + String encoded = buf.toString(CharsetUtil.US_ASCII); |
| 345 | + assertTrue(encoded.contains("hello"), "GET response should contain body"); |
| 346 | + assertTrue(encoded.contains("content-length: 5")); |
| 347 | + buf.release(); |
| 348 | + |
| 349 | + assertFalse(ch.finishAndReleaseAll()); |
| 350 | + } |
| 351 | + |
| 352 | + @Test |
| 353 | + public void testPostMethodHasNormalBody() { |
| 354 | + // POST should also be METHOD_FLAG_NONE. |
| 355 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 356 | + |
| 357 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer( |
| 358 | + "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 0\r\n\r\n", CharsetUtil.UTF_8))); |
| 359 | + |
| 360 | + for (;;) { |
| 361 | + Object msg = ch.readInbound(); |
| 362 | + if (msg == null) { |
| 363 | + break; |
| 364 | + } |
| 365 | + if (msg instanceof HttpContent) { |
| 366 | + ((HttpContent) msg).release(); |
| 367 | + } |
| 368 | + } |
| 369 | + |
| 370 | + FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 371 | + Unpooled.copiedBuffer("result", CharsetUtil.UTF_8)); |
| 372 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 6); |
| 373 | + assertTrue(ch.writeOutbound(resp)); |
| 374 | + |
| 375 | + ByteBuf buf = ch.readOutbound(); |
| 376 | + String encoded = buf.toString(CharsetUtil.US_ASCII); |
| 377 | + assertTrue(encoded.contains("result"), "POST response should contain body"); |
| 378 | + buf.release(); |
| 379 | + |
| 380 | + assertFalse(ch.finishAndReleaseAll()); |
| 381 | + } |
| 382 | + |
| 383 | + @Test |
| 384 | + public void testOverflowDrainsBeforeInlineRefill() { |
| 385 | + // Verify that once overflow is activated, subsequent enqueues also go to overflow |
| 386 | + // until the overflow drains, keeping FIFO order correct. |
| 387 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 388 | + |
| 389 | + int totalRequests = 34; // 32 inline + 2 overflow |
| 390 | + |
| 391 | + // Send 34 pipelined requests: position 33 (overflow) is HEAD. |
| 392 | + StringBuilder requests = new StringBuilder(); |
| 393 | + for (int i = 0; i < totalRequests; i++) { |
| 394 | + if (i == 33) { |
| 395 | + requests.append("HEAD /").append(i).append(" HTTP/1.1\r\nHost: a\r\n\r\n"); |
| 396 | + } else { |
| 397 | + requests.append("GET /").append(i).append(" HTTP/1.1\r\nHost: a\r\n\r\n"); |
| 398 | + } |
| 399 | + } |
| 400 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer(requests.toString(), CharsetUtil.UTF_8))); |
| 401 | + |
| 402 | + // Drain inbound. |
| 403 | + for (;;) { |
| 404 | + Object msg = ch.readInbound(); |
| 405 | + if (msg == null) { |
| 406 | + break; |
| 407 | + } |
| 408 | + if (msg instanceof HttpContent) { |
| 409 | + ((HttpContent) msg).release(); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + // Send responses for first 33 (all GET). |
| 414 | + for (int i = 0; i < 33; i++) { |
| 415 | + FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 416 | + Unpooled.copiedBuffer("ok", CharsetUtil.UTF_8)); |
| 417 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 2); |
| 418 | + assertTrue(ch.writeOutbound(resp)); |
| 419 | + ByteBuf buf = ch.readOutbound(); |
| 420 | + String encoded = buf.toString(CharsetUtil.US_ASCII); |
| 421 | + assertTrue(encoded.contains("ok"), "GET response at position " + i + " should contain body"); |
| 422 | + buf.release(); |
| 423 | + } |
| 424 | + |
| 425 | + // Response for position 33 — HEAD from overflow queue. |
| 426 | + HttpResponse headResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); |
| 427 | + headResp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 42); |
| 428 | + assertTrue(ch.writeOutbound(headResp)); |
| 429 | + assertTrue(ch.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT)); |
| 430 | + |
| 431 | + ByteBuf buf = ch.readOutbound(); |
| 432 | + buf.release(); |
| 433 | + |
| 434 | + buf = ch.readOutbound(); |
| 435 | + assertFalse(buf.isReadable(), "HEAD response from overflow queue body should be empty"); |
| 436 | + buf.release(); |
| 437 | + |
| 438 | + assertFalse(ch.finishAndReleaseAll()); |
| 439 | + } |
| 440 | + |
| 441 | + @Test |
| 442 | + public void testPollFromEmptyQueueReturnsNone() { |
| 443 | + // If a response is written without a matching request (unusual but defensive), |
| 444 | + // pollMethod should return METHOD_FLAG_NONE, so body is treated normally. |
| 445 | + final EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 446 | + |
| 447 | + // Write a response without any prior request. |
| 448 | + final FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 449 | + Unpooled.copiedBuffer("data", CharsetUtil.UTF_8)); |
| 450 | + resp.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 4); |
| 451 | + assertDoesNotThrow(new Executable() { |
| 452 | + @Override |
| 453 | + public void execute() throws Throwable { |
| 454 | + ch.writeOutbound(resp); |
| 455 | + } |
| 456 | + }); |
| 457 | + |
| 458 | + ByteBuf buf = ch.readOutbound(); |
| 459 | + assertNotNull(buf); |
| 460 | + String encoded = buf.toString(CharsetUtil.US_ASCII); |
| 461 | + assertTrue(encoded.contains("data"), "Response body should be present even without prior request"); |
| 462 | + buf.release(); |
| 463 | + |
| 464 | + ch.finishAndReleaseAll(); |
| 465 | + } |
| 466 | + |
| 467 | + @Test |
| 468 | + public void testHeadResponseHasNoContentAfterInterimResponse() { |
| 469 | + // A 1xx interim response must not consume an entry of the method queue, as it is not the |
| 470 | + // final response for the queued request. |
| 471 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 472 | + |
| 473 | + // Send a single HEAD request. Method queue = [HEAD]. |
| 474 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer( |
| 475 | + "HEAD /a HTTP/1.1\r\nHost: a\r\n\r\n", CharsetUtil.UTF_8))); |
| 476 | + HttpRequest request = ch.readInbound(); |
| 477 | + assertEquals(HttpMethod.HEAD, request.method()); |
| 478 | + LastHttpContent requestContent = ch.readInbound(); |
| 479 | + assertFalse(requestContent.content().isReadable()); |
| 480 | + requestContent.release(); |
| 481 | + |
| 482 | + // Write a 103 Early Hints interim response. The HEAD entry must stay in the queue. |
| 483 | + FullHttpResponse earlyHints = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.EARLY_HINTS); |
| 484 | + assertTrue(ch.writeOutbound(earlyHints)); |
| 485 | + ByteBuf earlyHintsBuf = ch.readOutbound(); |
| 486 | + assertEquals("HTTP/1.1 103 Early Hints\r\n\r\n", earlyHintsBuf.toString(CharsetUtil.US_ASCII)); |
| 487 | + earlyHintsBuf.release(); |
| 488 | + |
| 489 | + // Now write the final response for the HEAD request, with content attached. |
| 490 | + FullHttpResponse finalResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 491 | + Unpooled.copiedBuffer("body", CharsetUtil.UTF_8)); |
| 492 | + finalResponse.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 4); |
| 493 | + assertTrue(ch.writeOutbound(finalResponse)); |
| 494 | + |
| 495 | + // A HEAD response must never carry a body per RFC 9110 section 9.3.2, regardless of |
| 496 | + // any interim responses sent before it. |
| 497 | + ByteBuf buf = ch.readOutbound(); |
| 498 | + assertEquals("HTTP/1.1 200 OK\r\ncontent-length: 4\r\n\r\n", buf.toString(CharsetUtil.US_ASCII)); |
| 499 | + buf.release(); |
| 500 | + |
| 501 | + assertFalse(ch.finishAndReleaseAll()); |
| 502 | + } |
| 503 | + |
| 504 | + @Test |
| 505 | + public void testPipelinedResponseContentPreservedAfterInterimResponse() { |
| 506 | + // A 1xx interim response for one pipelined request must not consume a method queue entry, |
| 507 | + // so the queue order is preserved for the remaining pipelined requests. |
| 508 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 509 | + |
| 510 | + // Pipeline two requests: GET /a then HEAD /b. Method queue = [OTHER, HEAD]. |
| 511 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer( |
| 512 | + "GET /a HTTP/1.1\r\nHost: a\r\n\r\n" + |
| 513 | + "HEAD /b HTTP/1.1\r\nHost: a\r\n\r\n", CharsetUtil.UTF_8))); |
| 514 | + |
| 515 | + HttpRequest requestA = ch.readInbound(); |
| 516 | + assertEquals(HttpMethod.GET, requestA.method()); |
| 517 | + LastHttpContent requestAContent = ch.readInbound(); |
| 518 | + assertFalse(requestAContent.content().isReadable()); |
| 519 | + requestAContent.release(); |
| 520 | + |
| 521 | + HttpRequest requestB = ch.readInbound(); |
| 522 | + assertEquals(HttpMethod.HEAD, requestB.method()); |
| 523 | + LastHttpContent requestBContent = ch.readInbound(); |
| 524 | + assertFalse(requestBContent.content().isReadable()); |
| 525 | + requestBContent.release(); |
| 526 | + |
| 527 | + // Send a 1xx interim response for /a. The queue must stay at [OTHER, HEAD]. |
| 528 | + FullHttpResponse earlyHints = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.EARLY_HINTS); |
| 529 | + assertTrue(ch.writeOutbound(earlyHints)); |
| 530 | + ByteBuf earlyHintsBuf = ch.readOutbound(); |
| 531 | + assertEquals("HTTP/1.1 103 Early Hints\r\n\r\n", earlyHintsBuf.toString(CharsetUtil.US_ASCII)); |
| 532 | + earlyHintsBuf.release(); |
| 533 | + |
| 534 | + // Now send the final response to /a, with content attached. |
| 535 | + FullHttpResponse finalResponseA = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, |
| 536 | + Unpooled.copiedBuffer("body-a", CharsetUtil.UTF_8)); |
| 537 | + finalResponseA.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 6); |
| 538 | + assertTrue(ch.writeOutbound(finalResponseA)); |
| 539 | + |
| 540 | + // The GET /a response body must be preserved. |
| 541 | + ByteBuf buf = ch.readOutbound(); |
| 542 | + assertEquals("HTTP/1.1 200 OK\r\ncontent-length: 6\r\n\r\nbody-a", buf.toString(CharsetUtil.US_ASCII)); |
| 543 | + buf.release(); |
| 544 | + |
| 545 | + assertFalse(ch.finishAndReleaseAll()); |
| 546 | + } |
| 547 | + |
| 548 | + @Test |
| 549 | + public void testSwitchingProtocolsResponseHasNoContent() { |
| 550 | + // A 101 Switching Protocols response must never carry a body. |
| 551 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpServerCodec()); |
| 552 | + |
| 553 | + // Send a HEAD request that asks to switch protocols. Method queue = [HEAD]. |
| 554 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer( |
| 555 | + "HEAD / HTTP/1.1\r\n" + |
| 556 | + "Host: a\r\n" + |
| 557 | + "Connection: upgrade\r\n" + |
| 558 | + "Upgrade: websocket\r\n\r\n", CharsetUtil.UTF_8))); |
| 559 | + HttpRequest request = ch.readInbound(); |
| 560 | + assertEquals(HttpMethod.HEAD, request.method()); |
| 561 | + LastHttpContent requestContent = ch.readInbound(); |
| 562 | + assertFalse(requestContent.content().isReadable()); |
| 563 | + requestContent.release(); |
| 564 | + |
| 565 | + HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS); |
| 566 | + response.headers().set(HttpHeaderNames.CONNECTION, "upgrade"); |
| 567 | + response.headers().set(HttpHeaderNames.UPGRADE, "websocket"); |
| 568 | + assertTrue(ch.writeOutbound(response)); |
| 569 | + assertTrue(ch.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT)); |
| 570 | + |
| 571 | + ByteBuf buf = ch.readOutbound(); |
| 572 | + assertEquals("HTTP/1.1 101 Switching Protocols\r\n" + |
| 573 | + "connection: upgrade\r\n" + |
| 574 | + "upgrade: websocket\r\n\r\n", buf.toString(CharsetUtil.US_ASCII)); |
| 575 | + buf.release(); |
| 576 | + |
| 577 | + buf = ch.readOutbound(); |
| 578 | + assertFalse(buf.isReadable()); |
| 579 | + buf.release(); |
| 580 | + |
| 581 | + assertFalse(ch.finishAndReleaseAll()); |
| 582 | + } |
| 583 | + |
230 | 584 | private static ByteBuf prepareDataChunk(int size) { |
231 | 585 | StringBuilder sb = new StringBuilder(); |
232 | 586 | for (int i = 0; i < size; ++i) { |
|