DefaultContentLengthStrategy implements ContentLengthStrategy { @Override public long determineLength(final HttpMessage message) throws HttpException { Args.notNull(message, "HTTP message"); final Header transferEncodingHeader = message.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); if (transferEncodingHeader != null) { final String headerValue = transferEncodingHeader.getValue(); if (HeaderElements.CHUNKED_ENCODING.equalsIgnoreCase(headerValue)) { return CHUNKED; } throw new NotImplementedException("Unsupported transfer encoding: " + headerValue); } if (message.countHeaders(HttpHeaders.CONTENT_LENGTH) > 1) { throw new ProtocolException("Multiple Content-Length headers"); } final Header contentLengthHeader = message.getFirstHeader(HttpHeaders.CONTENT_LENGTH); if (contentLengthHeader != null) { final String s = contentLengthHeader.getValue(); try { final long len = Long.parseLong(s); if (len < 0) { throw new ProtocolException("Negative content length: " + s); } return len; } catch (final NumberFormatException e) { throw new ProtocolException("Invalid content length: " + s); } } return UNDEFINED; } DefaultContentLengthStrategy(); @Override long determineLength(final HttpMessage message); static final DefaultContentLengthStrategy INSTANCE; }
@Test public void testEntityWithChunkTransferEncoding() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); message.addHeader("Transfer-Encoding", "Chunked"); Assert.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message)); }
@Test(expected = NotImplementedException.class) public void testEntityWithIdentityTransferEncoding() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); message.addHeader("Transfer-Encoding", "Identity"); lenStrategy.determineLength(message); }
@Test(expected=ProtocolException.class) public void testEntityWithInvalidTransferEncoding() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); message.addHeader("Transfer-Encoding", "whatever"); lenStrategy.determineLength(message); }
@Test public void testEntityWithContentLength() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); message.addHeader("Content-Length", "100"); Assert.assertEquals(100, lenStrategy.determineLength(message)); }
@Test(expected=ProtocolException.class) public void testEntityWithInvalidContentLength() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); message.addHeader("Content-Length", "whatever"); lenStrategy.determineLength(message); }
@Test(expected=ProtocolException.class) public void testEntityWithNegativeContentLength() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); message.addHeader("Content-Length", "-10"); lenStrategy.determineLength(message); }
@Test public void testEntityNoContentDelimiter() throws Exception { final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy(); final HttpMessage message = new TestHttpMessage(); Assert.assertEquals(ContentLengthStrategy.UNDEFINED, lenStrategy.determineLength(message)); }