use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method test_POST_WithContent_NotifiesRequestContentListener.
@Test
public void test_POST_WithContent_NotifiesRequestContentListener() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
consume(request.getInputStream(), true);
}
});
final byte[] content = { 0, 1, 2, 3 };
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent((request, buffer) -> {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
if (!Arrays.equals(content, bytes))
request.abort(new Exception());
}).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method testHeaderProcessing.
@Test
public void testHeaderProcessing() throws Exception {
final String headerName = "X-Header-Test";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setHeader(headerName, "X");
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseHeader((response1, field) -> !field.getName().equals(headerName)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(headerName));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange.
@Test
public void test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange() throws Exception {
byte[] data = new byte[128 * 1024];
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletInputStream input = request.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
response.setContentLength(data.length);
response.getOutputStream().write(data);
try {
// Delay the server from sending the TCP FIN.
Thread.sleep(1000);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(new StringContentProvider("0")).onRequestSuccess(request -> {
HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
}).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertArrayEquals(data, response.getContent());
Assert.assertEquals(0, connectionPool.getConnectionCount());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerNoConnectionClose_ClientCloses.
@Test
public void test_ClientConnectionClose_ServerNoConnectionClose_ClientCloses() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setContentLength(0);
response.flushBuffer();
try {
// Delay the server from sending the TCP FIN.
Thread.sleep(1000);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).onRequestSuccess(request -> {
HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
}).onResponseHeaders(r -> r.getHeaders().remove(HttpHeader.CONNECTION)).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(0, connectionPool.getConnectionCount());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ContentResponseTest method testResponseWithoutContentType.
@Test
public void testResponseWithoutContentType() throws Exception {
final byte[] content = new byte[1024];
new Random().nextBytes(content);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.getOutputStream().write(content);
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(content, response.getContent());
Assert.assertNull(response.getMediaType());
Assert.assertNull(response.getEncoding());
}
Aggregations