use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class FilterTest method replayHeaderResponseFilterTest.
@Test(groups = "standalone")
public void replayHeaderResponseFilterTest() throws Exception {
final AtomicBoolean replay = new AtomicBoolean(true);
ResponseFilter responseFilter = new ResponseFilter() {
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
if (ctx.getResponseHeaders() != null && ctx.getResponseHeaders().getHeaders().get("Ping").equals("Pong") && replay.getAndSet(false)) {
Request request = new RequestBuilder(ctx.getRequest()).addHeader("Ping", "Pong").build();
return new FilterContext.FilterContextBuilder<T>().asyncHandler(ctx.getAsyncHandler()).request(request).replayRequest(true).build();
}
return ctx;
}
};
try (AsyncHttpClient c = asyncHttpClient(config().addResponseFilter(responseFilter))) {
Response response = c.preparePost(getTargetUrl()).addHeader("Ping", "Pong").execute().get();
assertNotNull(response);
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getHeader("Ping"), "Pong");
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrickWithFailure.
@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
public void deferredInputStreamTrickWithFailure() throws Throwable {
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
Future<Response> f = r.execute(bdah);
BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
Response resp = is.getAsapResponse();
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
// "consume" the body, but our code needs input stream
CountingOutputStream cos = new CountingOutputStream();
try {
try {
copy(is, cos);
} finally {
is.close();
cos.close();
}
} catch (IOException e) {
throw e.getCause();
}
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class BodyDeferringAsyncHandlerTest method deferredSimple.
@Test(groups = "standalone")
public void deferredSimple() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredSimple");
CountingOutputStream cos = new CountingOutputStream();
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
Future<Response> f = r.execute(bdah);
Response resp = bdah.getResponse();
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
// we got headers only, it's probably not all yet here (we have BIG file
// downloading)
assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE);
// now be polite and wait for body arrival too (otherwise we would be
// dropping the "line" on server)
f.get();
// it all should be here now
assertEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class InputStreamTest method testInvalidInputStream.
@Test(groups = "standalone")
public void testInvalidInputStream() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient c = asyncHttpClient()) {
HttpHeaders h = new DefaultHttpHeaders().add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
InputStream is = new InputStream() {
public int readAllowed;
@Override
public int available() {
// Fake
return 1;
}
@Override
public int read() throws IOException {
int fakeCount = readAllowed++;
if (fakeCount == 0) {
return (int) 'a';
} else if (fakeCount == 1) {
return (int) 'b';
} else if (fakeCount == 2) {
return (int) 'c';
} else {
return -1;
}
}
};
Response resp = c.preparePost(getTargetUrl()).setHeaders(h).setBody(is).execute().get();
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader("X-Param"), "abc");
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class PutLargeFileTest method testPutSmallFile.
@Test(groups = "standalone")
public void testPutSmallFile() throws Exception {
File file = createTempFile(1024);
try (AsyncHttpClient client = asyncHttpClient()) {
Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get();
assertEquals(response.getStatusCode(), 200);
}
}
Aggregations