@@ -6,277 +6,7 @@ const http = require('http')
66const EE = require ( 'events' )
77const { kBusy } = require ( '../lib/core/symbols' )
88
9- test ( 'basic connect' , ( t ) => {
10- t . plan ( 3 )
11-
12- const server = http . createServer ( ( c ) => {
13- t . fail ( )
14- } )
15- server . on ( 'connect' , ( req , socket , firstBodyChunk ) => {
16- socket . write ( 'HTTP/1.1 200 Connection established\r\n\r\n' )
17-
18- let data = firstBodyChunk . toString ( )
19- socket . on ( 'data' , ( buf ) => {
20- data += buf . toString ( )
21- } )
22-
23- socket . on ( 'end' , ( ) => {
24- socket . end ( data )
25- } )
26- } )
27- t . teardown ( server . close . bind ( server ) )
28-
29- server . listen ( 0 , async ( ) => {
30- const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
31- t . teardown ( client . close . bind ( client ) )
32-
33- const signal = new EE ( )
34- const promise = client . connect ( {
35- signal,
36- path : '/'
37- } )
38- t . equal ( signal . listenerCount ( 'abort' ) , 1 )
39- const { socket } = await promise
40- t . equal ( signal . listenerCount ( 'abort' ) , 0 )
41-
42- let recvData = ''
43- socket . on ( 'data' , ( d ) => {
44- recvData += d
45- } )
46-
47- socket . on ( 'end' , ( ) => {
48- t . equal ( recvData . toString ( ) , 'Body' )
49- } )
50-
51- socket . write ( 'Body' )
52- socket . end ( )
53- } )
54- } )
55-
56- test ( 'connect error' , ( t ) => {
57- t . plan ( 1 )
58-
59- const server = http . createServer ( ( c ) => {
60- t . fail ( )
61- } )
62- server . on ( 'connect' , ( req , socket , firstBodyChunk ) => {
63- socket . destroy ( )
64- } )
65- t . teardown ( server . close . bind ( server ) )
66-
67- server . listen ( 0 , async ( ) => {
68- const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
69- t . teardown ( client . close . bind ( client ) )
70-
71- try {
72- await client . connect ( {
73- path : '/'
74- } )
75- } catch ( err ) {
76- t . ok ( err )
77- }
78- } )
79- } )
80-
81- test ( 'connect invalid opts' , ( t ) => {
82- t . plan ( 6 )
83-
84- const client = new Client ( 'http://localhost:5432' )
85-
86- client . connect ( null , err => {
87- t . type ( err , errors . InvalidArgumentError )
88- t . equal ( err . message , 'invalid opts' )
89- } )
90-
91- try {
92- client . connect ( null , null )
93- t . fail ( )
94- } catch ( err ) {
95- t . type ( err , errors . InvalidArgumentError )
96- t . equal ( err . message , 'invalid opts' )
97- }
98-
99- try {
100- client . connect ( { path : '/' } , null )
101- t . fail ( )
102- } catch ( err ) {
103- t . type ( err , errors . InvalidArgumentError )
104- t . equal ( err . message , 'invalid callback' )
105- }
106- } )
107-
108- test ( 'connect wait for empty pipeline' , ( t ) => {
109- t . plan ( 7 )
110-
111- let canConnect = false
112- const server = http . createServer ( ( req , res ) => {
113- res . end ( )
114- canConnect = true
115- } )
116- server . on ( 'connect' , ( req , socket , firstBodyChunk ) => {
117- t . equal ( canConnect , true )
118- socket . write ( 'HTTP/1.1 200 Connection established\r\n\r\n' )
119-
120- let data = firstBodyChunk . toString ( )
121- socket . on ( 'data' , ( buf ) => {
122- data += buf . toString ( )
123- } )
124-
125- socket . on ( 'end' , ( ) => {
126- socket . end ( data )
127- } )
128- } )
129- t . teardown ( server . close . bind ( server ) )
130-
131- server . listen ( 0 , async ( ) => {
132- const client = new Client ( `http://localhost:${ server . address ( ) . port } ` , {
133- pipelining : 3
134- } )
135- t . teardown ( client . close . bind ( client ) )
136-
137- client . request ( {
138- path : '/' ,
139- method : 'GET'
140- } , ( err ) => {
141- t . error ( err )
142- } )
143- client . once ( 'connect' , ( ) => {
144- process . nextTick ( ( ) => {
145- t . equal ( client [ kBusy ] , false )
146-
147- client . connect ( {
148- path : '/'
149- } , ( err , { socket } ) => {
150- t . error ( err )
151- let recvData = ''
152- socket . on ( 'data' , ( d ) => {
153- recvData += d
154- } )
155-
156- socket . on ( 'end' , ( ) => {
157- t . equal ( recvData . toString ( ) , 'Body' )
158- } )
159-
160- socket . write ( 'Body' )
161- socket . end ( )
162- } )
163- t . equal ( client [ kBusy ] , true )
164-
165- client . request ( {
166- path : '/' ,
167- method : 'GET'
168- } , ( err ) => {
169- t . error ( err )
170- } )
171- } )
172- } )
173- } )
174- } )
175-
176- test ( 'connect aborted' , ( t ) => {
177- t . plan ( 6 )
178-
179- const server = http . createServer ( ( req , res ) => {
180- t . fail ( )
181- } )
182- server . on ( 'connect' , ( req , c , firstBodyChunk ) => {
183- t . fail ( )
184- } )
185- t . teardown ( server . close . bind ( server ) )
186-
187- server . listen ( 0 , ( ) => {
188- const client = new Client ( `http://localhost:${ server . address ( ) . port } ` , {
189- pipelining : 3
190- } )
191- t . teardown ( client . destroy . bind ( client ) )
192-
193- const signal = new EE ( )
194- client . connect ( {
195- path : '/' ,
196- signal,
197- opaque : 'asd'
198- } , ( err , { opaque } ) => {
199- t . equal ( opaque , 'asd' )
200- t . equal ( signal . listenerCount ( 'abort' ) , 0 )
201- t . type ( err , errors . RequestAbortedError )
202- } )
203- t . equal ( client [ kBusy ] , true )
204- t . equal ( signal . listenerCount ( 'abort' ) , 1 )
205- signal . emit ( 'abort' )
206-
207- client . close ( ( ) => {
208- t . pass ( )
209- } )
210- } )
211- } )
212-
213- test ( 'basic connect error' , ( t ) => {
214- t . plan ( 2 )
215-
216- const server = http . createServer ( ( c ) => {
217- t . fail ( )
218- } )
219- server . on ( 'connect' , ( req , socket , firstBodyChunk ) => {
220- socket . write ( 'HTTP/1.1 200 Connection established\r\n\r\n' )
221-
222- let data = firstBodyChunk . toString ( )
223- socket . on ( 'data' , ( buf ) => {
224- data += buf . toString ( )
225- } )
226-
227- socket . on ( 'end' , ( ) => {
228- socket . end ( data )
229- } )
230- } )
231- t . teardown ( server . close . bind ( server ) )
232-
233- server . listen ( 0 , async ( ) => {
234- const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
235- t . teardown ( client . close . bind ( client ) )
236-
237- const _err = new Error ( )
238- client . connect ( {
239- path : '/'
240- } , ( err , { socket } ) => {
241- t . error ( err )
242- socket . on ( 'error' , ( err ) => {
243- t . equal ( err , _err )
244- } )
245- throw _err
246- } )
247- } )
248- } )
249-
250- test ( 'connect invalid signal' , ( t ) => {
251- t . plan ( 2 )
252-
253- const server = http . createServer ( ( req , res ) => {
254- t . fail ( )
255- } )
256- server . on ( 'connect' , ( req , c , firstBodyChunk ) => {
257- t . fail ( )
258- } )
259- t . teardown ( server . close . bind ( server ) )
260-
261- server . listen ( 0 , ( ) => {
262- const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
263- t . teardown ( client . destroy . bind ( client ) )
264-
265- client . on ( 'disconnect' , ( ) => {
266- t . fail ( )
267- } )
268-
269- client . connect ( {
270- path : '/' ,
271- signal : 'error' ,
272- opaque : 'asd'
273- } , ( err , { opaque } ) => {
274- t . equal ( opaque , 'asd' )
275- t . type ( err , errors . InvalidArgumentError )
276- } )
277- } )
278- } )
279-
9+ // TODO: move to test/node-test/client-connect.js
28010test ( 'connect aborted after connect' , ( t ) => {
28111 t . plan ( 3 )
28212
0 commit comments