Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matthiasblaesing fixes #258

Merged
merged 3 commits into from Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions container-test/example-webapp/pom.xml
Expand Up @@ -66,6 +66,12 @@
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<!-- required for proxying -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependencies>

<properties>
Expand Down
@@ -0,0 +1,25 @@

package org.tuckey.web.filters.urlrewriteviacontainer;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

public class CookieServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try (Writer writer = resp.getWriter()) {
writer.write("Cookie-Servlet\n");
for(Cookie cookie: req.getCookies()) {
writer.write(cookie.getName());
writer.write((": "));
writer.write(cookie.getValue());
}
}
}
}
14 changes: 9 additions & 5 deletions container-test/example-webapp/src/main/webapp/WEB-INF/web.xml
Expand Up @@ -39,6 +39,7 @@

# simple test rule
RewriteRule ^/mod/simple/test$ /mod/index.jsp [L]
RewriteRule ^/mod/cookie$ http://localhost:8080/webapp/cookie [P]

]]></param-value>
</init-param>
Expand All @@ -55,11 +56,14 @@
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>Cookie Test</servlet-name>
<servlet-class>org.tuckey.web.filters.urlrewriteviacontainer.CookieServlet</servlet-class>
</servlet>






<servlet-mapping>
<servlet-name>Cookie Test</servlet-name>
<url-pattern>/cookie</url-pattern>
</servlet-mapping>

</web-app>
Expand Up @@ -43,6 +43,8 @@
import jakarta.servlet.ServletException;
import java.io.IOException;

import org.apache.commons.httpclient.Cookie;

import static org.junit.jupiter.api.Assertions.*;

/**
Expand All @@ -63,7 +65,13 @@ public void beforeEach() throws Exception {

@AfterEach
public void afterEach() throws InterruptedException {
super.tearDown();
tearDown();
}

@Override
public void setUp() throws Exception {
super.setUp();
client.getState().clearCookies();
}

@Test
Expand Down Expand Up @@ -91,4 +99,13 @@ public void testStatus1() throws IOException {
assertFalse(method.getResponseBodyAsString().contains("Error"));
}

@Test
public void testCookiePassing() throws ServletException, IOException, SAXException {
GetMethod method = new GetMethod(getBaseUrl() + "/mod/cookie");
method.setFollowRedirects(false);
method.setRequestHeader("Cookie", new Cookie("localhost", "Dummy", "TestValue").toExternalForm());
client.executeMethod(method);
assertEquals(200, method.getStatusCode());
assertEquals("Cookie-Servlet\nDummy: TestValue", method.getResponseBodyAsString());
}
}
Expand Up @@ -213,6 +213,8 @@ private NormalRule processRule(String line) {
} else {
log.error("cannot parse " + line);
}
// mod_rewrite passes cookies through to the backend unmodified
rule.setDropCookies("false");
return rule;
}

Expand Down
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

public class ModRewriteConfLoaderTest extends TestCase {

Expand All @@ -30,19 +31,25 @@ public void testEngine2() {
assertFalse(conf.isEngineEnabled());
}

public void testLoadFromFile() throws IOException {
public void testLoadFromFile() throws Exception {
InputStream is = ModRewriteConfLoaderTest.class.getResourceAsStream(BASE_PATH + "htaccess-test1.txt");
loader.process(is, conf);
assertTrue(conf.isEngineEnabled());
assertEquals(1, conf.getRules().size());
Field dropCookiesField = NormalRule.class.getDeclaredField("dropCookies");
dropCookiesField.setAccessible(true);
assertFalse(dropCookiesField.getBoolean(conf.getRules().get(0)));
}

public void testLoadFromFile2() throws IOException {
public void testLoadFromFile2() throws Exception {
InputStream is = ModRewriteConfLoaderTest.class.getResourceAsStream(BASE_PATH + "htaccess-test1.txt");
Conf conf = new Conf(null, is, "htaccess-test1.txt", null, true);
assertTrue(conf.isEngineEnabled());
assertTrue(conf.isOk());
assertEquals(1, conf.getRules().size());
Field dropCookiesField = NormalRule.class.getDeclaredField("dropCookies");
dropCookiesField.setAccessible(true);
assertFalse(dropCookiesField.getBoolean(conf.getRules().get(0)));
}

public void testSimple2() {
Expand Down