Search in sources :

Example 6 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class TestAsyncContextImpl method testBug50753.

@Test
public void testBug50753() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug50753Servlet servlet = new Bug50753Servlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    // Call the servlet once
    Map<String, List<String>> headers = new CaseInsensitiveKeyMap<>();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, headers);
    Assert.assertEquals(200, rc);
    Assert.assertEquals("OK", bc.toString());
    String testHeader = getSingleHeader("A", headers);
    Assert.assertEquals("xyz", testHeader);
    // Check the access log
    alv.validateAccessLog(1, 200, Bug50753Servlet.THREAD_SLEEP_TIME, Bug50753Servlet.THREAD_SLEEP_TIME + REQUEST_TIME);
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(jakarta.servlet.ServletResponseWrapper) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class TestAsyncContextImpl method testAsyncStartWithComplete.

@Test
public void testAsyncStartWithComplete() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncStartWithCompleteServlet servlet = new AsyncStartWithCompleteServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    // Call the servlet once
    ByteChunk bc = new ByteChunk();
    Map<String, List<String>> headers = new CaseInsensitiveKeyMap<>();
    getUrl("http://localhost:" + getPort() + "/", bc, headers);
    Assert.assertEquals("OK", bc.toString());
    String contentLength = getSingleHeader("Content-Length", headers);
    Assert.assertEquals("2", contentLength);
    // Check the access log
    alv.validateAccessLog(1, 200, 0, REQUEST_TIME);
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(jakarta.servlet.ServletResponseWrapper) ServletRequestWrapper(jakarta.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 8 with CaseInsensitiveKeyMap

use of org.apache.tomcat.util.collections.CaseInsensitiveKeyMap in project tomcat by apache.

the class TestExpiresFilter method testBug63909.

/*
     * Tests Expires filter with:
     * - per content type expires
     * - no default
     * - Default servlet returning 304s (without content-type)
     */
@Test
public void testBug63909() throws Exception {
    Tomcat tomcat = getTomcatInstanceTestWebapp(false, false);
    Context ctxt = (Context) tomcat.getHost().findChild("/test");
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("ExpiresByType text/xml;charset=utf-8", "access plus 3 minutes");
    filterDef.addInitParameter("ExpiresByType text/xml", "access plus 5 minutes");
    filterDef.addInitParameter("ExpiresByType text", "access plus 7 minutes");
    filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "");
    filterDef.setFilterClass(ExpiresFilter.class.getName());
    filterDef.setFilterName(ExpiresFilter.class.getName());
    ctxt.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(ExpiresFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    ctxt.addFilterMap(filterMap);
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    Map<String, List<String>> requestHeaders = new CaseInsensitiveKeyMap<>();
    List<String> ifModifiedSinceValues = new ArrayList<>();
    ifModifiedSinceValues.add(FastHttpDateFormat.getCurrentDate());
    requestHeaders.put("If-Modified-Since", ifModifiedSinceValues);
    Map<String, List<String>> responseHeaders = new CaseInsensitiveKeyMap<>();
    int rc = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug69303.txt", bc, requestHeaders, responseHeaders);
    Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, rc);
    StringBuilder msg = new StringBuilder();
    for (Entry<String, List<String>> field : responseHeaders.entrySet()) {
        for (String value : field.getValue()) {
            msg.append((field.getKey() == null ? "" : field.getKey() + ": ") + value + "\n");
        }
    }
    System.out.println(msg);
    Integer actualMaxAgeInSeconds;
    String cacheControlHeader = getSingleHeader("Cache-Control", responseHeaders);
    if (cacheControlHeader == null) {
        actualMaxAgeInSeconds = null;
    } else {
        actualMaxAgeInSeconds = null;
        StringTokenizer cacheControlTokenizer = new StringTokenizer(cacheControlHeader, ",");
        while (cacheControlTokenizer.hasMoreTokens() && actualMaxAgeInSeconds == null) {
            String cacheDirective = cacheControlTokenizer.nextToken();
            StringTokenizer cacheDirectiveTokenizer = new StringTokenizer(cacheDirective, "=");
            if (cacheDirectiveTokenizer.countTokens() == 2) {
                String key = cacheDirectiveTokenizer.nextToken().trim();
                String value = cacheDirectiveTokenizer.nextToken().trim();
                if (key.equalsIgnoreCase("max-age")) {
                    actualMaxAgeInSeconds = Integer.valueOf(value);
                }
            }
        }
    }
    Assert.assertNotNull(actualMaxAgeInSeconds);
    Assert.assertTrue(Math.abs(actualMaxAgeInSeconds.intValue() - 420) < 3);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) StartingPoint(org.apache.catalina.filters.ExpiresFilter.StartingPoint) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

List (java.util.List)8 CaseInsensitiveKeyMap (org.apache.tomcat.util.collections.CaseInsensitiveKeyMap)8 Tomcat (org.apache.catalina.startup.Tomcat)7 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)7 Test (org.junit.Test)6 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 Context (org.apache.catalina.Context)4 Map (java.util.Map)3 Wrapper (org.apache.catalina.Wrapper)3 TesterContext (org.apache.tomcat.unittest.TesterContext)3 AsyncContext (jakarta.servlet.AsyncContext)2 ServletRequestWrapper (jakarta.servlet.ServletRequestWrapper)2 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 StandardContext (org.apache.catalina.core.StandardContext)2 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)2 ClientEndpoint (jakarta.websocket.ClientEndpoint)1 Endpoint (jakarta.websocket.Endpoint)1 EOFException (java.io.EOFException)1