/
AbstractGrailsMockHttpServletResponse.groovy
140 lines (120 loc) · 4.16 KB
/
AbstractGrailsMockHttpServletResponse.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright 2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.plugins.testing
import grails.artefact.ApiDelegate
import grails.converters.JSON
import groovy.util.slurpersupport.GPathResult
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.codehaus.groovy.grails.plugins.web.api.ResponseMimeTypesApi
import org.codehaus.groovy.grails.web.json.JSONElement
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.util.ReflectionUtils
import org.codehaus.groovy.grails.io.support.IOUtils
/**
* Simple sub-class of Spring's MockHttpServletResponse that adds the
* left-shift operator, "<<".
*/
abstract class AbstractGrailsMockHttpServletResponse extends MockHttpServletResponse {
@ApiDelegate(HttpServletResponse) ResponseMimeTypesApi responseMimeTypesApi = new ResponseMimeTypesApi()
/**
* Sets the response format
*
* @param format The format of the response
*/
void setFormat(String format) {
HttpServletRequest request = GrailsWebRequest.lookup().getCurrentRequest()
request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT, format)
// remove so that is can be repopulated
request.setAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPE, null)
}
/**
* Appends the given content string to the response's output stream.
*/
void leftShift(String content) {
writer << content
}
/**
* Return the primary value for the given header as a String, if any.
* Will return the first value in case of multiple values.
*
* @param name the name of the header
* @return the associated header value, or <code>null<code> if none
*/
String header(String name) {
super.getHeader(name)
}
/**
* Return all values for the given header as a List of Strings.
* @param name the name of the header
* @return the associated header values, or an empty List if none
*/
List<String> headers(String name) {
super.getHeaders(name)
}
/**
* Get the response XML
*
* @return The response XML
*/
GPathResult getXml() {
IOUtils.createXmlSlurper().parseText(contentAsString)
}
/**
* Get the response JSON
*
* @return The JSON response
*/
JSONElement getJson() {
JSON.parse(contentAsString)
}
/**
* The response body as text
*
* @return The text within the response body
*/
String getText() {
contentAsString
}
@Override
void reset() {
final webRequest = GrailsWebRequest.lookup()
webRequest?.currentRequest?.removeAttribute("org.codehaus.groovy.grails.REDIRECT_ISSUED")
setCommitted(false)
def field = ReflectionUtils.findField(MockHttpServletResponse, "writer")
ReflectionUtils.makeAccessible(field)
field.set(this, null)
webRequest.setOut(getWriter())
super.reset()
}
String getRedirectUrl() {
getRedirectedUrl()
}
@Override
String getRedirectedUrl() {
final webRequest = GrailsWebRequest.lookup()
final redirectURI = webRequest?.currentRequest?.getAttribute(GrailsApplicationAttributes.REDIRECT_ISSUED)
if (redirectURI != null) {
return redirectURI
}
if (getStatus() in [301, 302]) {
return super.getHeader("Location")
}
return super.getRedirectedUrl()
}
}