|
29 | 29 | import java.nio.charset.Charset;
|
30 | 30 | import java.util.concurrent.ExecutionException;
|
31 | 31 |
|
| 32 | +import static io.netty.util.internal.ObjectUtil.checkNotNull; |
32 | 33 | import static org.hamcrest.MatcherAssert.assertThat;
|
33 | 34 | import static org.hamcrest.Matchers.instanceOf;
|
34 | 35 | import static org.hamcrest.Matchers.is;
|
35 | 36 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
36 | 37 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
| 38 | +import static org.junit.jupiter.api.Assertions.assertNull; |
37 | 39 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
38 | 40 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
39 | 41 |
|
@@ -182,6 +184,134 @@ public void testEmptyContentNotsChunkedWithTrailers() throws Exception {
|
182 | 184 | testEmptyContents(false, true);
|
183 | 185 | }
|
184 | 186 |
|
| 187 | + // this is not using Full types on purpose!!! |
| 188 | + private static class CustomFullHttpRequest extends DefaultHttpRequest implements LastHttpContent { |
| 189 | + private final ByteBuf content; |
| 190 | + private final HttpHeaders trailingHeader; |
| 191 | + |
| 192 | + CustomFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, ByteBuf content) { |
| 193 | + this(httpVersion, method, uri, content, true); |
| 194 | + } |
| 195 | + |
| 196 | + CustomFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, |
| 197 | + ByteBuf content, boolean validateHeaders) { |
| 198 | + super(httpVersion, method, uri, validateHeaders); |
| 199 | + this.content = checkNotNull(content, "content"); |
| 200 | + trailingHeader = new DefaultHttpHeaders(validateHeaders); |
| 201 | + } |
| 202 | + |
| 203 | + private CustomFullHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, |
| 204 | + ByteBuf content, HttpHeaders headers, HttpHeaders trailingHeader) { |
| 205 | + super(httpVersion, method, uri, headers); |
| 206 | + this.content = checkNotNull(content, "content"); |
| 207 | + this.trailingHeader = checkNotNull(trailingHeader, "trailingHeader"); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public HttpHeaders trailingHeaders() { |
| 212 | + return trailingHeader; |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public ByteBuf content() { |
| 217 | + return content; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public int refCnt() { |
| 222 | + return content.refCnt(); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public CustomFullHttpRequest retain() { |
| 227 | + content.retain(); |
| 228 | + return this; |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public CustomFullHttpRequest retain(int increment) { |
| 233 | + content.retain(increment); |
| 234 | + return this; |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public CustomFullHttpRequest touch() { |
| 239 | + content.touch(); |
| 240 | + return this; |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public CustomFullHttpRequest touch(Object hint) { |
| 245 | + content.touch(hint); |
| 246 | + return this; |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public boolean release() { |
| 251 | + return content.release(); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public boolean release(int decrement) { |
| 256 | + return content.release(decrement); |
| 257 | + } |
| 258 | + |
| 259 | + @Override |
| 260 | + public CustomFullHttpRequest setProtocolVersion(HttpVersion version) { |
| 261 | + super.setProtocolVersion(version); |
| 262 | + return this; |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public CustomFullHttpRequest setMethod(HttpMethod method) { |
| 267 | + super.setMethod(method); |
| 268 | + return this; |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + public CustomFullHttpRequest setUri(String uri) { |
| 273 | + super.setUri(uri); |
| 274 | + return this; |
| 275 | + } |
| 276 | + |
| 277 | + @Override |
| 278 | + public CustomFullHttpRequest copy() { |
| 279 | + return replace(content().copy()); |
| 280 | + } |
| 281 | + |
| 282 | + @Override |
| 283 | + public CustomFullHttpRequest duplicate() { |
| 284 | + return replace(content().duplicate()); |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + public CustomFullHttpRequest retainedDuplicate() { |
| 289 | + return replace(content().retainedDuplicate()); |
| 290 | + } |
| 291 | + |
| 292 | + @Override |
| 293 | + public CustomFullHttpRequest replace(ByteBuf content) { |
| 294 | + CustomFullHttpRequest request = new CustomFullHttpRequest(protocolVersion(), method(), uri(), content, |
| 295 | + headers().copy(), trailingHeaders().copy()); |
| 296 | + request.setDecoderResult(decoderResult()); |
| 297 | + return request; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void testCustomMessageEmptyLastContent() { |
| 303 | + HttpRequestEncoder encoder = new HttpRequestEncoder(); |
| 304 | + EmbeddedChannel channel = new EmbeddedChannel(encoder); |
| 305 | + HttpRequest customMsg = new CustomFullHttpRequest(HttpVersion.HTTP_1_1, |
| 306 | + HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER); |
| 307 | + assertTrue(channel.writeOutbound(customMsg)); |
| 308 | + // Ensure we only produce ByteBuf instances. |
| 309 | + ByteBuf head = channel.readOutbound(); |
| 310 | + assertTrue(head.release()); |
| 311 | + assertNull(channel.readOutbound()); |
| 312 | + assertFalse(channel.finish()); |
| 313 | + } |
| 314 | + |
185 | 315 | private void testEmptyContents(boolean chunked, boolean trailers) throws Exception {
|
186 | 316 | HttpRequestEncoder encoder = new HttpRequestEncoder();
|
187 | 317 | EmbeddedChannel channel = new EmbeddedChannel(encoder);
|
|
0 commit comments