Skip to content

Commit

Permalink
fix #30 Strips query parameters from template matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane Maldini committed Sep 16, 2018
1 parent 9d7f477 commit 54a7297
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/reactor/netty/http/server/HttpPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,23 @@ static final class UriPathTemplate {

private final Pattern uriPattern;

static String filterQueryParams(String uri) {
int hasQuery = uri.lastIndexOf("?");
if (hasQuery != -1) {
return uri.substring(0, hasQuery);
}
else {
return uri;
}
}

/**
* Creates a new {@code UriPathTemplate} from the given {@code uriPattern}.
*
* @param uriPattern The pattern to be used by the template
*/
public UriPathTemplate(String uriPattern) {
String s = "^" + uriPattern;
UriPathTemplate(String uriPattern) {
String s = "^" + filterQueryParams(uriPattern);

Matcher m = NAME_SPLAT_PATTERN.matcher(s);
while (m.find()) {
Expand Down Expand Up @@ -350,6 +360,7 @@ final Map<String, String> match(String uri) {
}

private Matcher matcher(String uri) {
uri = filterQueryParams(uri);
Matcher m = matchers.get(uri);
if (null == m) {
m = uriPattern.matcher(uri);
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/reactor/netty/http/server/UriPathTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,46 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasEntry;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

public class UriPathTemplateTest {

@Test
public void patternShouldMatchPathWithOnlyLetters() {
UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/{order}");
// works as expected
assertThat(uriPathTemplate.match("/test/1").get("order"), is("1"));
}

@Test
public void patternShouldMatchPathWithDots() {
UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/{order}");
// does not match, the dot in the segment parameter breaks matching
// expected: a map containing {"order": "2.0"}, found: empty map
assertThat(uriPathTemplate.match("/test/2.0").get("order"), is("2.0"));
}

@Test
public void staticPatternShouldMatchPathWithQueryParams() {
UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/3");
// does not match, the query parameter breaks matching
// expected: true, found: false
assertTrue(uriPathTemplate.matches("/test/3?q=reactor"));
// assertThat(uriPathTemplate.matches("/test/3?q=reactor"), is(true));
}

@Test
public void parameterizedPatternShouldMatchPathWithQueryParams() {
UriPathTemplate uriPathTemplate = new UriPathTemplate("/test/{order}");
// does not match, the query parameter breaks matching
// expected: a map containing {"order": "3"}, found: a map containing {"order": "3?q=reactor"}
assertEquals("3",
uriPathTemplate.match("/test/3?q=reactor")
.get("order"));
// assertThat(uriPathTemplate.match("/test/3?q=reactor").get("order"), is("3"));
}

@Test
public void staticPathShouldBeMatched() {
UriPathTemplate template = new UriPathTemplate("/comments");
Expand Down

0 comments on commit 54a7297

Please sign in to comment.