GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / groovy / org / codehaus / groovy / grails / plugins / web / filters / FilterExecutionTests.groovy
blob4b3a2a6df904c80960a29b13c9d44bc5c678d817
1 /**
2 * @author Graeme Rocher
3 * @since 1.0
4 *
5 * Created: Oct 11, 2007
6 */
7 package org.codehaus.groovy.grails.plugins.web.filters
9 import org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerTests
10 import org.codehaus.groovy.grails.web.util.WebUtils
11 import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
12 import org.springframework.web.servlet.HandlerInterceptor
13 import org.springframework.web.servlet.ModelAndView
15 class FilterExecutionTests extends AbstractGrailsControllerTests {
17 public void onSetUp() {
18 FilterConfig.metaClass.getLog = {->
19 [ error: { msg -> println "ERROR: $msg" },
20 warn: { msg -> println "WARN: $msg" },
21 info: { msg -> println "INFO: $msg" }]
24 gcl.parseClass(
25 '''
26 class ItemController {
27 def count = {
28 render(view:'testView')
31 def show = {
32 render(template:"xmlTemplate",contentType:"text/xml")
35 ''')
37 gcl.parseClass('''\
38 import junit.framework.Assert
40 class AuthorController {
41 def index = {}
42 def list = {}
44 class Filters {
45 // Test property on the filters definition.
46 def myName = "John Doe"
48 // Test method that returns a string.
49 def sum(list) {
50 return list.sum()
53 // Test multi-argument method.
54 def fullName(String firstName, String lastName) {
55 return "$firstName $lastName".toString()
58 def beforeClosure = { ctx ->
59 println "***beforeClosure: $ctx.uri"
60 return true
63 def afterClosure = {
64 println "afterClosure!!!"
67 def afterCompleteClosure = {
68 println "afterCompleteClosure!!!"
71 def filters = {
72 "default"(controller:"*", action:"*") {
73 before = {
74 // Check that the filters property is available. This
75 // tests that FilterConfig's propertyMissing handling
76 // is working.
77 Assert.assertEquals("Filters property 'myName' not available.", "John Doe", myName)
79 // And check the multi-arg method.
80 Assert.assertEquals("Multi-arg method 'fullName' not available.", "Jane Doe", fullName('Jane', 'Doe'))
82 request.beforeOne = "one"
84 after = afterClosure
85 afterView = afterCompleteClosure
88 author(controller:"author") {
89 before = {
90 // Check that the filters method is available. This
91 // tests that FilterConfig's methodMissing handling
92 // is working.
93 Assert.assertEquals("Filters method 'sum' not available.", 10, sum([ 1, 2, 3, 4 ]))
95 request.beforeTwo = "two"
99 list(action:"list") {
100 before = {
101 request.beforeThree = "three"
105 uri(uri:"/*/list") {
106 before = {
107 request.beforeFour = "four"
108 false
112 testRedirect(controller: "admin", action: "index") {
113 before = {
114 request.beforeSix = "number6"
115 redirect(uri: '/')
116 return false
120 testRedirectToController(controller: "admin", action: "save") {
121 after = {
122 redirect(controller: "admin", action: "show")
126 testRender(controller: "person", action: "*") {
127 before = {
128 render(text:"<xml>some xml</xml>",contentType:"text/xml",encoding:"UTF-8")
129 return false
133 testRenderWithViewBefore(controller: "item", action: "count") {
134 before = {
135 render(view: "error", model: [ total: 1000 ])
139 testRenderWithViewAfter(controller: "item", action: "*") {
140 after = {
141 render(view: "happyPath")
145 testRenderString(controller: "account", action: "index") {
146 before = {
147 render "Page not available"
148 return false
152 shouldNotGetHere(controller:"*") {
153 before = {
154 request.beforeFive = "five"
155 false
160 ''')
164 void testFilterMatching() {
165 HandlerInterceptor filterInterceptor = appCtx.getBean("filterInterceptor")
167 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/author/list")
168 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "author")
169 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "list")
171 filterInterceptor.preHandle(request, response, null)
173 // all befores should have been executed
174 assertEquals "one", request.beforeOne
175 assertEquals "two", request.beforeTwo
176 assertEquals "three", request.beforeThree
177 assertEquals "four", request.beforeFour
178 assert !request.beforeFive
180 request.clearAttributes()
182 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/author/show")
183 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "author")
184 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "show")
186 filterInterceptor.preHandle(request, response, null)
188 // only first two should have been
189 assertEquals "one", request.beforeOne
190 assertEquals "two", request.beforeTwo
191 assert !request.beforeThree
192 assert !request.beforeFour
193 assertEquals "five", request.beforeFive
195 request.clearAttributes()
197 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/book/show")
198 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "book")
199 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "show")
201 filterInterceptor.preHandle(request, response, null)
203 // only first two should have been
204 assertEquals "one", request.beforeOne
205 assert !request.beforeTwo
206 assert !request.beforeThree
207 assert !request.beforeFour
208 assertEquals "five", request.beforeFive
210 // Test the plain redirect within a 'before' interceptor.
211 request.clearAttributes()
212 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/admin/index")
213 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "admin")
214 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "index")
216 // Check that 'false' is returned by the interceptor and that
217 // we have have been redirected to the home page.
218 assert !filterInterceptor.preHandle(request, response, null)
219 assertEquals "number6", request.beforeSix
220 assertEquals "/", response.redirectedUrl
222 // Test the redirect to a specific controller and action, within
223 // an 'after' interceptor.
224 response.committed = false
225 response.reset()
226 request.clearAttributes()
227 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/admin/save")
228 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "admin")
229 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "save")
231 // Check that we have been redirected to the expected URL.
232 filterInterceptor.postHandle(request, response, null, null)
233 assertEquals "/admin/show", response.redirectedUrl
235 // Test the rendering of some XML in a 'before' interceptor.
236 response.committed = false
237 response.reset()
238 request.clearAttributes()
239 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/person/show/5")
240 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "person")
241 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "show")
243 // Check that the response contains the expected XML.
244 assert !filterInterceptor.preHandle(request, response, null)
245 assertEquals "<xml>some xml</xml>", response.contentAsString
246 assertTrue response.contentType.startsWith("text/xml")
247 assertEquals "UTF-8", response.characterEncoding
249 // Test the rendering of some plain text in a 'before' interceptor.
250 response.committed = false
251 response.reset()
252 request.clearAttributes()
253 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/account/index")
254 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "account")
255 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "index")
257 // Check that the response contains the expected XML.
258 assert !filterInterceptor.preHandle(request, response, null)
259 assertEquals "Page not available", response.contentAsString
261 // Test the rendering of a view in a 'before' interceptor.
262 response.committed = false
263 response.reset()
264 request.clearAttributes()
265 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/item/count")
266 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "item")
267 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "count")
268 webRequest.controllerName = "item"
270 // Check that the new model and view have been set.
271 def filterConfig = filterInterceptor.handlers.find{ it.filterConfig.name == "testRenderWithViewBefore" }.filterConfig
273 assert !filterInterceptor.preHandle(request, response, null)
274 assertEquals 1000, filterConfig.modelAndView.model['total']
275 assertEquals "/item/error", filterConfig.modelAndView.viewName
277 // Test the rendering of a view in an 'after' interceptor.
278 response.committed = false
279 response.reset()
280 request.clearAttributes()
281 request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/item/show/5")
282 request.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, "item")
283 request.setAttribute(GrailsApplicationAttributes.ACTION_NAME_ATTRIBUTE, "show")
284 webRequest.controllerName = "item"
286 // Check that the new model and view have been set.
287 ModelAndView mv = new ModelAndView()
288 filterInterceptor.postHandle(request, response, null, mv)
289 assertEquals "/item/happyPath", mv.viewName