@Override public long getContentLength() { return wrappedEntity.getContentLength(); }
@Override public long getContentLength() { return wrappedEntity.getContentLength(); }
@Override public long getContentLength() { return original.getContentLength(); }
@Override public long getContentLength() { return wrappedEntity.getContentLength(); }
@Override public long getContentLength() { return wrappedEntity.getContentLength(); }
@Test public void testResponseToAHEADRequestMustNotHaveABody() throws Exception { request = new BasicClassicHttpRequest("HEAD", "/"); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); final ClassicHttpResponse result = execute(request); verifyMocks(); Assert.assertTrue(result.getEntity() == null || result.getEntity().getContentLength() == 0); }
org.easymock.EasyMock.expect(mockBody.getContentLength()).andReturn(-1L).anyTimes(); post.setEntity(mockBody);
/** * Creates a new buffered entity wrapper. * * @param entity the entity to wrap, not null * @throws IllegalArgumentException if wrapped is null */ public BufferedHttpEntity(final HttpEntity entity) throws IOException { super(entity); if (!entity.isRepeatable() || entity.getContentLength() < 0) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); out.flush(); this.buffer = out.toByteArray(); } else { this.buffer = null; } }
/** * Creates a new buffered entity wrapper. * * @param entity the entity to wrap, not null * @throws IllegalArgumentException if wrapped is null */ public BufferedHttpEntity(final HttpEntity entity) throws IOException { super(entity); if (!entity.isRepeatable() || entity.getContentLength() < 0) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); out.flush(); this.buffer = out.toByteArray(); } else { this.buffer = null; } }
return Collections.emptyList(); final long len = entity.getContentLength(); Args.checkRange(len, 0, Integer.MAX_VALUE, "HTTP entity is too large"); final Charset charset = contentType.getCharset() != null ? contentType.getCharset()
return Collections.emptyList(); final long len = entity.getContentLength(); Args.checkRange(len, 0, Integer.MAX_VALUE, "HTTP entity is too large"); final Charset charset = contentType.getCharset() != null ? contentType.getCharset()
@Test public void testCreateEntityInputChunked() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.CHUNKED); Assert.assertNotNull(entity); Assert.assertTrue(entity.isChunked()); Assert.assertEquals(-1, entity.getContentLength()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof ChunkedInputStream)); }
@Test public void testCreateEntityInputUndefined() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.UNDEFINED); Assert.assertNotNull(entity); Assert.assertFalse(entity.isChunked()); Assert.assertEquals(-1, entity.getContentLength()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof IdentityInputStream)); }
if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) { final String contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) {
@Test public void testReadResponseEntityWithoutContentLength() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\n\r\n123"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); final HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(-1, entity.getContentLength()); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertEquals(3, content.available()); Assert.assertEquals('1', content.read()); Assert.assertEquals('2', content.read()); Assert.assertEquals('3', content.read()); }
@Test public void testReadResponseEntityWithContentLength() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\nContent-Length: 3\r\n\r\n123"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); final HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(3, entity.getContentLength()); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ContentLengthInputStream); }
@Test public void testReadResponseEntityChunkCoded() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\nTransfer-Encoding: " + "chunked\r\n\r\n3\r\n123\r\n0\r\n\r\n"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); final HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(-1, entity.getContentLength()); Assert.assertEquals(true, entity.isChunked()); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ChunkedInputStream); }
@Test public void testReadRequestEntityWithContentLength() throws Exception { final String s = "POST / HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = conn.receiveRequestHeader(); Assert.assertNotNull(request); Assert.assertEquals("/", request.getPath()); Assert.assertEquals("POST", request.getMethod()); Assert.assertTrue(request.containsHeader("User-Agent")); Assert.assertNull(request.getEntity()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); conn.receiveRequestEntity(request); final HttpEntity entity = request.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(3, entity.getContentLength()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ContentLengthInputStream); }
@Test public void testCreateEntityLengthDelimited() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); message.addHeader("Content-Length", "10"); message.addHeader("Content-Type", "stuff"); message.addHeader("Content-Encoding", "chunked"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, 10); Assert.assertNotNull(entity); Assert.assertFalse(entity.isChunked()); Assert.assertEquals(10, entity.getContentLength()); Assert.assertEquals("stuff", entity.getContentType()); Assert.assertEquals("chunked", entity.getContentEncoding()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof ContentLengthInputStream)); }
@Test public void testReadRequestEntityChunckCoded() throws Exception { final String s = "POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nTransfer-Encoding: " + "chunked\r\n\r\n3\r\n123\r\n0\r\n\r\n"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = conn.receiveRequestHeader(); Assert.assertNotNull(request); Assert.assertEquals("/stuff", request.getPath()); Assert.assertEquals("POST", request.getMethod()); Assert.assertTrue(request.containsHeader("User-Agent")); Assert.assertNull(request.getEntity()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); conn.receiveRequestEntity(request); final HttpEntity entity = request.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(-1, entity.getContentLength()); Assert.assertEquals(true, entity.isChunked()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ChunkedInputStream); }