use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.
the class ConstraintTest method startServer.
@Before
public void startServer() {
_server = new Server();
_connector = new LocalConnector(_server);
_config = _connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
_server.setConnectors(new Connector[] { _connector });
ContextHandler _context = new ContextHandler();
SessionHandler _session = new SessionHandler();
TestLoginService _loginService = new TestLoginService(TEST_REALM);
_loginService.putUser("user0", new Password("password"), new String[] {});
_loginService.putUser("user", new Password("password"), new String[] { "user" });
_loginService.putUser("user2", new Password("password"), new String[] { "user" });
_loginService.putUser("admin", new Password("password"), new String[] { "user", "administrator" });
_loginService.putUser("user3", new Password("password"), new String[] { "foo" });
_context.setContextPath("/ctx");
_server.setHandler(_context);
_context.setHandler(_session);
_server.addBean(_loginService);
_security = new ConstraintSecurityHandler();
_session.setHandler(_security);
RequestHandler _handler = new RequestHandler();
_security.setHandler(_handler);
_security.setConstraintMappings(getConstraintMappings(), getKnownRoles());
}
use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.
the class BadRequestLogHandlerTest method testLogHandler.
@Test(timeout = 4000)
public void testLogHandler() 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(new HelloHandler());
server.setHandler(requestLog);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
InetAddress destAddr = InetAddress.getByName(host);
int port = connector.getLocalPort();
SocketAddress endpoint = new InetSocketAddress(destAddr, port);
Socket socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(endpoint);
try (OutputStream out = socket.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
InputStream in = socket.getInputStream();
InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
StringReader request = new StringReader(requestHeader);
IO.copy(request, writer);
writer.flush();
StringWriter response = new StringWriter();
IO.copy(reader, response);
LOG.info("Response: {}", response);
} finally {
socket.close();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.
the class ResourceHandlerTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
File bigger = new File(dir, "bigger.txt");
File big = new File(dir, "big.txt");
try (OutputStream out = new FileOutputStream(bigger)) {
for (int i = 0; i < 100; i++) {
try (InputStream in = new FileInputStream(big)) {
IO.copy(in, out);
}
}
}
bigger.deleteOnExit();
// determine how the SCM of choice checked out the big.txt EOL
// we can't just use whatever is the OS default.
// because, for example, a windows system using git can be configured for EOL handling using
// local, remote, file lists, patterns, etc, rendering assumptions about the OS EOL choice
// wrong for unit tests.
LN = System.getProperty("line.separator");
try (BufferedReader reader = Files.newBufferedReader(big.toPath(), StandardCharsets.UTF_8)) {
// a buffer large enough to capture at least 1 EOL
char[] cbuf = new char[128];
reader.read(cbuf);
String sample = new String(cbuf);
if (sample.contains("\r\n")) {
LN = "\r\n";
} else if (sample.contains("\n\r")) {
LN = "\n\r";
} else {
LN = "\n";
}
}
_server = new Server();
_config = new HttpConfiguration();
_config.setOutputBufferSize(2048);
_connector = new ServerConnector(_server, new HttpConnectionFactory(_config));
_local = new LocalConnector(_server);
_server.setConnectors(new Connector[] { _connector, _local });
_resourceHandler = new ResourceHandler();
_resourceHandler.setResourceBase(MavenTestingUtils.getTargetFile("test-classes/simple").getAbsolutePath());
_resourceHandler.setWelcomeFiles(new String[] { "welcome.txt" });
_contextHandler = new ContextHandler("/resource");
_contextHandler.setHandler(_resourceHandler);
_server.setHandler(_contextHandler);
_server.start();
}
use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.
the class ScopedHandlerTest method testSimpleDouble.
@Test
public void testSimpleDouble() throws Exception {
TestHandler handler0 = new TestHandler("0");
TestHandler handler1 = new TestHandler("1");
handler0.setServer(new Server());
handler1.setServer(handler0.getServer());
handler0.setHandler(handler1);
handler0.start();
handler0.handle("target", null, null, null);
handler0.stop();
String history = _history.toString();
assertEquals(">S0>S1>W0>W1<W1<W0<S1<S0", history);
}
use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.
the class ScopedHandlerTest method testSimpleTriple.
@Test
public void testSimpleTriple() throws Exception {
TestHandler handler0 = new TestHandler("0");
TestHandler handler1 = new TestHandler("1");
TestHandler handler2 = new TestHandler("2");
handler0.setServer(new Server());
handler1.setServer(handler0.getServer());
handler2.setServer(handler0.getServer());
handler0.setHandler(handler1);
handler1.setHandler(handler2);
handler0.start();
handler0.handle("target", null, null, null);
handler0.stop();
String history = _history.toString();
assertEquals(">S0>S1>S2>W0>W1>W2<W2<W1<W0<S2<S1<S0", history);
}
Aggregations