@@ -5,9 +5,11 @@ import { afterEach, describe, expect, test, vi } from 'vitest'
55import type { InlineConfig , PluginOption } from '..'
66import type { UserConfig , UserConfigExport } from '../config'
77import { defineConfig , loadConfigFromFile , resolveConfig } from '../config'
8+ import { resolveServerOptions } from '../server'
89import { resolveEnvPrefix } from '../env'
910import { hasBothRollupOptionsAndRolldownOptions , mergeConfig } from '../utils'
1011import { createLogger } from '../logger'
12+ import type { Logger } from '../logger'
1113
1214describe ( 'mergeConfig' , ( ) => {
1315 test ( 'handles configs with different alias schemas' , ( ) => {
@@ -1417,3 +1419,106 @@ describe('loadConfigFromFile', () => {
14171419 } )
14181420 } )
14191421} )
1422+
1423+ describe ( 'resolveServerOptions' , ( ) => {
1424+ const warnFn = vi . fn ( )
1425+ const logger = { warn : warnFn } as unknown as Logger
1426+
1427+ afterEach ( ( ) => {
1428+ delete process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS
1429+ } )
1430+
1431+ test ( 'adds single host from __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS' , async ( ) => {
1432+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = 'example.com'
1433+ const resolved = await resolveServerOptions (
1434+ '/root' ,
1435+ { allowedHosts : [ ] } ,
1436+ logger ,
1437+ )
1438+ expect ( resolved . allowedHosts ) . toEqual ( [ 'example.com' ] )
1439+ } )
1440+
1441+ test ( 'adds multiple hosts from __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS' , async ( ) => {
1442+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS =
1443+ 'example.com,test.com,dev.example.org'
1444+ const resolved = await resolveServerOptions (
1445+ '/root' ,
1446+ { allowedHosts : [ ] } ,
1447+ logger ,
1448+ )
1449+ expect ( resolved . allowedHosts ) . toEqual ( [
1450+ 'example.com' ,
1451+ 'test.com' ,
1452+ 'dev.example.org' ,
1453+ ] )
1454+ } )
1455+
1456+ test ( 'trims whitespace from hosts in __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS' , async ( ) => {
1457+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS =
1458+ ' example.com , test.com , dev.example.org '
1459+ const resolved = await resolveServerOptions (
1460+ '/root' ,
1461+ { allowedHosts : [ ] } ,
1462+ logger ,
1463+ )
1464+ expect ( resolved . allowedHosts ) . toEqual ( [
1465+ 'example.com' ,
1466+ 'test.com' ,
1467+ 'dev.example.org' ,
1468+ ] )
1469+ } )
1470+
1471+ test ( 'filters empty hosts from __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS' , async ( ) => {
1472+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS =
1473+ 'example.com,,test.com,,'
1474+ const resolved = await resolveServerOptions (
1475+ '/root' ,
1476+ { allowedHosts : [ ] } ,
1477+ logger ,
1478+ )
1479+ expect ( resolved . allowedHosts ) . toEqual ( [ 'example.com' , 'test.com' ] )
1480+ } )
1481+
1482+ test ( 'appends to existing allowedHosts' , async ( ) => {
1483+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = 'new.com,another.com'
1484+ const resolved = await resolveServerOptions (
1485+ '/root' ,
1486+ { allowedHosts : [ 'existing.com' ] } ,
1487+ logger ,
1488+ )
1489+ expect ( resolved . allowedHosts ) . toEqual ( [
1490+ 'existing.com' ,
1491+ 'new.com' ,
1492+ 'another.com' ,
1493+ ] )
1494+ } )
1495+
1496+ test ( 'does not modify allowedHosts when set to true' , async ( ) => {
1497+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = 'example.com'
1498+ const resolved = await resolveServerOptions (
1499+ '/root' ,
1500+ { allowedHosts : true } ,
1501+ logger ,
1502+ )
1503+ expect ( resolved . allowedHosts ) . toBe ( true )
1504+ } )
1505+
1506+ test ( 'throw an error if it contains `"` or `\'` or `\\`' , async ( ) => {
1507+ const envs = [ '"example.com"' , "'example.com'" , '\\example.com' ]
1508+ for ( const env of envs ) {
1509+ process . env . __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS = env
1510+ const resolved = await resolveServerOptions (
1511+ '/root' ,
1512+ { allowedHosts : [ ] } ,
1513+ logger ,
1514+ )
1515+ expect ( resolved . allowedHosts ) . toEqual ( [ ] )
1516+ expect ( warnFn ) . toHaveBeenCalledWith (
1517+ expect . stringContaining (
1518+ 'Skipping additional allowed hosts from environment variable due to reserved characters' ,
1519+ ) ,
1520+ )
1521+ warnFn . mockClear ( )
1522+ }
1523+ } )
1524+ } )
0 commit comments