From 0a31c047330e1592a376bac8d49236b21fa0bd4c Mon Sep 17 00:00:00 2001 From: Ta Van Dung Date: Sat, 6 Jan 2024 13:41:31 +0700 Subject: [PATCH] version 1.2.6: allow * path (#54) --- ezyhttp-client/pom.xml | 2 +- ezyhttp-core/pom.xml | 2 +- .../tvd12/ezyhttp/core/net/PathVariables.java | 15 ++++- .../com/tvd12/ezyhttp/core/net/URITree.java | 3 +- .../core/test/net/PathVariablesTest.java | 58 +++++++++++++++++-- .../ezyhttp/core/test/net/URITreeTest.java | 16 +++++ ezyhttp-server-boot/pom.xml | 2 +- ezyhttp-server-core/pom.xml | 2 +- ezyhttp-server-graphql/pom.xml | 2 +- ezyhttp-server-jetty/pom.xml | 2 +- ezyhttp-server-management/pom.xml | 2 +- ezyhttp-server-thymeleaf/pom.xml | 2 +- ezyhttp-server-tomcat/pom.xml | 2 +- pom.xml | 2 +- 14 files changed, 94 insertions(+), 18 deletions(-) diff --git a/ezyhttp-client/pom.xml b/ezyhttp-client/pom.xml index bd591a59..41bf2e63 100644 --- a/ezyhttp-client/pom.xml +++ b/ezyhttp-client/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-client diff --git a/ezyhttp-core/pom.xml b/ezyhttp-core/pom.xml index 66fcab10..5375bcd4 100644 --- a/ezyhttp-core/pom.xml +++ b/ezyhttp-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-core diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/PathVariables.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/PathVariables.java index 769364bb..7bbca0d1 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/PathVariables.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/PathVariables.java @@ -1,11 +1,11 @@ package com.tvd12.ezyhttp.core.net; +import com.tvd12.ezyfox.util.EzyEntry; + import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; -import com.tvd12.ezyfox.util.EzyEntry; - public final class PathVariables { private PathVariables() {} @@ -23,6 +23,17 @@ public static List> getVariables( String varName = getVariableName(templatePath); String varValue = uriPaths[i]; answer.add(EzyEntry.of(varName, varValue)); + } else if (templatePath.equals("*")) { + StringBuilder varValue = new StringBuilder(); + int lastIndex = uriPaths.length - 1; + for (; i < uriPaths.length; ++i) { + varValue.append(uriPaths[i]); + if (i < lastIndex) { + varValue.append("/"); + } + } + answer.add(EzyEntry.of("*", varValue.toString())); + break; } } return answer; diff --git a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/URITree.java b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/URITree.java index 77f25f5c..1e73213b 100644 --- a/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/URITree.java +++ b/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/net/URITree.java @@ -41,7 +41,8 @@ public String getMatchedURI(String uri) { child = lastChild.children.get("{}"); } if (child == null) { - return null; + child = lastChild.children.get("*"); + return child == null ? null : child.uri; } lastChild = child; } diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/PathVariablesTest.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/PathVariablesTest.java index 631918f5..c1d0d5cb 100644 --- a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/PathVariablesTest.java +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/PathVariablesTest.java @@ -1,14 +1,15 @@ package com.tvd12.ezyhttp.core.test.net; -import java.util.List; -import java.util.Map.Entry; - -import org.testng.annotations.Test; - +import com.tvd12.ezyfox.util.EzyMapBuilder; import com.tvd12.ezyhttp.core.net.PathVariables; import com.tvd12.test.assertion.Asserts; import com.tvd12.test.base.BaseTest; import com.tvd12.test.performance.Performance; +import org.testng.annotations.Test; + +import java.util.List; +import java.util.Map.Entry; +import java.util.stream.Collectors; public class PathVariablesTest extends BaseTest { @@ -30,4 +31,51 @@ public void isPathVariableTest() { Asserts.assertFalse(PathVariables.isPathVariable("a")); Asserts.assertFalse(PathVariables.isPathVariable("a}")); } + + @Test + public void anyPathTest() { + // given + String template = "/market/items/{projectName}/java/docs/*"; + String uri = "/market/items/hello/java/docs/world/index.html"; + + // when + List> actual = PathVariables.getVariables( + template, + uri + ); + + // then + Asserts.assertEquals( + actual.stream() + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)), + EzyMapBuilder.mapBuilder() + .put("projectName", "hello") + .put("*", "world/index.html") + .build() + ); + } + + @Test + public void anyPathComplexTest() { + // given + String template = "/versions/{ezyplatformVersion}/java-docs/{moduleName}/*"; + String uri = "/versions/0.0.2/java-docs/ezyplatform-web/org/youngmonkeys/ezyplatform/web/package-frame.html"; + + // when + List> actual = PathVariables.getVariables( + template, + uri + ); + + // then + Asserts.assertEquals( + actual.stream() + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)), + EzyMapBuilder.mapBuilder() + .put("ezyplatformVersion", "0.0.2") + .put("moduleName", "ezyplatform-web") + .put("*", "org/youngmonkeys/ezyplatform/web/package-frame.html") + .build() + ); + } } diff --git a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/URITreeTest.java b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/URITreeTest.java index 71de9ad8..c616be1b 100644 --- a/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/URITreeTest.java +++ b/ezyhttp-core/src/test/java/com/tvd12/ezyhttp/core/test/net/URITreeTest.java @@ -64,4 +64,20 @@ public void getMatchedURIChildNull2() { Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}/{foo}")); Asserts.assertNull(tree.getMatchedURI("/api/v1/hello/{world}/{foo}/{bar}/unknown")); } + + @Test + public void matchAllTest() { + // given + URITree tree = new URITree(); + tree.addURI("/market/items/hello/java/docs/*"); + + // when + // then + System.out.println(tree); + Asserts.assertNull(tree.getMatchedURI("/market/items/hello/java")); + Asserts.assertNull(tree.getMatchedURI("/market/items/hello/java/docs")); + Asserts.assertNull(tree.getMatchedURI("/market/items/hello/java/docs/")); + Asserts.assertNotNull(tree.getMatchedURI("/market/items/hello/java/docs/index.html")); + Asserts.assertNotNull(tree.getMatchedURI("/market/items/hello/java/docs/com/tvd12/index.html")); + } } diff --git a/ezyhttp-server-boot/pom.xml b/ezyhttp-server-boot/pom.xml index 132e8d20..2af34280 100644 --- a/ezyhttp-server-boot/pom.xml +++ b/ezyhttp-server-boot/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-boot diff --git a/ezyhttp-server-core/pom.xml b/ezyhttp-server-core/pom.xml index 773f9ace..fe0ec5f4 100644 --- a/ezyhttp-server-core/pom.xml +++ b/ezyhttp-server-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-core diff --git a/ezyhttp-server-graphql/pom.xml b/ezyhttp-server-graphql/pom.xml index 997e5809..0e540223 100644 --- a/ezyhttp-server-graphql/pom.xml +++ b/ezyhttp-server-graphql/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-graphql diff --git a/ezyhttp-server-jetty/pom.xml b/ezyhttp-server-jetty/pom.xml index 56cca6fa..98cdd764 100644 --- a/ezyhttp-server-jetty/pom.xml +++ b/ezyhttp-server-jetty/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-jetty diff --git a/ezyhttp-server-management/pom.xml b/ezyhttp-server-management/pom.xml index d3e5acb1..9b87d4e1 100644 --- a/ezyhttp-server-management/pom.xml +++ b/ezyhttp-server-management/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-management ezyhttp-server-management diff --git a/ezyhttp-server-thymeleaf/pom.xml b/ezyhttp-server-thymeleaf/pom.xml index 81497ba9..d03de485 100644 --- a/ezyhttp-server-thymeleaf/pom.xml +++ b/ezyhttp-server-thymeleaf/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-thymeleaf diff --git a/ezyhttp-server-tomcat/pom.xml b/ezyhttp-server-tomcat/pom.xml index 13469671..31302c27 100644 --- a/ezyhttp-server-tomcat/pom.xml +++ b/ezyhttp-server-tomcat/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.2.5 + 1.2.6 ezyhttp-server-tomcat diff --git a/pom.xml b/pom.xml index 2eda014c..68fccafd 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 1.0.6 ezyhttp - 1.2.5 + 1.2.6 pom ezyhttp