Skip to content

Commit

Permalink
fix: allow localhost as a valid hostname (#7092)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkishi committed Mar 5, 2022
1 parent 9f7b859 commit 4194cce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
83 changes: 53 additions & 30 deletions packages/vite/src/node/__tests__/utils.spec.ts
@@ -1,42 +1,65 @@
import { getPotentialTsSrcPaths, injectQuery, isWindows } from '../utils'
import {
getPotentialTsSrcPaths,
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'
})
})
})

test('ts import of file with .js extension', () => {
Expand Down
6 changes: 1 addition & 5 deletions packages/vite/src/node/utils.ts
Expand Up @@ -638,11 +638,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 4194cce

Please sign in to comment.