use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.
@Test
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, InterruptedException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
public void onThrowable(Throwable t) {
}
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
stream.write(bodyPart.getBodyPartBytes());
if (bodyPart.getBodyPartBytes().length == 0) {
return State.ABORT;
}
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus responseStatus) {
return State.CONTINUE;
}
public State onHeadersReceived(HttpHeaders headers) {
return State.CONTINUE;
}
public Response onCompleted() {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileTest.
@Test
public void zeroCopyFileTest() throws IOException, ExecutionException, InterruptedException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
public void onThrowable(Throwable t) {
}
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
stream.write(bodyPart.getBodyPartBytes());
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus responseStatus) {
return State.CONTINUE;
}
public State onHeadersReceived(HttpHeaders headers) {
return State.CONTINUE;
}
public Response onCompleted() {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class MultipartPartTest method transferToShouldWriteStringPart.
@Test
public void transferToShouldWriteStringPart() throws IOException, URISyntaxException {
String text = FileUtils.readFileToString(TestUtils.resourceAsFile("test_sample_message.eml"), UTF_8);
List<Part> parts = new ArrayList<>();
parts.add(new StringPart("test_sample_message.eml", text));
HttpHeaders headers = new DefaultHttpHeaders();
headers.set("Cookie", "open-xchange-public-session-d41d8cd98f00b204e9800998ecf8427e=bfb98150b24f42bd844fc9ef2a9eaad5; open-xchange-secret-TSlq4Cm4nCBnDpBL1Px2A=9a49b76083e34c5ba2ef5c47362313fd; JSESSIONID=6883138728830405130.OX2");
headers.set("Content-Length", "9241");
headers.set("Content-Type", "multipart/form-data; boundary=5gigAKQyqDCVdlZ1fCkeLlEDDauTNoOOEhRnFg");
headers.set("Host", "appsuite.qa.open-xchange.com");
headers.set("Accept", "*/*");
String boundary = "uwyqQolZaSmme019O2kFKvAeHoC14Npp";
List<MultipartPart<? extends Part>> multipartParts = MultipartUtils.generateMultipartParts(parts, boundary.getBytes());
try (MultipartBody multipartBody = new MultipartBody(multipartParts, "multipart/form-data; boundary=" + boundary, boundary.getBytes())) {
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(8 * 1024);
multipartBody.transferTo(byteBuf);
try {
byteBuf.toString(StandardCharsets.UTF_8);
} finally {
byteBuf.release();
}
}
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ResumableAsyncHandlerTest method testOnHeadersReceived.
@Test
public void testOnHeadersReceived() throws Exception {
ResumableAsyncHandler handler = new ResumableAsyncHandler();
HttpHeaders responseHeaders = new DefaultHttpHeaders();
State status = handler.onHeadersReceived(responseHeaders);
assertEquals(status, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onHeadersReceived");
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class NettyAsyncResponseTest method testCookieParseExpires.
@Test
public void testCookieParseExpires() {
// e.g. "Tue, 27 Oct 2015 12:54:24 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(System.currentTimeMillis() + 60000);
final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef);
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
List<Cookie> cookies = response.getCookies();
assertEquals(cookies.size(), 1);
Cookie cookie = cookies.get(0);
assertTrue(cookie.maxAge() >= 58 && cookie.maxAge() <= 60);
}
Aggregations