|
34 | 34 | import io.netty.handler.codec.http.FullHttpRequest; |
35 | 35 | import io.netty.handler.codec.http.FullHttpResponse; |
36 | 36 | import io.netty.handler.codec.http.HttpContent; |
| 37 | +import io.netty.handler.codec.http.HttpHeaderNames; |
37 | 38 | import io.netty.handler.codec.http.HttpHeaders; |
38 | 39 | import io.netty.handler.codec.http.HttpMethod; |
39 | 40 | import io.netty.handler.codec.http.HttpRequest; |
@@ -104,6 +105,80 @@ public void execute() { |
104 | 105 | ch.finishAndReleaseAll(); |
105 | 106 | } |
106 | 107 |
|
| 108 | + @ParameterizedTest |
| 109 | + @ValueSource(ints = {100, 103}) |
| 110 | + public void encodeInformationalResponseWithoutContentLength(int code) throws Exception { |
| 111 | + EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true)); |
| 112 | + // Like the 100 (Continue) that HttpServerExpectContinueHandler writes. |
| 113 | + FullHttpResponse response = new DefaultFullHttpResponse( |
| 114 | + HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code), Unpooled.EMPTY_BUFFER); |
| 115 | + response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); |
| 116 | + response.headers().set("link", "</style.css>; rel=preload; as=style"); |
| 117 | + assertTrue(ch.writeOutbound(response)); |
| 118 | + |
| 119 | + Http2HeadersFrame headersFrame = ch.readOutbound(); |
| 120 | + assertEquals(String.valueOf(code), headersFrame.headers().status().toString()); |
| 121 | + assertFalse(headersFrame.headers().contains(HttpHeaderNames.CONTENT_LENGTH)); |
| 122 | + assertEquals("</style.css>; rel=preload; as=style", headersFrame.headers().get("link").toString()); |
| 123 | + assertFalse(headersFrame.isEndStream()); |
| 124 | + |
| 125 | + assertNull(ch.readOutbound()); |
| 126 | + assertFalse(ch.finish()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void encodeInformationalFullResponseDropsContent() throws Exception { |
| 131 | + EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true)); |
| 132 | + ByteBuf content = Unpooled.copiedBuffer("ignored", CharsetUtil.UTF_8); |
| 133 | + FullHttpResponse response = new DefaultFullHttpResponse( |
| 134 | + HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, content); |
| 135 | + response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()); |
| 136 | + assertTrue(ch.writeOutbound(response)); |
| 137 | + assertEquals(0, content.refCnt()); |
| 138 | + |
| 139 | + Http2HeadersFrame headersFrame = ch.readOutbound(); |
| 140 | + assertEquals("100", headersFrame.headers().status().toString()); |
| 141 | + assertFalse(headersFrame.headers().contains(HttpHeaderNames.CONTENT_LENGTH)); |
| 142 | + assertFalse(headersFrame.isEndStream()); |
| 143 | + |
| 144 | + assertNull(ch.readOutbound()); |
| 145 | + assertFalse(ch.finish()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void encode100ContinueThenFinalResponse() throws Exception { |
| 150 | + EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true)); |
| 151 | + FullHttpResponse continueResponse = new DefaultFullHttpResponse( |
| 152 | + HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER); |
| 153 | + continueResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); |
| 154 | + ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8); |
| 155 | + FullHttpResponse response = new DefaultFullHttpResponse( |
| 156 | + HttpVersion.HTTP_1_1, HttpResponseStatus.OK, hello); |
| 157 | + response.headers().set(HttpHeaderNames.CONTENT_LENGTH, hello.readableBytes()); |
| 158 | + assertTrue(ch.writeOutbound(continueResponse, response)); |
| 159 | + |
| 160 | + Http2HeadersFrame continueFrame = ch.readOutbound(); |
| 161 | + Http2HeadersFrame headersFrame = ch.readOutbound(); |
| 162 | + Http2DataFrame dataFrame = ch.readOutbound(); |
| 163 | + try { |
| 164 | + assertEquals("100", continueFrame.headers().status().toString()); |
| 165 | + assertFalse(continueFrame.headers().contains(HttpHeaderNames.CONTENT_LENGTH)); |
| 166 | + assertFalse(continueFrame.isEndStream()); |
| 167 | + |
| 168 | + assertEquals("200", headersFrame.headers().status().toString()); |
| 169 | + assertEquals("11", headersFrame.headers().get(HttpHeaderNames.CONTENT_LENGTH).toString()); |
| 170 | + assertFalse(headersFrame.isEndStream()); |
| 171 | + |
| 172 | + assertEquals("hello world", dataFrame.content().toString(CharsetUtil.UTF_8)); |
| 173 | + assertTrue(dataFrame.isEndStream()); |
| 174 | + } finally { |
| 175 | + dataFrame.release(); |
| 176 | + } |
| 177 | + |
| 178 | + assertNull(ch.readOutbound()); |
| 179 | + assertFalse(ch.finish()); |
| 180 | + } |
| 181 | + |
107 | 182 | @Test |
108 | 183 | public void testUpgradeNonEmptyFullResponse() throws Exception { |
109 | 184 | EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true)); |
|