/** * @since 4.4 */ public Request bodyByteArray(final byte[] b, final ContentType contentType) { return body(new ByteArrayEntity(b, contentType)); }
final ByteArrayEntity bae = new ByteArrayEntity(data); if (entity != null) { bae.setContentType(entity.getContentType());
@Test public void testBasics() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final ByteArrayEntity httpentity = new ByteArrayEntity(bytes); Assert.assertEquals(bytes.length, httpentity.getContentLength()); Assert.assertNotNull(httpentity.getContent()); Assert.assertTrue(httpentity.isRepeatable()); Assert.assertFalse(httpentity.isStreaming()); }
@Test public void testTerminateRequestContentLengthLong() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.addHeader("Content-Length", "3000"); final ByteArrayEntity entity = new ByteArrayEntity(new byte[3000], ContentType.TEXT_PLAIN); entity.setChunked(false); request.setEntity(entity); conn.sendRequestHeader(request); conn.terminateRequest(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: " + "3000\r\n\r\n", s); Assert.assertFalse(conn.isConsistent()); }
@Test public void testWriteTo() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final ByteArrayEntity httpentity = new ByteArrayEntity(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.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(); httpentity.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 { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
/** * @since 4.2 */ public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) < 0) || ((off + len) > b.length)) { throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length); } this.b = b; this.off = off; this.len = len; if (contentType != null) { setContentType(contentType.toString()); } }
@Test public void testBasicOffLen() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, 8, 7); Assert.assertEquals(7, httpentity.getContentLength()); Assert.assertNotNull(httpentity.getContent()); Assert.assertTrue(httpentity.isRepeatable()); Assert.assertFalse(httpentity.isStreaming()); }
@Test public void testWrappingEntity() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final ByteArrayEntity httpentity = new ByteArrayEntity(bytes); httpentity.setChunked(true); final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity); Assert.assertEquals(bytes.length, bufentity.getContentLength()); Assert.assertTrue(bufentity.isRepeatable()); Assert.assertTrue(bufentity.isChunked()); Assert.assertFalse(bufentity.isStreaming()); // test if we can obtain contain multiple times Assert.assertNotNull(bufentity.getContent ()); Assert.assertNotNull(bufentity.getContent ()); }
@Test public void testWriteToOffLen() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final int off = 8; final int len = 7; final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, off, len); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); byte[] bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(len, bytes2.length); for (int i = 0; i < len; i++) { Assert.assertEquals(bytes[i+off], bytes2[i]); } out = new ByteArrayOutputStream(); httpentity.writeTo(out); bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(len, bytes2.length); for (int i = 0; i < len; i++) { Assert.assertEquals(bytes[i+off], bytes2[i]); } try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
/** * @since 4.2 */ public ByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = this.b.length; if (contentType != null) { setContentType(contentType.toString()); } }
/** * @since 4.4 */ public Request bodyByteArray(final byte[] b, final int off, final int len, final ContentType contentType) { return body(new ByteArrayEntity(b, off, len, contentType)); }
public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
/** * @since 4.2 */ public ByteArrayEntity(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.b = b; this.off = 0; this.len = this.b.length; if (contentType != null) { setContentType(contentType.toString()); } }
public Request bodyByteArray(final byte[] b) { return body(new ByteArrayEntity(b)); }
public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
/** * @since 4.2 */ public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) < 0) || ((off + len) > b.length)) { throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length); } this.b = b; this.off = off; this.len = len; if (contentType != null) { setContentType(contentType.toString()); } }
public Request bodyByteArray(final byte[] b, final int off, final int len) { return body(new ByteArrayEntity(b, off, len)); }
public Request bodyString(final String s, final ContentType contentType) { final Charset charset = contentType != null ? contentType.getCharset() : null; final byte[] raw = charset != null ? s.getBytes(charset) : s.getBytes(); return body(new ByteArrayEntity(raw, contentType)); }
private HttpEntity makeBody(final int nbytes) { final byte[] bytes = new byte[nbytes]; new Random().nextBytes(bytes); return new ByteArrayEntity(bytes); }
/** Generates a response body with random content. * @param nbytes length of the desired response body * @return an {@link HttpEntity} */ public static HttpEntity makeBody(final int nbytes) { return new ByteArrayEntity(getRandomBytes(nbytes)); }