2 * @author Graeme Rocher
5 * Created: Nov 26, 2007
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
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
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
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
79 def mockHttpRequest
= new MockHttpServletRequest()
80 def mockWebRequest
= new GrailsWebRequest(
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(
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
)