1
- import { nodeHandler } from "./utils"
1
+ import { mockReqRes , nextHandler } from "./utils"
2
2
3
3
it ( "Missing req.url throws in dev" , async ( ) => {
4
- await expect ( nodeHandler ) . rejects . toThrow ( new Error ( "Missing url" ) )
4
+ await expect ( nextHandler ) . rejects . toThrow ( new Error ( "Missing url" ) )
5
5
} )
6
6
7
7
const configErrorMessage =
@@ -10,7 +10,7 @@ const configErrorMessage =
10
10
it ( "Missing req.url returns config error in prod" , async ( ) => {
11
11
// @ts -expect-error
12
12
process . env . NODE_ENV = "production"
13
- const { res, logger } = await nodeHandler ( )
13
+ const { res, logger } = await nextHandler ( )
14
14
15
15
expect ( logger . error ) . toBeCalledTimes ( 1 )
16
16
const error = new Error ( "Missing url" )
@@ -26,7 +26,7 @@ it("Missing req.url returns config error in prod", async () => {
26
26
it ( "Missing host throws in dev" , async ( ) => {
27
27
await expect (
28
28
async ( ) =>
29
- await nodeHandler ( {
29
+ await nextHandler ( {
30
30
req : { query : { nextauth : [ "session" ] } } ,
31
31
} )
32
32
) . rejects . toThrow ( Error )
@@ -35,7 +35,7 @@ it("Missing host throws in dev", async () => {
35
35
it ( "Missing host config error in prod" , async ( ) => {
36
36
// @ts -expect-error
37
37
process . env . NODE_ENV = "production"
38
- const { res, logger } = await nodeHandler ( {
38
+ const { res, logger } = await nextHandler ( {
39
39
req : { query : { nextauth : [ "session" ] } } ,
40
40
} )
41
41
expect ( res . status ) . toBeCalledWith ( 400 )
@@ -49,7 +49,7 @@ it("Missing host config error in prod", async () => {
49
49
it ( "Defined host throws 400 in production if not trusted" , async ( ) => {
50
50
// @ts -expect-error
51
51
process . env . NODE_ENV = "production"
52
- const { res } = await nodeHandler ( {
52
+ const { res } = await nextHandler ( {
53
53
req : { headers : { host : "http://localhost" } } ,
54
54
} )
55
55
expect ( res . status ) . toBeCalledWith ( 400 )
@@ -60,7 +60,7 @@ it("Defined host throws 400 in production if not trusted", async () => {
60
60
it ( "Defined host throws 400 in production if trusted but invalid URL" , async ( ) => {
61
61
// @ts -expect-error
62
62
process . env . NODE_ENV = "production"
63
- const { res } = await nodeHandler ( {
63
+ const { res } = await nextHandler ( {
64
64
req : { headers : { host : "localhost" } } ,
65
65
options : { trustHost : true } ,
66
66
} )
@@ -72,52 +72,57 @@ it("Defined host throws 400 in production if trusted but invalid URL", async ()
72
72
it ( "Defined host does not throw in production if trusted and valid URL" , async ( ) => {
73
73
// @ts -expect-error
74
74
process . env . NODE_ENV = "production"
75
- const { res } = await nodeHandler ( {
75
+ const { res } = await nextHandler ( {
76
76
req : {
77
77
url : "/api/auth/session" ,
78
78
headers : { host : "http://localhost" } ,
79
79
} ,
80
80
options : { trustHost : true } ,
81
81
} )
82
82
expect ( res . status ) . toBeCalledWith ( 200 )
83
+ // @ts -expect-error
83
84
expect ( JSON . parse ( res . send . mock . calls [ 0 ] [ 0 ] ) ) . toEqual ( { } )
84
85
// @ts -expect-error
85
86
process . env . NODE_ENV = "test"
86
87
} )
87
88
88
89
it ( "Use process.env.NEXTAUTH_URL for host if present" , async ( ) => {
89
90
process . env . NEXTAUTH_URL = "http://localhost"
90
- const { res } = await nodeHandler ( {
91
+ const { res } = await nextHandler ( {
91
92
req : { url : "/api/auth/session" } ,
92
93
} )
93
94
expect ( res . status ) . toBeCalledWith ( 200 )
95
+ // @ts -expect-error
94
96
expect ( JSON . parse ( res . send . mock . calls [ 0 ] [ 0 ] ) ) . toEqual ( { } )
95
97
} )
96
98
97
99
it ( "Redirects if necessary" , async ( ) => {
98
100
process . env . NEXTAUTH_URL = "http://localhost"
99
- const { res } = await nodeHandler ( {
101
+ const { res } = await nextHandler ( {
100
102
req : {
101
103
method : "post" ,
102
104
url : "/api/auth/signin/github" ,
103
105
} ,
104
106
} )
105
107
expect ( res . status ) . toBeCalledWith ( 302 )
106
- expect ( res . setHeader ) . toBeCalledWith ( "set-cookie" , [
107
- expect . stringMatching (
108
- / n e x t - a u t h .c s r f - t o k e n = .* ; P a t h = \/ ; H t t p O n l y ; S a m e S i t e = L a x /
109
- ) ,
110
- `next-auth.callback-url=${ encodeURIComponent (
111
- process . env . NEXTAUTH_URL
112
- ) } ; Path=/; HttpOnly; SameSite=Lax`,
113
- ] )
114
- expect ( res . setHeader ) . toBeCalledTimes ( 2 )
108
+ expect ( res . getHeaders ( ) ) . toEqual ( {
109
+ location : "http://localhost/api/auth/signin?csrf=true" ,
110
+ "set-cookie" : [
111
+ expect . stringMatching (
112
+ / n e x t - a u t h .c s r f - t o k e n = .* ; P a t h = \/ ; H t t p O n l y ; S a m e S i t e = L a x /
113
+ ) ,
114
+ `next-auth.callback-url=${ encodeURIComponent (
115
+ process . env . NEXTAUTH_URL
116
+ ) } ; Path=/; HttpOnly; SameSite=Lax`,
117
+ ] ,
118
+ } )
119
+
115
120
expect ( res . send ) . toBeCalledWith ( "" )
116
121
} )
117
122
118
123
it ( "Returns redirect if `X-Auth-Return-Redirect` header is present" , async ( ) => {
119
124
process . env . NEXTAUTH_URL = "http://localhost"
120
- const { res } = await nodeHandler ( {
125
+ const { res } = await nextHandler ( {
121
126
req : {
122
127
method : "post" ,
123
128
url : "/api/auth/signin/github" ,
@@ -126,16 +131,48 @@ it("Returns redirect if `X-Auth-Return-Redirect` header is present", async () =>
126
131
} )
127
132
128
133
expect ( res . status ) . toBeCalledWith ( 200 )
129
- expect ( res . setHeader ) . toBeCalledWith ( "content-type" , "application/json" )
130
- expect ( res . setHeader ) . toBeCalledWith ( "set-cookie" , [
131
- expect . stringMatching (
132
- / n e x t - a u t h .c s r f - t o k e n = .* ; P a t h = \/ ; H t t p O n l y ; S a m e S i t e = L a x /
133
- ) ,
134
- `next-auth.callback-url=${ encodeURIComponent (
135
- process . env . NEXTAUTH_URL
136
- ) } ; Path=/; HttpOnly; SameSite=Lax`,
137
- ] )
138
- expect ( res . setHeader ) . toBeCalledTimes ( 2 )
134
+
135
+ expect ( res . getHeaders ( ) ) . toEqual ( {
136
+ "content-type" : "application/json" ,
137
+ "set-cookie" : [
138
+ expect . stringMatching (
139
+ / n e x t - a u t h .c s r f - t o k e n = .* ; P a t h = \/ ; H t t p O n l y ; S a m e S i t e = L a x /
140
+ ) ,
141
+ `next-auth.callback-url=${ encodeURIComponent (
142
+ process . env . NEXTAUTH_URL
143
+ ) } ; Path=/; HttpOnly; SameSite=Lax`,
144
+ ] ,
145
+ } )
146
+
147
+ expect ( res . send ) . toBeCalledWith (
148
+ JSON . stringify ( { url : "http://localhost/api/auth/signin?csrf=true" } )
149
+ )
150
+ } )
151
+
152
+ it ( "Should preserve user's `set-cookie` headers" , async ( ) => {
153
+ const { req, res } = mockReqRes ( {
154
+ method : "post" ,
155
+ url : "/api/auth/signin/credentials" ,
156
+ headers : { host : "localhost" , "X-Auth-Return-Redirect" : "1" } ,
157
+ } )
158
+ res . setHeader ( "set-cookie" , [ "foo=bar" , "bar=baz" ] )
159
+
160
+ await nextHandler ( { req, res } )
161
+
162
+ expect ( res . getHeaders ( ) ) . toEqual ( {
163
+ "content-type" : "application/json" ,
164
+ "set-cookie" : [
165
+ "foo=bar" ,
166
+ "bar=baz" ,
167
+ expect . stringMatching (
168
+ / n e x t - a u t h .c s r f - t o k e n = .* ; P a t h = \/ ; H t t p O n l y ; S a m e S i t e = L a x /
169
+ ) ,
170
+ `next-auth.callback-url=${ encodeURIComponent (
171
+ "http://localhost"
172
+ ) } ; Path=/; HttpOnly; SameSite=Lax`,
173
+ ] ,
174
+ } )
175
+
139
176
expect ( res . send ) . toBeCalledWith (
140
177
JSON . stringify ( { url : "http://localhost/api/auth/signin?csrf=true" } )
141
178
)
0 commit comments