@@ -6,12 +6,12 @@ import { deepStrictEqual, ok, strictEqual } from "node:assert";
6
6
import { createServer } from "node:http" ;
7
7
import { describe , it } from "node:test" ;
8
8
9
+ import { listen } from "async-listen" ;
9
10
import express from "express" ;
10
11
import createError from "http-errors" ;
11
12
12
13
import graphqlUploadExpress from "./graphqlUploadExpress.mjs" ;
13
14
import processRequest from "./processRequest.mjs" ;
14
- import listen from "./test/listen.mjs" ;
15
15
16
16
describe (
17
17
"Function `graphqlUploadExpress`." ,
@@ -22,22 +22,23 @@ describe(
22
22
it ( "Non multipart request." , async ( ) => {
23
23
let processRequestRan = false ;
24
24
25
- const app = express ( ) . use (
26
- graphqlUploadExpress ( {
27
- /** @type {any } */
28
- async processRequest ( ) {
29
- processRequestRan = true ;
30
- } ,
31
- } ) ,
25
+ const server = createServer (
26
+ express ( ) . use (
27
+ graphqlUploadExpress ( {
28
+ /** @type {any } */
29
+ async processRequest ( ) {
30
+ processRequestRan = true ;
31
+ } ,
32
+ } ) ,
33
+ ) ,
32
34
) ;
33
-
34
- const { port, close } = await listen ( createServer ( app ) ) ;
35
+ const url = await listen ( server ) ;
35
36
36
37
try {
37
- await fetch ( `http://localhost: ${ port } ` , { method : "POST" } ) ;
38
+ await fetch ( url , { method : "POST" } ) ;
38
39
strictEqual ( processRequestRan , false ) ;
39
40
} finally {
40
- close ( ) ;
41
+ server . close ( ) ;
41
42
}
42
43
} ) ;
43
44
@@ -51,14 +52,15 @@ describe(
51
52
*/
52
53
let requestBody ;
53
54
54
- const app = express ( )
55
- . use ( graphqlUploadExpress ( ) )
56
- . use ( ( request , response , next ) => {
57
- requestBody = request . body ;
58
- next ( ) ;
59
- } ) ;
60
-
61
- const { port, close } = await listen ( createServer ( app ) ) ;
55
+ const server = createServer (
56
+ express ( )
57
+ . use ( graphqlUploadExpress ( ) )
58
+ . use ( ( request , response , next ) => {
59
+ requestBody = request . body ;
60
+ next ( ) ;
61
+ } ) ,
62
+ ) ;
63
+ const url = await listen ( server ) ;
62
64
63
65
try {
64
66
const body = new FormData ( ) ;
@@ -70,13 +72,13 @@ describe(
70
72
body . append ( "map" , JSON . stringify ( { 1 : [ "variables.file" ] } ) ) ;
71
73
body . append ( "1" , new File ( [ "a" ] , "a.txt" , { type : "text/plain" } ) ) ;
72
74
73
- await fetch ( `http://localhost: ${ port } ` , { method : "POST" , body } ) ;
75
+ await fetch ( url , { method : "POST" , body } ) ;
74
76
75
77
ok ( requestBody ) ;
76
78
ok ( requestBody . variables ) ;
77
79
ok ( requestBody . variables . file ) ;
78
80
} finally {
79
- close ( ) ;
81
+ server . close ( ) ;
80
82
}
81
83
} ) ;
82
84
@@ -92,21 +94,23 @@ describe(
92
94
*/
93
95
let requestBody ;
94
96
95
- const app = express ( )
96
- . use (
97
- graphqlUploadExpress ( {
98
- processRequest ( ...args ) {
99
- processRequestRan = true ;
100
- return processRequest ( ...args ) ;
101
- } ,
97
+ const server = createServer (
98
+ express ( )
99
+ . use (
100
+ graphqlUploadExpress ( {
101
+ processRequest ( ...args ) {
102
+ processRequestRan = true ;
103
+ return processRequest ( ...args ) ;
104
+ } ,
105
+ } ) ,
106
+ )
107
+ . use ( ( request , response , next ) => {
108
+ requestBody = request . body ;
109
+ next ( ) ;
102
110
} ) ,
103
- )
104
- . use ( ( request , response , next ) => {
105
- requestBody = request . body ;
106
- next ( ) ;
107
- } ) ;
111
+ ) ;
108
112
109
- const { port , close } = await listen ( createServer ( app ) ) ;
113
+ const url = await listen ( server ) ;
110
114
111
115
try {
112
116
const body = new FormData ( ) ;
@@ -118,14 +122,14 @@ describe(
118
122
body . append ( "map" , JSON . stringify ( { 1 : [ "variables.file" ] } ) ) ;
119
123
body . append ( "1" , new File ( [ "a" ] , "a.txt" , { type : "text/plain" } ) ) ;
120
124
121
- await fetch ( `http://localhost: ${ port } ` , { method : "POST" , body } ) ;
125
+ await fetch ( url , { method : "POST" , body } ) ;
122
126
123
127
strictEqual ( processRequestRan , true ) ;
124
128
ok ( requestBody ) ;
125
129
ok ( requestBody . variables ) ;
126
130
ok ( requestBody . variables . file ) ;
127
131
} finally {
128
- close ( ) ;
132
+ server . close ( ) ;
129
133
}
130
134
} ) ;
131
135
@@ -135,47 +139,49 @@ describe(
135
139
let responseStatusCode ;
136
140
137
141
const error = createError ( 400 , "Message." ) ;
138
- const app = express ( )
139
- . use ( ( request , response , next ) => {
140
- const { send } = response ;
141
-
142
- // @ts -ignore Todo: Find a less hacky way.
143
- response . send = ( ...args ) => {
144
- requestCompleted = request . complete ;
145
- response . send = send ;
146
- response . send ( ...args ) ;
147
- } ;
148
-
149
- next ( ) ;
150
- } )
151
- . use (
152
- graphqlUploadExpress ( {
153
- async processRequest ( request ) {
154
- request . resume ( ) ;
155
- throw error ;
142
+ const server = createServer (
143
+ express ( )
144
+ . use ( ( request , response , next ) => {
145
+ const { send } = response ;
146
+
147
+ // @ts -ignore Todo: Find a less hacky way.
148
+ response . send = ( ...args ) => {
149
+ requestCompleted = request . complete ;
150
+ response . send = send ;
151
+ response . send ( ...args ) ;
152
+ } ;
153
+
154
+ next ( ) ;
155
+ } )
156
+ . use (
157
+ graphqlUploadExpress ( {
158
+ async processRequest ( request ) {
159
+ request . resume ( ) ;
160
+ throw error ;
161
+ } ,
162
+ } ) ,
163
+ )
164
+ . use (
165
+ /**
166
+ * @param {Error } error
167
+ * @param {import("express").Request } request
168
+ * @param {import("express").Response } response
169
+ * @param {import("express").NextFunction } next
170
+ */
171
+ ( error , request , response , next ) => {
172
+ expressError = error ;
173
+ responseStatusCode = response . statusCode ;
174
+
175
+ // Sending a response here prevents the default Express error
176
+ // handler from running, which would undesirably (in this case)
177
+ // display the error in the console.
178
+ if ( response . headersSent ) next ( error ) ;
179
+ else response . send ( ) ;
156
180
} ,
157
- } ) ,
158
- )
159
- . use (
160
- /**
161
- * @param {Error } error
162
- * @param {import("express").Request } request
163
- * @param {import("express").Response } response
164
- * @param {import("express").NextFunction } next
165
- */
166
- ( error , request , response , next ) => {
167
- expressError = error ;
168
- responseStatusCode = response . statusCode ;
169
-
170
- // Sending a response here prevents the default Express error handler
171
- // from running, which would undesirably (in this case) display the
172
- // error in the console.
173
- if ( response . headersSent ) next ( error ) ;
174
- else response . send ( ) ;
175
- } ,
176
- ) ;
181
+ ) ,
182
+ ) ;
177
183
178
- const { port , close } = await listen ( createServer ( app ) ) ;
184
+ const url = await listen ( server ) ;
179
185
180
186
try {
181
187
const body = new FormData ( ) ;
@@ -187,7 +193,7 @@ describe(
187
193
body . append ( "map" , JSON . stringify ( { 1 : [ "variables.file" ] } ) ) ;
188
194
body . append ( "1" , new File ( [ "a" ] , "a.txt" , { type : "text/plain" } ) ) ;
189
195
190
- await fetch ( `http://localhost: ${ port } ` , { method : "POST" , body } ) ;
196
+ await fetch ( url , { method : "POST" , body } ) ;
191
197
192
198
deepStrictEqual ( expressError , error ) ;
193
199
ok (
@@ -196,7 +202,7 @@ describe(
196
202
) ;
197
203
strictEqual ( responseStatusCode , error . status ) ;
198
204
} finally {
199
- close ( ) ;
205
+ server . close ( ) ;
200
206
}
201
207
} ) ;
202
208
@@ -205,42 +211,44 @@ describe(
205
211
let requestCompleted ;
206
212
207
213
const error = new Error ( "Message." ) ;
208
- const app = express ( )
209
- . use ( ( request , response , next ) => {
210
- const { send } = response ;
211
-
212
- // @ts -ignore Todo: Find a less hacky way.
213
- response . send = ( ...args ) => {
214
- requestCompleted = request . complete ;
215
- response . send = send ;
216
- response . send ( ...args ) ;
217
- } ;
218
-
219
- next ( ) ;
220
- } )
221
- . use ( graphqlUploadExpress ( ) )
222
- . use ( ( ) => {
223
- throw error ;
224
- } )
225
- . use (
226
- /**
227
- * @param {Error } error
228
- * @param {import("express").Request } request
229
- * @param {import("express").Response } response
230
- * @param {import("express").NextFunction } next
231
- */
232
- ( error , request , response , next ) => {
233
- expressError = error ;
234
-
235
- // Sending a response here prevents the default Express error handler
236
- // from running, which would undesirably (in this case) display the
237
- // error in the console.
238
- if ( response . headersSent ) next ( error ) ;
239
- else response . send ( ) ;
240
- } ,
241
- ) ;
214
+ const server = createServer (
215
+ express ( )
216
+ . use ( ( request , response , next ) => {
217
+ const { send } = response ;
218
+
219
+ // @ts -ignore Todo: Find a less hacky way.
220
+ response . send = ( ...args ) => {
221
+ requestCompleted = request . complete ;
222
+ response . send = send ;
223
+ response . send ( ...args ) ;
224
+ } ;
225
+
226
+ next ( ) ;
227
+ } )
228
+ . use ( graphqlUploadExpress ( ) )
229
+ . use ( ( ) => {
230
+ throw error ;
231
+ } )
232
+ . use (
233
+ /**
234
+ * @param {Error } error
235
+ * @param {import("express").Request } request
236
+ * @param {import("express").Response } response
237
+ * @param {import("express").NextFunction } next
238
+ */
239
+ ( error , request , response , next ) => {
240
+ expressError = error ;
241
+
242
+ // Sending a response here prevents the default Express error
243
+ // handler from running, which would undesirably (in this case)
244
+ // display the error in the console.
245
+ if ( response . headersSent ) next ( error ) ;
246
+ else response . send ( ) ;
247
+ } ,
248
+ ) ,
249
+ ) ;
242
250
243
- const { port , close } = await listen ( createServer ( app ) ) ;
251
+ const url = await listen ( server ) ;
244
252
245
253
try {
246
254
const body = new FormData ( ) ;
@@ -252,15 +260,15 @@ describe(
252
260
body . append ( "map" , JSON . stringify ( { 1 : [ "variables.file" ] } ) ) ;
253
261
body . append ( "1" , new File ( [ "a" ] , "a.txt" , { type : "text/plain" } ) ) ;
254
262
255
- await fetch ( `http://localhost: ${ port } ` , { method : "POST" , body } ) ;
263
+ await fetch ( url , { method : "POST" , body } ) ;
256
264
257
265
deepStrictEqual ( expressError , error ) ;
258
266
ok (
259
267
requestCompleted ,
260
268
"Response wasn’t delayed until the request completed." ,
261
269
) ;
262
270
} finally {
263
- close ( ) ;
271
+ server . close ( ) ;
264
272
}
265
273
} ) ;
266
274
} ,
0 commit comments