Search in sources :

Example 36 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testOptionsRequestButNotPreflight.

@Test
public void testOptionsRequestButNotPreflight() throws Exception {
    // We cannot know if an actual request has performed the preflight before:
    // we'll trust browsers to do it right, so responses to actual requests
    // will contain the CORS response headers.
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "OPTIONS / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 37 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testPreflightWithWildcardCustomHeaders.

@Test
public void testPreflightWithWildcardCustomHeaders() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "*");
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "OPTIONS / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + CrossOriginFilter.ACCESS_CONTROL_REQUEST_HEADERS_HEADER + ": X-Foo-Bar\r\n" + CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER + ": GET\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_HEADERS_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 38 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testSimpleRequestWithMatchingMultipleOrigins.

@Test
public void testSimpleRequestWithMatchingMultipleOrigins() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    String origin = "http://localhost";
    String otherOrigin = origin.replace("localhost", "127.0.0.1");
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin + "," + otherOrigin);
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + // Use 2 spaces as separator to test that the implementation does not fail
    "Origin: " + otherOrigin + " " + " " + origin + "\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(response.contains("Vary"));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 39 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testSimpleRequestWithMatchingWildcardOrigin.

@Test
public void testSimpleRequestWithMatchingWildcardOrigin() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    String origin = "http://subdomain.example.com";
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "http://*.example.com");
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(response.contains("Vary"));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 40 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testSimpleRequestWithWildcardOrigin.

@Test
public void testSimpleRequestWithWildcardOrigin() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String origin = "http://foo.example.com";
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(!response.contains("Vary"));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ServletHolder (org.eclipse.jetty.servlet.ServletHolder)473 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)255 Server (org.eclipse.jetty.server.Server)177 Test (org.junit.Test)81 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)74 ServerConnector (org.eclipse.jetty.server.ServerConnector)67 HttpServletRequest (javax.servlet.http.HttpServletRequest)47 IOException (java.io.IOException)44 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)36 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)33 File (java.io.File)32 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)32 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)30 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)27 HttpServletResponse (javax.servlet.http.HttpServletResponse)26 HttpClient (org.eclipse.jetty.client.HttpClient)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 ServletException (javax.servlet.ServletException)23 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)23