Skip to content

Commit e14077c

Browse files
authored
Merge pull request #61 from gpc/fix/60-render-outside-web-request
Fix rendering outside a web request in non TEST env (#60)
2 parents f9c2bb0 + 1462b37 commit e14077c

7 files changed

Lines changed: 578 additions & 38 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<body>
3+
<p>${request.getAttribute('foo')}</p>
4+
<p>${params.q}</p>
5+
<p>${params.id}</p>
6+
<p>${session.getAttribute('user')}</p>
7+
</body>
8+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<g:hiddenField name="taglib" value="${value}"/>
4+
</body>
5+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package grails.plugins.rendering.document
2+
3+
import grails.util.Environment
4+
import org.grails.web.servlet.WrappedResponseHolder
5+
import org.springframework.web.context.request.RequestContextHolder
6+
import spock.lang.Specification
7+
8+
abstract class ProductionEnvironmentSpec extends Specification {
9+
10+
private String previousEnv
11+
12+
def setup() {
13+
previousEnv = System.getProperty(Environment.KEY)
14+
System.setProperty(Environment.KEY, Environment.PRODUCTION.name)
15+
assert Environment.current == Environment.PRODUCTION
16+
RequestContextHolder.resetRequestAttributes()
17+
WrappedResponseHolder.wrappedResponse = null
18+
}
19+
20+
def cleanup() {
21+
if (previousEnv == null) {
22+
System.clearProperty(Environment.KEY)
23+
} else {
24+
System.setProperty(Environment.KEY, previousEnv)
25+
}
26+
RequestContextHolder.resetRequestAttributes()
27+
WrappedResponseHolder.wrappedResponse = null
28+
}
29+
}
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package grails.plugins.rendering.document
2+
3+
import grails.core.GrailsApplication
4+
import grails.testing.mixin.integration.Integration
5+
import grails.util.GrailsWebMockUtil
6+
import org.grails.web.servlet.WrappedResponseHolder
7+
import org.grails.web.servlet.mvc.GrailsWebRequest
8+
import org.springframework.context.support.StaticApplicationContext
9+
import org.springframework.mock.web.MockHttpServletRequest
10+
import org.springframework.mock.web.MockHttpServletResponse
11+
import org.springframework.web.context.request.RequestAttributes
12+
import org.springframework.web.context.request.RequestContextHolder
13+
import org.springframework.web.context.request.ServletRequestAttributes
14+
import org.springframework.web.servlet.DispatcherServlet
15+
import org.springframework.web.servlet.i18n.FixedLocaleResolver
16+
import org.springframework.web.servlet.support.RequestContextUtils
17+
18+
@Integration
19+
class RenderEnvironmentSpec extends ProductionEnvironmentSpec {
20+
21+
GrailsApplication grailsApplication
22+
23+
def "bind a web request when one does not exist"() {
24+
given:
25+
def out = new StringWriter()
26+
GrailsWebRequest bound = null
27+
28+
when:
29+
RenderEnvironment.with(grailsApplication.mainContext, out) {
30+
bound = GrailsWebRequest.lookup()
31+
}
32+
33+
then:
34+
bound != null
35+
bound.out.is(out)
36+
}
37+
38+
def "leave no request bound afterwards"() {
39+
when:
40+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter()) {}
41+
42+
then:
43+
RequestContextHolder.requestAttributes == null
44+
WrappedResponseHolder.wrappedResponse == null
45+
}
46+
47+
def "reuse the callers request and restore it afterwards"() {
48+
given:
49+
GrailsWebRequest original = GrailsWebMockUtil.bindMockWebRequest(grailsApplication.mainContext)
50+
original.controllerName = 'book'
51+
def callerOut = new StringWriter()
52+
original.out = callerOut
53+
def callerLocaleResolver = new FixedLocaleResolver(Locale.ITALIAN)
54+
original.currentRequest.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, callerLocaleResolver)
55+
def layoutBufferingResponse = new MockHttpServletResponse()
56+
WrappedResponseHolder.wrappedResponse = layoutBufferingResponse
57+
def renderOut = new StringWriter()
58+
GrailsWebRequest bound = null
59+
Writer boundOut = null
60+
String controllerName = null
61+
62+
when:
63+
RenderEnvironment.with(grailsApplication.mainContext, renderOut) { RenderEnvironment env ->
64+
bound = GrailsWebRequest.lookup()
65+
boundOut = bound.out
66+
controllerName = env.controllerName
67+
}
68+
69+
then:
70+
bound.is(original)
71+
boundOut.is(renderOut)
72+
controllerName == 'book'
73+
RequestContextHolder.requestAttributes.is(original)
74+
original.out.is(callerOut)
75+
original.currentRequest.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE).is(callerLocaleResolver)
76+
WrappedResponseHolder.wrappedResponse.is(layoutBufferingResponse)
77+
}
78+
79+
def "restore state when the block throws"() {
80+
given:
81+
def callerOut = new StringWriter()
82+
GrailsWebRequest original = GrailsWebMockUtil.bindMockWebRequest(grailsApplication.mainContext)
83+
original.out = callerOut
84+
85+
when:
86+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter()) {
87+
throw new IllegalStateException('boom')
88+
}
89+
90+
then:
91+
thrown(IllegalStateException)
92+
RequestContextHolder.requestAttributes.is(original)
93+
original.out.is(callerOut)
94+
}
95+
96+
def "restore the outer renders writer after a nested render"() {
97+
given:
98+
def outerOut = new StringWriter()
99+
Writer afterInner = null
100+
101+
when:
102+
RenderEnvironment.with(grailsApplication.mainContext, outerOut) {
103+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter()) {}
104+
afterInner = GrailsWebRequest.lookup().out
105+
}
106+
107+
then:
108+
afterInner.is(outerOut)
109+
RequestContextHolder.requestAttributes == null
110+
}
111+
112+
def "render locale is #expected when explicit=#explicit and request=#requestLocale"() {
113+
given:
114+
if (requestLocale) {
115+
GrailsWebRequest original = GrailsWebMockUtil.bindMockWebRequest(grailsApplication.mainContext)
116+
(original.currentRequest as MockHttpServletRequest).addPreferredLocale(requestLocale)
117+
}
118+
Locale seen = null
119+
120+
when:
121+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter(), explicit) {
122+
seen = RequestContextUtils.getLocale(GrailsWebRequest.lookup().currentRequest)
123+
}
124+
125+
then:
126+
seen == expected
127+
128+
where:
129+
explicit | requestLocale | expected
130+
Locale.FRENCH | Locale.GERMAN | Locale.FRENCH
131+
null | Locale.GERMAN | Locale.GERMAN
132+
null | null | Locale.default
133+
}
134+
135+
def "expose the application context path outside of a web request"() {
136+
given:
137+
String contextPath = null
138+
139+
when:
140+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter(), {
141+
contextPath = GrailsWebRequest.lookup().contextPath
142+
})
143+
144+
then:
145+
contextPath == '/rendering'
146+
}
147+
148+
def "inherit the locale of a plain servlet request"() {
149+
given:
150+
def request = new MockHttpServletRequest()
151+
request.addPreferredLocale(Locale.JAPANESE)
152+
def plain = new ServletRequestAttributes(request)
153+
RequestContextHolder.requestAttributes = plain
154+
Locale seen = null
155+
156+
when:
157+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter(), {
158+
seen = RequestContextUtils.getLocale(GrailsWebRequest.lookup().currentRequest)
159+
})
160+
161+
then:
162+
seen == Locale.JAPANESE
163+
RequestContextHolder.requestAttributes.is(plain)
164+
}
165+
166+
def "run request destruction callbacks registered during the render"() {
167+
given:
168+
boolean destroyed = false
169+
170+
when:
171+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter(), {
172+
GrailsWebRequest.lookup().registerDestructionCallback('probe', { destroyed = true } as Runnable,
173+
RequestAttributes.SCOPE_REQUEST)
174+
})
175+
176+
then:
177+
destroyed
178+
}
179+
180+
def "leave the caller's request destruction callbacks alone"() {
181+
given:
182+
GrailsWebRequest original = GrailsWebMockUtil.bindMockWebRequest(grailsApplication.mainContext)
183+
boolean destroyed = false
184+
original.registerDestructionCallback('probe', { destroyed = true } as Runnable, RequestAttributes.SCOPE_REQUEST)
185+
186+
when:
187+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter()) {}
188+
189+
then:
190+
!destroyed
191+
}
192+
193+
def "restore state when a destruction callback throws"() {
194+
when:
195+
RenderEnvironment.with(grailsApplication.mainContext, new StringWriter()) {
196+
GrailsWebRequest.lookup().registerDestructionCallback('probe', { throw new IllegalStateException('boom') } as Runnable, RequestAttributes.SCOPE_REQUEST)
197+
}
198+
199+
then:
200+
thrown(IllegalStateException)
201+
RequestContextHolder.requestAttributes == null
202+
WrappedResponseHolder.wrappedResponse == null
203+
}
204+
205+
def "bind a web request for a non web application context"() {
206+
given:
207+
def out = new StringWriter()
208+
GrailsWebRequest bound = null
209+
210+
when:
211+
RenderEnvironment.with(new StaticApplicationContext(), out) {
212+
bound = GrailsWebRequest.lookup()
213+
}
214+
215+
then:
216+
bound.out.is(out)
217+
RequestContextHolder.requestAttributes == null
218+
}
219+
}

‎example/src/integration-test/groovy/grails/plugins/rendering/document/XhtmlDocumentServiceSpec.groovy‎

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,90 @@
1515
*/
1616
package grails.plugins.rendering.document
1717

18+
import grails.core.GrailsApplication
1819
import grails.testing.mixin.integration.Integration
19-
import spock.lang.Specification
20+
import grails.util.GrailsWebMockUtil
21+
import org.grails.web.servlet.mvc.GrailsWebRequest
22+
import org.springframework.mock.web.MockHttpServletRequest
23+
import org.springframework.web.context.request.RequestContextHolder
24+
import org.w3c.dom.Document
2025

21-
/**
22-
* The xhtmlDocumentService is exercised in the rendering
23-
* service tests. This is here for testing future functionality.
24-
*/
2526
@Integration
26-
class XhtmlDocumentServiceSpec extends Specification {
27+
class XhtmlDocumentServiceSpec extends ProductionEnvironmentSpec {
28+
29+
XhtmlDocumentService xhtmlDocumentService
30+
GrailsApplication grailsApplication
31+
32+
def "render a taglib template outside web request"() {
33+
when:
34+
Document document = xhtmlDocumentService.createDocument(template: '/taglib', model: [value: 'from a job'])
35+
36+
then:
37+
hiddenFieldValue(document) == 'from a job'
38+
RequestContextHolder.requestAttributes == null
39+
}
40+
41+
def "render taglib template on a background thread"() {
42+
given:
43+
Document document = null
44+
Throwable error = null
45+
46+
when:
47+
def thread = Thread.start {
48+
try {
49+
document = xhtmlDocumentService.createDocument(template: '/taglib', model: [value: 'from a thread'])
50+
} catch (Throwable t) {
51+
error = t
52+
}
53+
}
54+
thread.join(30_000)
55+
56+
then:
57+
!thread.alive
58+
error == null
59+
hiddenFieldValue(document) == 'from a thread'
60+
}
2761

28-
def xhtmlDocumentService
62+
def "does not write into current request's output"() {
63+
given:
64+
GrailsWebRequest original = GrailsWebMockUtil.bindMockWebRequest(grailsApplication.mainContext)
65+
def requestOut = new StringWriter()
66+
original.out = requestOut
67+
68+
when:
69+
Document document = xhtmlDocumentService.createDocument(template: '/taglib', model: [value: 'in a request'])
70+
71+
then:
72+
hiddenFieldValue(document) == 'in a request'
73+
original.out.is(requestOut)
74+
requestOut.toString() == ''
75+
RequestContextHolder.requestAttributes.is(original)
76+
}
77+
78+
def "renders template that reads caller's request state"() {
79+
given:
80+
GrailsWebRequest original = GrailsWebMockUtil.bindMockWebRequest(grailsApplication.mainContext)
81+
MockHttpServletRequest request = original.currentRequest as MockHttpServletRequest
82+
request.setAttribute('foo', 'bar')
83+
request.addParameter('q', 'query')
84+
request.session.setAttribute('user', 'alice')
85+
original.params.id = '42'
86+
87+
when:
88+
Document document = xhtmlDocumentService.createDocument(template: '/requestState')
89+
90+
then:
91+
paragraphs(document) == ['bar', 'query', '42', 'alice']
92+
original.currentResponse.contentType == null
93+
RequestContextHolder.requestAttributes.is(original)
94+
}
95+
96+
private static String hiddenFieldValue(Document document) {
97+
document.getElementsByTagName('input').item(0).getAttribute('value')
98+
}
2999

30-
def emptyScenario() {
100+
private static List<String> paragraphs(Document document) {
101+
def nodes = document.getElementsByTagName('p')
102+
(0..<nodes.length).collect { nodes.item(it).textContent }
31103
}
32104
}

0 commit comments

Comments
 (0)