use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postWithHeadersAndFormParams.
@Test
public void postWithHeadersAndFormParams() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
Map<String, List<String>> m = new HashMap<>();
for (int i = 0; i < 5; i++) {
m.put("param_" + i, Arrays.asList("value_" + i));
}
Request request = post(getTargetUrl()).setHeaders(h).setFormParams(m).build();
server.enqueueEcho();
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
for (int i = 1; i < 5; i++) {
assertEquals(response.getHeader("X-param_" + i), "value_" + i);
}
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method cancellingFutureNotifiesOnThrowableWithCancellationException.
@Test
public void cancellingFutureNotifiesOnThrowableWithCancellationException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
h.add("X-Delay", 2_000);
CountDownLatch latch = new CountDownLatch(1);
Future<Response> future = client.preparePost(getTargetUrl()).setHeaders(h).setBody("Body").execute(new AsyncCompletionHandlerAdapter() {
@Override
public void onThrowable(Throwable t) {
if (t instanceof CancellationException) {
latch.countDown();
}
}
});
future.cancel(true);
if (!latch.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
});
});
}
use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method configTimeoutNotifiesOnThrowableAndFuture.
@Test(expectedExceptions = TimeoutException.class)
public void configTimeoutNotifiesOnThrowableAndFuture() throws Throwable {
withClient(config().setRequestTimeout(1_000)).run(client -> {
withServer(server).run(server -> {
HttpHeaders headers = new DefaultHttpHeaders();
headers.add("X-Delay", 5_000);
final AtomicBoolean onCompletedWasNotified = new AtomicBoolean();
final AtomicBoolean onThrowableWasNotifiedWithTimeoutException = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
server.enqueueEcho();
Future<Response> whenResponse = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
onCompletedWasNotified.set(true);
latch.countDown();
return response;
}
@Override
public void onThrowable(Throwable t) {
onThrowableWasNotifiedWithTimeoutException.set(t instanceof TimeoutException);
latch.countDown();
}
});
if (!latch.await(TIMEOUT, SECONDS)) {
fail("Timed out");
}
assertFalse(onCompletedWasNotified.get());
assertTrue(onThrowableWasNotifiedWithTimeoutException.get());
try {
whenResponse.get(TIMEOUT, SECONDS);
} catch (ExecutionException e) {
throw e.getCause();
}
});
});
}
use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method futureTimeOutThrowsTimeoutException.
@Test(expectedExceptions = TimeoutException.class)
public void futureTimeOutThrowsTimeoutException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders headers = new DefaultHttpHeaders();
headers.add("X-Delay", 5_000);
server.enqueueEcho();
Future<Response> future = client.prepareGet(getTargetUrl()).setHeaders(headers).execute(new AsyncCompletionHandlerAdapter() {
@Override
public void onThrowable(Throwable t) {
}
});
future.get(2, SECONDS);
});
});
}
use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method putFormParametersAsBodyStream.
@Test
public void putFormParametersAsBodyStream() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
sb.append("param_").append(i).append("=value_").append(i).append("&");
}
sb.setLength(sb.length() - 1);
ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes());
server.enqueueEcho();
client.preparePut(getTargetUrl()).setHeaders(h).setBody(is).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
for (int i = 1; i < 5; i++) {
assertEquals(response.getHeader("X-param_" + i), "value_" + i);
}
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
Aggregations