use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class NettyAsyncResponseTest method testCookieParseExpires.
@Test(groups = "standalone")
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));
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(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);
}
use of org.asynchttpclient.HttpResponseHeaders 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();
HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
State status = handler.onHeadersReceived(headers);
assertEquals(status, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onHeadersReceived");
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileTest.
@Test(groups = "standalone")
public void zeroCopyFileTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (FileOutputStream stream = new FileOutputStream(tmp)) {
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) throws Exception {
return State.CONTINUE;
}
public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
return State.CONTINUE;
}
public Response onCompleted() throws Exception {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.
@Test(groups = "standalone")
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (FileOutputStream stream = new FileOutputStream(tmp)) {
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) throws Exception {
return State.CONTINUE;
}
public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
return State.CONTINUE;
}
public Response onCompleted() throws Exception {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class HttpHandler method handleChunk.
private //
void handleChunk(//
HttpContent chunk, //
final Channel channel, //
final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws IOException, Exception {
boolean abort = false;
boolean last = chunk instanceof LastHttpContent;
// Netty 4: the last chunk is not empty
if (last) {
LastHttpContent lastChunk = (LastHttpContent) chunk;
HttpHeaders trailingHeaders = lastChunk.trailingHeaders();
if (!trailingHeaders.isEmpty()) {
abort = handler.onHeadersReceived(new HttpResponseHeaders(trailingHeaders, true)) == State.ABORT;
}
}
ByteBuf buf = chunk.content();
if (!abort && !(handler instanceof StreamedAsyncHandler) && (buf.readableBytes() > 0 || last)) {
HttpResponseBodyPart bodyPart = config.getResponseBodyPartFactory().newResponseBodyPart(buf, last);
abort = handler.onBodyPartReceived(bodyPart) == State.ABORT;
}
if (abort || last) {
boolean keepAlive = !abort && future.isKeepAlive();
finishUpdate(future, channel, keepAlive, !last);
}
}
Aggregations