Skip to content

Commit ccd837a

Browse files
rumdidumdumchkal
authored andcommitted
Fix ocpsoft#243: Use queryLiteral() instead of queryDecoded()
1 parent f38d396 commit ccd837a

3 files changed

Lines changed: 111 additions & 2 deletions

File tree

‎config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/PathAndQuery.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
130130
return false;
131131
}
132132
else
133-
url = AddressBuilder.begin().pathDecoded(event.getInboundAddress().getPath())
134-
.queryDecoded(event.getInboundAddress().getQuery()).build().toString();
133+
url = AddressBuilder.begin()
134+
.pathDecoded(event.getInboundAddress().getPath())
135+
.queryLiteral(event.getInboundAddress().getQuery())
136+
.build().toString();
135137

136138
String contextPath = event.getContextPath();
137139
if (!contextPath.equals("/") && url.startsWith(contextPath))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.config;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
23+
/**
24+
* Configuration provider for {@link PathAndQueryTest}.
25+
*/
26+
public class PathAndQueryProvider extends HttpConfigurationProvider
27+
{
28+
29+
@Override
30+
public int priority()
31+
{
32+
return 0;
33+
}
34+
35+
@Override
36+
public Configuration getConfiguration(final ServletContext context)
37+
{
38+
return ConfigurationBuilder.begin()
39+
.addRule()
40+
.when(PathAndQuery.matches("/pathAndQuery/{tail}"))
41+
.perform(Redirect.temporary("/resultingPath/{tail}"));
42+
}
43+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2016 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.config;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.jboss.arquillian.container.test.api.Deployment;
21+
import org.jboss.arquillian.junit.Arquillian;
22+
import org.jboss.arquillian.test.api.ArquillianResource;
23+
import org.jboss.shrinkwrap.api.spec.WebArchive;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
27+
import org.ocpsoft.rewrite.test.RewriteTest;
28+
import org.openqa.selenium.WebDriver;
29+
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
30+
31+
/**
32+
* @see https://github.com/ocpsoft/rewrite/issues/243
33+
*/
34+
@RunWith(Arquillian.class)
35+
public class PathAndQueryTest extends RewriteTest
36+
{
37+
38+
@Deployment(testable = false)
39+
public static WebArchive getDeployment()
40+
{
41+
return RewriteTest.getDeployment()
42+
.addClass(PathAndQueryProvider.class)
43+
.addAsServiceProvider(ConfigurationProvider.class, PathAndQueryProvider.class);
44+
}
45+
46+
@ArquillianResource
47+
private java.net.URL baseUrl;
48+
49+
@Test
50+
public void testWithQuery() throws Exception
51+
{
52+
WebDriver driver = new HtmlUnitDriver();
53+
driver.get(baseUrl.toString() + "pathAndQuery/pathWithQuery?p1=foo&p2=bar");
54+
assertThat(driver.getCurrentUrl()).endsWith("/resultingPath/pathWithQuery%3Fp1=foo&p2=bar");
55+
}
56+
57+
@Test
58+
public void testWithoutQuery() throws Exception
59+
{
60+
WebDriver driver = new HtmlUnitDriver();
61+
driver.get(baseUrl.toString() + "pathAndQuery/pathWithoutQuery");
62+
assertThat(driver.getCurrentUrl()).endsWith("/resultingPath/pathWithoutQuery");
63+
}
64+
}

0 commit comments

Comments
 (0)