|
| 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 | +} |
0 commit comments