use of java.io.InputStream in project jetty.project by eclipse.
the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingSmallContent.
@Test
public void testProxyRequestFailureInTheMiddleOfProxyingSmallContent() throws Exception {
final CountDownLatch chunk1Latch = new CountDownLatch(1);
final int chunk1 = 'q';
final int chunk2 = 'w';
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
response.flushBuffer();
// Wait for the client to receive this chunk.
await(chunk1Latch, 5000);
// Send second chunk, must not be received by proxy.
output.write(chunk2);
}
private boolean await(CountDownLatch latch, long ms) throws IOException {
try {
return latch.await(ms, TimeUnit.MILLISECONDS);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
final long proxyTimeout = 1000;
Map<String, String> proxyParams = new HashMap<>();
proxyParams.put("timeout", String.valueOf(proxyTimeout));
startProxy(proxyParams);
startClient();
InputStreamResponseListener listener = new InputStreamResponseListener();
int port = serverConnector.getLocalPort();
client.newRequest("localhost", port).send(listener);
// Make the proxy request fail; given the small content, the
// proxy-to-client response is not committed yet so it will be reset.
TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(504, response.getStatus());
// Make sure there is no content, as the proxy-to-client response has been reset.
InputStream input = listener.getInputStream();
Assert.assertEquals(-1, input.read());
chunk1Latch.countDown();
// Result succeeds because a 504 is a valid HTTP response.
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertTrue(result.isSucceeded());
// Make sure the proxy does not receive chunk2.
Assert.assertEquals(-1, input.read());
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of java.io.InputStream in project jetty.project by eclipse.
the class ConnectHandlerTest method testCONNECTAndGETPipelined.
@Test
public void testCONNECTAndGETPipelined() throws Exception {
String hostPort = "localhost:" + serverConnector.getLocalPort();
String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n" + "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
try (Socket socket = newSocket()) {
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 200 OK from the CONNECT request
HttpTester.Response response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
// The pipelined request must have gone up to the server as is
response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals("GET /echo", response.getContent());
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class ConnectHandlerTest method testCONNECTAndMultipleGETs.
@Test
public void testCONNECTAndMultipleGETs() throws Exception {
String hostPort = "localhost:" + serverConnector.getLocalPort();
String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
try (Socket socket = newSocket()) {
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 200 OK from the CONNECT request
HttpTester.Response response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
for (int i = 0; i < 10; ++i) {
request = "" + "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals("GET /echo", response.getContent());
}
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class ConnectHandlerTest method testCONNECT10AndGET.
@Test
public void testCONNECT10AndGET() throws Exception {
String hostPort = "localhost:" + serverConnector.getLocalPort();
String request = "" + "CONNECT " + hostPort + " HTTP/1.0\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
try (Socket socket = newSocket()) {
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 200 OK from the CONNECT request
HttpTester.Response response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
request = "" + "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals("GET /echo", response.getContent());
}
}
use of java.io.InputStream in project jetty.project by eclipse.
the class ConnectHandlerTest method testCONNECTAndGETServerStop.
@Test
public void testCONNECTAndGETServerStop() throws Exception {
String hostPort = "localhost:" + serverConnector.getLocalPort();
String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
try (Socket socket = newSocket()) {
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 200 OK from the CONNECT request
HttpTester.Response response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
request = "" + "GET /echo HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals("GET /echo", response.getContent());
// Idle server is shut down
disposeServer();
int read = input.read();
Assert.assertEquals(-1, read);
}
}
Aggregations