use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncContextListenersTest method testListenerAddedFromListener.
@SuppressWarnings("Duplicates")
@Test
public void testListenerAddedFromListener() throws Exception {
final AtomicReference<CountDownLatch> completes = new AtomicReference<>(new CountDownLatch(1));
String path = "/path";
prepare(path, new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync(request, response);
asyncContext.addListener(new AsyncListener() {
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
// This method should not be invoked because we add the
// listener *after* having called startAsync(), but we
// add a listener to be sure it's not called (it will
// screw up the completes count and test will fail).
event.getAsyncContext().addListener(this);
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
completes.get().countDown();
}
@Override
public void onTimeout(AsyncEvent event) throws IOException {
}
@Override
public void onError(AsyncEvent event) throws IOException {
}
});
asyncContext.complete();
}
});
try (Socket socket = new Socket("localhost", _connector.getLocalPort())) {
OutputStream output = socket.getOutputStream();
String request = "" + "GET " + path + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
HttpTester.Input input = HttpTester.from(socket.getInputStream());
HttpTester.Response response = HttpTester.parseResponse(input);
Assert.assertEquals(200, response.getStatus());
completes.get().await(10, TimeUnit.SECONDS);
// Send a second request
completes.set(new CountDownLatch(1));
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
response = HttpTester.parseResponse(input);
Assert.assertEquals(200, response.getStatus());
completes.get().await(10, TimeUnit.SECONDS);
}
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncListenerTest method test_StartAsync_Throw_OnError.
private void test_StartAsync_Throw_OnError(IOConsumer<AsyncEvent> consumer) throws Exception {
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/ctx");
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
asyncContext.addListener(new AsyncListenerAdapter() {
@Override
public void onError(AsyncEvent event) throws IOException {
consumer.accept(event);
}
});
throw new QuietServletException(new TestRuntimeException());
}
}), "/path/*");
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpStatus.OK_200);
}
}), "/dispatch/*");
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().print("CUSTOM");
}
}), "/error/*");
startServer(context);
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncListenerTest method test_StartAsync_OnTimeout.
private void test_StartAsync_OnTimeout(long timeout, IOConsumer<AsyncEvent> consumer) throws Exception {
ServletContextHandler context = new ServletContextHandler();
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(timeout);
asyncContext.addListener(new AsyncListenerAdapter() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
consumer.accept(event);
}
});
}
}), "/*");
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpStatus.OK_200);
}
}), "/dispatch/*");
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().print("CUSTOM");
}
}), "/error/*");
startServer(context);
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class HTTP2Test method testCustomResponseCode.
@Test
public void testCustomResponseCode() throws Exception {
final int status = 475;
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(status);
}
});
Session session = newClient(new Session.Listener.Adapter());
HttpFields fields = new HttpFields();
MetaData.Request metaData = newRequest("GET", fields);
HeadersFrame frame = new HeadersFrame(metaData, null, true);
final CountDownLatch latch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(status, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class HTTP2Test method testHostHeader.
@Test
public void testHostHeader() throws Exception {
final String host = "fooBar";
final int port = 1313;
final String authority = host + ":" + port;
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Assert.assertEquals(host, request.getServerName());
Assert.assertEquals(port, request.getServerPort());
Assert.assertEquals(authority, request.getHeader("Host"));
}
});
Session session = newClient(new Session.Listener.Adapter());
HostPortHttpField hostHeader = new HostPortHttpField(authority);
MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, hostHeader, servletPath, HttpVersion.HTTP_2, new HttpFields());
HeadersFrame frame = new HeadersFrame(metaData, null, true);
final CountDownLatch latch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(200, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations