@@ -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,136 @@ 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
+
65
103
test ( 'appends a new header correctly' , ( ) => {
66
104
const response = new Response ( ) ;
67
- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
105
+ expect ( response [ 'headers' ] ) . toEqual ( {
106
+ 'Set-Cookie' : [ ] ,
107
+ } ) ;
68
108
response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
69
109
expect ( response [ 'headers' ] ) . toEqual ( {
70
110
'Access-Control-Allow-Origin' : 'dkundel.com' ,
111
+ 'Set-Cookie' : [ ] ,
71
112
} ) ;
72
113
response . appendHeader ( 'Content-Type' , 'application/json' ) ;
73
114
expect ( response [ 'headers' ] ) . toEqual ( {
74
115
'Access-Control-Allow-Origin' : 'dkundel.com' ,
75
116
'Content-Type' : 'application/json' ,
117
+ 'Set-Cookie' : [ ] ,
76
118
} ) ;
77
119
} ) ;
78
120
79
121
test ( 'appends a header correctly with no existing one' , ( ) => {
80
122
const response = new Response ( ) ;
81
- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
123
+ expect ( response [ 'headers' ] ) . toEqual ( {
124
+ 'Set-Cookie' : [ ] ,
125
+ } ) ;
82
126
// @ts -ignore
83
127
response [ 'headers' ] = undefined ;
84
128
response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
85
129
expect ( response [ 'headers' ] ) . toEqual ( {
86
130
'Access-Control-Allow-Origin' : 'dkundel.com' ,
131
+ 'Set-Cookie' : [ ] ,
132
+ } ) ;
133
+ } ) ;
134
+
135
+ test ( 'appends multi value headers' , ( ) => {
136
+ const response = new Response ( ) ;
137
+ expect ( response [ 'headers' ] ) . toEqual ( {
138
+ 'Set-Cookie' : [ ] ,
139
+ } ) ;
140
+ response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
141
+ response . appendHeader ( 'Access-Control-Allow-Origin' , 'philna.sh' ) ;
142
+ response . appendHeader ( 'Access-Control-Allow-Methods' , 'GET' ) ;
143
+ response . appendHeader ( 'Access-Control-Allow-Methods' , 'DELETE' ) ;
144
+ response . appendHeader ( 'Access-Control-Allow-Methods' , [ 'PUT' , 'POST' ] ) ;
145
+ expect ( response [ 'headers' ] ) . toEqual ( {
146
+ 'Access-Control-Allow-Origin' : [ 'dkundel.com' , 'philna.sh' ] ,
147
+ 'Access-Control-Allow-Methods' : [ 'GET' , 'DELETE' , 'PUT' , 'POST' ] ,
148
+ 'Set-Cookie' : [ ] ,
149
+ } ) ;
150
+ } ) ;
151
+
152
+ test ( 'sets a single cookie correctly' , ( ) => {
153
+ const response = new Response ( ) ;
154
+ expect ( response [ 'headers' ] ) . toEqual ( {
155
+ 'Set-Cookie' : [ ] ,
156
+ } ) ;
157
+ response . setCookie ( 'name' , 'value' ) ;
158
+ expect ( response [ 'headers' ] ) . toEqual ( {
159
+ 'Set-Cookie' : [ 'name=value' ] ,
160
+ } ) ;
161
+ } ) ;
162
+
163
+ test ( 'sets a cookie with attributes' , ( ) => {
164
+ const response = new Response ( ) ;
165
+ expect ( response [ 'headers' ] ) . toEqual ( {
166
+ 'Set-Cookie' : [ ] ,
167
+ } ) ;
168
+ response . setCookie ( 'Hello' , 'World' , [
169
+ 'HttpOnly' ,
170
+ 'Secure' ,
171
+ 'SameSite=Strict' ,
172
+ 'Max-Age=86400' ,
173
+ ] ) ;
174
+ expect ( response [ 'headers' ] ) . toEqual ( {
175
+ 'Set-Cookie' : [ 'Hello=World;HttpOnly;Secure;SameSite=Strict;Max-Age=86400' ] ,
176
+ } ) ;
177
+ } ) ;
178
+
179
+ test ( 'removes a cookie' , ( ) => {
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
+ response . removeCookie ( 'Hello' ) ;
191
+ expect ( response [ 'headers' ] ) . toEqual ( {
192
+ 'Set-Cookie' : [ 'Hello=;Max-Age=0' ] ,
87
193
} ) ;
88
194
} ) ;
89
195
@@ -107,6 +213,16 @@ test('appendHeader returns the response', () => {
107
213
expect ( response . appendHeader ( 'X-Test' , 'Hello' ) ) . toBe ( response ) ;
108
214
} ) ;
109
215
216
+ test ( 'setCookie returns the response' , ( ) => {
217
+ const response = new Response ( ) ;
218
+ expect ( response . setCookie ( 'name' , 'value' ) ) . toBe ( response ) ;
219
+ } ) ;
220
+
221
+ test ( 'removeCookie returns the response' , ( ) => {
222
+ const response = new Response ( ) ;
223
+ expect ( response . removeCookie ( 'name' ) ) . toBe ( response ) ;
224
+ } ) ;
225
+
110
226
test ( 'calls express response correctly' , ( ) => {
111
227
const mockRes = {
112
228
status : jest . fn ( ) ,
@@ -121,7 +237,10 @@ test('calls express response correctly', () => {
121
237
122
238
expect ( mockRes . send ) . toHaveBeenCalledWith ( `I'm a teapot!` ) ;
123
239
expect ( mockRes . status ) . toHaveBeenCalledWith ( 418 ) ;
124
- expect ( mockRes . set ) . toHaveBeenCalledWith ( { 'Content-Type' : 'text/plain' } ) ;
240
+ expect ( mockRes . set ) . toHaveBeenCalledWith ( {
241
+ 'Content-Type' : 'text/plain' ,
242
+ 'Set-Cookie' : [ ] ,
243
+ } ) ;
125
244
} ) ;
126
245
127
246
test ( 'serializes a response' , ( ) => {
@@ -134,7 +253,10 @@ test('serializes a response', () => {
134
253
135
254
expect ( serialized . body ) . toEqual ( "I'm a teapot!" ) ;
136
255
expect ( serialized . statusCode ) . toEqual ( 418 ) ;
137
- expect ( serialized . headers ) . toEqual ( { 'Content-Type' : 'text/plain' } ) ;
256
+ expect ( serialized . headers ) . toEqual ( {
257
+ 'Content-Type' : 'text/plain' ,
258
+ 'Set-Cookie' : [ ] ,
259
+ } ) ;
138
260
} ) ;
139
261
140
262
test ( 'serializes a response with content type set to application/json' , ( ) => {
@@ -149,5 +271,8 @@ test('serializes a response with content type set to application/json', () => {
149
271
JSON . stringify ( { url : 'https://dkundel.com' } )
150
272
) ;
151
273
expect ( serialized . statusCode ) . toEqual ( 200 ) ;
152
- expect ( serialized . headers ) . toEqual ( { 'Content-Type' : 'application/json' } ) ;
274
+ expect ( serialized . headers ) . toEqual ( {
275
+ 'Content-Type' : 'application/json' ,
276
+ 'Set-Cookie' : [ ] ,
277
+ } ) ;
153
278
} ) ;
0 commit comments