use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postFormParametersAsBodyStream.
@Test
public void postFormParametersAsBodyStream() 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);
server.enqueueEcho();
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8))).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);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postSingleStringPart.
@Test
public void postSingleStringPart() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
server.enqueueEcho();
client.preparePost(getTargetUrl()).addBodyPart(new StringPart("foo", "bar")).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
String requestContentType = response.getHeader("X-" + CONTENT_TYPE);
String boundary = requestContentType.substring((requestContentType.indexOf("boundary") + "boundary".length() + 1));
assertTrue(response.getResponseBody().regionMatches(false, "--".length(), boundary, 0, boundary.length()));
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method getUrlWithPathWithQuery.
@Test
public void getUrlWithPathWithQuery() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
String targetUrl = getTargetUrl() + "?q=+%20x";
Request request = get(targetUrl).build();
assertEquals(request.getUrl(), targetUrl);
server.enqueueOk();
Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get(TIMEOUT, SECONDS);
assertEquals(response.getUri().toUrl(), targetUrl);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postInputStreamWithContentLengthAsBodyGenerator.
@Test
public void postInputStreamWithContentLengthAsBodyGenerator() 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 {
assertNull(request.getHeader(TRANSFER_ENCODING.toString()));
assertEquals(request.getHeader(CONTENT_LENGTH.toString()), Integer.toString("{}".getBytes(StandardCharsets.ISO_8859_1).length));
chain.handle(target, request, httpServletRequest, httpServletResponse);
}
});
byte[] bodyBytes = "{}".getBytes(StandardCharsets.ISO_8859_1);
InputStream bodyStream = new ByteArrayInputStream(bodyBytes);
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new InputStreamBodyGenerator(bodyStream, bodyBytes.length)).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBody(), "{}");
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter 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);
});
});
}
Aggregations