Absolutely ages ago, I found this great article on JUnit testing for UrlRewrite by Sujit Pal. It was very useful as at the time as I was looking for a way of testing that my usage of UrlRewrite was working correctly without having a servlet container running. Together with my contribution, both inbound and outbound rules are covered.
The solution is great for JUnit 3 but not really in the spirit of JUnit 4, where the test cases aren't necessarily part of an inheritance hierarchy, as this article demonstrates.
I had cause to test some rewrites again recently. However, I‘ve moved over to JUnit4 as a preference these days, so I decided to refresh Sujit‘s solution, decouple it from TestCase.
package some.pakage.name;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.tuckey.web.MockRequest;
import org.tuckey.web.MockResponse;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.RewrittenUrl;
import org.tuckey.web.filters.urlrewrite.UrlRewriteWrappedResponse;
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class UrlRewriteTesting {
protected Conf conf;
protected UrlRewriter rewriter;
public UrlRewriteTesting(Conf conf) {
org.tuckey.web.filters.urlrewrite.utils.Log.setLevel("DEBUG");
this.conf = conf;
rewriter = new UrlRewriter(conf);
}
public UrlRewriteTesting(String pathToCfg, String confFile) throws FileNotFoundException {
this(new Conf(new FileInputStream(pathToCfg + "/" + confFile), confFile));
}
@param
@param
public void assertInboundRewrite(String fromUrl, String toUrl) {
RewrittenUrl rewrittenUrl = rewriter.processRequest(new MockRequest(fromUrl), new MockResponse());
assertNotNull("Could not rewrite URL from:" + fromUrl + " to:" + toUrl, rewrittenUrl);
assertEquals("Rewrite from:" + fromUrl + " to:" + toUrl + " did not succeed", toUrl, rewrittenUrl.getTarget());
}
@param
@param
public void assertOutboundRewrite(String fromUrl, String toUrl) {
UrlRewriteWrappedResponse wrappedResponse =
new UrlRewriteWrappedResponse(new MockResponse(), new MockRequest(fromUrl), new UrlRewriter(conf));
assertEquals("Rewrite from:" + fromUrl + " to:" + toUrl + " did not succeed", toUrl, wrappedResponse.encodeURL(fromUrl));
}
}
Usage
Below is an example of how SeemoreJ's rewrite rules in urlrewrite.xml for this site might look for the home page. At the time of writing, this site does not use UrlRewrite, so it's entirely hypothetical.
<rule>
<from>^/$</from>
<to>/seemore?sjController=blog&sjAction=home&sjFormat=html</to>
</rule>
<rule>
<from>^/index.(xml|html)$</from>
<to>/seemore?sjController=blog&sjAction=home&sjFormat=$1</to>
</rule>
<outbound-rule>
<from>/seemore\?sjController=blog&sjAction=home&sjFormat=(xml|html)</from>
<to>/index.$1</to>
</outbound-rule>
I might be making numerous minor or major changes to this file and I want the unit tests to defend against unwanted side-effects. So the test case below will (hopefully) make sure I don‘t mess things up.
package com.malethan.blog.urlrewrite;
import some.pakage.name.UrlRewriteTesting;
import org.junit.Test;
import java.io.FileNotFoundException;
public class UrlRewriteRulesTest {
UrlRewriteTesting context;
public UrlRewriteRulesTest() throws FileNotFoundException {
context = new UrlRewriteTesting(System.getProperty("basedir") +"/src/main/webapp/WEB-INF", "urlrewrite.xml");
}
@Test
public void testIndexInbound() {
context.assertInboundRewrite("/", "/seemore?sjController=blog&sjAction=home&sjFormat=html");
context.assertInboundRewrite("/index.html", "/seemore?sjController=blog&sjAction=home&sjFormat=html");
context.assertInboundRewrite("/index.xml", "/seemore?sjController=blog&sjAction=home&sjFormat=xml");
}
@Test
public void testIndexOutbound() {
context.assertOutboundRewrite("/seemore?sjController=blog&sjAction=home&sjFormat=html", "/index.html");
context.assertOutboundRewrite("/seemore?sjController=blog&sjAction=home&sjFormat=xml", "/index.xml");
}
}