From 5cefda767608decf822e40721c0951abe7dec11b Mon Sep 17 00:00:00 2001 From: FhToday Date: Fri, 21 May 2021 18:06:17 +0800 Subject: [PATCH 1/7] fix issue --- pom.xml | 14 +++-- .../embeddedserver/jetty/JettyHandler.java | 21 +++++-- src/test/java/spark/InvalidRequestTest.java | 61 +++++++++++++++++++ 3 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 src/test/java/spark/InvalidRequestTest.java diff --git a/pom.xml b/pom.xml index 70728ade51..646e9785e6 100644 --- a/pom.xml +++ b/pom.xml @@ -40,17 +40,23 @@ org.slf4j - slf4j-api - 1.7.25 + slf4j-simple + 1.7.21 org.slf4j - slf4j-simple + slf4j-api 1.7.25 - test + + + + + + + org.eclipse.jetty diff --git a/src/main/java/spark/embeddedserver/jetty/JettyHandler.java b/src/main/java/spark/embeddedserver/jetty/JettyHandler.java index ef0c432c9a..0bc1a53b5d 100644 --- a/src/main/java/spark/embeddedserver/jetty/JettyHandler.java +++ b/src/main/java/spark/embeddedserver/jetty/JettyHandler.java @@ -4,7 +4,7 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -41,12 +41,21 @@ public JettyHandler(Filter filter) { @Override public void doHandle( - String target, - Request baseRequest, - HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException { + String target, + Request baseRequest, + HttpServletRequest request, + HttpServletResponse response) throws IOException, ServletException { HttpRequestWrapper wrapper = new HttpRequestWrapper(request); + final String[] METHODS = {"GET", "POST", "HEAD", "PUT", "OPTIONS", "DELETE", "TRACE", "CONNECT "}; + boolean isValid = false; + for (String METHOD : METHODS) { + if (request.getMethod().equalsIgnoreCase(METHOD)) { + isValid = true; + break; + } + } + if (!isValid) return; filter.doFilter(wrapper, response, null); if (wrapper.notConsumed()) { @@ -57,4 +66,4 @@ public void doHandle( } -} \ No newline at end of file +} diff --git a/src/test/java/spark/InvalidRequestTest.java b/src/test/java/spark/InvalidRequestTest.java new file mode 100644 index 0000000000..97d315aaa9 --- /dev/null +++ b/src/test/java/spark/InvalidRequestTest.java @@ -0,0 +1,61 @@ +package spark; + +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +import static spark.Spark.halt; + +public class InvalidRequestTest { + @Test + public void invalidRequestTest(){ + Service service = Service.ignite().port(4567); + service.staticFiles.externalLocation("/Users/"); + + service.get("/", (req, res) -> { + if (!req.requestMethod().equalsIgnoreCase("GET")) { + halt(401, "invalid Http method"); + } + return null; + }); + + String result = ""; + String url = "http://localhost:4567"; + BufferedReader in = null; + try { + URL realUrl = new URL(url); + URLConnection connection = realUrl.openConnection(); + connection.setRequestProperty("Method", "XYZ"); + connection.connect(); + Map> map = connection.getHeaderFields(); + for (String key : map.keySet()) { + System.out.println(key + "--->" + map.get(key)); + } + in = new BufferedReader(new InputStreamReader( + connection.getInputStream())); + String line; + while ((line = in.readLine()) != null) { + result += line; + } + } catch (Exception e) { + return; + } + finally { + try { + if (in != null) { + in.close(); + } + } catch (Exception e2) { + e2.printStackTrace(); + } + } + assertEquals("", result); + } +} From 52581e6c79605950f6972febe36dedb4bd1a32f7 Mon Sep 17 00:00:00 2001 From: Chauncey-Xxy <11810113@mail.sustech.edu.cn> Date: Fri, 21 May 2021 21:50:33 +0800 Subject: [PATCH 2/7] fix --- .../spark/http/matching/MatcherFilter.java | 14 +++ src/test/java/spark/Issue1077Test.java | 105 ++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/test/java/spark/Issue1077Test.java diff --git a/src/main/java/spark/http/matching/MatcherFilter.java b/src/main/java/spark/http/matching/MatcherFilter.java index 3fa05bea0a..0eaf4a5b77 100644 --- a/src/main/java/spark/http/matching/MatcherFilter.java +++ b/src/main/java/spark/http/matching/MatcherFilter.java @@ -17,6 +17,7 @@ package spark.http.matching; import java.io.IOException; +import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -34,6 +35,7 @@ import spark.Response; import spark.embeddedserver.jetty.HttpRequestWrapper; import spark.route.HttpMethod; +import spark.routematch.RouteMatch; import spark.serialization.SerializerChain; import spark.staticfiles.StaticFilesConfiguration; @@ -107,6 +109,18 @@ public void doFilter(ServletRequest servletRequest, String uri = httpRequest.getRequestURI(); String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER); + List routes = routeMatcher.findAll(); + String firstAcceptType = null; + for (RouteMatch rm : routes) { + if (rm.getMatchUri().equals(uri)) { + firstAcceptType = rm.getAcceptType(); + break; + } + } + if ("*/*".equals(acceptType) && firstAcceptType != null) { + acceptType = firstAcceptType; + } + Body body = Body.create(); RequestWrapper requestWrapper = RequestWrapper.create(); diff --git a/src/test/java/spark/Issue1077Test.java b/src/test/java/spark/Issue1077Test.java new file mode 100644 index 0000000000..418fb3f793 --- /dev/null +++ b/src/test/java/spark/Issue1077Test.java @@ -0,0 +1,105 @@ +package spark; + +import org.junit.Before; +import org.junit.Test; + +import spark.util.SparkTestUtil; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; +import static spark.Spark.*; + +// Try to fix issue 1077: https://github.com/perwendel/spark/issues/1077 +// I am not sure whether it is a bug, because it is tagged as Bug ..? +// But I think it conflict with the documentation, so I try to fix it +// In short, we expect the input and output are: +// curl -i -H "Accept: application/json" http://localhost:4567/hello : Hello application json +// curl -i -H "Accept: text/html" http://localhost:4567/hello : Go Away!!! +// curl http://localhost:4567/hello : Hello application json +// The first and second are right, but now the last command will get output: Go Away!!! +// I think it is not reasonable because the empty acceptType should match every possibilities +// so with the earliest match, it should match the first possible acceptType +// TestWheteherMatchRight() checks whether it matchs right +// TestWhetherMatchFirst() checks whether it matchs first + +public class Issue1077Test { + + public static final String HELLO = "/hello"; + + public static final String TEST="/test"; + + public static final int PORT = 4567; + + private static final SparkTestUtil http = new SparkTestUtil(PORT); + + @Before + public void setup() { + + get(HELLO,"application/json", (request, response) -> "Hello application json"); + + get(HELLO,"text/json", (request, response) -> "Hello text json"); + + get(HELLO, (request, response) -> { + response.status(406); + return "Go Away!!!"; + }); + + get(TEST,"text/json", (request, response) -> "Hello text json"); + + get(TEST,"application/json", (request, response) -> "Hello application json"); + + get(TEST, (request, response) -> { + response.status(406); + return "Go Away!!!"; + }); + } + + // CS304 Issue link: https://github.com/perwendel/spark/issues/1077 + @Test + public void testWheteherMatchRight() { + + try { + Map requestHeader = new HashMap<>(); + requestHeader.put("Host", "localhost:" + PORT); + requestHeader.put("User-Agent", "curl/7.55.1"); + requestHeader.put("x-forwarded-host", "proxy.mydomain.com"); + + SparkTestUtil.UrlResponse response = http.doMethod("GET",HELLO, "", false, "*/*", requestHeader); + assertEquals(200, response.status); + assertEquals("Hello application json", response.body); + + response = http.doMethod("GET",HELLO, "", false, "text/json", requestHeader); + assertEquals(200, response.status); + assertEquals("Hello text json", response.body); + + response = http.doMethod("GET",HELLO, "", false, "text/html*", requestHeader); + assertEquals(406, response.status); + assertEquals("Go Away!!!", response.body); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + // CS304 Issue link: https://github.com/perwendel/spark/issues/1077 + @Test + public void testWhetherMatchFirst() { + try { + Map requestHeader = new HashMap<>(); + requestHeader.put("Host", "localhost:" + PORT); + requestHeader.put("User-Agent", "curl/7.55.1"); + requestHeader.put("x-forwarded-host", "proxy.mydomain.com"); + + SparkTestUtil.UrlResponse response = http.doMethod("GET",TEST, "", false, "*/*", requestHeader); + assertEquals(200, response.status); + assertEquals("Hello text json", response.body); + + response = http.doMethod("GET",HELLO, "", false, "*/*", requestHeader); + assertEquals(200, response.status); + assertEquals("Hello application json", response.body); + } catch (Exception e) { + e.printStackTrace(); + } + } +} From adf9519a33252bb8b1ea58d1f447f4c624022fe1 Mon Sep 17 00:00:00 2001 From: "A.Lepe" Date: Thu, 11 Aug 2022 11:45:47 +0900 Subject: [PATCH 3/7] Updated PR-STATUS.md --- PR-STATUS.md | 7 +++++-- pom.xml | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/PR-STATUS.md b/PR-STATUS.md index 576e46b24b..63f29798d8 100644 --- a/PR-STATUS.md +++ b/PR-STATUS.md @@ -66,6 +66,11 @@ This is the current status for each PR: * :green_circle: **MERGED**: HTTP 2 Support Feature request (Major 3.0 candidate) * 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 + * perwendel/spark#1238 opened on May 21, 2021 by Chauncey-Xxy + + ### Rejected * :red_circle: **REJECTED (Travis was removed)**: Improve Travis CI build Performance @@ -83,8 +88,6 @@ This is the current status for each PR: ### To Fix / To Discuss -* :yellow_circle: **TEST FAILING**: Fix perwendel/spark/issues/1077 : Solve the bad route selection based on acceptType - * perwendel/spark#1238 opened on May 21, 2021 by Chauncey-Xxy * :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 diff --git a/pom.xml b/pom.xml index 90327fb70d..34326f8656 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.intellisrc spark-core bundle - 2.9.4-unofficial-3-SNAPSHOT + 2.9.4-unofficial-4-SNAPSHOT Spark A micro framework for creating web applications in Kotlin, Groovy and Java with minimal effort http://www.sparkjava.com From ffab5281dff3894f621d721a62cdafcc6cbf3fb8 Mon Sep 17 00:00:00 2001 From: "A.Lepe" Date: Thu, 11 Aug 2022 15:15:23 +0900 Subject: [PATCH 4/7] Fixed issue #1024 implementation and unit test. --- .../embeddedserver/jetty/JettyHandler.java | 19 +-- src/test/java/spark/InvalidRequestTest.java | 117 +++++++++++------- src/test/java/spark/Issue1077Test.java | 2 + .../java/spark/examples/hello/HelloWorld.java | 5 +- 4 files changed, 83 insertions(+), 60 deletions(-) diff --git a/src/main/java/spark/embeddedserver/jetty/JettyHandler.java b/src/main/java/spark/embeddedserver/jetty/JettyHandler.java index 9b1d2d2443..bed476ae09 100644 --- a/src/main/java/spark/embeddedserver/jetty/JettyHandler.java +++ b/src/main/java/spark/embeddedserver/jetty/JettyHandler.java @@ -24,6 +24,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.session.SessionHandler; @@ -50,15 +52,11 @@ public void doHandle( HttpServletResponse response) throws IOException, ServletException { HttpRequestWrapper wrapper = new HttpRequestWrapper(request); - final String[] METHODS = {"GET", "POST", "HEAD", "PUT", "OPTIONS", "DELETE", "TRACE", "CONNECT "}; - boolean isValid = false; - for (String METHOD : METHODS) { - if (request.getMethod().equalsIgnoreCase(METHOD)) { - isValid = true; - break; - } + HttpMethod method = HttpMethod.fromString(request.getMethod().trim().toUpperCase()); + if(method == null) { + response.sendError(HttpStatus.METHOD_NOT_ALLOWED_405); + return; } - if (!isValid) return; if(consume!=null && consume.contains(baseRequest.getRequestURI())){ wrapper.notConsumed(true); @@ -66,11 +64,6 @@ public void doHandle( filter.doFilter(wrapper, response, null); } - if (wrapper.notConsumed()) { - baseRequest.setHandled(false); - } else { - baseRequest.setHandled(true); - } baseRequest.setHandled(!wrapper.notConsumed()); } diff --git a/src/test/java/spark/InvalidRequestTest.java b/src/test/java/spark/InvalidRequestTest.java index 97d315aaa9..cebf1fb8db 100644 --- a/src/test/java/spark/InvalidRequestTest.java +++ b/src/test/java/spark/InvalidRequestTest.java @@ -1,61 +1,88 @@ package spark; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Before; import org.junit.Test; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.util.List; -import java.util.Map; +import java.net.URI; -import static org.junit.Assert.assertEquals; +import spark.util.SparkTestUtil; -import static spark.Spark.halt; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static spark.Spark.awaitInitialization; +import static spark.Spark.get; +import static spark.Spark.staticFileLocation; public class InvalidRequestTest { - @Test - public void invalidRequestTest(){ - Service service = Service.ignite().port(4567); - service.staticFiles.externalLocation("/Users/"); - - service.get("/", (req, res) -> { - if (!req.requestMethod().equalsIgnoreCase("GET")) { - halt(401, "invalid Http method"); - } - return null; + + public static class HttpFoo extends HttpRequestBase { + public final static String METHOD_NAME = "FOO"; + @Override + public String getMethod() { + return METHOD_NAME; + } + public HttpFoo(final String uri) { + super(); + setURI(URI.create(uri)); + } + public String getName() { + return "FOO"; + } + } + + public static final String FILE = "/page.html"; + + public static final String SERVICE="/test"; + + public static final int PORT = 4567; + + private static final SparkTestUtil http = new SparkTestUtil(PORT); + + @Before + public void setup() { + staticFileLocation("/public"); + get(SERVICE, (request, response) -> { + assertTrue(request.requestMethod().equalsIgnoreCase("GET")); + return "Hello"; }); - String result = ""; - String url = "http://localhost:4567"; - BufferedReader in = null; + awaitInitialization(); + } + + public int requestPathWithInvalidMethod(String path) { + int code = 0; try { - URL realUrl = new URL(url); - URLConnection connection = realUrl.openConnection(); - connection.setRequestProperty("Method", "XYZ"); - connection.connect(); - Map> map = connection.getHeaderFields(); - for (String key : map.keySet()) { - System.out.println(key + "--->" + map.get(key)); - } - in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - String line; - while ((line = in.readLine()) != null) { - result += line; - } + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpFoo fooMethod = new HttpFoo("http://localhost:" + PORT + path); + HttpResponse response = httpClient.execute(fooMethod); + code = response.getStatusLine().getStatusCode(); } catch (Exception e) { - return; + fail("Unexpected exception: " + e.getMessage()); } - finally { - try { - if (in != null) { - in.close(); - } - } catch (Exception e2) { - e2.printStackTrace(); - } + return code; + } + + @Test + public void invalidRequestTest(){ + // Testing that file and service is up: + try { + SparkTestUtil.UrlResponse response = http.doMethod("GET", SERVICE, ""); + assertEquals(200, response.status); + assertEquals("Hello", response.body); + + response = http.doMethod("GET", FILE, ""); + assertEquals(200, response.status); + } catch (Exception e) { + e.printStackTrace(); + fail("Unexpected exception: " + e.getMessage()); } - assertEquals("", result); + // Testing wrong method (we cannot use http.doMethod as it can not handle invalid methods) + assertEquals(405, requestPathWithInvalidMethod(FILE)); + assertEquals(405, requestPathWithInvalidMethod(SERVICE)); } } diff --git a/src/test/java/spark/Issue1077Test.java b/src/test/java/spark/Issue1077Test.java index 418fb3f793..ec53bdbae8 100644 --- a/src/test/java/spark/Issue1077Test.java +++ b/src/test/java/spark/Issue1077Test.java @@ -53,6 +53,8 @@ public void setup() { response.status(406); return "Go Away!!!"; }); + + awaitInitialization(); } // CS304 Issue link: https://github.com/perwendel/spark/issues/1077 diff --git a/src/test/java/spark/examples/hello/HelloWorld.java b/src/test/java/spark/examples/hello/HelloWorld.java index d3e7ef8985..47fabda81d 100644 --- a/src/test/java/spark/examples/hello/HelloWorld.java +++ b/src/test/java/spark/examples/hello/HelloWorld.java @@ -17,17 +17,18 @@ package spark.examples.hello; import static spark.Spark.get; +import static spark.Spark.staticFileLocation; /** * Minimal example - * * You can test from command with: * > curl -i 'http://localhost:4567/' */ +@SuppressWarnings("JavadocLinkAsPlainText") public class HelloWorld { public static void main(String[] args) { - + staticFileLocation("/public/"); get("/", (request, response) -> "Hello World!"); } From de9a849999683c5159748d04c11d5ca0ac137e07 Mon Sep 17 00:00:00 2001 From: "A.Lepe" Date: Thu, 11 Aug 2022 16:15:40 +0900 Subject: [PATCH 5/7] 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!"); } From 4a1737a0b99a8639f6e54c1e8c50f8f726158aa2 Mon Sep 17 00:00:00 2001 From: Alberto Lepe Date: Thu, 11 Aug 2022 16:17:58 +0900 Subject: [PATCH 6/7] Update README.md --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 948d39ed64..809b3e160c 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ In order to use this fork version, you need to change your spark dependency. com.intellisrc spark-core - 2.9.4-unofficial-3 + 2.9.4-unofficial-4 ``` @@ -32,7 +32,7 @@ In order to use this fork version, you need to change your spark dependency. ```groovy dependencies { - implementation 'com.intellisrc:spark-core:2.9.4-unofficial-3' + implementation 'com.intellisrc:spark-core:2.9.4-unofficial-4' } ``` @@ -56,6 +56,8 @@ Security: ## Release 2 +These are the patches included in `unofficial-2`: + Bug fixes: * Initiate the servlet instance in exception mapper (perwendel#1137) @@ -68,6 +70,8 @@ Improvements: ## Release 3 +These are the patches included in `unofficial-3`: + Bug fixes: * Fixed GZip content-length problem - Issue: perwendel#1157 (also perwendel#459, perwendel#742 and perwendel#937) @@ -79,11 +83,17 @@ Improvements: * HTTP/2 support (perwendel#1183) ## Release4 + +These are the patches included in `unofficial-1`: + +Bug fixes: * 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) +Improvements: +* Added unicode support in paths (issue perwendel#1026) (PR: perwendel/spark#1222) + More details and examples on the differences between the Official version and this one: [DIFFERENCES.md](DIFFERENCES.md) ---------------------------------------- From 7186d6657e5d646eaf4e6dcc2dd8a03d99b6d49a Mon Sep 17 00:00:00 2001 From: Alberto Lepe Date: Thu, 11 Aug 2022 16:20:54 +0900 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 809b3e160c..7b9d7eda8a 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Improvements: ## Release4 -These are the patches included in `unofficial-1`: +These are the patches included in `unofficial-4`: Bug fixes: * Fixed optional trailing slash when used with params