use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.
the class RequestLogHandlerTest method testLogHandlerCollection_OKErrorHandler.
/**
* Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
* @throws Exception if test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection_OKErrorHandler() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
OKErrorHandler errorDispatcher = new OKErrorHandler();
server.addBean(errorDispatcher);
ContextHandlerCollection contexts = new ContextHandlerCollection();
ContextHandler errorContext = new ContextHandler("/errorpage");
errorContext.setHandler(new AltErrorHandler());
ContextHandler testContext = new ContextHandler("/test");
testContext.setHandler(testHandler);
contexts.addHandler(errorContext);
contexts.addHandler(testContext);
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { contexts, requestLog });
server.setHandler(handlers);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.debug("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.debug("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.
the class RequestLogHandlerTest method testLogHandlerWrapped.
@Test(timeout = 4000)
public void testLogHandlerWrapped() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
CaptureLog captureLog = new CaptureLog();
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
requestLog.setHandler(testHandler);
server.setHandler(requestLog);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class, HttpChannelState.class)) {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.info("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.info("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.
the class RequestLogHandlerTest method testLogHandlerCollection_ErrorHandler_ServerBean.
/**
* Test a RequestLogHandler at the end of a HandlerCollection and also with the default ErrorHandler as server bean in place.
* @throws Exception if test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection_ErrorHandler_ServerBean() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
ErrorHandler errorHandler = new ErrorHandler();
server.addBean(errorHandler);
CaptureLog captureLog = new CaptureLog();
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { testHandler, requestLog });
server.setHandler(handlers);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.debug("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.debug("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.
the class CookiesTest method startServer.
protected void startServer(Handler handler) throws Exception {
server = new Server();
connector = new ServerConnector(server);
server.addConnector(connector);
ContextHandler context = new ContextHandler();
context.setContextPath("/");
context.setHandler(handler);
server.setHandler(context);
server.start();
}
use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.
the class MisbehavingClassTest method startServer.
@SuppressWarnings("Duplicates")
@BeforeClass
public static void startServer() throws Exception {
server = new Server();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
handler = new EchoHandler();
ContextHandler context = new ContextHandler();
context.setContextPath("/");
context.setHandler(handler);
server.setHandler(context);
// Start Server
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
serverUri = new URI(String.format("ws://%s:%d/", host, port));
}
Aggregations