use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestResponseNoContent.
@Test
public void testRequestResponseNoContent() throws Exception {
final CountDownLatch latch = new CountDownLatch(3);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
latch.countDown();
}
});
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
MetaData.Request metaData = newRequest("GET", new HttpFields());
generator.control(lease, new HeadersFrame(1, metaData, null, true));
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
final AtomicReference<HeadersFrame> frameRef = new AtomicReference<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onSettings(SettingsFrame frame) {
latch.countDown();
}
@Override
public void onHeaders(HeadersFrame frame) {
frameRef.set(frame);
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
HeadersFrame response = frameRef.get();
Assert.assertNotNull(response);
MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
Assert.assertEquals(200, responseMetaData.getStatus());
}
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class HTTP2ServerTest method testCommitFailure.
@Test
public void testCommitFailure() throws Exception {
final long delay = 1000;
final AtomicBoolean broken = new AtomicBoolean();
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Wait for the SETTINGS frames to be exchanged.
Thread.sleep(delay);
broken.set(true);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
server.stop();
ServerConnector connector2 = new ServerConnector(server, new HTTP2ServerConnectionFactory(new HttpConfiguration())) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {
@Override
public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException {
if (broken.get())
callback.failed(new IOException("explicitly_thrown_by_test"));
else
super.write(callback, buffers);
}
};
}
};
server.addConnector(connector2);
server.start();
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
MetaData.Request metaData = newRequest("GET", new HttpFields());
generator.control(lease, new HeadersFrame(1, metaData, null, true));
try (Socket client = new Socket("localhost", connector2.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
// The server will close the connection abruptly since it
// cannot write and therefore cannot even send the GO_AWAY.
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
boolean closed = parseResponse(client, parser, 2 * delay);
Assert.assertTrue(closed);
}
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testRequestHasHTTP2Version.
@Test
public void testRequestHasHTTP2Version() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
HttpVersion version = HttpVersion.fromString(request.getProtocol());
response.setStatus(version == HttpVersion.HTTP_2 ? HttpStatus.OK_200 : HttpStatus.INTERNAL_SERVER_ERROR_500);
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).onRequestBegin(request -> {
if (request.getVersion() != HttpVersion.HTTP_2)
request.abort(new Exception("Not a HTTP/2 request"));
}).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testRequestViaForwardHttpProxy.
@Test
public void testRequestViaForwardHttpProxy() throws Exception {
String path = "/path";
String query = "a=b";
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
Assert.assertEquals(query, request.getQueryString());
}
});
int proxyPort = connector.getLocalPort();
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyPort));
// Any port will do, just not the same as the proxy.
int serverPort = proxyPort + 1;
ContentResponse response = client.newRequest("localhost", serverPort).path(path + "?" + query).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class MaxConcurrentStreamsTest method testTwoConcurrentStreamsThirdWaits.
@Test
public void testTwoConcurrentStreamsThirdWaits() throws Exception {
int maxStreams = 2;
long sleep = 1000;
start(maxStreams, new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
sleep(sleep);
}
});
client.setMaxConnectionsPerDestination(1);
primeConnection();
// Send requests up to the max allowed.
for (int i = 0; i < maxStreams; ++i) {
client.newRequest("localhost", connector.getLocalPort()).path("/" + i).send(null);
}
// Send the request in excess.
CountDownLatch latch = new CountDownLatch(1);
String path = "/excess";
client.newRequest("localhost", connector.getLocalPort()).path(path).send(result -> {
if (result.getResponse().getStatus() == HttpStatus.OK_200)
latch.countDown();
});
// The last exchange should remain in the queue.
HttpDestinationOverHTTP2 destination = (HttpDestinationOverHTTP2) client.getDestination("http", "localhost", connector.getLocalPort());
Assert.assertEquals(1, destination.getHttpExchanges().size());
Assert.assertEquals(path, destination.getHttpExchanges().peek().getRequest().getPath());
Assert.assertTrue(latch.await(5 * sleep, TimeUnit.MILLISECONDS));
}
Aggregations