Using LocalConnector in test

This commit is contained in:
Joakim Erdfelt 2016-07-13 09:20:34 -07:00
parent 22c78d79f0
commit b28064c2c8
1 changed files with 21 additions and 79 deletions

View File

@ -18,29 +18,22 @@
package org.eclipse.jetty.servlet;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.IO;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@ -62,15 +55,14 @@ public class ResponseHeadersTest
}
private static Server server;
private static ServerConnector connector;
private static URI serverUri;
private static LocalConnector connector;
@BeforeClass
public static void startServer() throws Exception
{
// Configure Server
server = new Server();
connector = new ServerConnector(server);
connector = new LocalConnector(server);
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
@ -78,18 +70,10 @@ public class ResponseHeadersTest
server.setHandler(context);
// Serve capture servlet
context.addServlet(new ServletHolder(new SimulateUpgradeServlet()),"/*");
context.addServlet(new ServletHolder(new SimulateUpgradeServlet()),"/ws/*");
// Start Server
server.start();
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
serverUri = new URI(String.format("http://%s:%d/",host,port));
}
@AfterClass
@ -106,62 +90,20 @@ public class ResponseHeadersTest
}
@Test
public void testResponseHeaderFormat() throws IOException
public void testResponseWebSocketHeaderFormat() throws Exception
{
Socket socket = new Socket();
SocketAddress endpoint = new InetSocketAddress(serverUri.getHost(),serverUri.getPort());
socket.connect(endpoint);
HttpTester.Request request = new HttpTester.Request();
request.setMethod("GET");
request.setURI("/ws/");
request.setVersion(HttpVersion.HTTP_1_1);
request.setHeader("Host", "test");
StringBuilder req = new StringBuilder();
req.append("GET / HTTP/1.1\r\n");
req.append(String.format("Host: %s:%d\r\n",serverUri.getHost(),serverUri.getPort()));
req.append("\r\n");
ByteBuffer responseBuffer = connector.getResponse(request.generate());
HttpTester.Response response = HttpTester.parseResponse(responseBuffer);
OutputStream out = null;
InputStream in = null;
try
{
out = socket.getOutputStream();
in = socket.getInputStream();
// Write request
out.write(req.toString().getBytes());
out.flush();
// Read response
String respHeader = readResponseHeader(in);
// Now test for properly formatted HTTP Response Headers.
Assert.assertThat("Response Code",respHeader,startsWith("HTTP/1.1 101 Switching Protocols"));
Assert.assertThat("Response Header Upgrade",respHeader,containsString("Upgrade: WebSocket\r\n"));
Assert.assertThat("Response Header Connection",respHeader,containsString("Connection: Upgrade\r\n"));
}
finally
{
IO.close(in);
IO.close(out);
socket.close();
}
}
private String readResponseHeader(InputStream in) throws IOException
{
InputStreamReader isr = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
StringBuilder header = new StringBuilder();
// Read the response header
String line = reader.readLine();
Assert.assertNotNull(line);
Assert.assertThat(line,startsWith("HTTP/1.1 "));
header.append(line).append("\r\n");
while ((line = reader.readLine()) != null)
{
if (line.trim().length() == 0)
{
break;
}
header.append(line).append("\r\n");
}
return header.toString();
// Now test for properly formatted HTTP Response Headers.
assertThat("Response Code",response.getStatus(),is(101));
assertThat("Response Header Upgrade",response.get("Upgrade"),is("WebSocket"));
assertThat("Response Header Connection",response.get("Connection"),is("Upgrade"));
}
}