@@ -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