33const { test } = require ( 'tap' )
44const { promisify } = require ( 'util' )
55const Agent11 = require ( './' )
6- const { kGetKey } = require ( './symbols' )
76
87const sleep = promisify ( setTimeout )
98
@@ -41,89 +40,121 @@ test('invalid options', t => {
4140 t . end ( )
4241} )
4342
44- test ( 'kGetKey from url and options' , t => {
43+ test ( 'Agent11.urlToObject' , t => {
44+ let obj = Agent11 . urlToObject ( 'http://example.com:3444/some/path?q=1' )
45+ t . same ( obj , {
46+ protocol : 'http:' ,
47+ hostname : 'example.com' ,
48+ port : '3444'
49+ } )
50+ obj = Agent11 . urlToObject ( new URL ( 'http://example.com:3444/some/path?q=1' ) )
51+ t . same ( obj , {
52+ protocol : 'http:' ,
53+ hostname : 'example.com' ,
54+ port : '3444'
55+ } )
56+ obj = Agent11 . urlToObject ( 'https://example.com/some/path?q=1' )
57+ t . same ( obj , {
58+ protocol : 'https:' ,
59+ hostname : 'example.com' ,
60+ port : undefined
61+ } )
62+ obj = Agent11 . urlToObject ( 'example.com/some/path?q=1' )
63+ t . same ( obj , {
64+ protocol : undefined ,
65+ hostname : 'example.com' ,
66+ port : undefined
67+ } )
68+ t . end ( )
69+ } )
70+
71+ test ( 'Agent11.getKey from url and options' , t => {
72+ // TODO: convert this list in sequential manual tests, lists are harder to debug.
4573 const list = [
4674 {
4775 opts : [
48- new URL ( 'http://localhost:3333' )
76+ {
77+ protocol : 'http:' ,
78+ hostname : 'example.com' ,
79+ port : '3000'
80+ }
4981 ] ,
50- expected : 'http:localhost:3333 '
82+ expected : 'http:example.com:3000 '
5183 } ,
5284 {
5385 opts : [
54- { hostname : 'localhost' }
86+ {
87+ protocol : 'http:' ,
88+ hostname : 'example.com' ,
89+ port : 3000
90+ }
5591 ] ,
56- expected : 'http:localhost '
92+ expected : 'http:example.com:3000 '
5793 } ,
5894 {
5995 opts : [
6096 {
61- protocol : 'http' ,
62- hostname : 'localhost'
97+ protocol : 'http:' ,
98+ hostname : 'example.com' ,
99+ port : 0
63100 }
64101 ] ,
65- expected : 'http:localhost '
102+ expected : 'http:example.com:0 '
66103 } ,
67104 {
68105 opts : [
69- new URL ( 'https://localhost:3400' ) ,
70- {
71- socketPath : '/tmp/agent-11/agent.sock'
72- }
106+ { hostname : 'example.com' }
73107 ] ,
74- expected : 'https:localhost:3400:/tmp/agent-11/agent.sock '
108+ expected : 'http:example.com '
75109 } ,
76110 {
77111 opts : [
78- ' example.com/1/2/3?some=false'
112+ new URL ( 'http:// example.com' )
79113 ] ,
80114 expected : 'http:example.com'
81115 } ,
82116 {
83117 opts : [
84- 'https://example.some.com/1/2/3?some=false' ,
118+ {
119+ protocol : 'https:' ,
120+ hostname : 'example.com' ,
121+ port : 3400
122+ } ,
85123 {
86124 socketPath : '/tmp/agent-11/agent.sock'
87125 }
88126 ] ,
89- expected : 'https:example.some. com:/tmp/agent-11/agent.sock'
127+ expected : 'https:example.com:3400 :/tmp/agent-11/agent.sock'
90128 } ,
91129 {
92130 opts : [
93- 'localhost:3000/1/2/3?some=false'
94- ] ,
95- expected : 'http:localhost:3000'
96- } ,
97- {
98- opts : [
99- 'https://localhost:3000/1/2/3?some=false' ,
131+ new URL ( 'https://example.com:3400' ) ,
100132 {
101133 socketPath : '/tmp/agent-11/agent.sock'
102134 }
103135 ] ,
104- expected : 'https:localhost:3000 :/tmp/agent-11/agent.sock'
136+ expected : 'https:example.com:3400 :/tmp/agent-11/agent.sock'
105137 } ,
106138 {
107139 opts : [
108140 {
109- hostname : 'some.com' ,
110- port : 0
141+ protocol : 'http' ,
142+ hostname : 'example.com' ,
143+ pathname : '/some/path?q=1'
111144 }
112145 ] ,
113- expected : 'http:some .com:0 '
146+ expected : 'http:example .com'
114147 } ,
115148 {
116149 opts : [
117- 'some .com:0'
150+ new URL ( 'http://example .com/some/path?q=1' )
118151 ] ,
119- expected : 'http:some .com:0 '
152+ expected : 'http:example .com'
120153 }
121154 ]
122155
123- const agent = new Agent11 ( )
124-
125156 for ( const [ index , item ] of list . entries ( ) ) {
126- const key = agent [ kGetKey ] ( ...item . opts )
157+ const key = Agent11 . getKey ( ...item . opts )
127158 t . is ( key , item . expected , `list item ${ index } ` )
128159 }
129160 t . end ( )
@@ -169,6 +200,24 @@ test('getConnection with a unix socket', t => {
169200 t . end ( )
170201} )
171202
203+ test ( 'getConnection should return the same pool' , t => {
204+ const agent = new Agent11 ( )
205+ t . teardown ( ( ) => agent . close ( ) )
206+ const string = 'http://xyz.xyz/some/path?q=1'
207+ const obj = {
208+ protocol : 'http:' ,
209+ hostname : 'xyz.xyz'
210+ }
211+ const url = new URL ( 'http://xyz.xyz/some/other/path?q=2' )
212+ const options = {
213+ socketPath : '/tmp/agent-11/agent.sock'
214+ }
215+ const pool = agent . getConnection ( string , options )
216+ t . is ( pool , agent . getConnection ( obj , options ) )
217+ t . is ( pool , agent . getConnection ( url , options ) )
218+ t . end ( )
219+ } )
220+
172221test ( 'getConnection should error if max hosts is reached' , t => {
173222 const agent = new Agent11 ( { maxHosts : 1 } )
174223 t . teardown ( ( ) => agent . close ( ) )
@@ -184,9 +233,9 @@ test('getConnection should error if the url is invalid', t => {
184233 const agent = new Agent11 ( )
185234 t . teardown ( ( ) => agent . close ( ) )
186235 let error = t . throws ( ( ) => agent . getConnection ( null ) )
187- t . is ( error . message , 'Can\'t get key from url: \' null\' ' )
236+ t . is ( error . message , 'Invalid url, received: null' )
188237 error = t . throws ( ( ) => agent . getConnection ( '' ) )
189- t . is ( error . message , 'Invalid URL : ' )
238+ t . is ( error . message , 'Invalid url, received : ' )
190239 error = t . throws ( ( ) => agent . getConnection ( { } ) )
191240 t . is ( error . message , 'invalid protocol' )
192241 t . end ( )
0 commit comments