diff --git a/sentinel-adapter/sentinel-zuul-adapter/pom.xml b/sentinel-adapter/sentinel-zuul-adapter/pom.xml index e87b401b8f..4cf3a0bb48 100755 --- a/sentinel-adapter/sentinel-zuul-adapter/pom.xml +++ b/sentinel-adapter/sentinel-zuul-adapter/pom.xml @@ -69,6 +69,12 @@ mockito-core test + + org.springframework + spring-test + 4.3.20.RELEASE + test + \ No newline at end of file diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java index 21e636e589..b592a9cf03 100644 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java +++ b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java @@ -17,11 +17,12 @@ import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.function.Predicate; - import com.netflix.zuul.context.RequestContext; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; +import javax.servlet.http.HttpServletRequest; + /** * @author Eric Zhao * @since 1.6.0 @@ -42,7 +43,12 @@ public PrefixRoutePathMatcher(String pattern) { @Override public boolean test(RequestContext context) { - String path = context.getRequest().getServletPath(); + //Solve the problem of prefix matching + HttpServletRequest request = context.getRequest(); + String path = request.getRequestURI(); + if (path == null) { + AssertUtil.assertNotBlank(pattern, "requesturi cannot be blank"); + } if (canMatch) { return pathMatcher.match(pattern, path); } diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java index daf1310ec3..4363e4e795 100644 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java +++ b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java @@ -15,13 +15,13 @@ */ package com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route; -import java.util.regex.Pattern; - import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.function.Predicate; - import com.netflix.zuul.context.RequestContext; +import javax.servlet.http.HttpServletRequest; +import java.util.regex.Pattern; + /** * @author Eric Zhao * @since 1.6.0 @@ -39,7 +39,12 @@ public RegexRoutePathMatcher(String pattern) { @Override public boolean test(RequestContext context) { - String path = context.getRequest().getServletPath(); + //Solve the problem of route matching + HttpServletRequest request = context.getRequest(); + String path = request.getRequestURI(); + if (path == null) { + AssertUtil.assertNotBlank(pattern, "requesturi cannot be blank"); + } return regex.matcher(path).matches(); } diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java new file mode 100644 index 0000000000..eb67921a77 --- /dev/null +++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java @@ -0,0 +1,56 @@ +package com.alibaba.csp.sentinel.adapter.gateway.zuul.route; + +import com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route.PrefixRoutePathMatcher; +import com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route.RegexRoutePathMatcher; +import com.netflix.zuul.context.RequestContext; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; + +import static com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant.SERVICE_ID_KEY; + +/** + * @author: jiangzian + **/ +public class SentinelZuulRouteTest { + + private final String SERVICE_ID = "servicea"; + + private final String SERVER_NAME = "www.example.com"; + private final String REQUEST_URI = "/servicea/test.jsp"; + private final String QUERY_STRING = "param1=value1¶m"; + + private RequestContext requestContext = new RequestContext(); + + + @Before + public void setUp() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setServerName(SERVER_NAME); + request.setRequestURI(REQUEST_URI); + request.setQueryString(QUERY_STRING); + requestContext.set(SERVICE_ID_KEY, SERVICE_ID); + requestContext.setRequest(request); + RequestContext.testSetCurrentContext(requestContext); + } + + @Test + public void testPrefixRoutePathMatche() { + PrefixRoutePathMatcher prefixRoutePathMatcher = new PrefixRoutePathMatcher("/servicea/????.jsp"); + Assert.assertTrue(prefixRoutePathMatcher.test(requestContext)); + + prefixRoutePathMatcher = new PrefixRoutePathMatcher("/servicea/????.do"); + Assert.assertTrue(!prefixRoutePathMatcher.test(requestContext)); + } + + @Test + public void testRegexRoutePathMatcher() { + RegexRoutePathMatcher regexRoutePathMatcher = new RegexRoutePathMatcher("/servicea/[a-zA-z]+(\\.jsp)"); + Assert.assertTrue(regexRoutePathMatcher.test(requestContext)); + + regexRoutePathMatcher = new RegexRoutePathMatcher("/serviceb/[a-zA-z]+(\\.jsp)"); + Assert.assertTrue(!regexRoutePathMatcher.test(requestContext)); + } + +} \ No newline at end of file