@@ -160,6 +160,76 @@ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
160160 } )
161161} )
162162
163+ test ( 'resolve canonicalises the host using the final resolved scheme' , ( t ) => {
164+ const cases = [
165+ {
166+ base : 'http://trusted.example/base' ,
167+ relative : '//127\u30020\u30020\u30021/private' ,
168+ expected : 'http://127.0.0.1/private' ,
169+ expectedHost : '127.0.0.1' ,
170+ description : 'scheme-relative loopback host'
171+ } ,
172+ {
173+ base : 'https://ex\u00ADample.com/base' ,
174+ relative : 'child' ,
175+ expected : 'https://example.com/child' ,
176+ expectedHost : 'example.com' ,
177+ description : 'host inherited from the base'
178+ } ,
179+ {
180+ base : 'http://trusted.example/base' ,
181+ relative : 'http://ex\u200Bample.com/' ,
182+ expected : 'http://example.com/' ,
183+ expectedHost : 'example.com' ,
184+ description : 'absolute relative reference'
185+ }
186+ ]
187+
188+ t . plan ( cases . length * 2 )
189+
190+ cases . forEach ( ( { base, relative, expected, expectedHost, description } ) => {
191+ const resolved = fastURI . resolve ( base , relative )
192+ t . equal ( resolved , expected , description )
193+ t . equal ( fastURI . parse ( resolved ) . host , expectedHost , `${ description } reparses consistently` )
194+ } )
195+ } )
196+
197+ test ( 'resolve applies domain canonicalisation only when the effective scheme opts in' , ( t ) => {
198+ const host = 'ex\u00ADample.com'
199+
200+ t . plan ( 2 )
201+ t . equal (
202+ fastURI . resolve ( 'uri://trusted.example/' , `//${ host } /` ) ,
203+ `uri://${ host } /` ,
204+ 'an unsupported scheme preserves the host'
205+ )
206+ t . equal (
207+ fastURI . resolve ( 'http://trusted.example/' , `//${ host } /` , { unicodeSupport : true } ) ,
208+ `http://${ host } /` ,
209+ 'unicodeSupport preserves the Unicode host'
210+ )
211+ } )
212+
213+ test ( 'resolve throws when the final scheme cannot canonicalise the host' , ( t ) => {
214+ const invalidHost = '\u200D.example'
215+ const cases = [
216+ [ 'http://trusted.example/' , `//${ invalidHost } /` ] ,
217+ [ `https://${ invalidHost } /base` , 'child' ] ,
218+ [ 'http://trusted.example/' , `http://${ invalidHost } /` ] ,
219+ [ 'http://trusted.example/' , `//${ invalidHost } %2Etest/` ]
220+ ]
221+
222+ t . plan ( cases . length )
223+
224+ cases . forEach ( ( [ base , relative ] ) => {
225+ t . throws (
226+ ( ) => fastURI . resolve ( base , relative ) ,
227+ / H o s t ' s d o m a i n n a m e c a n n o t b e c o n v e r t e d t o A S C I I / ,
228+ `${ base } + ${ relative } `
229+ )
230+ } )
231+ } )
232+
163233test ( 'parse rejects a literal backslash in the authority as malformed (RFC 3986)' , ( t ) => {
164234 // Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
165235 // syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
0 commit comments