From de9a849999683c5159748d04c11d5ca0ac137e07 Mon Sep 17 00:00:00 2001 From: "A.Lepe" Date: Thu, 11 Aug 2022 16:15:40 +0900 Subject: [PATCH] Fixed issue #1026 implementation and unit test. --- PR-STATUS.md | 10 ++--- README.md | 8 +++- .../spark/http/matching/MatcherFilter.java | 2 + .../java/spark/GenericIntegrationTest.java | 38 +++++++------------ src/test/java/spark/Issue1026Test.java | 28 ++++++++++++++ .../java/spark/examples/hello/HelloWorld.java | 2 - 6 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 src/test/java/spark/Issue1026Test.java diff --git a/PR-STATUS.md b/PR-STATUS.md index 63f29798d8..218aa41741 100644 --- a/PR-STATUS.md +++ b/PR-STATUS.md @@ -67,8 +67,12 @@ This is the current status for each PR: * perwendel/spark#1183 opened on Jul 21, 2020 by luneo7 ### Merged (Release 4) -* :green_circle: **MERGERD**: Fix perwendel/spark/issues/1077 : Solve the bad route selection based on acceptType +* :green_circle: **MERGED**: Fix perwendel/spark/issues/1077 : Solve the bad route selection based on acceptType * perwendel/spark#1238 opened on May 21, 2021 by Chauncey-Xxy +* :green_circle: **MERGED AND FIXED**: fix issue perwendel/spark/issues/1204 + * perwendel/spark#1236 opened on May 21, 2021 by FhToday +* :green_circle: **CHERRY PICKED**: Solve the problem of non-ASCII characters in URL. Try to fix #1026 + * perwendel/spark#1222 opened on Apr 23, 2021 by Bugjudger ### Rejected @@ -88,10 +92,6 @@ This is the current status for each PR: ### To Fix / To Discuss -* :yellow_circle: **TEST FAILING**: fix issue perwendel/spark/issues/1204 - * perwendel/spark#1236 opened on May 21, 2021 by FhToday -* :yellow_circle: **TEST FAILING**: Solve the problem of non-ASCII characters in URL. Try to fix #1026 - * perwendel/spark#1222 opened on Apr 23, 2021 by Bugjudger * :yellow_circle: **MAY BREAK CODE (package name will change)**: Provide Automatic-Module-Name attribute in MANIFEST.MF (issue perwendel/spark/issues/961) * perwendel/spark#1212 opened on Mar 10, 2021 by apssouza22 * :yellow_circle: **MERGE FAILS (#1030 was closed as not-wanted)**: Try to Fix issue perwendel/spark/issues/1022 & perwendel/spark/issues/1030 bug-fix diff --git a/README.md b/README.md index 350aaca40f..948d39ed64 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,13 @@ Improvements: * Regex support in paths * HTTP/2 support (perwendel#1183) -More details on these features: [DIFFERENCES.md](DIFFERENCES.md) +## Release4 +* Fixed optional trailing slash when used with params +* Added unicode support in paths (issue perwendel#1026) (PR: perwendel/spark#1222) +* Fixed incorrect response based on acceptType (perwendel#1077) (PR: perwendel/spark#1238) +* Fixed incorrect handling when using invalid methods (perwendel/spark#1236) (PR: perwendel#1204) + +More details and examples on the differences between the Official version and this one: [DIFFERENCES.md](DIFFERENCES.md) ---------------------------------------- diff --git a/src/main/java/spark/http/matching/MatcherFilter.java b/src/main/java/spark/http/matching/MatcherFilter.java index d3820bf0d7..529ba37ec4 100644 --- a/src/main/java/spark/http/matching/MatcherFilter.java +++ b/src/main/java/spark/http/matching/MatcherFilter.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.util.List; +import java.net.URLDecoder; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -106,6 +107,7 @@ public void doFilter(ServletRequest servletRequest, String httpMethodStr = method.toLowerCase(); String uri = httpRequest.getRequestURI(); + uri = URLDecoder.decode(uri, "UTF-8"); String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER); List routes = routeMatcher.findAll(); diff --git a/src/test/java/spark/GenericIntegrationTest.java b/src/test/java/spark/GenericIntegrationTest.java index da1a7012cb..2b686190f6 100644 --- a/src/test/java/spark/GenericIntegrationTest.java +++ b/src/test/java/spark/GenericIntegrationTest.java @@ -18,7 +18,6 @@ import org.eclipse.jetty.websocket.client.WebSocketClient; import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -328,24 +327,14 @@ public void testEchoParam3() throws Exception { Assert.assertEquals("echo: " + polyglot, response.body); } + // NOTE: To support multiple languages in paths, we are using URLDecode, which in this case, + // it will convert '+' into ' ' @Test public void testPathParamsWithPlusSign() throws Exception { String pathParamWithPlusSign = "not+broken+path+param"; UrlResponse response = doMethod("GET", "/param/" + pathParamWithPlusSign, null); Assert.assertEquals(200, response.status); - Assert.assertEquals("echo: " + pathParamWithPlusSign, response.body); - } - - // FIXME: http2 is failing due to: https://github.com/eclipse/jetty.project/issues/6132 - // also: https://www.eclipse.org/jetty/javadoc/jetty-10/org/eclipse/jetty/http/UriCompliance.html - // to fix it: update to Jetty 10+ ? - @Ignore @Test - public void testParamWithEncodedSlash() throws Exception { - String polyglot = "te/st"; - String encoded = URLEncoder.encode(polyglot, "UTF-8"); - UrlResponse response = doMethod("GET", "/param/" + encoded, null); - Assert.assertEquals(200, response.status); - Assert.assertEquals("echo: " + polyglot, response.body); + Assert.assertEquals("echo: " + pathParamWithPlusSign.replace("+"," "), response.body); } @Test @@ -354,32 +343,31 @@ public void testParamWithEncodedSpace() throws Exception { String encoded = URLEncoder.encode(polyglot, "UTF-8"); UrlResponse response = doMethod("GET", "/param/" + encoded, null); Assert.assertEquals(200, response.status); - Assert.assertEquals("echo: " + polyglot.replace(" ", "+"), response.body); + Assert.assertEquals("echo: " + polyglot, response.body); } - // FIXME: http2 FIXME comment above - @Ignore @Test - public void testSplatWithEncodedSlash() throws Exception { - String param = "fo/shizzle"; + @Test + public void testSplatWithEncodedSpace() throws Exception { + String param = "fo shizzle"; String encodedParam = URLEncoder.encode(param, "UTF-8"); - String splat = "mah/FRIEND"; + String splat = "mah FRIEND"; String encodedSplat = URLEncoder.encode(splat, "UTF-8"); UrlResponse response = doMethod("GET", "/paramandwild/" + encodedParam + "/stuff/" + encodedSplat, null); Assert.assertEquals(200, response.status); - Assert.assertEquals("paramandwild: " + param.replace(" ", "+") + splat, response.body); + Assert.assertEquals("paramandwild: " + param + splat, response.body); } @Test - public void testSplatWithEncodedSpace() throws Exception { - String param = "fo shizzle"; + public void testSplatWithUTF8() throws Exception { + String param = "私のもの"; String encodedParam = URLEncoder.encode(param, "UTF-8"); - String splat = "mah FRIEND"; + String splat = "何でも大丈夫です。"; String encodedSplat = URLEncoder.encode(splat, "UTF-8"); UrlResponse response = doMethod("GET", "/paramandwild/" + encodedParam + "/stuff/" + encodedSplat, null); Assert.assertEquals(200, response.status); - Assert.assertEquals("paramandwild: " + param.replace(" ", "+") + splat, response.body); + Assert.assertEquals("paramandwild: " + param + splat, response.body); } @Test diff --git a/src/test/java/spark/Issue1026Test.java b/src/test/java/spark/Issue1026Test.java new file mode 100644 index 0000000000..48780a0032 --- /dev/null +++ b/src/test/java/spark/Issue1026Test.java @@ -0,0 +1,28 @@ +package spark; +import org.junit.Before; +import org.junit.Test; + +import spark.util.SparkTestUtil; + +import static org.junit.Assert.*; +import static spark.Spark.*; +/** + * @since 2022/08/11. + */ +public class Issue1026Test { + private static final String ROUTE = "/api/v1/管理者/"; + private static SparkTestUtil http; + + @Before + public void setup() { + http = new SparkTestUtil(4567); + get(ROUTE, (q,a)-> "Get filter matched"); + awaitInitialization(); + } + + @Test + public void testUrl() throws Exception { + SparkTestUtil.UrlResponse response = http.get(ROUTE); + assertEquals(200,response.status); + } +} diff --git a/src/test/java/spark/examples/hello/HelloWorld.java b/src/test/java/spark/examples/hello/HelloWorld.java index 47fabda81d..66fb7bb259 100644 --- a/src/test/java/spark/examples/hello/HelloWorld.java +++ b/src/test/java/spark/examples/hello/HelloWorld.java @@ -17,7 +17,6 @@ package spark.examples.hello; import static spark.Spark.get; -import static spark.Spark.staticFileLocation; /** * Minimal example @@ -28,7 +27,6 @@ public class HelloWorld { public static void main(String[] args) { - staticFileLocation("/public/"); get("/", (request, response) -> "Hello World!"); }