diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpAsserts.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpAsserts.java new file mode 100644 index 00000000000..e0410057571 --- /dev/null +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpAsserts.java @@ -0,0 +1,29 @@ +package org.eclipse.jetty.client; + +import java.util.Collections; +import java.util.List; + +import org.eclipse.jetty.http.HttpFields; +import org.junit.Assert; + +public final class HttpAsserts +{ + public static void assertContainsHeaderKey(String expectedKey, HttpFields headers) + { + if (headers.containsKey(expectedKey)) + { + return; + } + List names = Collections.list(headers.getFieldNames()); + StringBuilder err = new StringBuilder(); + err.append("Missing expected header key [").append(expectedKey); + err.append("] (of ").append(names.size()).append(" header fields)"); + for (int i = 0; i < names.size(); i++) + { + String value = headers.getStringField(names.get(i)); + err.append("\n").append(i).append("] ").append(names.get(i)); + err.append(": ").append(value); + } + Assert.fail(err.toString()); + } +} diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java index 97d65f7cf16..77b353a0420 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java @@ -544,7 +544,7 @@ public class HttpExchangeTest public void testOptionsWithExchange() throws Exception { ContentExchange httpExchange = new ContentExchange(true); - httpExchange.setURI(getBaseURI()); + httpExchange.setURL(getBaseURI().toASCIIString()); httpExchange.setRequestURI("*"); httpExchange.setMethod(HttpMethods.OPTIONS); // httpExchange.setRequestHeader("Connection","close"); @@ -555,8 +555,10 @@ public class HttpExchangeTest assertEquals(HttpStatus.OK_200,httpExchange.getResponseStatus()); HttpFields headers = httpExchange.getResponseFields(); - assertTrue("Response does not contain Allow header", headers.containsKey("Allow")); + HttpAsserts.assertContainsHeaderKey("Content-Length", headers); + assertEquals("Content-Length header value", 0, headers.getLongField("Content-Length")); + HttpAsserts.assertContainsHeaderKey("Allow",headers); String allow = headers.getStringField("Allow"); String expectedMethods[] = { "GET", "HEAD", "POST", "PUT", "DELETE", "MOVE", "OPTIONS", "TRACE" }; @@ -564,9 +566,6 @@ public class HttpExchangeTest { assertThat(allow,containsString(expectedMethod)); } - - assertTrue("Response contains Content-Length header", headers.containsKey("Content-Length")); - assertEquals("Content-Length header value", 0, headers.getLongField("Content-Length")); } /* ------------------------------------------------------------ */ diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AbstractSslServerAndClientCreator.java b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AbstractSslServerAndClientCreator.java index 3d230ff2a29..72e626e0087 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AbstractSslServerAndClientCreator.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AbstractSslServerAndClientCreator.java @@ -14,22 +14,11 @@ package org.eclipse.jetty.client.helperClasses; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.ssl.SslContextFactory; import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.ssl.SslSocketConnector; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; -import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -58,65 +47,7 @@ public abstract class AbstractSslServerAndClientCreator implements ServerAndClie connector.setAllowRenegotiate(true); server.setConnectors(new Connector[]{ connector }); - server.setHandler(new AbstractHandler() - { - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, - ServletException - { - int i = 0; - try - { - baseRequest.setHandled(true); - response.setStatus(200); - - if (request.getServerName().equals("jetty.eclipse.org")) - { - response.getOutputStream().println("Proxy request: " + request.getRequestURL()); - response.getOutputStream().println(request.getHeader(HttpHeaders.PROXY_AUTHORIZATION)); - } - else if (request.getMethod().equalsIgnoreCase("GET")) - { - response.getOutputStream().println(""); - for (; i < 100; i++) - { - response.getOutputStream().println(" " + i + ""); - } - else if (request.getMethod().equalsIgnoreCase("SLEEP")) - { - Thread.sleep(10000); - } - else - { - response.setContentType(request.getContentType()); - int size = request.getContentLength(); - ByteArrayOutputStream bout = new ByteArrayOutputStream(size > 0?size:32768); - IO.copy(request.getInputStream(),bout); - response.getOutputStream().write(bout.toByteArray()); - } - } - catch (InterruptedException e) - { - LOG.debug(e); - } - catch (IOException e) - { - LOG.warn(e); - throw e; - } - catch (Throwable e) - { - LOG.warn(e); - throw new ServletException(e); - } - finally - { - } - } - }); + server.setHandler(new GenericServerHandler()); server.start(); return server; } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AsyncSslServerAndClientCreator.java b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AsyncSslServerAndClientCreator.java index 564713fc65b..6d08d216f90 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AsyncSslServerAndClientCreator.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/AsyncSslServerAndClientCreator.java @@ -16,7 +16,6 @@ public class AsyncSslServerAndClientCreator extends AbstractSslServerAndClientCr httpClient.setMaxConnectionsPerAddress(2); String keystore = MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath(); - httpClient.setKeyStoreInputStream(new FileInputStream(keystore)); httpClient.setKeyStorePassword("storepwd"); httpClient.setKeyManagerPassword("keypwd"); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/GenericServerHandler.java b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/GenericServerHandler.java new file mode 100644 index 00000000000..71cd3cd1e1d --- /dev/null +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/GenericServerHandler.java @@ -0,0 +1,84 @@ +package org.eclipse.jetty.client.helperClasses; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.http.HttpHeaders; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.util.IO; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; + +/** + * Generic Server Handler used for various client tests. + */ +public class GenericServerHandler extends AbstractHandler +{ + private static final Logger LOG = Log.getLogger(GenericServerHandler.class); + + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + int i = 0; + try + { + baseRequest.setHandled(true); + response.setStatus(200); + + if (request.getServerName().equals("jetty.eclipse.org")) + { + response.getOutputStream().println("Proxy request: " + request.getRequestURL()); + response.getOutputStream().println(request.getHeader(HttpHeaders.PROXY_AUTHORIZATION)); + } + else if (request.getMethod().equalsIgnoreCase("GET")) + { + response.getOutputStream().println(""); + for (; i < 100; i++) + { + response.getOutputStream().println(" " + i + ""); + } + else if (request.getMethod().equalsIgnoreCase("OPTIONS")) + { + if ("*".equals(target)) + { + response.setContentLength(0); + response.setHeader("Allow","GET,HEAD,POST,PUT,DELETE,MOVE,OPTIONS,TRACE"); + } + } + else if (request.getMethod().equalsIgnoreCase("SLEEP")) + { + Thread.sleep(10000); + } + else + { + response.setContentType(request.getContentType()); + int size = request.getContentLength(); + ByteArrayOutputStream bout = new ByteArrayOutputStream(size > 0?size:32768); + IO.copy(request.getInputStream(),bout); + response.getOutputStream().write(bout.toByteArray()); + } + } + catch (InterruptedException e) + { + LOG.warn(e); + } + catch (IOException e) + { + LOG.warn(e); + throw e; + } + catch (Throwable e) + { + LOG.warn(e); + throw new ServletException(e); + } + } +} diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/HttpServerAndClientCreator.java b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/HttpServerAndClientCreator.java index 21019ff5b45..d9a2f1d3eef 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/HttpServerAndClientCreator.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/helperClasses/HttpServerAndClientCreator.java @@ -1,27 +1,12 @@ package org.eclipse.jetty.client.helperClasses; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.nio.SelectChannelConnector; -import org.eclipse.jetty.util.IO; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; public class HttpServerAndClientCreator implements ServerAndClientCreator { - private static final Logger LOG = Log.getLogger(HttpServerAndClientCreator.class); - public HttpClient createClient(long idleTimeout, long timeout, int connectTimeout) throws Exception { HttpClient httpClient = new HttpClient(); @@ -43,67 +28,8 @@ public class HttpServerAndClientCreator implements ServerAndClientCreator _connector.setMaxIdleTime(3000000); _connector.setPort(0); - _server.setConnectors(new Connector[] - { _connector }); - _server.setHandler(new AbstractHandler() - { - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, - ServletException - { - int i = 0; - try - { - baseRequest.setHandled(true); - response.setStatus(200); - - if (request.getServerName().equals("jetty.eclipse.org")) - { - response.getOutputStream().println("Proxy request: " + request.getRequestURL()); - response.getOutputStream().println(request.getHeader(HttpHeaders.PROXY_AUTHORIZATION)); - } - else if (request.getMethod().equalsIgnoreCase("GET")) - { - response.getOutputStream().println(""); - for (; i < 100; i++) - { - response.getOutputStream().println(" " + i + ""); - } - else if (request.getMethod().equalsIgnoreCase("SLEEP")) - { - Thread.sleep(10000); - } - else - { - response.setContentType(request.getContentType()); - int size = request.getContentLength(); - ByteArrayOutputStream bout = new ByteArrayOutputStream(size > 0?size:32768); - IO.copy(request.getInputStream(),bout); - response.getOutputStream().write(bout.toByteArray()); - } - } - catch (InterruptedException e) - { - LOG.warn(e); - } - catch (IOException e) - { - LOG.warn(e); - throw e; - } - catch (Throwable e) - { - LOG.warn(e); - throw new ServletException(e); - } - finally - { - } - } - }); + _server.setConnectors(new Connector[]{ _connector }); + _server.setHandler(new GenericServerHandler()); _server.start(); return _server; }