2021-10-20 19:52:11 +02:00
|
|
|
/* eslint-env jest */
|
|
|
|
import { NextURL } from 'next/dist/server/web/next-url'
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
// TODO Make NextURL extend URL
|
|
|
|
it.skip('has the right shape and prototype', () => {
|
|
|
|
const parsed = new NextURL('/about?param1=value1', 'http://127.0.0.1')
|
2021-10-20 19:52:11 +02:00
|
|
|
expect(parsed).toBeInstanceOf(URL)
|
|
|
|
})
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
it('allows to the pathname', async () => {
|
|
|
|
const parsed = new NextURL('/about?param1=value1', 'http://127.0.0.1:3000')
|
2021-10-20 19:52:11 +02:00
|
|
|
expect(parsed.basePath).toEqual('')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(parsed.hostname).toEqual('localhost')
|
|
|
|
expect(parsed.host).toEqual('localhost:3000')
|
|
|
|
expect(parsed.href).toEqual('http://localhost:3000/about?param1=value1')
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
parsed.pathname = '/hihi'
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(parsed.href).toEqual('http://localhost:3000/hihi?param1=value1')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
it('allows to change the host', () => {
|
|
|
|
const parsed = new NextURL('/about?param1=value1', 'http://127.0.0.1')
|
|
|
|
expect(parsed.hostname).toEqual('localhost')
|
|
|
|
expect(parsed.host).toEqual('localhost')
|
|
|
|
expect(parsed.href).toEqual('http://localhost/about?param1=value1')
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
parsed.hostname = 'foo.com'
|
|
|
|
expect(parsed.hostname).toEqual('foo.com')
|
|
|
|
expect(parsed.host).toEqual('foo.com')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(parsed.href).toEqual('http://foo.com/about?param1=value1')
|
|
|
|
expect(parsed.toString()).toEqual('http://foo.com/about?param1=value1')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
it('does noop changing to an invalid hostname', () => {
|
2021-10-20 19:52:11 +02:00
|
|
|
const url = new NextURL('https://foo.com/example')
|
|
|
|
url.hostname = ''
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('https://foo.com/example')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
it('allows to change the whole href', () => {
|
2021-10-20 19:52:11 +02:00
|
|
|
const url = new NextURL('https://localhost.com/foo')
|
|
|
|
expect(url.hostname).toEqual('localhost.com')
|
|
|
|
expect(url.protocol).toEqual('https:')
|
|
|
|
expect(url.host).toEqual('localhost.com')
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
url.href = 'http://foo.com/bar'
|
|
|
|
expect(url.hostname).toEqual('foo.com')
|
|
|
|
expect(url.protocol).toEqual('http:')
|
|
|
|
expect(url.host).toEqual('foo.com')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('allows to update search params', () => {
|
2021-12-13 19:30:24 +01:00
|
|
|
const url = new NextURL('/example', 'http://localhost.com')
|
2021-10-20 19:52:11 +02:00
|
|
|
url.searchParams.set('foo', 'bar')
|
|
|
|
expect(url.search).toEqual('?foo=bar')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('http://localhost.com/example?foo=bar')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('parses and formats the basePath', () => {
|
|
|
|
const url = new NextURL('/root/example', {
|
2021-12-13 19:30:24 +01:00
|
|
|
base: 'http://127.0.0.1',
|
2021-10-20 19:52:11 +02:00
|
|
|
basePath: '/root',
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(url.basePath).toEqual('/root')
|
|
|
|
expect(url.pathname).toEqual('/example')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('http://localhost/root/example')
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
const url2 = new NextURL('https://foo.com/root/bar', {
|
|
|
|
basePath: '/root',
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(url2.basePath).toEqual('/root')
|
|
|
|
expect(url2.pathname).toEqual('/bar')
|
|
|
|
expect(url2.toString()).toEqual('https://foo.com/root/bar')
|
|
|
|
|
|
|
|
url2.basePath = '/test'
|
|
|
|
expect(url2.basePath).toEqual('/test')
|
|
|
|
expect(url2.pathname).toEqual('/bar')
|
|
|
|
expect(url2.toString()).toEqual('https://foo.com/test/bar')
|
|
|
|
|
|
|
|
const url3 = new NextURL('https://foo.com/example', {
|
|
|
|
basePath: '/root',
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(url3.basePath).toEqual('')
|
|
|
|
|
2021-12-13 19:30:24 +01:00
|
|
|
url3.href = 'http://localhost.com/root/example'
|
|
|
|
expect(url3.basePath).toEqual('/root')
|
|
|
|
expect(url3.pathname).toEqual('/example')
|
|
|
|
expect(url3.toString()).toEqual('http://localhost.com/root/example')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows to get empty locale when there is no locale', () => {
|
|
|
|
const url = new NextURL('https://localhost:3000/foo')
|
|
|
|
expect(url.locale).toEqual('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('doesnt allow to set an unexisting locale', () => {
|
|
|
|
const url = new NextURL('https://localhost:3000/foo')
|
|
|
|
let error: Error | null = null
|
|
|
|
try {
|
|
|
|
url.locale = 'foo'
|
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toBeInstanceOf(TypeError)
|
|
|
|
expect(error.message).toEqual(
|
|
|
|
'The NextURL configuration includes no locale "foo"'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('always get a default locale', () => {
|
|
|
|
const url = new NextURL('/bar', {
|
|
|
|
base: 'http://127.0.0.1',
|
|
|
|
i18n: {
|
|
|
|
defaultLocale: 'en',
|
|
|
|
locales: ['en', 'es', 'fr'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(url.locale).toEqual('en')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('parses and formats the default locale', () => {
|
|
|
|
const url = new NextURL('/es/bar', {
|
2021-12-13 19:30:24 +01:00
|
|
|
base: 'http://127.0.0.1',
|
2021-10-20 19:52:11 +02:00
|
|
|
basePath: '/root',
|
|
|
|
i18n: {
|
|
|
|
defaultLocale: 'en',
|
|
|
|
locales: ['en', 'es', 'fr'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(url.locale).toEqual('es')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('http://localhost/es/bar')
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
url.basePath = '/root'
|
|
|
|
expect(url.locale).toEqual('es')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('http://localhost/root/es/bar')
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
url.locale = 'en'
|
|
|
|
expect(url.locale).toEqual('en')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('http://localhost/root/bar')
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
url.locale = 'fr'
|
|
|
|
expect(url.locale).toEqual('fr')
|
2021-12-13 19:30:24 +01:00
|
|
|
expect(url.toString()).toEqual('http://localhost/root/fr/bar')
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
2021-11-19 22:24:22 +01:00
|
|
|
|
|
|
|
it('consider 127.0.0.1 and variations as localhost', () => {
|
|
|
|
const httpUrl = new NextURL('http://localhost:3000/hello')
|
|
|
|
expect(new NextURL('http://127.0.0.1:3000/hello')).toStrictEqual(httpUrl)
|
|
|
|
expect(new NextURL('http://127.0.1.0:3000/hello')).toStrictEqual(httpUrl)
|
|
|
|
expect(new NextURL('http://::1:3000/hello')).toStrictEqual(httpUrl)
|
|
|
|
|
|
|
|
const httpsUrl = new NextURL('https://localhost:3000/hello')
|
|
|
|
expect(new NextURL('https://127.0.0.1:3000/hello')).toStrictEqual(httpsUrl)
|
|
|
|
expect(new NextURL('https://127.0.1.0:3000/hello')).toStrictEqual(httpsUrl)
|
|
|
|
expect(new NextURL('https://::1:3000/hello')).toStrictEqual(httpsUrl)
|
|
|
|
})
|
2021-12-13 19:30:24 +01:00
|
|
|
|
|
|
|
it('allows to change the port', () => {
|
|
|
|
const url = new NextURL('https://localhost:3000/foo')
|
|
|
|
url.port = '3001'
|
|
|
|
expect(url.href).toEqual('https://localhost:3001/foo')
|
|
|
|
url.port = '80'
|
|
|
|
expect(url.href).toEqual('https://localhost:80/foo')
|
|
|
|
url.port = ''
|
|
|
|
expect(url.href).toEqual('https://localhost/foo')
|
|
|
|
})
|