From b9a27f792a2d5e839172d452cf89babcbc8089fc Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 13 Jun 2016 16:24:53 +0200 Subject: [PATCH 1/4] Updated test to print out test method via TestTracker. --- .../org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java index 215926ef740..3d557cd462d 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java @@ -26,12 +26,16 @@ import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.toolchain.test.http.SimpleHttpParser; import org.eclipse.jetty.toolchain.test.http.SimpleHttpResponse; import org.junit.After; +import org.junit.Rule; public abstract class AbstractConnectHandlerTest { + @Rule + public final TestTracker tracker = new TestTracker(); protected Server server; protected ServerConnector serverConnector; protected Server proxy; From 2b539b6383d1ca0c4a4c0cf4e0ef4f42562a5b71 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 13 Jun 2016 17:54:41 +0200 Subject: [PATCH 2/4] Fixes #638 - ConnectHandler responses should have Content-Length. Added Content-Length: 0 to the responses. --- .../src/main/java/org/eclipse/jetty/proxy/ConnectHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ConnectHandler.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ConnectHandler.java index 643cf1f35b9..d7387f505ca 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ConnectHandler.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/ConnectHandler.java @@ -362,6 +362,7 @@ public class ConnectHandler extends HandlerWrapper try { response.setStatus(statusCode); + response.setContentLength(0); if (statusCode != HttpServletResponse.SC_OK) response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString()); response.getOutputStream().close(); From 7c0d1ea0264f6809046944f614856fab20c3d7a9 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 13 Jun 2016 17:55:03 +0200 Subject: [PATCH 3/4] Refactored tests to use HttpTester instead of SimpleHttpParser. --- jetty-proxy/pom.xml | 7 + .../proxy/AbstractConnectHandlerTest.java | 10 +- .../jetty/proxy/ConnectHandlerSSLTest.java | 43 ++-- .../jetty/proxy/ConnectHandlerTest.java | 193 +++++++++--------- 4 files changed, 125 insertions(+), 128 deletions(-) diff --git a/jetty-proxy/pom.xml b/jetty-proxy/pom.xml index 41a053959aa..6ee87cbd894 100644 --- a/jetty-proxy/pom.xml +++ b/jetty-proxy/pom.xml @@ -51,6 +51,13 @@ ${project.version} test + + org.eclipse.jetty + jetty-http + ${project.version} + tests + test + org.eclipse.jetty.toolchain jetty-test-helper diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java index 3d557cd462d..05a1ebf98a4 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java @@ -18,17 +18,16 @@ package org.eclipse.jetty.proxy; -import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.net.Socket; +import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.toolchain.test.TestTracker; -import org.eclipse.jetty.toolchain.test.http.SimpleHttpParser; -import org.eclipse.jetty.toolchain.test.http.SimpleHttpResponse; import org.junit.After; import org.junit.Rule; @@ -69,9 +68,10 @@ public abstract class AbstractConnectHandlerTest proxy.stop(); } - protected SimpleHttpResponse readResponse(BufferedReader reader) throws IOException + protected HttpTester.Response readResponse(InputStream inputStream) throws IOException { - return new SimpleHttpParser().readResponse(reader); + HttpTester.Input input = HttpTester.from(inputStream); + return HttpTester.parseResponse(input); } protected Socket newSocket() throws IOException diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java index a3379793439..126b9715941 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java @@ -18,11 +18,9 @@ package org.eclipse.jetty.proxy; -import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.StandardCharsets; @@ -35,12 +33,13 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; -import org.eclipse.jetty.toolchain.test.http.SimpleHttpResponse; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.junit.Assert; import org.junit.Before; @@ -77,23 +76,18 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - - // Be sure the buffered input does not have anything buffered - Assert.assertFalse(input.ready()); + HttpTester.Response response = readResponse(socket.getInputStream()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); // Upgrade the socket to SSL try (SSLSocket sslSocket = wrapSocket(socket)) { output = sslSocket.getOutputStream(); - input = new BufferedReader(new InputStreamReader(sslSocket.getInputStream())); request = "GET /echo HTTP/1.1\r\n" + @@ -102,9 +96,9 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); - response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + response = readResponse(sslSocket.getInputStream()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } } @@ -120,23 +114,18 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - - // Be sure the buffered input does not have anything buffered - Assert.assertFalse(input.ready()); + HttpTester.Response response = readResponse(socket.getInputStream()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); // Upgrade the socket to SSL try (SSLSocket sslSocket = wrapSocket(socket)) { output = sslSocket.getOutputStream(); - input = new BufferedReader(new InputStreamReader(sslSocket.getInputStream())); for (int i = 0; i < 10; ++i) { @@ -149,9 +138,9 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); - response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("POST /echo?param=" + i + "\r\nHELLO", response.getBody()); + response = readResponse(sslSocket.getInputStream()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("POST /echo?param=" + i + "\r\nHELLO", response.getContent()); } } } @@ -187,10 +176,14 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest while ((read = input.read()) >= 0) baos.write(read); baos.close(); + byte[] bytes = baos.toByteArray(); ServletOutputStream output = httpResponse.getOutputStream(); - output.println(builder.toString()); - output.write(baos.toByteArray()); + if (bytes.length == 0) + output.print(builder.toString()); + else + output.println(builder.toString()); + output.write(bytes); } else { diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java index 4ef15e3e648..88c348e4a87 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java @@ -18,11 +18,9 @@ package org.eclipse.jetty.proxy; -import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; @@ -38,12 +36,13 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.AbstractHandler; -import org.eclipse.jetty.toolchain.test.http.SimpleHttpResponse; import org.eclipse.jetty.util.B64Code; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Promise; @@ -75,14 +74,13 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(socket.getInputStream()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); } } @@ -97,14 +95,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -114,8 +112,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -134,14 +132,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 403 from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("403", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.FORBIDDEN_403, response.getStatus()); // Socket should be closed Assert.assertEquals(-1, input.read()); @@ -155,14 +153,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -172,8 +170,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -192,14 +190,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 403 from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("403", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.FORBIDDEN_403, response.getStatus()); // Socket should be closed Assert.assertEquals(-1, input.read()); @@ -213,14 +211,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -230,8 +228,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -269,15 +267,15 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 407 from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("407", response.getCode()); - Assert.assertTrue(response.getHeaders().containsKey("Proxy-Authenticate".toLowerCase(Locale.ENGLISH))); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407, response.getStatus()); + Assert.assertTrue(response.containsKey("Proxy-Authenticate".toLowerCase(Locale.ENGLISH))); // Socket should be closed Assert.assertEquals(-1, input.read()); @@ -293,14 +291,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -310,8 +308,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -341,23 +339,18 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - Socket socket = newSocket(); - socket.setSoTimeout(30000); - try + try (Socket socket = newSocket()) { + socket.setSoTimeout(30000); OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 500 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("Response Code", "500", response.getCode()); - } - finally - { - socket.close(); + HttpTester.Response response = readResponse(input); + Assert.assertEquals("Response Code", HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus()); } } @@ -372,14 +365,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -389,8 +382,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -408,19 +401,19 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + 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("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -435,14 +428,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); for (int i = 0; i < 10; ++i) { @@ -454,8 +447,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } } @@ -471,14 +464,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo HTTP/1.1\r\n" + @@ -488,8 +481,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); // Idle server is shut down disposeServer(); @@ -510,14 +503,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /close HTTP/1.1\r\n" + @@ -542,14 +535,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "POST /echo HTTP/1.1\r\n" + @@ -561,8 +554,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("POST /echo\r\nHELLO", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("POST /echo\r\nHELLO", response.getContent()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -572,8 +565,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -589,14 +582,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); StringBuilder body = new StringBuilder(); String chunk = "0123456789ABCDEF"; @@ -613,8 +606,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("POST /echo\r\n" + body, response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("POST /echo\r\n" + body, response.getContent()); } } @@ -674,14 +667,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); String body = "0123456789ABCDEF"; request = "" + @@ -694,8 +687,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest output.flush(); response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("POST /echo\r\n" + body, response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("POST /echo\r\n" + body, response.getContent()); } } @@ -713,20 +706,20 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); socket.shutdownOutput(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + 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("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -741,14 +734,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest try (Socket socket = newSocket()) { OutputStream output = socket.getOutputStream(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + InputStream input = socket.getInputStream(); output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request - SimpleHttpResponse response = readResponse(input); - Assert.assertEquals("200", response.getCode()); + HttpTester.Response response = readResponse(input); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); request = "" + "GET /echo" + " HTTP/1.1\r\n" + @@ -760,8 +753,8 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest // The pipelined request must have gone up to the server as is response = readResponse(input); - Assert.assertEquals("200", response.getCode()); - Assert.assertEquals("GET /echo", response.getBody()); + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + Assert.assertEquals("GET /echo", response.getContent()); } } @@ -787,10 +780,14 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest while ((read = input.read()) >= 0) baos.write(read); baos.close(); + byte[] bytes = baos.toByteArray(); ServletOutputStream output = httpResponse.getOutputStream(); - output.println(builder.toString()); - output.write(baos.toByteArray()); + if (bytes.length == 0) + output.print(builder.toString()); + else + output.println(builder.toString()); + output.write(bytes); break; } case "/close": From 45eac1de69d505e86d97ab3f34f544d8b7c718ef Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 13 Jun 2016 18:11:07 +0200 Subject: [PATCH 4/4] Enlarged idle timeout to avoid failures in slower machines. --- .../eclipse/jetty/proxy/AbstractConnectHandlerTest.java | 6 ++---- .../java/org/eclipse/jetty/proxy/ConnectHandlerTest.java | 7 +++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java index 05a1ebf98a4..5e72b33fd5e 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractConnectHandlerTest.java @@ -23,8 +23,6 @@ import java.io.InputStream; import java.net.Socket; import org.eclipse.jetty.http.HttpTester; -import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.toolchain.test.TestTracker; @@ -38,7 +36,7 @@ public abstract class AbstractConnectHandlerTest protected Server server; protected ServerConnector serverConnector; protected Server proxy; - protected Connector proxyConnector; + protected ServerConnector proxyConnector; protected ConnectHandler connectHandler; protected void prepareProxy() throws Exception @@ -76,7 +74,7 @@ public abstract class AbstractConnectHandlerTest protected Socket newSocket() throws IOException { - Socket socket = new Socket("localhost", ((NetworkConnector)proxyConnector).getLocalPort()); + Socket socket = new Socket("localhost", proxyConnector.getLocalPort()); socket.setSoTimeout(5000); return socket; } diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java index 88c348e4a87..c1890d16eaf 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java @@ -573,6 +573,13 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest @Test public void testCONNECTAndPOSTWithBigBody() throws Exception { + // Use a longer idle timeout since this test + // may take a long time on slower machines. + long idleTimeout = 5 * 60 * 1000; + serverConnector.setIdleTimeout(idleTimeout); + proxyConnector.setIdleTimeout(idleTimeout); + connectHandler.setIdleTimeout(idleTimeout); + String hostPort = "localhost:" + serverConnector.getLocalPort(); String request = "" +