use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class AsyncStreamHandlerTest method asyncStreamJustStatusLine.
@Test(timeOut = 3000)
public void asyncStreamJustStatusLine() throws Throwable {
withClient().run(client -> withServer(server).run(server -> {
server.enqueueEcho();
final int STATUS = 0;
final int COMPLETED = 1;
final int OTHER = 2;
final boolean[] whatCalled = new boolean[] { false, false, false };
final CountDownLatch latch = new CountDownLatch(1);
Future<Integer> statusCode = client.prepareGet(getTargetUrl()).execute(new AsyncHandler<Integer>() {
private int status = -1;
@Override
public void onThrowable(Throwable t) {
whatCalled[OTHER] = true;
latch.countDown();
}
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) {
whatCalled[OTHER] = true;
latch.countDown();
return State.ABORT;
}
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) {
whatCalled[STATUS] = true;
status = responseStatus.getStatusCode();
latch.countDown();
return State.ABORT;
}
@Override
public State onHeadersReceived(HttpHeaders headers) {
whatCalled[OTHER] = true;
latch.countDown();
return State.ABORT;
}
@Override
public Integer onCompleted() {
whatCalled[COMPLETED] = true;
latch.countDown();
return status;
}
});
if (!latch.await(2, TimeUnit.SECONDS)) {
fail("Timeout");
return;
}
Integer status = statusCode.get(TIMEOUT, TimeUnit.SECONDS);
assertEquals((int) status, 200, "Expected status code failed.");
if (!whatCalled[STATUS]) {
fail("onStatusReceived not called.");
}
if (!whatCalled[COMPLETED]) {
fail("onCompleted not called.");
}
if (whatCalled[OTHER]) {
fail("Other method of AsyncHandler got called.");
}
}));
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class AsyncStreamLifecycleTest method testStream.
@Test
public void testStream() throws Exception {
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 {
if (e.length() != 0) {
String s = new String(e.getBodyPartBytes());
logger.info("got part: {}", s);
queue.put(s);
}
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus e) {
status.set(true);
return State.CONTINUE;
}
public State onHeadersReceived(HttpHeaders e) throws Exception {
if (headers.incrementAndGet() == 2) {
throw new Exception("Analyze this.");
}
return State.CONTINUE;
}
public Object onCompleted() {
latch.countDown();
return null;
}
});
assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
assertFalse(err.get());
assertEquals(queue.size(), 2);
assertTrue(queue.contains("part1"));
assertTrue(queue.contains("part2"));
assertTrue(status.get());
assertEquals(headers.get(), 1);
}
}
use of io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.
@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
withClient().run(client -> withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
server.enqueue(new AbstractHandler() {
EchoHandler chain = new EchoHandler();
@Override
public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
assertNull(request.getHeader(CONTENT_LENGTH.toString()));
chain.handle(target, request, httpServletRequest, httpServletResponse);
}
});
server.enqueueEcho();
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBody(), "{}");
return response;
}
}).get(TIMEOUT, SECONDS);
}));
}
use of io.netty.handler.codec.http.HttpHeaders in project dubbo by alibaba.
the class HttpProcessHandler method http_200.
private static final FullHttpResponse http_200(String result) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes()));
HttpHeaders httpHeaders = response.headers();
httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
use of io.netty.handler.codec.http.HttpHeaders in project dubbo by alibaba.
the class HttpProcessHandler method http_500.
private static final FullHttpResponse http_500(String errorMessage) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(errorMessage.getBytes()));
HttpHeaders httpHeaders = response.headers();
httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
Aggregations