Java Code Examples for org.apache.catalina.Context#setCookieProcessor()

The following examples show how to use org.apache.catalina.Context#setCookieProcessor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCookieProcessorGenerationHttp.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testUtf8CookieValue() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setCookieProcessor(new Rfc6265CookieProcessor());
    Tomcat.addServlet(ctx, "test", new CookieServlet("\u0120"));
    ctx.addServletMappingDecoded("/test", "test");
    tomcat.start();

    Map<String,List<String>> headers = new HashMap<>();
    ByteChunk res = new ByteChunk();
    getUrl("http://localhost:" + getPort() + "/test", res, headers);
    List<String> cookieHeaders = headers.get("Set-Cookie");
    Assert.assertEquals("There should only be one Set-Cookie header in this test",
            1, cookieHeaders.size());
    // Client is assuming header is ISO-8859-1 encoding which it isn't. Turn
    // the header value back into the received bytes (this isn't guaranteed
    // to work with all values but it will for this test value)
    byte[] headerBytes = cookieHeaders.get(0).getBytes(StandardCharsets.ISO_8859_1);
    // Now convert those bytes to a String using UTF-8
    String utf8Header = new String(headerBytes, StandardCharsets.UTF_8);
    Assert.assertEquals("Test=\u0120", utf8Header);
}
 
Example 2
Source File: CookiesBaseTest.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public static void addServlets(Tomcat tomcat) {
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setCookieProcessor(new LegacyCookieProcessor());

    Tomcat.addServlet(ctx, "invalid", new CookieServlet("na;me", "value"));
    ctx.addServletMappingDecoded("/invalid", "invalid");
    Tomcat.addServlet(ctx, "null", new CookieServlet(null, "value"));
    ctx.addServletMappingDecoded("/null", "null");
    Tomcat.addServlet(ctx, "blank", new CookieServlet("", "value"));
    ctx.addServletMappingDecoded("/blank", "blank");
    Tomcat.addServlet(ctx, "invalidFwd",
            new CookieServlet("na/me", "value"));
    ctx.addServletMappingDecoded("/invalidFwd", "invalidFwd");
    Tomcat.addServlet(ctx, "invalidStrict",
            new CookieServlet("$name", "value"));
    ctx.addServletMappingDecoded("/invalidStrict", "invalidStrict");
    Tomcat.addServlet(ctx, "valid", new CookieServlet("name", "value"));
    ctx.addServletMappingDecoded("/valid", "valid");
    Tomcat.addServlet(ctx, "switch", new CookieServlet("name", "val?ue"));
    ctx.addServletMappingDecoded("/switch", "switch");

}
 
Example 3
Source File: TestCookieParsing.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doRequest() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    root.setCookieProcessor(cookieProcessor);

    if (echoHeader) {
        Tomcat.addServlet(root, "Cookies", new EchoCookieHeader());
    } else {
        Tomcat.addServlet(root, "Cookies", new EchoCookies());
    }
    root.addServletMappingDecoded("/test", "Cookies");

    tomcat.start();
    // Open connection
    setPort(tomcat.getConnector().getLocalPort());
    connect();

    StringBuilder request = new StringBuilder();
    request.append("GET /test HTTP/1.0");
    request.append(CRLF);
    for (String cookie : cookies) {
        request.append("Cookie: ");
        request.append(cookie);
        request.append(CRLF);
    }
    request.append(CRLF);
    setRequest(new String[] {request.toString()});
    processRequest(true); // blocks until response has been read
    String response = getResponseBody();

    // Close the connection
    disconnect();
    reset();
    tomcat.stop();

    Assert.assertEquals(expected, response);
}