use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class BodyDeferringAsyncHandlerTest method testConnectionRefused.
@Test(groups = "standalone", expectedExceptions = IOException.class)
public void testConnectionRefused() throws IOException, ExecutionException, TimeoutException, InterruptedException {
int newPortWithoutAnyoneListening = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
BoundRequestBuilder r = client.prepareGet("http://localhost:" + newPortWithoutAnyoneListening + "/testConnectionRefused");
CountingOutputStream cos = new CountingOutputStream();
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
r.execute(bdah);
bdah.getResponse();
}
}
use of org.asynchttpclient.AsyncHttpClient 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.AsyncHttpClient 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.AsyncHttpClient 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);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class PutLargeFileTest method testPutLargeFile.
@Test(groups = "standalone")
public void testPutLargeFile() throws Exception {
File file = createTempFile(1024 * 1024);
int timeout = (int) file.length() / 1000;
try (AsyncHttpClient client = asyncHttpClient(config().setConnectTimeout(timeout))) {
Response response = client.preparePut(getTargetUrl()).setBody(file).execute().get();
assertEquals(response.getStatusCode(), 200);
}
}
Aggregations