@Test public void testIsResponseSelfTerminating() { HttpResponse httpResponse; boolean isResponseSelfTerminating; // test cases from the scenarios listed in RFC 2616, section 4.4 // #1: 1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and // 304 responses and any response to a HEAD request) is always terminated by the first empty // line after the header fields, regardless of the entity-header fields present in the message. httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(true, isResponseSelfTerminating); // #2: 2.If a Transfer-Encoding header field (section 14.41) is present and has any value other // than "identity", then the transfer-length is defined by use of the "chunked" transfer-coding // (section 3.6), unless the message is terminated by closing the connection. httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "chunked"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(true, isResponseSelfTerminating); httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "gzip, chunked"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(true, isResponseSelfTerminating); // chunked encoding is not last, so not self terminating httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "chunked, gzip"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(false, isResponseSelfTerminating); // four encodings on two lines, chunked is not last, so not self terminating httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "gzip, chunked"); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "deflate, gzip"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(false, isResponseSelfTerminating); // three encodings on two lines, chunked is last, so self terminating httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "gzip"); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "deflate,chunked"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(true, isResponseSelfTerminating); // #3: 3.If a Content-Length header field (section 14.13) is present, its decimal value in // OCTETs represents both the entity-length and the transfer-length. httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.CONTENT_LENGTH, "15"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(true, isResponseSelfTerminating); // continuing #3: If a message is received with both a Transfer-Encoding header field and a // Content-Length header field, the latter MUST be ignored. // chunked is last Transfer-Encoding, so message is self-terminating httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "gzip, chunked"); httpResponse.headers().add(HttpHeaders.Names.CONTENT_LENGTH, "15"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(true, isResponseSelfTerminating); // chunked is not last Transfer-Encoding, so message is not self-terminating, since // Content-Length is ignored httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, "gzip"); httpResponse.headers().add(HttpHeaders.Names.CONTENT_LENGTH, "15"); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(false, isResponseSelfTerminating); // without any of the above conditions, the message should not be self-terminating // (multipart/byteranges is ignored, see note in method javadoc) httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); isResponseSelfTerminating = ProxyUtils.isResponseSelfTerminating(httpResponse); assertEquals(false, isResponseSelfTerminating); }