@@ -5,7 +5,9 @@ test('has correct defaults', () => {
5
5
const response = new Response ( ) ;
6
6
expect ( response [ 'body' ] ) . toBeNull ( ) ;
7
7
expect ( response [ 'statusCode' ] ) . toBe ( 200 ) ;
8
- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
8
+ expect ( response [ 'headers' ] ) . toEqual ( {
9
+ 'Set-Cookie' : [ ] ,
10
+ } ) ;
9
11
} ) ;
10
12
11
13
test ( 'sets status code, body and headers from constructor' , ( ) => {
@@ -24,6 +26,7 @@ test('sets status code, body and headers from constructor', () => {
24
26
'Access-Control-Allow-Origin' : 'example.com' ,
25
27
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE' ,
26
28
'Access-Control-Allow-Headers' : 'Content-Type' ,
29
+ 'Set-Cookie' : [ ] ,
27
30
} ) ;
28
31
} ) ;
29
32
@@ -45,7 +48,9 @@ test('sets body correctly', () => {
45
48
46
49
test ( 'sets headers correctly' , ( ) => {
47
50
const response = new Response ( ) ;
48
- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
51
+ expect ( response [ 'headers' ] ) . toEqual ( {
52
+ 'Set-Cookie' : [ ] ,
53
+ } ) ;
49
54
response . setHeaders ( {
50
55
'Access-Control-Allow-Origin' : 'example.com' ,
51
56
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE' ,
@@ -55,35 +60,152 @@ test('sets headers correctly', () => {
55
60
'Access-Control-Allow-Origin' : 'example.com' ,
56
61
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE' ,
57
62
'Access-Control-Allow-Headers' : 'Content-Type' ,
63
+ 'Set-Cookie' : [ ] ,
58
64
} ;
59
65
expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
60
66
// @ts -ignore
61
67
response . setHeaders ( undefined ) ;
62
68
expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
63
69
} ) ;
64
70
71
+ test ( 'sets headers with string cookies' , ( ) => {
72
+ const response = new Response ( ) ;
73
+ expect ( response [ 'headers' ] ) . toEqual ( {
74
+ 'Set-Cookie' : [ ] ,
75
+ } ) ;
76
+ response . setHeaders ( {
77
+ 'Access-Control-Allow-Origin' : 'example.com' ,
78
+ 'Set-Cookie' : 'Hi=Bye' ,
79
+ } ) ;
80
+ const expected = {
81
+ 'Access-Control-Allow-Origin' : 'example.com' ,
82
+ 'Set-Cookie' : [ 'Hi=Bye' ] ,
83
+ } ;
84
+ expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
85
+ } ) ;
86
+
87
+ test ( 'sets headers with an array of cookies' , ( ) => {
88
+ const response = new Response ( ) ;
89
+ expect ( response [ 'headers' ] ) . toEqual ( {
90
+ 'Set-Cookie' : [ ] ,
91
+ } ) ;
92
+ response . setHeaders ( {
93
+ 'Access-Control-Allow-Origin' : 'example.com' ,
94
+ 'Set-Cookie' : [ 'Hi=Bye' , 'Hello=World' ] ,
95
+ } ) ;
96
+ const expected = {
97
+ 'Access-Control-Allow-Origin' : 'example.com' ,
98
+ 'Set-Cookie' : [ 'Hi=Bye' , 'Hello=World' ] ,
99
+ } ;
100
+ expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
101
+ } ) ;
102
+
103
+ test ( 'sets cookies with lower case set-cookie' , ( ) => {
104
+ const response = new Response ( ) ;
105
+ expect ( response [ 'headers' ] ) . toEqual ( {
106
+ 'Set-Cookie' : [ ] ,
107
+ } ) ;
108
+ response . setHeaders ( {
109
+ 'Access-Control-Allow-Origin' : 'example.com' ,
110
+ 'set-cookie' : [ 'Hi=Bye' , 'Hello=World' ] ,
111
+ } ) ;
112
+ const expected = {
113
+ 'Access-Control-Allow-Origin' : 'example.com' ,
114
+ 'Set-Cookie' : [ 'Hi=Bye' , 'Hello=World' ] ,
115
+ } ;
116
+ expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
117
+ } ) ;
118
+
65
119
test ( 'appends a new header correctly' , ( ) => {
66
120
const response = new Response ( ) ;
67
- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
121
+ expect ( response [ 'headers' ] ) . toEqual ( {
122
+ 'Set-Cookie' : [ ] ,
123
+ } ) ;
68
124
response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
69
125
expect ( response [ 'headers' ] ) . toEqual ( {
70
126
'Access-Control-Allow-Origin' : 'dkundel.com' ,
127
+ 'Set-Cookie' : [ ] ,
71
128
} ) ;
72
129
response . appendHeader ( 'Content-Type' , 'application/json' ) ;
73
130
expect ( response [ 'headers' ] ) . toEqual ( {
74
131
'Access-Control-Allow-Origin' : 'dkundel.com' ,
75
132
'Content-Type' : 'application/json' ,
133
+ 'Set-Cookie' : [ ] ,
76
134
} ) ;
77
135
} ) ;
78
136
79
137
test ( 'appends a header correctly with no existing one' , ( ) => {
80
138
const response = new Response ( ) ;
81
- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
139
+ expect ( response [ 'headers' ] ) . toEqual ( {
140
+ 'Set-Cookie' : [ ] ,
141
+ } ) ;
82
142
// @ts -ignore
83
143
response [ 'headers' ] = undefined ;
84
144
response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
85
145
expect ( response [ 'headers' ] ) . toEqual ( {
86
146
'Access-Control-Allow-Origin' : 'dkundel.com' ,
147
+ 'Set-Cookie' : [ ] ,
148
+ } ) ;
149
+ } ) ;
150
+
151
+ test ( 'appends multi value headers' , ( ) => {
152
+ const response = new Response ( ) ;
153
+ expect ( response [ 'headers' ] ) . toEqual ( {
154
+ 'Set-Cookie' : [ ] ,
155
+ } ) ;
156
+ response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
157
+ response . appendHeader ( 'Access-Control-Allow-Origin' , 'philna.sh' ) ;
158
+ response . appendHeader ( 'Access-Control-Allow-Methods' , 'GET' ) ;
159
+ response . appendHeader ( 'Access-Control-Allow-Methods' , 'DELETE' ) ;
160
+ response . appendHeader ( 'Access-Control-Allow-Methods' , [ 'PUT' , 'POST' ] ) ;
161
+ expect ( response [ 'headers' ] ) . toEqual ( {
162
+ 'Access-Control-Allow-Origin' : [ 'dkundel.com' , 'philna.sh' ] ,
163
+ 'Access-Control-Allow-Methods' : [ 'GET' , 'DELETE' , 'PUT' , 'POST' ] ,
164
+ 'Set-Cookie' : [ ] ,
165
+ } ) ;
166
+ } ) ;
167
+
168
+ test ( 'sets a single cookie correctly' , ( ) => {
169
+ const response = new Response ( ) ;
170
+ expect ( response [ 'headers' ] ) . toEqual ( {
171
+ 'Set-Cookie' : [ ] ,
172
+ } ) ;
173
+ response . setCookie ( 'name' , 'value' ) ;
174
+ expect ( response [ 'headers' ] ) . toEqual ( {
175
+ 'Set-Cookie' : [ 'name=value' ] ,
176
+ } ) ;
177
+ } ) ;
178
+
179
+ test ( 'sets a cookie with attributes' , ( ) => {
180
+ const response = new Response ( ) ;
181
+ expect ( response [ 'headers' ] ) . toEqual ( {
182
+ 'Set-Cookie' : [ ] ,
183
+ } ) ;
184
+ response . setCookie ( 'Hello' , 'World' , [
185
+ 'HttpOnly' ,
186
+ 'Secure' ,
187
+ 'SameSite=Strict' ,
188
+ 'Max-Age=86400' ,
189
+ ] ) ;
190
+ expect ( response [ 'headers' ] ) . toEqual ( {
191
+ 'Set-Cookie' : [ 'Hello=World;HttpOnly;Secure;SameSite=Strict;Max-Age=86400' ] ,
192
+ } ) ;
193
+ } ) ;
194
+
195
+ test ( 'removes a cookie' , ( ) => {
196
+ const response = new Response ( ) ;
197
+ expect ( response [ 'headers' ] ) . toEqual ( {
198
+ 'Set-Cookie' : [ ] ,
199
+ } ) ;
200
+ response . setCookie ( 'Hello' , 'World' , [
201
+ 'HttpOnly' ,
202
+ 'Secure' ,
203
+ 'SameSite=Strict' ,
204
+ 'Max-Age=86400' ,
205
+ ] ) ;
206
+ response . removeCookie ( 'Hello' ) ;
207
+ expect ( response [ 'headers' ] ) . toEqual ( {
208
+ 'Set-Cookie' : [ 'Hello=;Max-Age=0' ] ,
87
209
} ) ;
88
210
} ) ;
89
211
@@ -107,6 +229,16 @@ test('appendHeader returns the response', () => {
107
229
expect ( response . appendHeader ( 'X-Test' , 'Hello' ) ) . toBe ( response ) ;
108
230
} ) ;
109
231
232
+ test ( 'setCookie returns the response' , ( ) => {
233
+ const response = new Response ( ) ;
234
+ expect ( response . setCookie ( 'name' , 'value' ) ) . toBe ( response ) ;
235
+ } ) ;
236
+
237
+ test ( 'removeCookie returns the response' , ( ) => {
238
+ const response = new Response ( ) ;
239
+ expect ( response . removeCookie ( 'name' ) ) . toBe ( response ) ;
240
+ } ) ;
241
+
110
242
test ( 'calls express response correctly' , ( ) => {
111
243
const mockRes = {
112
244
status : jest . fn ( ) ,
@@ -121,7 +253,10 @@ test('calls express response correctly', () => {
121
253
122
254
expect ( mockRes . send ) . toHaveBeenCalledWith ( `I'm a teapot!` ) ;
123
255
expect ( mockRes . status ) . toHaveBeenCalledWith ( 418 ) ;
124
- expect ( mockRes . set ) . toHaveBeenCalledWith ( { 'Content-Type' : 'text/plain' } ) ;
256
+ expect ( mockRes . set ) . toHaveBeenCalledWith ( {
257
+ 'Content-Type' : 'text/plain' ,
258
+ 'Set-Cookie' : [ ] ,
259
+ } ) ;
125
260
} ) ;
126
261
127
262
test ( 'serializes a response' , ( ) => {
@@ -134,7 +269,10 @@ test('serializes a response', () => {
134
269
135
270
expect ( serialized . body ) . toEqual ( "I'm a teapot!" ) ;
136
271
expect ( serialized . statusCode ) . toEqual ( 418 ) ;
137
- expect ( serialized . headers ) . toEqual ( { 'Content-Type' : 'text/plain' } ) ;
272
+ expect ( serialized . headers ) . toEqual ( {
273
+ 'Content-Type' : 'text/plain' ,
274
+ 'Set-Cookie' : [ ] ,
275
+ } ) ;
138
276
} ) ;
139
277
140
278
test ( 'serializes a response with content type set to application/json' , ( ) => {
@@ -149,5 +287,8 @@ test('serializes a response with content type set to application/json', () => {
149
287
JSON . stringify ( { url : 'https://dkundel.com' } )
150
288
) ;
151
289
expect ( serialized . statusCode ) . toEqual ( 200 ) ;
152
- expect ( serialized . headers ) . toEqual ( { 'Content-Type' : 'application/json' } ) ;
290
+ expect ( serialized . headers ) . toEqual ( {
291
+ 'Content-Type' : 'application/json' ,
292
+ 'Set-Cookie' : [ ] ,
293
+ } ) ;
153
294
} ) ;
0 commit comments