GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / groovy / org / codehaus / groovy / grails / web / util / WebUtilsTests.groovy
blob7be940a094974fcd4f7100424f59842c6ba373c0
1 /**
2 * @author Graeme Rocher
3 * @since 1.0
4 *
5 * Created: Nov 26, 2007
6 */
7 package org.codehaus.groovy.grails.web.util
9 import org.codehaus.groovy.grails.commons.ConfigurationHolder
10 import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
11 import org.springframework.mock.web.MockHttpServletRequest
12 import org.springframework.mock.web.MockHttpServletResponse
13 import org.springframework.mock.web.MockServletContext
14 import org.springframework.web.context.request.RequestContextHolder
15 import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
17 class WebUtilsTests extends GroovyTestCase {
19 protected void setUp() {
20 def config = new ConfigSlurper().parse( """
21 grails.mime.file.extensions=false
22 """)
24 ConfigurationHolder.setConfig config
28 protected void tearDown() {
29 ConfigurationHolder.setConfig null
30 RequestContextHolder.setRequestAttributes null
34 void testAreFileExtensionsEnabled() {
35 assert !WebUtils.areFileExtensionsEnabled()
37 def config = new ConfigSlurper().parse( """
38 grails.mime.file.extensions=true
39 """)
40 ConfigurationHolder.config = config
42 assert WebUtils.areFileExtensionsEnabled()
45 void testGetFormatFromURI() {
46 assertNull WebUtils.getFormatFromURI("/foo/bar/")
47 assertNull WebUtils.getFormatFromURI("/foo/bar.suff/bar")
48 assertEquals "xml", WebUtils.getFormatFromURI("/foo/bar.xml")
49 assertEquals "xml", WebUtils.getFormatFromURI("/foo.xml")
50 assertEquals "xml", WebUtils.getFormatFromURI("/foo/bar.suff/bar.xml")
53 void testRetrieveGrailsWebRequest() {
54 // Validate the initial conditions.
55 assertNull RequestContextHolder.getRequestAttributes()
57 // An exception should be thrown if no web request is attached
58 // to the thread.
59 shouldFail(IllegalStateException) {
60 WebUtils.retrieveGrailsWebRequest()
63 // Now check that the method returns the web request stored in
64 // RequestContextHolder.
65 def mockWebRequest = new GrailsWebRequest(
66 new MockHttpServletRequest(),
67 new MockHttpServletResponse(),
68 new MockServletContext())
69 RequestContextHolder.setRequestAttributes(mockWebRequest)
70 assertEquals mockWebRequest, WebUtils.retrieveGrailsWebRequest()
73 void testStoreGrailsWebRequest() {
74 // Validate the initial conditions.
75 assertNull RequestContextHolder.getRequestAttributes()
77 // Create a mock web request and pass it to the method under
78 // test.
79 def mockHttpRequest = new MockHttpServletRequest()
80 def mockWebRequest = new GrailsWebRequest(
81 mockHttpRequest,
82 new MockHttpServletResponse(),
83 new MockServletContext())
85 WebUtils.storeGrailsWebRequest(mockWebRequest)
87 // Check that both the RequestContextHolder and the HTTP request
88 // attribute have been initialised.
89 assertEquals mockWebRequest, RequestContextHolder.getRequestAttributes()
90 assertEquals mockWebRequest, mockHttpRequest.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
93 void clearGrailsWebRequest() {
94 // Create a mock web request and store it on the thread.
95 def mockHttpRequest = new MockHttpServletRequest()
96 def mockWebRequest = new GrailsWebRequest(
97 mockHttpRequest,
98 new MockHttpServletResponse(),
99 new MockServletContext())
101 WebUtils.storeGrailsWebRequest(mockWebRequest)
103 // Now call the test method and check that the web request has
104 // been removed from the thread.
105 WebUtils.clearGrailsWebRequest()
106 assertNull RequestContextHolder.getRequestAttributes()
107 assertNull mockHttpRequest.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
109 // Finally check that we can call the method again without any
110 // undesirable side-effects, like an exception.
111 WebUtils.clearGrailsWebRequest()
112 assertNull RequestContextHolder.getRequestAttributes()
113 assertNull mockHttpRequest.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)