aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java')
-rw-r--r--jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java115
1 files changed, 115 insertions, 0 deletions
diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java
new file mode 100644
index 0000000000..1d9b30d827
--- /dev/null
+++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java
@@ -0,0 +1,115 @@
+//========================================================================
+//Copyright 2012 Mort Bay Consulting Pty. Ltd.
+//------------------------------------------------------------------------
+//All rights reserved. This program and the accompanying materials
+//are made available under the terms of the Eclipse Public License v1.0
+//and Apache License v2.0 which accompanies this distribution.
+//The Eclipse Public License is available at
+//http://www.eclipse.org/legal/epl-v10.html
+//The Apache License v2.0 is available at
+//http://www.opensource.org/licenses/apache2.0.php
+//You may elect to redistribute this code under either of these licenses.
+//========================================================================
+package org.eclipse.jetty.server;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.net.URISyntaxException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.eclipse.jetty.server.util.SimpleHttpParser;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.StdErrLog;
+import org.junit.After;
+import org.junit.Before;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public abstract class AbstractHttpTest
+{
+ protected static Server server;
+ protected static SelectChannelConnector connector;
+ protected String httpVersion;
+ protected SimpleHttpParser httpParser;
+
+ public AbstractHttpTest(String httpVersion)
+ {
+ this.httpVersion = httpVersion;
+ }
+
+ @Before
+ public void setUp() throws Exception
+ {
+ server = new Server();
+ connector = new SelectChannelConnector(server);
+ server.setConnectors(new Connector[]{connector});
+ httpParser = new SimpleHttpParser(httpVersion);
+ ((StdErrLog)Log.getLogger(HttpChannel.class)).setHideStacks(true);
+ }
+
+ @After
+ public void tearDown() throws Exception
+ {
+ ((StdErrLog)Log.getLogger(HttpChannel.class)).setHideStacks(false);
+ server.stop();
+ }
+
+ protected TestHttpResponse executeRequest() throws URISyntaxException, IOException
+ {
+ Socket socket = new Socket("localhost", connector.getLocalPort());
+ BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
+ PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
+ String request = "GET / " + httpVersion + "\r\n";
+
+ writer.write(request);
+ writer.write("Host: localhost");
+ writer.println("\r\n");
+ writer.flush();
+
+ return httpParser.readResponse(reader);
+ }
+
+ protected void assertResponseBody(TestHttpResponse response, String expectedResponseBody)
+ {
+ assertThat("response body is" + expectedResponseBody, response.getBody(), is(expectedResponseBody));
+ }
+
+ protected void assertHeader(TestHttpResponse response, String headerName, String expectedValue)
+ {
+ assertThat(headerName + "=" + expectedValue, response.getHeaders().get(headerName), is(expectedValue));
+ }
+
+ private static class TestCommitException extends IllegalStateException
+ {
+ public TestCommitException()
+ {
+ super("Thrown by test");
+ }
+ }
+
+ protected class ThrowExceptionOnDemandHandler extends AbstractHandler
+ {
+ private final boolean throwException;
+
+ protected ThrowExceptionOnDemandHandler(boolean throwException)
+ {
+ this.throwException = throwException;
+ }
+
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+ {
+ if (throwException)
+ throw new TestCommitException();
+ }
+ }
+}