use of org.eclipse.jetty.client.ContinueProtocolHandler in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithInitialAndConcurrentDeferredContent_Respond100Continue.
@Test
public void test_Expect100Continue_WithInitialAndConcurrentDeferredContent_Respond100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and echo the content
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
final byte[] chunk1 = new byte[] { 0, 1, 2, 3 };
final byte[] chunk2 = new byte[] { 4, 5, 6 };
final byte[] data = new byte[chunk1.length + chunk2.length];
System.arraycopy(chunk1, 0, data, 0, chunk1.length);
System.arraycopy(chunk2, 0, data, chunk1.length, chunk2.length);
final DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(chunk1));
client.getProtocolHandlers().put(new ContinueProtocolHandler() {
@Override
public Response.Listener getResponseListener() {
return new ContinueListener() {
@Override
public void onHeaders(Response response) {
super.onHeaders(response);
content.offer(ByteBuffer.wrap(chunk2));
content.close();
}
};
}
});
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(content).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertArrayEquals(data, getContent());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.ContinueProtocolHandler in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_During100Continue.
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_During100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and consume the content
IO.copy(request.getInputStream(), new ByteArrayOutputStream());
}
});
client.getProtocolHandlers().clear();
client.getProtocolHandlers().put(new ContinueProtocolHandler() {
@Override
public Response.Listener getResponseListener() {
final Response.Listener listener = super.getResponseListener();
return new Response.Listener.Adapter() {
@Override
public void onBegin(Response response) {
response.abort(new Exception());
}
@Override
public void onFailure(Response response, Throwable failure) {
listener.onFailure(response, failure);
}
};
}
});
byte[] content = new byte[1024];
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
Assert.assertNotNull(result.getRequestFailure());
Assert.assertNotNull(result.getResponseFailure());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations