@@ -5,7 +5,9 @@ test('has correct defaults', () => {
55 const response = new Response ( ) ;
66 expect ( response [ 'body' ] ) . toBeNull ( ) ;
77 expect ( response [ 'statusCode' ] ) . toBe ( 200 ) ;
8- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
8+ expect ( response [ 'headers' ] ) . toEqual ( {
9+ 'Set-Cookie' : [ ] ,
10+ } ) ;
911} ) ;
1012
1113test ( 'sets status code, body and headers from constructor' , ( ) => {
@@ -24,6 +26,7 @@ test('sets status code, body and headers from constructor', () => {
2426 'Access-Control-Allow-Origin' : 'example.com' ,
2527 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE' ,
2628 'Access-Control-Allow-Headers' : 'Content-Type' ,
29+ 'Set-Cookie' : [ ] ,
2730 } ) ;
2831} ) ;
2932
@@ -45,7 +48,9 @@ test('sets body correctly', () => {
4548
4649test ( 'sets headers correctly' , ( ) => {
4750 const response = new Response ( ) ;
48- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
51+ expect ( response [ 'headers' ] ) . toEqual ( {
52+ 'Set-Cookie' : [ ] ,
53+ } ) ;
4954 response . setHeaders ( {
5055 'Access-Control-Allow-Origin' : 'example.com' ,
5156 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE' ,
@@ -55,35 +60,152 @@ test('sets headers correctly', () => {
5560 'Access-Control-Allow-Origin' : 'example.com' ,
5661 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE' ,
5762 'Access-Control-Allow-Headers' : 'Content-Type' ,
63+ 'Set-Cookie' : [ ] ,
5864 } ;
5965 expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
6066 // @ts -ignore
6167 response . setHeaders ( undefined ) ;
6268 expect ( response [ 'headers' ] ) . toEqual ( expected ) ;
6369} ) ;
6470
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+
65119test ( 'appends a new header correctly' , ( ) => {
66120 const response = new Response ( ) ;
67- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
121+ expect ( response [ 'headers' ] ) . toEqual ( {
122+ 'Set-Cookie' : [ ] ,
123+ } ) ;
68124 response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
69125 expect ( response [ 'headers' ] ) . toEqual ( {
70126 'Access-Control-Allow-Origin' : 'dkundel.com' ,
127+ 'Set-Cookie' : [ ] ,
71128 } ) ;
72129 response . appendHeader ( 'Content-Type' , 'application/json' ) ;
73130 expect ( response [ 'headers' ] ) . toEqual ( {
74131 'Access-Control-Allow-Origin' : 'dkundel.com' ,
75132 'Content-Type' : 'application/json' ,
133+ 'Set-Cookie' : [ ] ,
76134 } ) ;
77135} ) ;
78136
79137test ( 'appends a header correctly with no existing one' , ( ) => {
80138 const response = new Response ( ) ;
81- expect ( response [ 'headers' ] ) . toEqual ( { } ) ;
139+ expect ( response [ 'headers' ] ) . toEqual ( {
140+ 'Set-Cookie' : [ ] ,
141+ } ) ;
82142 // @ts -ignore
83143 response [ 'headers' ] = undefined ;
84144 response . appendHeader ( 'Access-Control-Allow-Origin' , 'dkundel.com' ) ;
85145 expect ( response [ 'headers' ] ) . toEqual ( {
86146 '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' ] ,
87209 } ) ;
88210} ) ;
89211
@@ -107,6 +229,16 @@ test('appendHeader returns the response', () => {
107229 expect ( response . appendHeader ( 'X-Test' , 'Hello' ) ) . toBe ( response ) ;
108230} ) ;
109231
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+
110242test ( 'calls express response correctly' , ( ) => {
111243 const mockRes = {
112244 status : jest . fn ( ) ,
@@ -121,7 +253,10 @@ test('calls express response correctly', () => {
121253
122254 expect ( mockRes . send ) . toHaveBeenCalledWith ( `I'm a teapot!` ) ;
123255 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+ } ) ;
125260} ) ;
126261
127262test ( 'serializes a response' , ( ) => {
@@ -134,7 +269,10 @@ test('serializes a response', () => {
134269
135270 expect ( serialized . body ) . toEqual ( "I'm a teapot!" ) ;
136271 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+ } ) ;
138276} ) ;
139277
140278test ( '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', () => {
149287 JSON . stringify ( { url : 'https://dkundel.com' } )
150288 ) ;
151289 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+ } ) ;
153294} ) ;
0 commit comments