diff --git a/src/main/java/spark/route/RouteEntry.java b/src/main/java/spark/route/RouteEntry.java index ab7a489ff1..d9b3f80219 100644 --- a/src/main/java/spark/route/RouteEntry.java +++ b/src/main/java/spark/route/RouteEntry.java @@ -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 @@ -83,6 +83,10 @@ private boolean matchPath(String input) { // NOSONAR // check params List thisPathList = SparkUtils.convertRouteToList(this.path); List 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(); diff --git a/src/test/java/spark/RequestSlashTest.java b/src/test/java/spark/RequestSlashTest.java index 9024008cbb..be1c3224b6 100644 --- a/src/test/java/spark/RequestSlashTest.java +++ b/src/test/java/spark/RequestSlashTest.java @@ -43,6 +43,7 @@ String getPath() { @BeforeClass public static void setup() throws IOException { checks = new ArrayList() {{ + /* // Root Match: Slash should be optional in the server: add(new Checker("/","/","/user/")); // File match: Opening slash should be optional (server). @@ -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) { diff --git a/src/test/java/spark/examples/websocket/WebSocketExample.java b/src/test/java/spark/examples/websocket/WebSocketExample.java index 7ab9a8aa2b..112033ae12 100644 --- a/src/test/java/spark/examples/websocket/WebSocketExample.java +++ b/src/test/java/spark/examples/websocket/WebSocketExample.java @@ -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();