use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileTest.
@Test
public void zeroCopyFileTest() throws IOException, ExecutionException, InterruptedException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
public void onThrowable(Throwable t) {
}
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
stream.write(bodyPart.getBodyPartBytes());
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus responseStatus) {
return State.CONTINUE;
}
public State onHeadersReceived(HttpHeaders headers) {
return State.CONTINUE;
}
public Response onCompleted() {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyFileWithBodyManipulationTest.
@Test
public void zeroCopyFileWithBodyManipulationTest() throws IOException, ExecutionException, InterruptedException {
File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
tmp.deleteOnExit();
try (AsyncHttpClient client = asyncHttpClient()) {
try (OutputStream stream = Files.newOutputStream(tmp.toPath())) {
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncHandler<Response>() {
public void onThrowable(Throwable t) {
}
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
stream.write(bodyPart.getBodyPartBytes());
if (bodyPart.getBodyPartBytes().length == 0) {
return State.ABORT;
}
return State.CONTINUE;
}
public State onStatusReceived(HttpResponseStatus responseStatus) {
return State.CONTINUE;
}
public State onHeadersReceived(HttpHeaders headers) {
return State.CONTINUE;
}
public Response onCompleted() {
return null;
}
}).get();
assertNull(resp);
assertEquals(SIMPLE_TEXT_FILE.length(), tmp.length());
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class InputStreamTest method testInvalidInputStream.
@Test
public void testInvalidInputStream() throws IOException, ExecutionException, InterruptedException {
try (AsyncHttpClient c = asyncHttpClient()) {
HttpHeaders h = new DefaultHttpHeaders().add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
InputStream is = new InputStream() {
int readAllowed;
@Override
public int available() {
// Fake
return 1;
}
@Override
public int read() {
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.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class MultipartPartTest method transferToShouldWriteStringPart.
@Test
public void transferToShouldWriteStringPart() throws IOException, URISyntaxException {
String text = FileUtils.readFileToString(TestUtils.resourceAsFile("test_sample_message.eml"), UTF_8);
List<Part> parts = new ArrayList<>();
parts.add(new StringPart("test_sample_message.eml", text));
HttpHeaders headers = new DefaultHttpHeaders();
headers.set("Cookie", "open-xchange-public-session-d41d8cd98f00b204e9800998ecf8427e=bfb98150b24f42bd844fc9ef2a9eaad5; open-xchange-secret-TSlq4Cm4nCBnDpBL1Px2A=9a49b76083e34c5ba2ef5c47362313fd; JSESSIONID=6883138728830405130.OX2");
headers.set("Content-Length", "9241");
headers.set("Content-Type", "multipart/form-data; boundary=5gigAKQyqDCVdlZ1fCkeLlEDDauTNoOOEhRnFg");
headers.set("Host", "appsuite.qa.open-xchange.com");
headers.set("Accept", "*/*");
String boundary = "uwyqQolZaSmme019O2kFKvAeHoC14Npp";
List<MultipartPart<? extends Part>> multipartParts = MultipartUtils.generateMultipartParts(parts, boundary.getBytes());
try (MultipartBody multipartBody = new MultipartBody(multipartParts, "multipart/form-data; boundary=" + boundary, boundary.getBytes())) {
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(8 * 1024);
multipartBody.transferTo(byteBuf);
try {
byteBuf.toString(StandardCharsets.UTF_8);
} finally {
byteBuf.release();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project async-http-client by AsyncHttpClient.
the class NettyAsyncResponseTest method testCookieParseExpires.
@Test
public void testCookieParseExpires() {
// e.g. "Tue, 27 Oct 2015 12:54:24 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(System.currentTimeMillis() + 60000);
final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef);
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
List<Cookie> cookies = response.getCookies();
assertEquals(cookies.size(), 1);
Cookie cookie = cookies.get(0);
assertTrue(cookie.maxAge() >= 58 && cookie.maxAge() <= 60);
}
Aggregations