|
21 | 21 | import io.netty.channel.embedded.EmbeddedChannel; |
22 | 22 | import io.netty.handler.codec.http.DefaultFullHttpRequest; |
23 | 23 | import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 24 | +import io.netty.handler.codec.http.DefaultLastHttpContent; |
24 | 25 | import io.netty.handler.codec.http.FullHttpRequest; |
25 | 26 | import io.netty.handler.codec.http.DefaultHttpHeadersFactory; |
| 27 | +import io.netty.handler.codec.http.DefaultHttpContent; |
| 28 | +import io.netty.handler.codec.http.HttpContent; |
| 29 | +import io.netty.handler.codec.http.LastHttpContent; |
26 | 30 | import io.netty.handler.codec.http.HttpMethod; |
27 | 31 | import io.netty.handler.codec.http.HttpResponse; |
28 | 32 | import io.netty.handler.codec.http.HttpUtil; |
29 | 33 | import io.netty.util.AsciiString; |
| 34 | +import io.netty.util.CharsetUtil; |
30 | 35 | import io.netty.util.ReferenceCountUtil; |
31 | 36 | import org.junit.jupiter.api.Test; |
32 | 37 |
|
@@ -512,6 +517,154 @@ public void simpleRequestDoNotAllowPrivateNetwork() { |
512 | 517 | assertTrue(ReferenceCountUtil.release(response)); |
513 | 518 | } |
514 | 519 |
|
| 520 | + @Test |
| 521 | + public void preflightEmptyLastDiscarded() { |
| 522 | + CorsConfig config = forOrigin("http://allowed").build(); |
| 523 | + EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config)); |
| 524 | + |
| 525 | + FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/test"); |
| 526 | + preflight.headers().set(ORIGIN, "http://allowed"); |
| 527 | + preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET"); |
| 528 | + |
| 529 | + assertFalse(ch.writeInbound(preflight)); |
| 530 | + |
| 531 | + Object outbound = ch.readOutbound(); |
| 532 | + assertNotNull(outbound); // preflight response |
| 533 | + |
| 534 | + LastHttpContent lastHttpContent = LastHttpContent.EMPTY_LAST_CONTENT; |
| 535 | + assertFalse(ch.writeInbound(lastHttpContent)); |
| 536 | + |
| 537 | + // Nothing should have been forwarded |
| 538 | + assertNull(ch.readInbound()); |
| 539 | + |
| 540 | + assertFalse(ch.finish()); |
| 541 | + } |
| 542 | + |
| 543 | + @Test |
| 544 | + public void preflightSecondEmptyLastForwardedAfterFirstDiscard() { |
| 545 | + CorsConfig config = forOrigin("http://allowed").build(); |
| 546 | + EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config)); |
| 547 | + |
| 548 | + FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/test"); |
| 549 | + preflight.headers().set(ORIGIN, "http://allowed"); |
| 550 | + preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET"); |
| 551 | + |
| 552 | + assertFalse(ch.writeInbound(preflight)); |
| 553 | + ReferenceCountUtil.release(ch.readOutbound()); |
| 554 | + |
| 555 | + LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT; |
| 556 | + LastHttpContent second = LastHttpContent.EMPTY_LAST_CONTENT; |
| 557 | + |
| 558 | + assertFalse(ch.writeInbound(first)); |
| 559 | + |
| 560 | + assertFalse(ch.writeInbound(second)); |
| 561 | + |
| 562 | + assertNull(ch.readInbound()); |
| 563 | + assertFalse(ch.finish()); |
| 564 | + } |
| 565 | + |
| 566 | + @Test |
| 567 | + public void preflightSecondNonEmptyLastDiscarded() { |
| 568 | + CorsConfig config = forOrigin("http://allowed").build(); |
| 569 | + EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config)); |
| 570 | + |
| 571 | + FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/test"); |
| 572 | + preflight.headers().set(ORIGIN, "http://allowed"); |
| 573 | + preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET"); |
| 574 | + |
| 575 | + assertFalse(ch.writeInbound(preflight)); |
| 576 | + ReferenceCountUtil.release(ch.readOutbound()); |
| 577 | + |
| 578 | + LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT; |
| 579 | + LastHttpContent second = new DefaultLastHttpContent( |
| 580 | + Unpooled.copiedBuffer("test message", CharsetUtil.UTF_8)); |
| 581 | + |
| 582 | + assertFalse(ch.writeInbound(first)); |
| 583 | + assertFalse(ch.writeInbound(second)); |
| 584 | + assertNull(ch.readInbound()); |
| 585 | + assertFalse(ch.finish()); |
| 586 | + } |
| 587 | + |
| 588 | + @Test |
| 589 | + public void preflightNonEmptyLastForwarded() { |
| 590 | + CorsConfig config = forOrigin("http://allowed").build(); |
| 591 | + EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config)); |
| 592 | + |
| 593 | + FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/x"); |
| 594 | + preflight.headers().set(ORIGIN, "http://allowed"); |
| 595 | + preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET"); |
| 596 | + |
| 597 | + assertFalse(ch.writeInbound(preflight)); |
| 598 | + Object outbound = ch.releaseOutbound(); |
| 599 | + assertNotNull(outbound); |
| 600 | + |
| 601 | + LastHttpContent nonEmpty = new DefaultLastHttpContent(Unpooled.copiedBuffer("x", CharsetUtil.UTF_8)); |
| 602 | + assertFalse(ch.writeInbound(nonEmpty)); |
| 603 | + |
| 604 | + Object inbound = ch.readInbound(); |
| 605 | + assertNull(inbound); |
| 606 | + |
| 607 | + assertFalse(ch.finish()); |
| 608 | + } |
| 609 | + |
| 610 | + @Test |
| 611 | + public void testNormalRequestForwarded() { |
| 612 | + CorsConfig config = forOrigin("http://allowed").build(); |
| 613 | + EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config)); |
| 614 | + |
| 615 | + FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, GET, "/test"); |
| 616 | + req.headers().set(ORIGIN, "http://allowed"); |
| 617 | + |
| 618 | + assertTrue(ch.writeInbound(req)); |
| 619 | + |
| 620 | + LastHttpContent last = LastHttpContent.EMPTY_LAST_CONTENT; |
| 621 | + assertTrue(ch.writeInbound(last)); |
| 622 | + |
| 623 | + Object firstInbound = ch.readInbound(); |
| 624 | + Object secondInbound = ch.readInbound(); |
| 625 | + |
| 626 | + assertNotNull(firstInbound); |
| 627 | + assertNotNull(secondInbound); |
| 628 | + |
| 629 | + assertNull(ch.readInbound()); |
| 630 | + assertFalse(ch.finish()); |
| 631 | + } |
| 632 | + |
| 633 | + @Test |
| 634 | + public void preflightEmptyLastDiscardedThenNewRequestForwarded() { |
| 635 | + CorsConfig config = forOrigin("http://allowed").build(); |
| 636 | + EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config)); |
| 637 | + |
| 638 | + // Preflight request |
| 639 | + FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/pre"); |
| 640 | + preflight.headers().set(ORIGIN, "http://allowed"); |
| 641 | + preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET"); |
| 642 | + assertFalse(ch.writeInbound(preflight)); |
| 643 | + Object preflightResp = ch.readOutbound(); |
| 644 | + assertNotNull(preflightResp); |
| 645 | + ReferenceCountUtil.release(preflightResp); |
| 646 | + |
| 647 | + // Empty last content should be discarded |
| 648 | + assertFalse(ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT)); |
| 649 | + assertNull(ch.readInbound()); |
| 650 | + |
| 651 | + // New request should be forwarded |
| 652 | + FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, GET, "/next"); |
| 653 | + req.headers().set(ORIGIN, "http://allowed"); |
| 654 | + assertTrue(ch.writeInbound(req)); |
| 655 | + |
| 656 | + Object firstInbound = ch.readInbound(); |
| 657 | + assertNotNull(firstInbound); |
| 658 | + |
| 659 | + HttpContent content = new DefaultHttpContent(Unpooled.copiedBuffer("test message", CharsetUtil.UTF_8)); |
| 660 | + assertTrue(ch.writeInbound(content)); |
| 661 | + Object secondInbound = ch.readInbound(); |
| 662 | + assertNotNull(secondInbound); |
| 663 | + |
| 664 | + assertNull(ch.readInbound()); |
| 665 | + assertFalse(ch.finish()); |
| 666 | + } |
| 667 | + |
515 | 668 | private static HttpResponse simpleRequest(final CorsConfig config, final String origin) { |
516 | 669 | return simpleRequest(config, origin, null); |
517 | 670 | } |
|