11import { describe , expect , it } from 'vitest'
2- import { normalizeHttpServerUrl } from '../utils'
2+ import { formatHostForUrl , normalizeHttpServerUrl , toDialableHost } from '../utils'
33
44describe ( 'normalizeHttpServerUrl' , ( ) => {
55 it ( 'formats ipv4 localhost as localhost' , ( ) => {
@@ -13,4 +13,35 @@ describe('normalizeHttpServerUrl', () => {
1313 it ( 'preserves non-ip hosts' , ( ) => {
1414 expect ( normalizeHttpServerUrl ( 'localhost' , 9999 ) ) . toBe ( 'http://localhost:9999' )
1515 } )
16+
17+ it ( 'maps the ipv4 wildcard bind host to a dialable loopback host' , ( ) => {
18+ expect ( normalizeHttpServerUrl ( '0.0.0.0' , 9710 ) ) . toBe ( 'http://localhost:9710' )
19+ } )
20+
21+ it ( 'maps the ipv6 wildcard bind host to a dialable loopback host' , ( ) => {
22+ expect ( normalizeHttpServerUrl ( '::' , 9710 ) ) . toBe ( 'http://localhost:9710' )
23+ } )
24+ } )
25+
26+ describe ( 'toDialableHost' , ( ) => {
27+ it ( 'rewrites wildcard and loopback bind hosts to localhost' , ( ) => {
28+ expect ( toDialableHost ( '0.0.0.0' ) ) . toBe ( 'localhost' )
29+ expect ( toDialableHost ( '::' ) ) . toBe ( 'localhost' )
30+ expect ( toDialableHost ( '127.0.0.1' ) ) . toBe ( 'localhost' )
31+ expect ( toDialableHost ( '' ) ) . toBe ( 'localhost' )
32+ } )
33+
34+ it ( 'preserves routable hosts' , ( ) => {
35+ expect ( toDialableHost ( 'example.com' ) ) . toBe ( 'example.com' )
36+ expect ( toDialableHost ( '192.168.1.10' ) ) . toBe ( '192.168.1.10' )
37+ expect ( toDialableHost ( '::1' ) ) . toBe ( '::1' )
38+ } )
39+ } )
40+
41+ describe ( 'formatHostForUrl' , ( ) => {
42+ it ( 'brackets ipv6 but leaves ipv4 / hostnames bare' , ( ) => {
43+ expect ( formatHostForUrl ( '::1' ) ) . toBe ( '[::1]' )
44+ expect ( formatHostForUrl ( 'example.com' ) ) . toBe ( 'example.com' )
45+ expect ( formatHostForUrl ( '0.0.0.0' ) ) . toBe ( 'localhost' )
46+ } )
1647} )
0 commit comments