-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCookieResponseSpec.groovy
189 lines (177 loc) · 8.24 KB
/
CookieResponseSpec.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package grails.plugin.cookie
import org.grails.web.servlet.mvc.GrailsWebRequest
import org.grails.web.util.WebUtils
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.mock.web.MockServletContext
import spock.lang.Specification
import spock.lang.Unroll
import javax.servlet.http.Cookie
import javax.servlet.http.HttpServletResponse
abstract class CookieResponseSpec extends Specification {
protected HttpServletResponse response = new MockHttpServletResponse()
protected obj
private MockHttpServletRequest request = new MockHttpServletRequest()
def setup() {
request.contextPath = '/ctx'
def mockWebRequest = new GrailsWebRequest(request, response, new MockServletContext())
WebUtils.storeGrailsWebRequest(mockWebRequest)
}
def cleanup() {
WebUtils.clearGrailsWebRequest()
}
@Unroll
void "setCookie() with args as list: #maxAge #path #domain #secure #httpOnly"() {
given:
def cookieThatWasSet = obj.setCookie(args)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == 'cookie_val'
cookieThatWasSet.maxAge == maxAge
cookieThatWasSet.path == path
cookieThatWasSet.domain == domain
cookieThatWasSet.secure == secure
cookieThatWasSet.httpOnly == httpOnly
cookieThatWasSet.version == 1
where:
args | maxAge | path | domain | secure | httpOnly
['cookie_name', 'cookie_val'] | 2592000 | '/ctx' | null | false | true
['cookie_name', 'cookie_val', 42] | 42 | '/ctx' | null | false | true
['cookie_name', 'cookie_val', 42, '/path'] | 42 | '/path' | null | false | true
['cookie_name', 'cookie_val', 42, '/path', '.example.com', true, false] | 42 | '/path' | '.example.com' | true | false
}
@Unroll
void "setCookie(): #maxAge #path #domain #secure #httpOnly"() {
given:
def cookieThatWasSet = obj.setCookie('cookie_name', 'cookie_val', maxAge, path, domain, secure, httpOnly)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == 'cookie_val'
cookieThatWasSet.maxAge == expectedMaxAge
cookieThatWasSet.path == expectedPath
cookieThatWasSet.domain == domain
cookieThatWasSet.secure == secure
cookieThatWasSet.httpOnly == httpOnly
cookieThatWasSet.version == 1
where:
maxAge | expectedMaxAge | path | expectedPath | domain | secure | httpOnly
null | 2592000 | '/ctx' | '/ctx' | null | false | false
42 | 42 | '/ctx' | '/ctx' | null | false | false
42 | 42 | '/path' | '/path' | null | false | false
42 | 42 | '/path' | '/path' | '.example.com' | true | true
}
@Unroll
void "setCookie() named params: #maxAge #path #domain #secure #httpOnly"() {
given:
def cookieThatWasSet = obj.setCookie(args)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == 'cookie_val'
cookieThatWasSet.maxAge == maxAge
cookieThatWasSet.path == path
cookieThatWasSet.domain == domain
cookieThatWasSet.secure == secure
cookieThatWasSet.httpOnly == httpOnly
cookieThatWasSet.version == 1
where:
args | maxAge | path | domain | secure | httpOnly
[name: 'cookie_name', value: 'cookie_val'] | 2592000 | '/ctx' | null | false | true
[name: 'cookie_name', value: 'cookie_val', maxAge: 42] | 42 | '/ctx' | null | false | true
[name: 'cookie_name', value: 'cookie_val', maxAge: 42, path: '/path'] | 42 | '/path' | null | false | true
[name: 'cookie_name', value: 'cookie_val', maxAge: 42, path: '/path', domain: '.example.com', secure: true, httpOnly: false] | 42 | '/path' | '.example.com' | true | false
}
@Unroll
void "setCookie(Cookie) doesn't set defaults: #maxAge #path #domain #secure #httpOnly"() {
given:
Cookie cookie = new Cookie('cookie_name', 'some_val')
cookie.path = path
cookie.maxAge = maxAge
cookie.path = path
if (domain) {
cookie.domain = domain
}
cookie.secure = secure
cookie.httpOnly = httpOnly
cookie.version = 0
def cookieThatWasSet = obj.setCookie(cookie)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == 'some_val'
cookieThatWasSet.maxAge == maxAge
cookieThatWasSet.path == path
cookieThatWasSet.domain == domain
cookieThatWasSet.secure == secure
cookieThatWasSet.httpOnly == httpOnly
cookieThatWasSet.version == 0
where:
maxAge | path | domain | secure | httpOnly
-1 | null | null | false | false
42 | '/' | null | false | false
42 | '/path' | null | false | false
42 | '/path' | '.example.com' | true | true
}
@Unroll
def "deleteCookie() with args as list, sets new cookie with same name but expired age: #path #domain"() {
given:
def cookieThatWasSet = obj.deleteCookie(args)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == null
cookieThatWasSet.path == path
cookieThatWasSet.domain == domain
cookieThatWasSet.maxAge == 0
cookieThatWasSet.version == 1
where:
args | path | domain
['cookie_name'] | '/ctx' | null
['cookie_name', '/path'] | '/path' | null
['cookie_name', '/path', '.example.com'] | '/path' | '.example.com'
}
@Unroll
def "deleteCookie() sets new cookie with same name but expired age: #path #domain"() {
given:
def cookieThatWasSet = obj.deleteCookie('cookie_name', path, domain)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == null
cookieThatWasSet.path == pathExpected
cookieThatWasSet.domain == domain
cookieThatWasSet.maxAge == 0
cookieThatWasSet.version == 1
where:
path | pathExpected | domain
null | '/ctx' | null
'/path' | '/path' | null
'/path' | '/path' | '.example.com'
}
@Unroll
def "deleteCookie(Cookie) sets new cookie with same name but expired age: #path #pathExpected #domain"() {
given:
Cookie cookieToDelete = new Cookie('cookie_name', 'some_val')
cookieToDelete.path = path
if (domain) {
cookieToDelete.domain = domain
}
def cookieThatWasSet = obj.deleteCookie(cookieToDelete)
expect:
cookieThatWasSet == response.cookies[0]
cookieThatWasSet.name == 'cookie_name'
cookieThatWasSet.value == null
cookieThatWasSet.path == pathExpected
cookieThatWasSet.domain == domain
cookieThatWasSet.maxAge == 0
cookieThatWasSet.version == 1
where:
path | pathExpected | domain
null | '/ctx' | null
'/' | '/' | null
'/path' | '/path' | null
'/path' | '/path' | '.example.com'
}
}