1
1
'use strict'
2
2
3
- const { test } = require ( 'tap ' )
3
+ const { test } = require ( 'node:test ' )
4
4
const EventEmitter = require ( 'events' )
5
- const { Client, errors } = require ( '..' )
5
+ const { Client, errors } = require ( '../.. ' )
6
6
const { createServer } = require ( 'http' )
7
7
const { createReadStream } = require ( 'fs' )
8
8
const { Readable } = require ( 'stream' )
9
- const { wrapWithAsyncIterable } = require ( './utils/async-iterators' )
9
+ const { tspl } = require ( '@matteo.collina/tspl' )
10
+ const { wrapWithAsyncIterable } = require ( '../utils/async-iterators' )
10
11
11
- test ( 'Abort before sending request (no body)' , ( t ) => {
12
- t . plan ( 4 )
12
+ test ( 'Abort before sending request (no body)' , async ( t ) => {
13
+ const p = tspl ( t , { plan : 4 } )
13
14
14
15
let count = 0
15
16
const server = createServer ( ( req , res ) => {
16
17
if ( count === 1 ) {
17
- t . fail ( 'The second request should never be executed' )
18
+ p . fail ( 'The second request should never be executed' )
18
19
}
19
20
count += 1
20
21
res . end ( 'hello' )
21
22
} )
22
23
23
- t . teardown ( server . close . bind ( server ) )
24
+ t . after ( server . close . bind ( server ) )
24
25
25
26
server . listen ( 0 , ( ) => {
26
27
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
27
28
const ee = new EventEmitter ( )
28
- t . teardown ( client . destroy . bind ( client ) )
29
+ t . after ( client . destroy . bind ( client ) )
29
30
30
31
client . request ( { path : '/' , method : 'GET' } , ( err , response ) => {
31
- t . error ( err )
32
+ p . ifError ( err )
32
33
const bufs = [ ]
33
34
response . body . on ( 'data' , ( buf ) => {
34
35
bufs . push ( buf )
35
36
} )
36
37
response . body . on ( 'end' , ( ) => {
37
- t . equal ( 'hello' , Buffer . concat ( bufs ) . toString ( 'utf8' ) )
38
+ p . strictEqual ( 'hello' , Buffer . concat ( bufs ) . toString ( 'utf8' ) )
38
39
} )
39
40
} )
40
41
41
42
const body = new Readable ( { read ( ) { } } )
42
43
body . on ( 'error' , ( err ) => {
43
- t . type ( err , errors . RequestAbortedError )
44
+ p . ok ( err instanceof errors . RequestAbortedError )
44
45
} )
45
46
client . request ( {
46
47
path : '/' ,
47
48
method : 'GET' ,
48
49
signal : ee ,
49
50
body
50
51
} , ( err , response ) => {
51
- t . type ( err , errors . RequestAbortedError )
52
+ p . ok ( err instanceof errors . RequestAbortedError )
52
53
} )
53
54
54
55
ee . emit ( 'abort' )
55
56
} )
57
+
58
+ await p . completed
56
59
} )
57
60
58
- test ( 'Abort before sending request (no body) async iterator' , ( t ) => {
59
- t . plan ( 3 )
61
+ test ( 'Abort before sending request (no body) async iterator' , async ( t ) => {
62
+ const p = tspl ( t , { plan : 3 } )
60
63
61
64
let count = 0
62
65
const server = createServer ( ( req , res ) => {
@@ -67,21 +70,21 @@ test('Abort before sending request (no body) async iterator', (t) => {
67
70
res . end ( 'hello' )
68
71
} )
69
72
70
- t . teardown ( server . close . bind ( server ) )
73
+ t . after ( server . close . bind ( server ) )
71
74
72
75
server . listen ( 0 , ( ) => {
73
76
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
74
77
const ee = new EventEmitter ( )
75
- t . teardown ( client . destroy . bind ( client ) )
78
+ t . after ( client . destroy . bind ( client ) )
76
79
77
80
client . request ( { path : '/' , method : 'GET' } , ( err , response ) => {
78
- t . error ( err )
81
+ p . ifError ( err )
79
82
const bufs = [ ]
80
83
response . body . on ( 'data' , ( buf ) => {
81
84
bufs . push ( buf )
82
85
} )
83
86
response . body . on ( 'end' , ( ) => {
84
- t . equal ( 'hello' , Buffer . concat ( bufs ) . toString ( 'utf8' ) )
87
+ p . strictEqual ( 'hello' , Buffer . concat ( bufs ) . toString ( 'utf8' ) )
85
88
} )
86
89
} )
87
90
@@ -92,36 +95,40 @@ test('Abort before sending request (no body) async iterator', (t) => {
92
95
signal : ee ,
93
96
body
94
97
} , ( err , response ) => {
95
- t . type ( err , errors . RequestAbortedError )
98
+ p . ok ( err instanceof errors . RequestAbortedError )
96
99
} )
97
100
98
101
ee . emit ( 'abort' )
99
102
} )
103
+
104
+ await p . completed
100
105
} )
101
106
102
- test ( 'Abort while waiting response (no body)' , ( t ) => {
103
- t . plan ( 1 )
107
+ test ( 'Abort while waiting response (no body)' , async ( t ) => {
108
+ const p = tspl ( t , { plan : 1 } )
104
109
105
110
const ee = new EventEmitter ( )
106
111
const server = createServer ( ( req , res ) => {
107
112
ee . emit ( 'abort' )
108
113
res . setHeader ( 'content-type' , 'text/plain' )
109
114
res . end ( 'hello world' )
110
115
} )
111
- t . teardown ( server . close . bind ( server ) )
116
+ t . after ( server . close . bind ( server ) )
112
117
113
118
server . listen ( 0 , ( ) => {
114
119
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
115
- t . teardown ( client . destroy . bind ( client ) )
120
+ t . after ( client . destroy . bind ( client ) )
116
121
117
122
client . request ( { path : '/' , method : 'GET' , signal : ee } , ( err , response ) => {
118
- t . type ( err , errors . RequestAbortedError )
123
+ p . ok ( err instanceof errors . RequestAbortedError )
119
124
} )
120
125
} )
126
+
127
+ await p . completed
121
128
} )
122
129
123
- test ( 'Abort while waiting response (write headers started) (no body)' , ( t ) => {
124
- t . plan ( 1 )
130
+ test ( 'Abort while waiting response (write headers started) (no body)' , async ( t ) => {
131
+ const p = tspl ( t , { plan : 1 } )
125
132
126
133
const ee = new EventEmitter ( )
127
134
const server = createServer ( ( req , res ) => {
@@ -130,64 +137,68 @@ test('Abort while waiting response (write headers started) (no body)', (t) => {
130
137
ee . emit ( 'abort' )
131
138
res . end ( 'hello world' )
132
139
} )
133
- t . teardown ( server . close . bind ( server ) )
140
+ t . after ( server . close . bind ( server ) )
134
141
135
142
server . listen ( 0 , ( ) => {
136
143
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
137
- t . teardown ( client . destroy . bind ( client ) )
144
+ t . after ( client . destroy . bind ( client ) )
138
145
139
146
client . request ( { path : '/' , method : 'GET' , signal : ee } , ( err , response ) => {
140
- t . type ( err , errors . RequestAbortedError )
147
+ p . ok ( err instanceof errors . RequestAbortedError )
141
148
} )
142
149
} )
150
+
151
+ await p . completed
143
152
} )
144
153
145
- test ( 'Abort while waiting response (write headers and write body started) (no body)' , ( t ) => {
146
- t . plan ( 2 )
154
+ test ( 'Abort while waiting response (write headers and write body started) (no body)' , async ( t ) => {
155
+ const p = tspl ( t , { plan : 2 } )
147
156
148
157
const ee = new EventEmitter ( )
149
158
const server = createServer ( ( req , res ) => {
150
159
res . writeHead ( 200 , { 'content-type' : 'text/plain' } )
151
160
res . write ( 'hello' )
152
161
} )
153
- t . teardown ( server . close . bind ( server ) )
162
+ t . after ( server . close . bind ( server ) )
154
163
155
164
server . listen ( 0 , ( ) => {
156
165
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
157
- t . teardown ( client . destroy . bind ( client ) )
166
+ t . after ( client . destroy . bind ( client ) )
158
167
159
168
client . request ( { path : '/' , method : 'GET' , signal : ee } , ( err , response ) => {
160
- t . error ( err )
169
+ p . ifError ( err )
161
170
response . body . on ( 'data' , ( ) => {
162
171
ee . emit ( 'abort' )
163
172
} )
164
173
response . body . on ( 'error' , err => {
165
- t . type ( err , errors . RequestAbortedError )
174
+ p . ok ( err instanceof errors . RequestAbortedError )
166
175
} )
167
176
} )
168
177
} )
178
+ await p . completed
169
179
} )
170
180
171
181
function waitingWithBody ( body , type ) {
172
- test ( `Abort while waiting response (with body ${ type } )` , ( t ) => {
173
- t . plan ( 1 )
182
+ test ( `Abort while waiting response (with body ${ type } )` , async ( t ) => {
183
+ const p = tspl ( t , { plan : 1 } )
174
184
175
185
const ee = new EventEmitter ( )
176
186
const server = createServer ( ( req , res ) => {
177
187
ee . emit ( 'abort' )
178
188
res . setHeader ( 'content-type' , 'text/plain' )
179
189
res . end ( 'hello world' )
180
190
} )
181
- t . teardown ( server . close . bind ( server ) )
191
+ t . after ( server . close . bind ( server ) )
182
192
183
193
server . listen ( 0 , ( ) => {
184
194
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
185
- t . teardown ( client . destroy . bind ( client ) )
195
+ t . after ( client . destroy . bind ( client ) )
186
196
187
197
client . request ( { path : '/' , method : 'POST' , body, signal : ee } , ( err , response ) => {
188
- t . type ( err , errors . RequestAbortedError )
198
+ p . ok ( err instanceof errors . RequestAbortedError )
189
199
} )
190
200
} )
201
+ await p . completed
191
202
} )
192
203
}
193
204
@@ -197,8 +208,8 @@ waitingWithBody(new Uint8Array([42]), 'Uint8Array')
197
208
waitingWithBody ( wrapWithAsyncIterable ( createReadStream ( __filename ) ) , 'async-iterator' )
198
209
199
210
function writeHeadersStartedWithBody ( body , type ) {
200
- test ( `Abort while waiting response (write headers started) (with body ${ type } )` , ( t ) => {
201
- t . plan ( 1 )
211
+ test ( `Abort while waiting response (write headers started) (with body ${ type } )` , async ( t ) => {
212
+ const p = tspl ( t , { plan : 1 } )
202
213
203
214
const ee = new EventEmitter ( )
204
215
const server = createServer ( ( req , res ) => {
@@ -207,16 +218,17 @@ function writeHeadersStartedWithBody (body, type) {
207
218
ee . emit ( 'abort' )
208
219
res . end ( 'hello world' )
209
220
} )
210
- t . teardown ( server . close . bind ( server ) )
221
+ t . after ( server . close . bind ( server ) )
211
222
212
223
server . listen ( 0 , ( ) => {
213
224
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
214
- t . teardown ( client . destroy . bind ( client ) )
225
+ t . after ( client . destroy . bind ( client ) )
215
226
216
227
client . request ( { path : '/' , method : 'POST' , body, signal : ee } , ( err , response ) => {
217
- t . type ( err , errors . RequestAbortedError )
228
+ p . ok ( err instanceof errors . RequestAbortedError )
218
229
} )
219
230
} )
231
+ await p . completed
220
232
} )
221
233
}
222
234
@@ -226,30 +238,31 @@ writeHeadersStartedWithBody(new Uint8Array([42]), 'Uint8Array')
226
238
writeHeadersStartedWithBody ( wrapWithAsyncIterable ( createReadStream ( __filename ) ) , 'async-iterator' )
227
239
228
240
function writeBodyStartedWithBody ( body , type ) {
229
- test ( `Abort while waiting response (write headers and write body started) (with body ${ type } )` , ( t ) => {
230
- t . plan ( 2 )
241
+ test ( `Abort while waiting response (write headers and write body started) (with body ${ type } )` , async ( t ) => {
242
+ const p = tspl ( t , { plan : 2 } )
231
243
232
244
const ee = new EventEmitter ( )
233
245
const server = createServer ( ( req , res ) => {
234
246
res . writeHead ( 200 , { 'content-type' : 'text/plain' } )
235
247
res . write ( 'hello' )
236
248
} )
237
- t . teardown ( server . close . bind ( server ) )
249
+ t . after ( server . close . bind ( server ) )
238
250
239
251
server . listen ( 0 , ( ) => {
240
252
const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
241
- t . teardown ( client . destroy . bind ( client ) )
253
+ t . after ( client . destroy . bind ( client ) )
242
254
243
255
client . request ( { path : '/' , method : 'POST' , body, signal : ee } , ( err , response ) => {
244
- t . error ( err )
256
+ p . ifError ( err )
245
257
response . body . on ( 'data' , ( ) => {
246
258
ee . emit ( 'abort' )
247
259
} )
248
260
response . body . on ( 'error' , err => {
249
- t . type ( err , errors . RequestAbortedError )
261
+ p . ok ( err instanceof errors . RequestAbortedError )
250
262
} )
251
263
} )
252
264
} )
265
+ await p . completed
253
266
} )
254
267
}
255
268
0 commit comments