public Request bodyStream(final InputStream inStream) { return body(new InputStreamEntity(inStream, -1, null)); }
@Test public void testBasics() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length); Assert.assertEquals(bytes.length, httpentity.getContentLength()); Assert.assertEquals(inStream, httpentity.getContent()); Assert.assertNotNull(httpentity.getContent()); Assert.assertFalse(httpentity.isRepeatable()); Assert.assertTrue(httpentity.isStreaming()); }
@Test(expected = IllegalArgumentException.class) public void testWriteToNull() throws Exception { final InputStream inStream = new ByteArrayInputStream(new byte[0]); final InputStreamEntity httpentity = new InputStreamEntity(inStream, 0); httpentity.writeTo(null); } }
@Test public void testUnknownLengthConstructor() throws Exception { final InputStream inStream = new ByteArrayInputStream(new byte[0]); final InputStreamEntity httpentity = new InputStreamEntity(inStream); Assert.assertEquals(-1, httpentity.getContentLength()); }
/** * @param inStream input stream * @param length of the input stream, {@code -1} if unknown * @param contentType for specifying the {@code Content-Type} header, may be {@code null} * @throws IllegalArgumentException if {@code inStream} is {@code null} * @since 4.2 */ public InputStreamEntity(final InputStream inStream, final long length, final ContentType contentType) { super(); this.content = Args.notNull(inStream, "Source input stream"); this.length = length; if (contentType != null) { setContentType(contentType.toString()); } }
@Test public void testWriteToUnknownLength() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final InputStreamEntity httpentity = new InputStreamEntity(inStream); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(bytes.length, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message, s); }
/** * @param inStream input stream * @param length of the input stream, {@code -1} if unknown * @param contentType for specifying the {@code Content-Type} header, may be {@code null} * @throws IllegalArgumentException if {@code inStream} is {@code null} * @since 4.2 */ public InputStreamEntity(final InputStream inStream, final long length, final ContentType contentType) { super(); this.content = Args.notNull(inStream, "Source input stream"); this.length = length; if (contentType != null) { setContentType(contentType.toString()); } }
public Request bodyStream(final InputStream inStream, final ContentType contentType) { return body(new InputStreamEntity(inStream, -1, contentType)); }
@Test public void testWriteToPartialContent() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final int contentLength = 7; final InputStreamEntity httpentity = new InputStreamEntity(inStream, contentLength); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(contentLength, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message.substring(0, contentLength), s); }
@Test public void testConsumesErrorResponseWhenServingStale() throws Exception{ final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L); final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest(); final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo, "public, max-age=5, stale-if-error=60"); backendExpectsAnyRequestAndReturn(resp1); final ClassicHttpRequest req2 = HttpTestUtils.makeDefaultRequest(); final ClassicHttpResponse resp2 = HttpTestUtils.make500Response(); final byte[] body101 = HttpTestUtils.getRandomBytes(101); final ByteArrayInputStream buf = new ByteArrayInputStream(body101); final ConsumableInputStream cis = new ConsumableInputStream(buf); final HttpEntity entity = new InputStreamEntity(cis, 101); resp2.setEntity(entity); backendExpectsAnyRequestAndReturn(resp2); replayMocks(); execute(req1); execute(req2); verifyMocks(); assertTrue(cis.wasClosed()); }
@Test public void testWriteTo() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(bytes.length, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message, s); }
resp1.setEntity(new InputStreamEntity(new InputStream() { private Throwable closed;
public Request bodyStream(final InputStream inStream, final ContentType contentType) { return body(new InputStreamEntity(inStream, -1, contentType)); }
public Request bodyStream(final InputStream inStream) { return body(new InputStreamEntity(inStream, -1, null)); }
resp1.setEntity(new InputStreamEntity(new InputStream() { private boolean closed = false;
@Test(expected = IllegalArgumentException.class) public void testIllegalConstructor() throws Exception { new InputStreamEntity(null, 0); }
request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE); final InputStream inStream = Mockito.mock(InputStream.class); final InputStreamEntity entity = new InputStreamEntity(inStream, -1); request.setEntity(entity);
final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final InputStream inStream = Mockito.mock(InputStream.class); final InputStreamEntity entity = new InputStreamEntity(inStream, -1); request.setEntity(entity);
@Test public void testWriteToBuffered() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1); final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity); ByteArrayOutputStream out = new ByteArrayOutputStream(); bufentity.writeTo(out); byte[] bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } out = new ByteArrayOutputStream(); bufentity.writeTo(out); bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } try { bufentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testBufferingEntity() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1); final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity); Assert.assertEquals(bytes.length, bufentity.getContentLength()); Assert.assertTrue(bufentity.isRepeatable()); Assert.assertFalse(bufentity.isChunked()); Assert.assertFalse(bufentity.isStreaming()); // test if we can obtain contain multiple times Assert.assertNotNull(bufentity.getContent ()); Assert.assertNotNull(bufentity.getContent ()); }