Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
paultuckey committed Jun 9, 2023
1 parent 9a1d92f commit 2b10e16
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 60 deletions.
2 changes: 1 addition & 1 deletion container-test2/example-webapp/pom.xml
Expand Up @@ -28,7 +28,7 @@
</developers>

<build>
<finalName>example-webapp</finalName>
<finalName>webapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
20 changes: 17 additions & 3 deletions container-test2/test-with-testcontainters/pom.xml
Expand Up @@ -39,9 +39,17 @@
<version>5.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
<!-- run `mvn install` locally in example-webapp -->
<groupId>org.tuckey</groupId>
<artifactId>example-webapp</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -71,6 +79,12 @@
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
@@ -0,0 +1,127 @@
/**
* Copyright (c) 2005-2007, Paul Tuckey
* 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
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* - 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
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
package org.tuckey.web.filters.urlrewriteviacontainer;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
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;


@Testcontainers
public abstract class ContainerTestBase {

private int mappedPort = 0;

protected HttpClient client = new HttpClient();
private File systemPropBaseReportsDir = new File("container-test", "reports");
private String containerId = "test";

String webappPath = Paths.get("..", "example-webapp", "target", "webapp.war")
.toAbsolutePath().toString();

@Container
public GenericContainer<?> container = new GenericContainer<>("tomcat:10.1.9")
.withReuse(true)
.withExposedPorts(8080)
.withFileSystemBind(webappPath, "/usr/local/tomcat/webapps/webapp.war")
.waitingFor(Wait.forHttp("/webapp/test/test.jsp").forStatusCode(200));

//@BeforeEach
public void setUp() throws Exception {
container.start();
System.out.println(container.getContainerId());
System.out.println("PORT " + container.getFirstMappedPort());
this.mappedPort = container.getFirstMappedPort();
assert (container.isRunning());

String containerId = System.getProperty("test.container.id");
if (containerId != null) {
this.containerId = containerId;
}
// String systemPropBaseUrl = System.getProperty("test.base.url");
// if (systemPropBaseUrl != null) {
// baseUrl = systemPropBaseUrl;
// }
String systemPropBaseReports = System.getProperty("test.base.reports");
if (systemPropBaseReports != null) {
systemPropBaseReportsDir = new File(systemPropBaseReports);
}
System.err.println("systemPropBaseReportsDir: " + systemPropBaseReportsDir);

GetMethod method = new GetMethod(getBaseUrl() + "/rewrite-status/?conf=/WEB-INF/" + getConf());
client.executeMethod(method);
}

protected String getBaseUrl() {
return "http://localhost:" + this.mappedPort + "/" + getApp();
}

protected void recordRewriteStatus() throws IOException {
GetMethod method = new GetMethod(getBaseUrl() + "/" + getApp() + "/rewrite-status");
method.setFollowRedirects(false);
client.executeMethod(method);
File statusFile = new File(containerId + "-" + getApp() + "-" + getConf() + "-rewrite-status.html");
if (statusFile.exists() && !statusFile.delete()) {
System.out.println("could not remove status at " + statusFile.getAbsolutePath());
} else
if (!statusFile.createNewFile()) { // some containers don't let us do this
System.out.println("could not create status at " + statusFile.getAbsolutePath());
} else {
PrintWriter pw = new PrintWriter(statusFile);
pw.print(method.getResponseBodyAsString());
pw.close();
System.out.println("status saved to " + statusFile.getAbsolutePath());
}
}

abstract protected String getApp();

protected String getConf() {
return "urlrewrite.xml";
}

public String getContainerId() {
return containerId;
}
}

This file was deleted.

@@ -0,0 +1,70 @@
package org.tuckey.web.filters.urlrewriteviacontainer;


import org.apache.commons.httpclient.methods.GetMethod;

import jakarta.servlet.ServletException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;


import java.io.IOException;
import java.net.URLEncoder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
*
*/
public class WebappDecodeNoneIT extends ContainerTestBase {

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

protected String getConf() {
return "urlrewrite-decode-none.xml";
}

@BeforeEach
public void testSetup() throws Exception {
super.setUp();
super.recordRewriteStatus();
}

/**
* note, had trouble keeping true utf (multi byte) chars as cvs buggers them up!
*/
@Test
public void testTestUtf() throws ServletException, IOException {
if ( "orion2.0.5".equals(getContainerId())) return; // orion not supported
String encodedStr = URLEncoder.encode("m\u0101ori", "UTF8");
GetMethod method = new GetMethod(getBaseUrl() + "/utf/" + encodedStr + "/");
method.setRequestHeader("Accept-Encoding", "utf8");
method.setFollowRedirects(false);
client.executeMethod(method);
assertNotNull("no location header", method.getResponseHeader("Location"));
assertEquals(getBaseUrl() + "/utf-redir/done/" + encodedStr + "/", method.getResponseHeader("Location").getValue());
}

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());
}

public void testQueryStringNoDecode() throws IOException {
if ( "orion2.0.5".equals(getContainerId())) return; // orion cannot correctly encode & into %26

GetMethod method = new GetMethod(getBaseUrl() + "/query-string-no-decode/jack+%26+jones");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals("http://query-string-no-decode-result.com/?q=jack+%26+jones&another=jack & jones", method.getResponseHeader("Location").getValue());
}


}
@@ -0,0 +1,59 @@
package org.tuckey.web.filters.urlrewriteviacontainer;


import org.apache.commons.httpclient.methods.GetMethod;

import jakarta.servlet.ServletException;
import java.io.IOException;
import java.net.URLEncoder;

import static org.junit.Assert.assertEquals;

/**
* todo: need to do a few tests
* <p/>
* with eocode-using not set (ie, browser encoding used, step down to utf8)
* with eocode-using set to utf (force always with a specific decoding)
* with eocode-using not set to null (never decode)
* accept-encoding header?
* <p/>
* <p/>
* don't decode anything - null
* browser then utf then default - default browser,utf
* browser then don't decode - default browser,null
* always utf - utf
* <p/>
* <p/>
* options: browser (may fail), enc (unlikely fail)
*/
public class WebappDecodeUtf8IT extends ContainerTestBase {

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

protected String getConf() {
return "urlrewrite-decode-utf8.xml";
}

public void testSetup() throws IOException {
super.recordRewriteStatus();
}


/**
*
*/
public void testTestUtf() throws ServletException, IOException {
String utfSampleString = "m\u0101ori";
GetMethod method = new GetMethod(getBaseUrl() + "/utf/" + URLEncoder.encode(utfSampleString, "UTF8") + "/");
method.setRequestHeader("Accept-Encoding", "utf8");
method.setFollowRedirects(false);
client.executeMethod(method);
assertEquals(getBaseUrl() + "/utf-redir/done/", method.getResponseHeader("Location").getValue());
}




}

0 comments on commit 2b10e16

Please sign in to comment.