Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement for issue #1151: make route parameter optional #1229

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/spark/route/RouteEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ boolean matches(HttpMethod httpMethod, String path) {
return match;
}

//CS304 Issue link: https://github.com/perwendel/spark/issues/1151
private boolean matchPath(String path) { // NOSONAR
if (!this.path.endsWith("*") && ((path.endsWith("/") && !this.path.endsWith("/")) // NOSONAR
|| (this.path.endsWith("/") && !path.endsWith("/")))) {
Expand Down Expand Up @@ -122,6 +123,19 @@ private boolean matchPath(String path) { // NOSONAR
}
// End check wild card
}
if (thisPathSize > pathSize) {
for (int i = pathSize - 1; i > -1; i--) {
if(!thisPathList.get(i).equals(pathList.get(i))){
return false;
}
}
for (int i = pathSize; i < thisPathSize; i++) {
if (!thisPathList.get(i).endsWith("?")) {
return false;
}
}
return true;
}
return false;
}
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/spark/route/RouteEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,26 @@ public void testMatches_longRoutePathWildcard() {
entry.matches(HttpMethod.get, "/test/this/resource/child/id"));
}

}
//CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/1151
@Test
public void testMatches_WithoutOptionalParameters() {
RouteEntry entry = new RouteEntry();
entry.httpMethod = HttpMethod.get;
entry.path = "/test/:name?";

assertTrue("Should return true because the :name? section in path is optional",
entry.matches(HttpMethod.get, "/test"));
}
//CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/1151
@Test
public void testMatches_WithOptionalParameters() {
RouteEntry entry = new RouteEntry();
entry.httpMethod = HttpMethod.get;
entry.path = "/test/:name?";

assertTrue("Should return true because the :name? section in path is optional," +
"the statement can match /test/*",
entry.matches(HttpMethod.get, "/test/foo"));
}

}