use of javax.servlet.ServletException in project jetty.project by eclipse.
the class HttpClientTest method test_POST_WithParameters_WithContent.
@Test
public void test_POST_WithParameters_WithContent() throws Exception {
final byte[] content = { 0, 1, 2, 3 };
final String paramName = "a";
final String paramValue = "€";
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);
String value = request.getParameter(paramName);
if (paramValue.equals(value)) {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getOutputStream().write(content);
}
}
});
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(content, response.getContent());
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class HttpClientTest method testCustomHostHeader.
@Test
public void testCustomHostHeader() throws Exception {
final String host = "localhost";
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);
Assert.assertEquals(host, request.getServerName());
}
});
ContentResponse response = client.newRequest("http://127.0.0.1:" + connector.getLocalPort() + "/path").scheme(scheme).header(HttpHeader.HOST, host).send();
Assert.assertEquals(200, response.getStatus());
}
use of javax.servlet.ServletException 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 javax.servlet.ServletException in project jetty.project by eclipse.
the class HttpClientTest method testLongPollIsAbortedWhenClientIsStopped.
@Test
public void testLongPollIsAbortedWhenClientIsStopped() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
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);
request.startAsync();
latch.countDown();
}
});
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(result -> {
if (result.isFailed())
completeLatch.countDown();
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
// Stop the client, the complete listener must be invoked.
client.stop();
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ServletException 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));
}
Aggregations