Skip to content

Commit

Permalink
Fixed optional trailing slash combined with params.
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Lepe committed Aug 10, 2022
1 parent 017868b commit d4ee10c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main/java/spark/route/RouteEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private boolean matchPath(String input) { // NOSONAR
Pattern pattern = Pattern.compile(routePath, Pattern.CASE_INSENSITIVE);
return pattern.matcher(input).find();
}
// Match slashes
// Match slashes (return false if they don't match except when it is optional)
if (!this.path.endsWith("*")
// If the user input has a slash on the end, either our path should end in slash or optional
&& ((input.endsWith("/") && !(this.path.endsWith("/") || this.path.endsWith("/?"))) // NOSONAR
Expand All @@ -83,6 +83,10 @@ private boolean matchPath(String input) { // NOSONAR
// check params
List<String> thisPathList = SparkUtils.convertRouteToList(this.path);
List<String> pathList = SparkUtils.convertRouteToList(input);
// Remove optional "/?" when using params
if(thisPathList.indexOf("?") == thisPathList.size() - 1) {
thisPathList.remove("?");
}

int thisPathSize = thisPathList.size();
int pathSize = pathList.size();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/spark/RequestSlashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ String getPath() {
@BeforeClass
public static void setup() throws IOException {
checks = new ArrayList<Checker>() {{
/*
// Root Match: Slash should be optional in the server:
add(new Checker("/","/","/user/"));
// File match: Opening slash should be optional (server).
Expand All @@ -56,6 +57,12 @@ public static void setup() throws IOException {
add(new Checker("dir4/?","/dir4/","/dir4/dir/"));
add(new Checker("/dir5/?","/dir5","/dir"));
add(new Checker("/dir6/?","/dir6/","/dir/"));
*/
// Trailing slash optional with parameter:
add(new Checker("dir7/:id/?","/dir7/1234","/dir7/dir/1111"));
add(new Checker("dir8/:id/?","/dir8/1234/","/dir8/dir/2222/"));
add(new Checker("/dir9/:id/?","/dir9/1234","/dir9"));
add(new Checker("/dir10/:id/?","/dir10/1234/","/dir10/"));
//add(new Checker("","",""));
}};
for(Checker c : checks) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/spark/examples/websocket/WebSocketExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package spark.examples.websocket;

import static spark.Spark.init;
import static spark.Spark.webSocket;
import static spark.Spark.*;

public class WebSocketExample {

public static void main(String[] args) {
staticFileLocation("/public"); //Test webSocket combined with http
webSocket("/echo", EchoWebSocket.class);
webSocket("/ping", PingWebSocket.class);
init();
Expand Down

0 comments on commit d4ee10c

Please sign in to comment.