@@ -12,6 +12,7 @@ import {
1212 generateCodeFrame ,
1313 getHash ,
1414 getLocalhostAddressIfDiffersFromDNS ,
15+ getServerUrlByHost ,
1516 injectQuery ,
1617 isFileReadable ,
1718 mergeWithDefaults ,
@@ -21,6 +22,7 @@ import {
2122 resolveHostname ,
2223} from '../utils'
2324import { isWindows } from '../../shared/utils'
25+ import type { CommonServerOptions , ResolvedServerUrls } from '..'
2426
2527describe ( 'bareImportRE' , ( ) => {
2628 test ( 'should work with normal package name' , ( ) => {
@@ -725,3 +727,54 @@ describe('combineSourcemaps', () => {
725727 )
726728 } )
727729} )
730+
731+ describe ( 'getServerUrlByHost' , ( ) => {
732+ const urls : ResolvedServerUrls = {
733+ local : [ 'http://localhost:5173' ] ,
734+ network : [ 'http://foo.example.com:5173' ] ,
735+ }
736+ const cases = [
737+ {
738+ name : 'when host is undefined' ,
739+ urls,
740+ host : undefined ,
741+ expected : 'http://localhost:5173' ,
742+ } ,
743+ {
744+ name : 'when host is true' ,
745+ urls,
746+ host : true ,
747+ expected : 'http://localhost:5173' ,
748+ } ,
749+ {
750+ name : 'when host is explicit string' ,
751+ urls,
752+ host : 'foo.example.com' ,
753+ expected : 'http://foo.example.com:5173' ,
754+ } ,
755+ {
756+ name : 'when host is 0.0.0.0' ,
757+ urls,
758+ host : '0.0.0.0' ,
759+ expected : 'http://localhost:5173' ,
760+ } ,
761+ {
762+ name : 'when host is ::1' ,
763+ urls,
764+ host : '::1' ,
765+ expected : 'http://localhost:5173' ,
766+ } ,
767+ ] satisfies ReadonlyArray < {
768+ name : string
769+ urls : ResolvedServerUrls
770+ host : CommonServerOptions [ 'host' ]
771+ expected : string | undefined
772+ } >
773+
774+ for ( const { name, urls, host, expected } of cases ) {
775+ test ( name , ( ) => {
776+ const actual = getServerUrlByHost ( urls , host )
777+ expect ( actual ) . toBe ( expected )
778+ } )
779+ }
780+ } )
0 commit comments