@@ -178,7 +178,44 @@ describe('with-middleware-auth-required', () => {
178
178
} ;
179
179
const res = await setup ( { user : { name : 'dave' } , middleware } ) ;
180
180
expect ( res . status ) . toEqual ( 200 ) ;
181
- expect ( res . headers . get ( 'set-cookie' ) ) . toMatch ( / ^ a p p S e s s i o n = .+ , f o o = b a r ; / ) ;
181
+ // @ts -expect-errors ts dom doesn't have getAll
182
+ expect ( res . headers . getAll ( 'set-cookie' ) ) . toHaveLength ( 2 ) ;
183
+ expect ( res . headers . get ( 'set-cookie' ) ) . toMatch ( / a p p S e s s i o n = / ) ;
184
+ expect ( res . headers . get ( 'set-cookie' ) ) . toMatch ( / f o o = b a r ; / ) ;
185
+ } ) ;
186
+
187
+ test ( 'should allow responses from custom middleware' , async ( ) => {
188
+ const middleware = ( ) => {
189
+ return NextResponse . json ( { foo : 'bar' } ) ;
190
+ } ;
191
+ const res = await setup ( { user : { name : 'dave' } , middleware } ) ;
192
+ expect ( res . status ) . toEqual ( 200 ) ;
193
+ await expect ( res . json ( ) ) . resolves . toEqual ( { foo : 'bar' } ) ;
194
+ } ) ;
195
+
196
+ test ( 'should allow ReadableStream responses from custom middleware' , async ( ) => {
197
+ const middleware = ( ) => {
198
+ // @ts -expect-errors ts dom doesn't have json
199
+ return new Response ( Response . json ( { foo : 'bar' } ) . body ) ;
200
+ } ;
201
+ const res = await setup ( { user : { name : 'dave' } , middleware } ) ;
202
+ expect ( res . status ) . toEqual ( 200 ) ;
203
+ await expect ( res . json ( ) ) . resolves . toEqual ( { foo : 'bar' } ) ;
204
+ } ) ;
205
+
206
+ test ( 'should allow responses and cookies from custom middleware' , async ( ) => {
207
+ const middleware = ( ) => {
208
+ const res = NextResponse . json ( { foo : 'bar' } ) ;
209
+ res . cookies . set ( 'foo' , 'bar' ) ;
210
+ return res ;
211
+ } ;
212
+ const res = await setup ( { user : { name : 'dave' } , middleware } ) ;
213
+ expect ( res . status ) . toEqual ( 200 ) ;
214
+ await expect ( res . json ( ) ) . resolves . toEqual ( { foo : 'bar' } ) ;
215
+ // @ts -expect-errors ts dom doesn't have getAll
216
+ expect ( res . headers . getAll ( 'set-cookie' ) ) . toHaveLength ( 2 ) ;
217
+ expect ( res . headers . get ( 'set-cookie' ) ) . toMatch ( / a p p S e s s i o n = / ) ;
218
+ expect ( res . headers . get ( 'set-cookie' ) ) . toMatch ( / f o o = b a r ; / ) ;
182
219
} ) ;
183
220
184
221
test ( 'should set just a custom cookie when session is not rolling' , async ( ) => {
0 commit comments