use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class EmptyBodyTest method testEmptyBody.
@Test(groups = "standalone")
public void testEmptyBody() throws IOException {
try (AsyncHttpClient ahc = asyncHttpClient()) {
final AtomicBoolean err = new AtomicBoolean(false);
final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
final AtomicBoolean status = new AtomicBoolean(false);
final AtomicInteger headers = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(1);
ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {
public void onThrowable(Throwable t) {
fail("Got throwable.", t);
err.set(true);
}
public State onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
byte[] bytes = e.getBodyPartBytes();
if (bytes.length != 0) {
String s = new String(bytes);
logger.info("got part: {}", s);
logger.warn("Sampling stacktrace.", new Throwable("trace that, we should not get called for empty body."));
queue.put(s);
}
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus e) throws Exception {
status.set(true);
return AsyncHandler.State.CONTINUE;
}
public State onHeadersReceived(HttpResponseHeaders e) throws Exception {
if (headers.incrementAndGet() == 2) {
throw new Exception("Analyze this.");
}
return State.CONTINUE;
}
public Object onCompleted() throws Exception {
latch.countDown();
return null;
}
});
try {
assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
} catch (InterruptedException e) {
fail("Interrupted.", e);
}
assertFalse(err.get());
assertEquals(queue.size(), 0);
assertTrue(status.get());
assertEquals(headers.get(), 1);
}
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class ResumableAsyncHandlerTest method testOnHeadersReceivedContentLengthMinus.
@Test
public void testOnHeadersReceivedContentLengthMinus() throws Exception {
ResumableAsyncHandler handler = new ResumableAsyncHandler();
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(CONTENT_LENGTH, -1);
HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
State status = handler.onHeadersReceived(headers);
assertEquals(status, AsyncHandler.State.ABORT, "State should be ABORT for content length -1");
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class ResumableAsyncHandlerTest method testOnHeadersReceivedWithDecoratedAsyncHandler.
@Test
public void testOnHeadersReceivedWithDecoratedAsyncHandler() throws Exception {
HttpHeaders responseHeaders = new DefaultHttpHeaders();
HttpResponseHeaders headers = new HttpResponseHeaders(responseHeaders);
@SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class);
State mockState = mock(State.class);
when(decoratedAsyncHandler.onHeadersReceived(headers)).thenReturn(mockState);
ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler);
State status = handler.onHeadersReceived(headers);
assertEquals(status, mockState, "State should be equal to the state returned from decoratedAsyncHandler");
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class NettyAsyncResponseTest method testCookieParseWeirdExpiresValue.
@Test(groups = "standalone")
public void testCookieParseWeirdExpiresValue() {
final String cookieDef = "efmembercheck=true; expires=60; path=/; domain=.eclipse.org";
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);
assertEquals(cookie.maxAge(), Long.MIN_VALUE);
}
use of org.asynchttpclient.HttpResponseHeaders in project async-http-client by AsyncHttpClient.
the class NettyAsyncResponseTest method testCookieParseMaxAge.
@Test(groups = "standalone")
public void testCookieParseMaxAge() {
final String cookieDef = "efmembercheck=true; max-age=60; path=/; domain=.eclipse.org";
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);
assertEquals(cookie.maxAge(), 60);
}
Aggregations