Skip to content

Commit

Permalink
fix: allow localhost as a valid hostname (#7075)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkishi committed Feb 25, 2022
1 parent c703a33 commit ee658d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
78 changes: 48 additions & 30 deletions packages/vite/src/node/__tests__/utils.spec.ts
@@ -1,40 +1,58 @@
import { injectQuery, isWindows } from '../utils'
import { injectQuery, isWindows, resolveHostname } from '../utils'

if (isWindows) {
// this test will work incorrectly on unix systems
test('normalize windows path', () => {
expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual(
'C:/User/Vite/Project?direct'
describe('injectQuery', () => {
if (isWindows) {
// this test will work incorrectly on unix systems
test('normalize windows path', () => {
expect(injectQuery('C:\\User\\Vite\\Project', 'direct')).toEqual(
'C:/User/Vite/Project?direct'
)
})
}

test('path with multiple spaces', () => {
expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual(
'/usr/vite/path with space?direct'
)
})
}

test('path with multiple spaces', () => {
expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual(
'/usr/vite/path with space?direct'
)
})
test('path with multiple % characters', () => {
expect(injectQuery('/usr/vite/not%20a%20space', 'direct')).toEqual(
'/usr/vite/not%20a%20space?direct'
)
})

test('path with multiple % characters', () => {
expect(injectQuery('/usr/vite/not%20a%20space', 'direct')).toEqual(
'/usr/vite/not%20a%20space?direct'
)
})
test('path with %25', () => {
expect(injectQuery('/usr/vite/%25hello%25', 'direct')).toEqual(
'/usr/vite/%25hello%25?direct'
)
})

test('path with %25', () => {
expect(injectQuery('/usr/vite/%25hello%25', 'direct')).toEqual(
'/usr/vite/%25hello%25?direct'
)
})
test('path with unicode', () => {
expect(injectQuery('/usr/vite/東京', 'direct')).toEqual(
'/usr/vite/東京?direct'
)
})

test('path with unicode', () => {
expect(injectQuery('/usr/vite/東京', 'direct')).toEqual(
'/usr/vite/東京?direct'
)
test('path with unicode, space, and %', () => {
expect(injectQuery('/usr/vite/東京 %20 hello', 'direct')).toEqual(
'/usr/vite/東京 %20 hello?direct'
)
})
})

test('path with unicode, space, and %', () => {
expect(injectQuery('/usr/vite/東京 %20 hello', 'direct')).toEqual(
'/usr/vite/東京 %20 hello?direct'
)
describe('resolveHostname', () => {
test('defaults to 127.0.0.1', () => {
expect(resolveHostname(undefined)).toEqual({
host: '127.0.0.1',
name: 'localhost'
})
})

test('accepts localhost', () => {
expect(resolveHostname('localhost')).toEqual({
host: 'localhost',
name: 'localhost'
})
})
})
6 changes: 1 addition & 5 deletions packages/vite/src/node/utils.ts
Expand Up @@ -584,11 +584,7 @@ export function resolveHostname(
optionsHost: string | boolean | undefined
): Hostname {
let host: string | undefined
if (
optionsHost === undefined ||
optionsHost === false ||
optionsHost === 'localhost'
) {
if (optionsHost === undefined || optionsHost === false) {
// Use a secure default
host = '127.0.0.1'
} else if (optionsHost === true) {
Expand Down

0 comments on commit ee658d0

Please sign in to comment.