Skip to content

Commit ec4b590

Browse files
committed
Make interceptors context path aware
1 parent 59ddbae commit ec4b590

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

grails-plugin-interceptors/src/main/groovy/grails/artefact/Interceptor.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,18 @@ trait Interceptor implements ResponseRenderer, ResponseRedirector, RequestForwar
8787
}
8888

8989
def req = request
90+
def ctxPath = req.contextPath
9091
def uri = req.requestURI
92+
def noCtxUri = uri - ctxPath
93+
def checkNoCtxUri = ctxPath && uri.startsWith(ctxPath)
9194

9295
def matchedInfo = request.getAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST)
9396

9497
UrlMappingInfo grailsMappingInfo = (UrlMappingInfo)matchedInfo
9598

9699
for(Matcher matcher in allMatchers) {
97-
if(matcher.doesMatch(uri, grailsMappingInfo, req.method)) {
100+
if(matcher.doesMatch(uri, grailsMappingInfo, req.method) ||
101+
(checkNoCtxUri && matcher.doesMatch(noCtxUri, grailsMappingInfo, req.method))) {
98102
request.setAttribute(interceptorMatchKey, Boolean.TRUE)
99103
return true
100104
}

grails-plugin-interceptors/src/test/groovy/grails/artefact/InterceptorSpec.groovy

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,78 @@ class InterceptorSpec extends Specification {
178178
'GET' | false
179179
}
180180

181+
void "Test match with uri no context path"() {
182+
given:"A test interceptor"
183+
def i = new TestUriInterceptor()
184+
def webRequest = GrailsWebMockUtil.bindMockWebRequest(new MockServletContext(), new MockHttpServletRequest("", requestUri), new MockHttpServletResponse())
185+
def request = webRequest.request
186+
187+
when:"The uri of the current request is ${requestUri}"
188+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test", action: "save"))
189+
190+
then: "We match: ${shouldMatch}"
191+
i.doesMatch() == shouldMatch
192+
193+
where:
194+
requestUri | shouldMatch
195+
'/bar' | true
196+
'/bar/x' | true
197+
'/fooBar' | false
198+
'/foo' | true
199+
'/foo/x' | false
200+
'/foo/bar' | true
201+
}
202+
203+
void "Test match with uri and context path"() {
204+
given:"A test interceptor"
205+
def i = new TestUriInterceptor()
206+
def mockRequest = new MockHttpServletRequest("", requestUri)
207+
mockRequest.setContextPath('/grails')
208+
def webRequest = GrailsWebMockUtil.bindMockWebRequest(new MockServletContext(), mockRequest, new MockHttpServletResponse())
209+
210+
def request = webRequest.request
211+
212+
when:"The uri of the current request is ${requestUri}"
213+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test", action: "save"))
214+
215+
then: "We match: ${shouldMatch}"
216+
i.doesMatch() == shouldMatch
217+
218+
where:
219+
requestUri | shouldMatch
220+
'/grails/bar' | true
221+
'/grails/bar/x' | true
222+
'/grails/fooBar' | false
223+
'/grails/foo' | true
224+
'/grails/foo/x' | false
225+
'/grails/foo/bar' | true
226+
}
227+
228+
void "Test match with uri and context path with an interceptor that defines the context path"() {
229+
given:"A test interceptor"
230+
def i = new TestContextUriInterceptor()
231+
def mockRequest = new MockHttpServletRequest("", requestUri)
232+
mockRequest.setContextPath('/grails')
233+
def webRequest = GrailsWebMockUtil.bindMockWebRequest(new MockServletContext(), mockRequest, new MockHttpServletResponse())
234+
235+
def request = webRequest.request
236+
237+
when:"The uri of the current request is ${requestUri}"
238+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test", action: "save"))
239+
240+
then: "We match: ${shouldMatch}"
241+
i.doesMatch() == shouldMatch
242+
243+
where:
244+
requestUri | shouldMatch
245+
'/grails/bar' | true
246+
'/grails/bar/x' | true
247+
'/grails/fooBar' | false
248+
'/grails/foo' | true
249+
'/grails/foo/x' | false
250+
'/grails/foo/bar' | true
251+
}
252+
181253
void clearMatch(i, HttpServletRequest request) {
182254
request.removeAttribute(i.getClass().name + InterceptorArtefactHandler.MATCH_SUFFIX)
183255
}
@@ -226,3 +298,19 @@ class TestMethodInterceptor implements Interceptor {
226298
match(method: 'POST')
227299
}
228300
}
301+
302+
class TestUriInterceptor implements Interceptor {
303+
TestUriInterceptor() {
304+
match(uri: '/bar/**')
305+
match(uri: '/foo')
306+
match(uri: '/foo/bar')
307+
}
308+
}
309+
310+
class TestContextUriInterceptor implements Interceptor {
311+
TestContextUriInterceptor() {
312+
match(uri: '/grails/bar/**')
313+
match(uri: '/grails/foo')
314+
match(uri: '/grails/foo/bar')
315+
}
316+
}

0 commit comments

Comments
 (0)