Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
paultuckey committed Jul 1, 2023
1 parent 326222c commit 42eb4f8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
2 changes: 1 addition & 1 deletion container-test/example-webapp/pom.xml
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.tuckey</groupId>
<artifactId>example-webapp</artifactId>
<packaging>jar</packaging>
<packaging>war</packaging>
<version>5.0.0-SNAPSHOT</version>
<name>Example Webapp</name>
<url>http://www.tuckey.org/urlrewrite/</url>
Expand Down
Expand Up @@ -46,7 +46,6 @@
<set type="session" name="testsession">hello!</set>
<set type="response-header" name="cache-control">none-fool</set>
<set type="parameter" name="settest1">$1</set>
<set type="method">DELETE</set>
<to>/settest.jsp</to>
</rule>

Expand All @@ -62,7 +61,7 @@

<rule>
<condition type="year" operator="greater">1970</condition>
<condition type="year" operator="less">2020</condition>
<condition type="year" operator="less">2045</condition>
<from>/time/year/current</from>
<to type="passthrough">/echo.jsp?echo=yearisbetween1970and3000</to>
</rule>
Expand Down
Expand Up @@ -37,14 +37,16 @@
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.time.Duration;


@Testcontainers
Expand All @@ -63,9 +65,14 @@ public abstract class ContainerTestBase {
.withExposedPorts(8080)
.withFileSystemBind(webappPath, "/usr/local/tomcat/webapps/webapp.war");

ToStringConsumer toStringConsumer = new ToStringConsumer();

public void setUp() throws Exception {
container.start();

container.followOutput(toStringConsumer, OutputFrame.OutputType.STDOUT);
container.followOutput(toStringConsumer, OutputFrame.OutputType.STDERR);

System.out.println(container.getContainerId());
System.out.println("HOST " + container.getHost());
System.out.println("PORT " + container.getFirstMappedPort());
Expand All @@ -90,12 +97,19 @@ public void setUp() throws Exception {
client.executeMethod(method);
}

public void tearDown() throws InterruptedException {
Thread.sleep(1);
// useful for debugging
//Thread.sleep(5 * 60 * 1000);
// go to tomcat container then files /usr/local/tomcat/logs
}

protected String getBaseUrl() {
return "http://" + container.getHost() + ":" + container.getFirstMappedPort() + "/" + getApp();
}

protected void recordRewriteStatus() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/" + getApp() + "/rewrite-status");
GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status");
method.setFollowRedirects(false);
client.executeMethod(method);
File statusFile = new File(containerId + "-" + getApp() + "-" + getConf() + "-rewrite-status.html");
Expand Down
Expand Up @@ -48,16 +48,6 @@ public void testTestUtf() throws ServletException, IOException {
assertEquals("/" + getApp() + "/utf-redir/done/" + encodedStr + "/", method.getResponseHeader("Location").getValue());
}

@Test
public void testNoDecode() throws IOException {
if ( "orion2.0.5".equals(getContainerId())) return; // jsp's with % in path not supported
if ( "tomcat-4.1.31".equals(getContainerId())) return; // jsp's with % in path not supported

GetMethod method = new GetMethod(getBaseUrl() + "/no-decode-test/D%25%2cD");
client.executeMethod(method);
assertEquals("this is no-decode-test target jsp", method.getResponseBodyAsString());
}

@Test
public void testQueryStringNoDecode() throws IOException {
if ( "orion2.0.5".equals(getContainerId())) return; // orion cannot correctly encode & into %26
Expand Down
Expand Up @@ -56,10 +56,8 @@ public void testTestUtf() throws ServletException, IOException {
method.setRequestHeader("Accept-Encoding", "utf8");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals(getBaseUrl() + "/utf-redir/done/", method.getResponseHeader("Location").getValue());
assertEquals("/" + getApp() + "/utf-redir/done/", method.getResponseHeader("Location").getValue());
}




}
Expand Up @@ -3,11 +3,11 @@
* All rights reserved.
* ====================================================================
* Licensed under the BSD License. Text as follows.
*
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* <p>
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
Expand All @@ -17,7 +17,7 @@
* - Neither the name tuckey.org nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Expand All @@ -34,13 +34,12 @@
*/
package org.tuckey.web.filters.urlrewriteviacontainer;

import jakarta.servlet.ServletException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;
import org.xml.sax.SAXException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -68,41 +67,46 @@ public void beforeEach() throws Exception {
super.recordRewriteStatus();
}

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

@Test
public void testProduct() throws IOException, SAXException, InterruptedException {
public void testProduct() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/products/987");
client.executeMethod(method);
assertEquals("product 987", method.getResponseBodyAsString());
}


@Test
public void testSimpleDistEx() throws ServletException, IOException, SAXException {
public void testSimpleDistEx() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/test/status/");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals(getBaseUrl() + "/rewrite-status", method.getResponseHeader("Location").getValue());
assertEquals("/" + getApp() + "/rewrite-status", method.getResponseHeader("Location").getValue());
}

@Test
public void testBasicSets() throws ServletException, IOException, SAXException {
public void testBasicSets() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/settest/674");
client.executeMethod(method);
assertNotNull(method.getResponseHeader("cache-control"));
assertEquals("testsession: hello!, " +
"param.settest1: 674, " +
"method: DELETE", method.getResponseBodyAsString());
"method: GET", method.getResponseBodyAsString());
}

@Test
public void testMultipleProduct() throws ServletException, IOException, SAXException {
public void testMultipleProduct() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/multiple/products/987");
client.executeMethod(method);
assertEquals("product 987", method.getResponseBodyAsString());
}

@Test
public void testNullTo() throws ServletException, IOException {
public void testNullTo() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/hideme/anb.jsp;dsaddd?asdasds#sdsfd");
client.executeMethod(method);
assertEquals(403, method.getStatusCode()); // "should have status set",
Expand All @@ -111,22 +115,22 @@ public void testNullTo() throws ServletException, IOException {
}

@Test
public void testYear() throws ServletException, IOException {
public void testYear() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/time/year/current");
client.executeMethod(method);
assertEquals("echo yearisbetween1970and3000", method.getResponseBodyAsString());
}

@Test
public void testTestAxis() throws ServletException, IOException {
public void testTestAxis() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/services/blah?qwerty");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals("/" + getApp() + "/axis/services/blah", method.getResponseHeader("Location").getValue());
}

@Test
public void testTestErik() throws ServletException, IOException {
public void testTestErik() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/eriktest/hi.ho");
method.setFollowRedirects(false);
method.addRequestHeader(new Header("host", "blah.com"));
Expand All @@ -135,15 +139,15 @@ public void testTestErik() throws ServletException, IOException {
}

@Test
public void testTestEncode() throws ServletException, IOException {
public void testTestEncode() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/went%20to%20bahamas/");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals(getBaseUrl() + "/jamaica/", method.getResponseHeader("Location").getValue());
assertEquals("/" + getApp() + "/jamaica/", method.getResponseHeader("Location").getValue());
}

@Test
public void testSimpleRun() throws ServletException, IOException {
public void testSimpleRun() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/run/test/test1");
client.executeMethod(method);
assertEquals("this is " + TestRunObj.class.getName(), method.getResponseBodyAsString());
Expand Down Expand Up @@ -181,11 +185,4 @@ private String inflateGzipToString(InputStream is) throws IOException {
return os.toString();
}

@Test
public void testSampleAnnotation() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/do-something/7");
client.executeMethod(method);
assertEquals(method.getResponseBodyAsString(), "AnnotatedClassSample id=7");
}

}
Expand Up @@ -35,6 +35,7 @@
package org.tuckey.web.filters.urlrewriteviacontainer;

import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;
Expand All @@ -51,7 +52,7 @@
public class WebappModStyleHttpITTest extends ContainerTestBase {

protected String getApp() {
return "webapp/mod";
return "webapp";
}

@BeforeEach
Expand All @@ -60,25 +61,30 @@ public void beforeEach() throws Exception {
super.recordRewriteStatus();
}

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

@Test
public void testSimpleTest() throws ServletException, IOException, SAXException {
GetMethod method = new GetMethod(getBaseUrl() + "/index.jsp");
public void testSimpleTest() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/mod/index.jsp");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals("this is index.jsp", method.getResponseBodyAsString());
}

@Test
public void testSimpleTestRewrite() throws ServletException, IOException, SAXException {
GetMethod method = new GetMethod(getBaseUrl() + "/simple/test");
public void testSimpleTestRewrite() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/mod/simple/test");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals("this is index.jsp", method.getResponseBodyAsString());
}

@Test
public void testStatus1() throws ServletException, IOException, SAXException {
GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status");
public void testStatus1() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/mod/rewrite-status");
method.setFollowRedirects(false);
client.executeMethod(method);
assertTrue(method.getResponseBodyAsString().contains("Running Status"));
Expand Down

0 comments on commit 42eb4f8

Please sign in to comment.