Use `getRequestURI` instead of `getServletPath` to get URI of current request(Both in prefix and regex matching).master
@@ -69,6 +69,12 @@ | |||
<artifactId>mockito-core</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework</groupId> | |||
<artifactId>spring-test</artifactId> | |||
<version>4.3.20.RELEASE</version> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -17,11 +17,12 @@ package com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route; | |||
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 class PrefixRoutePathMatcher implements Predicate<RequestContext> { | |||
@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); | |||
} | |||
@@ -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 class RegexRoutePathMatcher implements Predicate<RequestContext> { | |||
@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(); | |||
} | |||
@@ -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)); | |||
} | |||
} |