/** * @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)); }
/** * @since 4.4 */ public Request bodyByteArray(final byte[] b, final ContentType contentType) { return body(new ByteArrayEntity(b, contentType)); }
public Request bodyByteArray(final byte[] b) { return body(new ByteArrayEntity(b)); }
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)); }
final ByteArrayEntity bae = new ByteArrayEntity(data); if (entity != null) { bae.setContentType(entity.getContentType());
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; } }
private static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse, final ExecChain.Scope scope) { if (cacheResponse == null) { return null; } final ClassicHttpResponse response = new BasicClassicHttpResponse(cacheResponse.getCode(), cacheResponse.getReasonPhrase()); for (final Iterator<Header> it = cacheResponse.headerIterator(); it.hasNext(); ) { response.addHeader(it.next()); } response.setVersion(cacheResponse.getVersion() != null ? cacheResponse.getVersion() : HttpVersion.DEFAULT); final SimpleBody body = cacheResponse.getBody(); if (body != null) { if (body.isText()) { response.setEntity(new StringEntity(body.getBodyText(), body.getContentType())); } else { response.setEntity(new ByteArrayEntity(body.getBodyBytes(), body.getContentType())); } } scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); return response; }
@Test public void test206ResponsesAreNotCachedIfTheCacheDoesNotSupportRangeAndContentRangeHeaders() throws Exception { if (!supportsRangeAndContentRangeHeaders(impl)) { emptyMockCacheExpectsNoPuts(); request = new BasicClassicHttpRequest("GET", "/"); request.addHeader("Range", "bytes=0-50"); originResponse = new BasicClassicHttpResponse(HttpStatus.SC_PARTIAL_CONTENT,"Partial Content"); originResponse.setHeader("Content-Range", "bytes 0-50/128"); originResponse.setHeader("Cache-Control", "max-age=3600"); final byte[] bytes = new byte[51]; new Random().nextBytes(bytes); originResponse.setEntity(new ByteArrayEntity(bytes)); EasyMock.expect( mockExecChain.proceed( EasyMock.isA(ClassicHttpRequest.class), EasyMock.isA(ExecChain.Scope.class))).andReturn(originResponse); replayMocks(); execute(request); verifyMocks(); } }
validated.setHeader("Last-Modified", DateUtils.formatDate(oneYearAgo)); validated.setHeader("Content-Length", "128"); validated.setEntity(new ByteArrayEntity(bytes));
/** * @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)); }
bytes1[i] = (byte) 1; resp1.setEntity(new ByteArrayEntity(bytes1)); bytes2[i] = (byte) 2; resp2.setEntity(new ByteArrayEntity(bytes2)); bytes3[i] = (byte) 2; resp3.setEntity(new ByteArrayEntity(bytes3));
bytes1[i] = (byte) 1; resp1.setEntity(new ByteArrayEntity(bytes1)); bytes2[i] = (byte) 2; resp2.setEntity(new ByteArrayEntity(bytes2)); bytes3[i] = (byte) 2; resp3.setEntity(new ByteArrayEntity(bytes3));
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)); }
@Test(expected=IndexOutOfBoundsException.class) public void testIllegalConstructorBadOff1() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); new ByteArrayEntity(bytes, -1, bytes.length); }
@Test(expected=IndexOutOfBoundsException.class) public void testIllegalConstructorBadOff2() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); new ByteArrayEntity(bytes, bytes.length + 1, bytes.length); }
@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 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()); }