From 5d20a69228ac097b91d6f385e910aaad3092eb00 Mon Sep 17 00:00:00 2001 From: MonteCrystal <58733371+MonteCrystal@users.noreply.github.com> Date: Tue, 18 May 2021 22:04:11 +0800 Subject: [PATCH 1/3] Enhancement for issue #1151 --- src/main/java/spark/route/RouteEntry.java | 8 +++++++ src/test/java/spark/route/RouteEntryTest.java | 23 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/spark/route/RouteEntry.java b/src/main/java/spark/route/RouteEntry.java index 87fc2e78bf..b654e793cc 100644 --- a/src/main/java/spark/route/RouteEntry.java +++ b/src/main/java/spark/route/RouteEntry.java @@ -122,6 +122,14 @@ private boolean matchPath(String path) { // NOSONAR } // End check wild card } + if (thisPathSize > pathSize) { + for (int i = pathSize; i < thisPathSize; i++) { + if (!thisPathList.get(i).endsWith("?")) { + return false; + } + } + return true; + } return false; } } diff --git a/src/test/java/spark/route/RouteEntryTest.java b/src/test/java/spark/route/RouteEntryTest.java index c4ac08fd9c..1b13fb3566 100644 --- a/src/test/java/spark/route/RouteEntryTest.java +++ b/src/test/java/spark/route/RouteEntryTest.java @@ -116,4 +116,25 @@ public void testMatches_longRoutePathWildcard() { entry.matches(HttpMethod.get, "/test/this/resource/child/id")); } -} \ No newline at end of file + @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")); + } + + @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")); + } + +} From 178c1415b2871b5979002a060a2f212bae857dd2 Mon Sep 17 00:00:00 2001 From: MonteCrystal <58733371+MonteCrystal@users.noreply.github.com> Date: Tue, 18 May 2021 22:10:38 +0800 Subject: [PATCH 2/3] added comments --- src/main/java/spark/route/RouteEntry.java | 1 + src/test/java/spark/route/RouteEntryTest.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/spark/route/RouteEntry.java b/src/main/java/spark/route/RouteEntry.java index b654e793cc..61ae6d6d16 100644 --- a/src/main/java/spark/route/RouteEntry.java +++ b/src/main/java/spark/route/RouteEntry.java @@ -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("/")))) { diff --git a/src/test/java/spark/route/RouteEntryTest.java b/src/test/java/spark/route/RouteEntryTest.java index 1b13fb3566..8f3d6039c4 100644 --- a/src/test/java/spark/route/RouteEntryTest.java +++ b/src/test/java/spark/route/RouteEntryTest.java @@ -116,6 +116,7 @@ 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(); @@ -125,7 +126,7 @@ public void testMatches_WithoutOptionalParameters() { 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(); From 26e0583d6e9f5bf8f8eea806abb2ab362f4281c9 Mon Sep 17 00:00:00 2001 From: MonteCrystal <58733371+MonteCrystal@users.noreply.github.com> Date: Fri, 21 May 2021 18:26:51 +0800 Subject: [PATCH 3/3] Update RouteEntry.java --- src/main/java/spark/route/RouteEntry.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/spark/route/RouteEntry.java b/src/main/java/spark/route/RouteEntry.java index 61ae6d6d16..304c2e9779 100644 --- a/src/main/java/spark/route/RouteEntry.java +++ b/src/main/java/spark/route/RouteEntry.java @@ -124,6 +124,11 @@ 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;