Unverified Commit 93cf074e authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[test] Move off of as much `url.parse` as possible (#87286)

`url.parse` is deprecated in favor of WHATWG URLs. 

Almost all tests don't actually test integration with `url.parse`.

The remaining places test integration with the Next.js request handler
which does accept `url.parse` values.

We may deprecate that specific signature in favor of accepting WHATWG
URLs instead.
parent 355694cb
import url from 'url'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
...@@ -69,7 +68,7 @@ describe.each([[''], ['/docs']])( ...@@ -69,7 +68,7 @@ describe.each([[''], ['/docs']])(
} }
) )
const { pathname, hostname } = url.parse( const { pathname, hostname } = new URL(
res.headers.get('location') || '' res.headers.get('location') || ''
) )
expect(res.status).toBe(308) expect(res.status).toBe(308)
......
...@@ -6,7 +6,6 @@ import { fetchViaHTTP, getDistDir, renderViaHTTP } from 'next-test-utils' ...@@ -6,7 +6,6 @@ import { fetchViaHTTP, getDistDir, renderViaHTTP } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from 'next/constants' import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from 'next/constants'
import path from 'path' import path from 'path'
import url from 'url'
const isReact18 = parseInt(process.env.NEXT_TEST_REACT_VERSION) === 18 const isReact18 = parseInt(process.env.NEXT_TEST_REACT_VERSION) === 18
...@@ -47,7 +46,7 @@ describe('Client Navigation rendering', () => { ...@@ -47,7 +46,7 @@ describe('Client Navigation rendering', () => {
it('should should not contain scripts that are not js', async () => { it('should should not contain scripts that are not js', async () => {
const $ = await get$('/') const $ = await get$('/')
$('script[src]').each((_index, element) => { $('script[src]').each((_index, element) => {
const parsedUrl = url.parse($(element).attr('src')) const parsedUrl = new URL($(element).attr('src'), next.url)
if (!parsedUrl.pathname.endsWith('.js')) { if (!parsedUrl.pathname.endsWith('.js')) {
throw new Error( throw new Error(
`Page includes script that is not a javascript file ${parsedUrl.pathname}` `Page includes script that is not a javascript file ${parsedUrl.pathname}`
......
import { findPort, retry } from 'next-test-utils' import { findPort, retry } from 'next-test-utils'
import http from 'http' import http from 'http'
import url from 'url'
import { outdent } from 'outdent' import { outdent } from 'outdent'
import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils' import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
...@@ -15,8 +14,11 @@ describe('app-fetch-deduping', () => { ...@@ -15,8 +14,11 @@ describe('app-fetch-deduping', () => {
beforeAll(async () => { beforeAll(async () => {
externalServerPort = await findPort() externalServerPort = await findPort()
externalServer = http.createServer((req, res) => { externalServer = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true) const parsedUrl = new URL(
const overrideStatus = parsedUrl.query.status req.url,
`http://localhost:${externalServerPort}`
)
const overrideStatus = parsedUrl.searchParams.get('status')
// if the requested url has a "status" search param, override the response status // if the requested url has a "status" search param, override the response status
if (overrideStatus) { if (overrideStatus) {
......
import url from 'url'
import assert from 'assert' import assert from 'assert'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
...@@ -456,14 +455,14 @@ describe('basePath', () => { ...@@ -456,14 +455,14 @@ describe('basePath', () => {
it('should have correct href for a link', async () => { it('should have correct href for a link', async () => {
const browser = await webdriver(next.url, `${basePath}/hello`) const browser = await webdriver(next.url, `${basePath}/hello`)
const href = await browser.elementByCss('a').getAttribute('href') const href = await browser.elementByCss('a').getAttribute('href')
const { pathname } = url.parse(href) const { pathname } = new URL(href, await browser.url())
expect(pathname).toBe(`${basePath}/other-page`) expect(pathname).toBe(`${basePath}/other-page`)
}) })
it('should have correct href for a link to /', async () => { it('should have correct href for a link to /', async () => {
const browser = await webdriver(next.url, `${basePath}/link-to-root`) const browser = await webdriver(next.url, `${basePath}/link-to-root`)
const href = await browser.elementByCss('#link-back').getAttribute('href') const href = await browser.elementByCss('#link-back').getAttribute('href')
const { pathname } = url.parse(href) const { pathname } = new URL(href, await browser.url())
expect(pathname).toBe(`${basePath}`) expect(pathname).toBe(`${basePath}`)
}) })
......
import url from 'url'
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
...@@ -110,7 +109,7 @@ describe('basePath', () => { ...@@ -110,7 +109,7 @@ describe('basePath', () => {
redirect: 'manual', redirect: 'manual',
} }
) )
const { pathname } = url.parse(res.headers.get('location') || '') const { pathname } = new URL(res.headers.get('location') || '')
expect(pathname).toBe(`${basePath}/somewhere-else`) expect(pathname).toBe(`${basePath}/somewhere-else`)
expect(res.status).toBe(307) expect(res.status).toBe(307)
const text = await res.text() const text = await res.text()
...@@ -144,7 +143,7 @@ describe('basePath', () => { ...@@ -144,7 +143,7 @@ describe('basePath', () => {
redirect: 'manual', redirect: 'manual',
} }
) )
const { pathname } = url.parse(res.headers.get('location') || '') const { pathname } = new URL(res.headers.get('location') || '')
expect(pathname).toBe('/another-destination') expect(pathname).toBe('/another-destination')
expect(res.status).toBe(307) expect(res.status).toBe(307)
const text = await res.text() const text = await res.text()
......
...@@ -8,7 +8,6 @@ import { ...@@ -8,7 +8,6 @@ import {
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import url from 'url'
let app let app
let appPort let appPort
...@@ -22,7 +21,7 @@ const runTests = () => { ...@@ -22,7 +21,7 @@ const runTests = () => {
await browser.waitForElementByCss('#url') await browser.waitForElementByCss('#url')
const dataUrl = await browser.elementByCss('#url').text() const dataUrl = await browser.elementByCss('#url').text()
const { pathname } = url.parse(dataUrl) const { pathname } = new URL(dataUrl, await browser.url())
expect(pathname).toBe(`/_next/data/${buildId}/root/catch-all.json`) expect(pathname).toBe(`/_next/data/${buildId}/root/catch-all.json`)
}) })
} }
......
/* eslint-env jest */ /* eslint-env jest */
import url from 'url'
import http from 'http' import http from 'http'
import { join } from 'path' import { join } from 'path'
import { import {
...@@ -35,9 +34,9 @@ const runTests = () => { ...@@ -35,9 +34,9 @@ const runTests = () => {
const text = await res.text() const text = await res.text()
expect(text).toEqual(dest) expect(text).toEqual(dest)
if (dest.startsWith('/')) { if (dest.startsWith('/')) {
const parsed = url.parse(res.headers.get('location')) const parsed = new URL(res.headers.get('location'))
expect(parsed.pathname).toBe(dest) expect(parsed.pathname).toBe(dest)
expect(parsed.query).toBe(null) expect(parsed.search).toBe('')
} else { } else {
expect(res.headers.get('location')).toBe(dest) expect(res.headers.get('location')).toBe(dest)
} }
......
/* eslint-env jest */ /* eslint-env jest */
import url from 'url'
import http from 'http' import http from 'http'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
...@@ -42,9 +41,9 @@ const runTests = () => { ...@@ -42,9 +41,9 @@ const runTests = () => {
const text = await res.text() const text = await res.text()
expect(text).toEqual(dest) expect(text).toEqual(dest)
if (dest.startsWith('/')) { if (dest.startsWith('/')) {
const parsed = url.parse(res.headers.get('location')) const parsed = new URL(res.headers.get('location'))
expect(parsed.pathname).toBe(dest) expect(parsed.pathname).toBe(dest)
expect(parsed.query).toBe(null) expect(parsed.search).toBe('')
} else { } else {
expect(res.headers.get('location')).toBe(dest) expect(res.headers.get('location')).toBe(dest)
} }
......
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import webdriver, { type Playwright } from 'next-webdriver'
import { join, dirname } from 'path' import { join, dirname } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import url from 'url'
import { import {
waitForRedbox, waitForRedbox,
renderViaHTTP, renderViaHTTP,
...@@ -234,12 +233,14 @@ function runTests({ dev }) { ...@@ -234,12 +233,14 @@ function runTests({ dev }) {
]) { ]) {
const { id, pathname, query, hash, navQuery } = expectedValues const { id, pathname, query, hash, navQuery } = expectedValues
const parsedHref = url.parse( const parsedHref = new URL(
await browser.elementByCss(`#${id}`).getAttribute('href'), await browser.elementByCss(`#${id}`).getAttribute('href'),
true await browser.url()
) )
expect(parsedHref.pathname).toBe(pathname) expect(parsedHref.pathname).toBe(pathname)
expect(parsedHref.query || {}).toEqual(query) expect(Object.fromEntries(parsedHref.searchParams.entries())).toEqual(
query
)
expect(parsedHref.hash || '').toBe(hash) expect(parsedHref.hash || '').toBe(hash)
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
...@@ -262,25 +263,25 @@ function runTests({ dev }) { ...@@ -262,25 +263,25 @@ function runTests({ dev }) {
it('should handle only hash on dynamic route', async () => { it('should handle only hash on dynamic route', async () => {
const browser = await webdriver(appPort, '/post-1') const browser = await webdriver(appPort, '/post-1')
const parsedHref = url.parse( const parsedHref = new URL(
await browser await browser
.elementByCss('#dynamic-route-only-hash') .elementByCss('#dynamic-route-only-hash')
.getAttribute('href'), .getAttribute('href'),
true await browser.url()
) )
expect(parsedHref.pathname).toBe('/post-1') expect(parsedHref.pathname).toBe('/post-1')
expect(parsedHref.hash).toBe('#only-hash') expect(parsedHref.hash).toBe('#only-hash')
expect(parsedHref.query || {}).toEqual({}) expect(Object.fromEntries(parsedHref.searchParams.entries())).toEqual({})
const parsedHref2 = url.parse( const parsedHref2 = new URL(
await browser await browser
.elementByCss('#dynamic-route-only-hash-obj') .elementByCss('#dynamic-route-only-hash-obj')
.getAttribute('href'), .getAttribute('href'),
true await browser.url()
) )
expect(parsedHref2.pathname).toBe('/post-1') expect(parsedHref2.pathname).toBe('/post-1')
expect(parsedHref2.hash).toBe('#only-hash-obj') expect(parsedHref2.hash).toBe('#only-hash-obj')
expect(parsedHref2.query || {}).toEqual({}) expect(Object.fromEntries(parsedHref2.searchParams.entries())).toEqual({})
expect(await browser.eval('window.location.hash')).toBe('') expect(await browser.eval('window.location.hash')).toBe('')
...@@ -543,9 +544,9 @@ function runTests({ dev }) { ...@@ -543,9 +544,9 @@ function runTests({ dev }) {
.elementByCss('#view-post-1-interpolated') .elementByCss('#view-post-1-interpolated')
.getAttribute('href') .getAttribute('href')
const parsedHref = url.parse(href, true) const parsedHref = new URL(href, await browser.url())
expect(parsedHref.pathname).toBe('/post-1') expect(parsedHref.pathname).toBe('/post-1')
expect(parsedHref.query).toEqual({}) expect(Object.fromEntries(parsedHref.searchParams.entries())).toEqual({})
await browser.elementByCss('#view-post-1-interpolated').click() await browser.elementByCss('#view-post-1-interpolated').click()
await browser.waitForElementByCss('#asdf') await browser.waitForElementByCss('#asdf')
...@@ -569,10 +570,11 @@ function runTests({ dev }) { ...@@ -569,10 +570,11 @@ function runTests({ dev }) {
.elementByCss('#view-post-1-interpolated-more-query') .elementByCss('#view-post-1-interpolated-more-query')
.getAttribute('href') .getAttribute('href')
const parsedHref = url.parse(href, true) const parsedHref = new URL(href, await browser.url())
expect(parsedHref.pathname).toBe('/post-1') expect(parsedHref.pathname).toBe('/post-1')
expect(parsedHref.query).toEqual({ another: 'value' }) expect(Object.fromEntries(parsedHref.searchParams.entries())).toEqual({
another: 'value',
})
await browser.elementByCss('#view-post-1-interpolated-more-query').click() await browser.elementByCss('#view-post-1-interpolated-more-query').click()
await browser.waitForElementByCss('#asdf') await browser.waitForElementByCss('#asdf')
...@@ -672,7 +674,9 @@ function runTests({ dev }) { ...@@ -672,7 +674,9 @@ function runTests({ dev }) {
.elementByCss('#view-post-1-comment-1-interpolated') .elementByCss('#view-post-1-comment-1-interpolated')
.getAttribute('href') .getAttribute('href')
expect(url.parse(href).pathname).toBe('/post-1/comment-1') expect(new URL(href, await browser.url()).pathname).toBe(
'/post-1/comment-1'
)
await browser.elementByCss('#view-post-1-comment-1-interpolated').click() await browser.elementByCss('#view-post-1-comment-1-interpolated').click()
await browser.waitForElementByCss('#asdf') await browser.waitForElementByCss('#asdf')
...@@ -904,7 +908,9 @@ function runTests({ dev }) { ...@@ -904,7 +908,9 @@ function runTests({ dev }) {
.elementByCss('#ssg-catch-all-single-interpolated') .elementByCss('#ssg-catch-all-single-interpolated')
.getAttribute('href') .getAttribute('href')
expect(url.parse(href).pathname).toBe('/p1/p2/all-ssg/hello') expect(new URL(href, await browser.url()).pathname).toBe(
'/p1/p2/all-ssg/hello'
)
await browser.elementByCss('#ssg-catch-all-single-interpolated').click() await browser.elementByCss('#ssg-catch-all-single-interpolated').click()
await browser.waitForElementByCss('#all-ssg-content') await browser.waitForElementByCss('#all-ssg-content')
...@@ -953,7 +959,7 @@ function runTests({ dev }) { ...@@ -953,7 +959,7 @@ function runTests({ dev }) {
}) })
it('[ssg: catch-all] should pass params in getStaticProps during client navigation (multi interpolated)', async () => { it('[ssg: catch-all] should pass params in getStaticProps during client navigation (multi interpolated)', async () => {
let browser let browser: Playwright
try { try {
browser = await webdriver(appPort, '/') browser = await webdriver(appPort, '/')
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
...@@ -962,7 +968,9 @@ function runTests({ dev }) { ...@@ -962,7 +968,9 @@ function runTests({ dev }) {
.elementByCss('#ssg-catch-all-multi-interpolated') .elementByCss('#ssg-catch-all-multi-interpolated')
.getAttribute('href') .getAttribute('href')
expect(url.parse(href).pathname).toBe('/p1/p2/all-ssg/hello1/hello2') expect(new URL(href, await browser.url()).pathname).toBe(
'/p1/p2/all-ssg/hello1/hello2'
)
await browser.elementByCss('#ssg-catch-all-multi-interpolated').click() await browser.elementByCss('#ssg-catch-all-multi-interpolated').click()
await browser.waitForElementByCss('#all-ssg-content') await browser.waitForElementByCss('#all-ssg-content')
......
/* eslint-env jest */ /* eslint-env jest */
import url from 'url'
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
...@@ -79,7 +78,7 @@ const runTests = (mode = 'dev', didReload = false) => { ...@@ -79,7 +78,7 @@ const runTests = (mode = 'dev', didReload = false) => {
const res = await fetchViaHTTP(appPort, '/hello', undefined, { const res = await fetchViaHTTP(appPort, '/hello', undefined, {
redirect: 'manual', redirect: 'manual',
}) })
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(res.status).toBe(307) expect(res.status).toBe(307)
expect(pathname).toBe('/another') expect(pathname).toBe('/another')
......
/* eslint-env jest */ /* eslint-env jest */
/* eslint-disable jest/no-identical-title */ /* eslint-disable jest/no-identical-title */
import url from 'url'
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { import {
...@@ -23,7 +22,7 @@ const expectStatus = async (path) => { ...@@ -23,7 +22,7 @@ const expectStatus = async (path) => {
const checkRes = async (res) => { const checkRes = async (res) => {
if (res.status === 308) { if (res.status === 308) {
const redirectDest = res.headers.get('location') const redirectDest = res.headers.get('location')
const parsedUrl = url.parse(redirectDest, true) const parsedUrl = new URL(redirectDest)
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
} else { } else {
try { try {
......
/* eslint-env jest */ /* eslint-env jest */
import url from 'url'
import fs from 'fs-extra' import fs from 'fs-extra'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
...@@ -32,7 +31,7 @@ const runTests = (isDev: boolean) => { ...@@ -32,7 +31,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(307) expect(res.status).toBe(307)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe(`${basePath}/404`) expect(pathname).toBe(`${basePath}/404`)
}) })
...@@ -51,7 +50,7 @@ const runTests = (isDev: boolean) => { ...@@ -51,7 +50,7 @@ const runTests = (isDev: boolean) => {
const text = await res.text() const text = await res.text()
expect(text).toEqual(`/404`) expect(text).toEqual(`/404`)
const parsedUrl = url.parse(res.headers.get('location')) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.pathname).toBe(`/404`) expect(parsedUrl.pathname).toBe(`/404`)
const browser = await webdriver(appPort, `${basePath}`) const browser = await webdriver(appPort, `${basePath}`)
...@@ -61,7 +60,7 @@ const runTests = (isDev: boolean) => { ...@@ -61,7 +60,7 @@ const runTests = (isDev: boolean) => {
/oops not found/ /oops not found/
) )
const parsedUrl2 = url.parse(await browser.eval('window.location.href')) const parsedUrl2 = new URL(await browser.eval('window.location.href'))
expect(parsedUrl2.pathname).toBe('/404') expect(parsedUrl2.pathname).toBe('/404')
}) })
...@@ -79,7 +78,7 @@ const runTests = (isDev: boolean) => { ...@@ -79,7 +78,7 @@ const runTests = (isDev: boolean) => {
const text = await res.text() const text = await res.text()
expect(text).toEqual(`${basePath}/404`) expect(text).toEqual(`${basePath}/404`)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe(`${basePath}/404`) expect(pathname).toBe(`${basePath}/404`)
expect(res.headers.get('refresh')).toContain(`url=${basePath}/404`) expect(res.headers.get('refresh')).toContain(`url=${basePath}/404`)
...@@ -99,7 +98,7 @@ const runTests = (isDev: boolean) => { ...@@ -99,7 +98,7 @@ const runTests = (isDev: boolean) => {
const text = await res.text() const text = await res.text()
expect(text).toEqual(`${basePath}/404`) expect(text).toEqual(`${basePath}/404`)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe(`${basePath}/404`) expect(pathname).toBe(`${basePath}/404`)
expect(res.headers.get('refresh')).toBe(null) expect(res.headers.get('refresh')).toBe(null)
...@@ -119,7 +118,7 @@ const runTests = (isDev: boolean) => { ...@@ -119,7 +118,7 @@ const runTests = (isDev: boolean) => {
const text = await res.text() const text = await res.text()
expect(text).toEqual(`${basePath}/404`) expect(text).toEqual(`${basePath}/404`)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe(`${basePath}/404`) expect(pathname).toBe(`${basePath}/404`)
expect(res.headers.get('refresh')).toBe(null) expect(res.headers.get('refresh')).toBe(null)
...@@ -143,7 +142,7 @@ const runTests = (isDev: boolean) => { ...@@ -143,7 +142,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe(`${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`) expect(pathname).toBe(`${basePath}/gsp-blog/redirect-dest-_gsp-blog_first`)
}) })
...@@ -166,7 +165,7 @@ const runTests = (isDev: boolean) => { ...@@ -166,7 +165,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
// since it was cached the initial value is now the redirect // since it was cached the initial value is now the redirect
// result // result
expect(pathname).toBe(`${basePath}/gsp-blog/first`) expect(pathname).toBe(`${basePath}/gsp-blog/first`)
...@@ -185,7 +184,7 @@ const runTests = (isDev: boolean) => { ...@@ -185,7 +184,7 @@ const runTests = (isDev: boolean) => {
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe(`${basePath}/gsp-blog/redirect-dest-_`) expect(pathname).toBe(`${basePath}/gsp-blog/redirect-dest-_`)
}) })
...@@ -202,7 +201,7 @@ const runTests = (isDev: boolean) => { ...@@ -202,7 +201,7 @@ const runTests = (isDev: boolean) => {
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe(`${basePath}`) expect(pathname).toBe(`${basePath}`)
}) })
} }
...@@ -225,7 +224,7 @@ const runTests = (isDev: boolean) => { ...@@ -225,7 +224,7 @@ const runTests = (isDev: boolean) => {
expect(initialHref).toBeFalsy() expect(initialHref).toBeFalsy()
const curUrl = await browser.url() const curUrl = await browser.url()
const { pathname } = url.parse(curUrl) const { pathname } = new URL(curUrl)
expect(pathname).toBe('/docs/missing') expect(pathname).toBe('/docs/missing')
}) })
...@@ -274,7 +273,7 @@ const runTests = (isDev: boolean) => { ...@@ -274,7 +273,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(307) expect(res.status).toBe(307)
const parsed = url.parse(res.headers.get('location')) const parsed = new URL(res.headers.get('location'))
expect(parsed.hostname).toBe('example.vercel.sh') expect(parsed.hostname).toBe('example.vercel.sh')
expect(parsed.pathname).toBe('/') expect(parsed.pathname).toBe('/')
}) })
...@@ -390,8 +389,8 @@ const runTests = (isDev: boolean) => { ...@@ -390,8 +389,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual(`${basePath}`) expect(pathname + search).toEqual(`${basePath}`)
}) })
it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => { it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => {
...@@ -418,8 +417,8 @@ const runTests = (isDev: boolean) => { ...@@ -418,8 +417,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual(`${basePath}`) expect(pathname + search).toEqual(`${basePath}`)
}) })
it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => { it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => {
...@@ -446,8 +445,8 @@ const runTests = (isDev: boolean) => { ...@@ -446,8 +445,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual(`${basePath}`) expect(pathname + search).toEqual(`${basePath}`)
}) })
it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => { it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => {
...@@ -474,8 +473,8 @@ const runTests = (isDev: boolean) => { ...@@ -474,8 +473,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual(`${basePath}`) expect(pathname + search).toEqual(`${basePath}`)
}) })
} }
......
/* eslint-env jest */ /* eslint-env jest */
import url from 'url'
import fs from 'fs-extra' import fs from 'fs-extra'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
...@@ -30,7 +29,7 @@ const runTests = (isDev: boolean) => { ...@@ -30,7 +29,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(307) expect(res.status).toBe(307)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe('/404') expect(pathname).toBe('/404')
}) })
...@@ -46,7 +45,7 @@ const runTests = (isDev: boolean) => { ...@@ -46,7 +45,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(308) expect(res.status).toBe(308)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe('/404') expect(pathname).toBe('/404')
expect(res.headers.get('refresh')).toMatch(/url=\/404/) expect(res.headers.get('refresh')).toMatch(/url=\/404/)
...@@ -63,7 +62,7 @@ const runTests = (isDev: boolean) => { ...@@ -63,7 +62,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(301) expect(res.status).toBe(301)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe('/404') expect(pathname).toBe('/404')
expect(res.headers.get('refresh')).toBe(null) expect(res.headers.get('refresh')).toBe(null)
...@@ -80,7 +79,7 @@ const runTests = (isDev: boolean) => { ...@@ -80,7 +79,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(303) expect(res.status).toBe(303)
const { pathname } = url.parse(res.headers.get('location')) const { pathname } = new URL(res.headers.get('location'))
expect(pathname).toBe('/404') expect(pathname).toBe('/404')
expect(res.headers.get('refresh')).toBe(null) expect(res.headers.get('refresh')).toBe(null)
...@@ -104,7 +103,7 @@ const runTests = (isDev: boolean) => { ...@@ -104,7 +103,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/gsp-blog/redirect-dest-_gsp-blog_first') expect(pathname).toBe('/gsp-blog/redirect-dest-_gsp-blog_first')
}) })
...@@ -126,7 +125,7 @@ const runTests = (isDev: boolean) => { ...@@ -126,7 +125,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/gsp-blog/first') expect(pathname).toBe('/gsp-blog/first')
}) })
...@@ -148,7 +147,7 @@ const runTests = (isDev: boolean) => { ...@@ -148,7 +147,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/gsp-blog/first') expect(pathname).toBe('/gsp-blog/first')
}) })
...@@ -170,7 +169,7 @@ const runTests = (isDev: boolean) => { ...@@ -170,7 +169,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/gsp-blog/first') expect(pathname).toBe('/gsp-blog/first')
}) })
...@@ -192,7 +191,7 @@ const runTests = (isDev: boolean) => { ...@@ -192,7 +191,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/gsp-blog/first') expect(pathname).toBe('/gsp-blog/first')
}) })
...@@ -215,7 +214,7 @@ const runTests = (isDev: boolean) => { ...@@ -215,7 +214,7 @@ const runTests = (isDev: boolean) => {
}, },
}) })
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
// since it was cached the initial value is now the redirect // since it was cached the initial value is now the redirect
// result // result
expect(pathname).toBe('/gsp-blog/first') expect(pathname).toBe('/gsp-blog/first')
...@@ -230,7 +229,7 @@ const runTests = (isDev: boolean) => { ...@@ -230,7 +229,7 @@ const runTests = (isDev: boolean) => {
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/gsp-blog/redirect-dest-_') expect(pathname).toBe('/gsp-blog/redirect-dest-_')
}) })
...@@ -243,7 +242,7 @@ const runTests = (isDev: boolean) => { ...@@ -243,7 +242,7 @@ const runTests = (isDev: boolean) => {
await browser.waitForElementByCss('#index') await browser.waitForElementByCss('#index')
const initialHref = await browser.eval(() => (window as any).initialHref) const initialHref = await browser.eval(() => (window as any).initialHref)
const { pathname } = url.parse(initialHref) const { pathname } = new URL(initialHref)
expect(pathname).toBe('/') expect(pathname).toBe('/')
}) })
} }
...@@ -266,7 +265,7 @@ const runTests = (isDev: boolean) => { ...@@ -266,7 +265,7 @@ const runTests = (isDev: boolean) => {
expect(initialHref).toBeFalsy() expect(initialHref).toBeFalsy()
const curUrl = await browser.url() const curUrl = await browser.url()
const { pathname } = url.parse(curUrl) const { pathname } = new URL(curUrl)
expect(pathname).toBe('/missing') expect(pathname).toBe('/missing')
}) })
...@@ -315,7 +314,7 @@ const runTests = (isDev: boolean) => { ...@@ -315,7 +314,7 @@ const runTests = (isDev: boolean) => {
) )
expect(res.status).toBe(307) expect(res.status).toBe(307)
const parsed = url.parse(res.headers.get('location')) const parsed = new URL(res.headers.get('location'))
expect(parsed.hostname).toBe('example.vercel.sh') expect(parsed.hostname).toBe('example.vercel.sh')
expect(parsed.pathname).toBe('/') expect(parsed.pathname).toBe('/')
}) })
...@@ -427,8 +426,8 @@ const runTests = (isDev: boolean) => { ...@@ -427,8 +426,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual('/') expect(pathname + search).toEqual('/')
}) })
it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => { it('should not replace history of the origin page when GSSP page is navigated to client-side (external)', async () => {
...@@ -451,8 +450,8 @@ const runTests = (isDev: boolean) => { ...@@ -451,8 +450,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual('/') expect(pathname + search).toEqual('/')
}) })
it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => { it('should not replace history of the origin page when GSP page is navigated to client-side (internal)', async () => {
...@@ -475,8 +474,8 @@ const runTests = (isDev: boolean) => { ...@@ -475,8 +474,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual('/') expect(pathname + search).toEqual('/')
}) })
it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => { it('should not replace history of the origin page when GSP page is navigated to client-side (external)', async () => {
...@@ -499,8 +498,8 @@ const runTests = (isDev: boolean) => { ...@@ -499,8 +498,8 @@ const runTests = (isDev: boolean) => {
})()`) })()`)
const curUrl = await browser.url() const curUrl = await browser.url()
const { path } = url.parse(curUrl) const { pathname, search } = new URL(curUrl)
expect(path).toEqual('/') expect(pathname + search).toEqual('/')
}) })
} }
......
import url from 'url'
import http from 'http' import http from 'http'
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
...@@ -238,9 +237,10 @@ describe('i18n Support', () => { ...@@ -238,9 +237,10 @@ describe('i18n Support', () => {
) )
hrefs.sort() hrefs.sort()
const baseURL = await browser.url()
assert.deepEqual( assert.deepEqual(
hrefs.map((href) => hrefs.map((href) =>
new URL(href).pathname new URL(href, baseURL).pathname
.replace(ctx.basePath, '') .replace(ctx.basePath, '')
.replace(/^\/_next\/data\/[^/]+/, '') .replace(/^\/_next\/data\/[^/]+/, '')
), ),
...@@ -287,9 +287,11 @@ describe('i18n Support', () => { ...@@ -287,9 +287,11 @@ describe('i18n Support', () => {
} else { } else {
expect(res.status).toBe(307) expect(res.status).toBe(307)
const parsed = url.parse(res.headers.get('location'), true) const parsed = new URL(res.headers.get('location'), res.url)
expect(parsed.pathname).toBe(`/${locale}/`) expect(parsed.pathname).toBe(`/${locale}/`)
expect(parsed.query).toEqual({}) expect(Object.fromEntries(parsed.searchParams.entries())).toEqual(
{}
)
} }
} }
}) })
...@@ -485,9 +487,11 @@ describe('i18n Support', () => { ...@@ -485,9 +487,11 @@ describe('i18n Support', () => {
} else { } else {
expect(res.status).toBe(307) expect(res.status).toBe(307)
const parsed = url.parse(res.headers.get('location'), true) const parsed = new URL(res.headers.get('location'), res.url)
expect(parsed.pathname).toBe(`/${locale}`) expect(parsed.pathname).toBe(`/${locale}`)
expect(parsed.query).toEqual({}) expect(Object.fromEntries(parsed.searchParams.entries())).toEqual(
{}
)
} }
} }
}) })
......
/* eslint-env jest */ /* eslint-env jest */
import { join } from 'path' import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import url from 'url'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp' import escapeRegex from 'escape-string-regexp'
import { import {
...@@ -66,11 +65,11 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -66,11 +65,11 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
) )
expect(res.status).toBe(307) expect(res.status).toBe(307)
const parsedUrl = url.parse(res.headers.get('location'), true) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl.pathname).toBe('/test/google.com') expect(parsedUrl.pathname).toBe('/test/google.com')
expect(parsedUrl.query).toEqual({}) expect(Object.fromEntries(parsedUrl.searchParams.entries())).toEqual({})
expect(await res.text()).toBe('/test/google.com') expect(await res.text()).toBe('/test/google.com')
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
...@@ -83,11 +82,11 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -83,11 +82,11 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
) )
expect(res2.status).toBe(307) expect(res2.status).toBe(307)
const parsedUrl2 = url.parse(res2.headers.get('location'), true) const parsedUrl2 = new URL(res2.headers.get('location'))
expect(parsedUrl2.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl2.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl2.pathname).toBe('/test/google.com') expect(parsedUrl2.pathname).toBe('/test/google.com')
expect(parsedUrl2.query).toEqual({}) expect(Object.fromEntries(parsedUrl2.searchParams.entries())).toEqual({})
expect(await res2.text()).toBe('/test/google.com') expect(await res2.text()).toBe('/test/google.com')
}) })
} }
...@@ -99,10 +98,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -99,10 +98,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
}) })
expect(res.status).toBe(308) expect(res.status).toBe(308)
const parsedUrl = url.parse(res.headers.get('location'), true) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.pathname).toBe('/google.com') expect(parsedUrl.pathname).toBe('/google.com')
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl.query).toEqual({}) expect(Object.fromEntries(parsedUrl.searchParams.entries())).toEqual({})
} }
const browser = await webdriver(appPort, '//google.com') const browser = await webdriver(appPort, '//google.com')
...@@ -128,10 +127,12 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -128,10 +127,12 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
} }
) )
expect(res.status).toBe(308) expect(res.status).toBe(308)
const parsedUrl = url.parse(res.headers.get('location'), true) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.pathname).toBe('/google.com') expect(parsedUrl.pathname).toBe('/google.com')
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl.query).toEqual({ h: '1' }) expect(Object.fromEntries(parsedUrl.searchParams.entries())).toEqual({
h: '1',
})
} }
const browser = await webdriver(appPort, '//google.com?h=1') const browser = await webdriver(appPort, '//google.com?h=1')
...@@ -152,10 +153,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -152,10 +153,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(308) expect(res.status).toBe(308)
const parsedUrl = url.parse(res.headers.get('location'), true) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.pathname).toBe('/google.com') expect(parsedUrl.pathname).toBe('/google.com')
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl.query).toEqual({}) expect(Object.fromEntries(parsedUrl.searchParams.entries())).toEqual({})
} }
const browser = await webdriver(appPort, '//google.com#hello') const browser = await webdriver(appPort, '//google.com#hello')
...@@ -240,10 +241,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -240,10 +241,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(308) expect(res.status).toBe(308)
const parsedUrl = url.parse(res.headers.get('location'), true) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.pathname).toBe('/google.com') expect(parsedUrl.pathname).toBe('/google.com')
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl.query).toEqual({}) expect(Object.fromEntries(parsedUrl.searchParams.entries())).toEqual({})
expect(await res.text()).toBe('/google.com') expect(await res.text()).toBe('/google.com')
} }
...@@ -265,10 +266,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -265,10 +266,10 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(308) expect(res.status).toBe(308)
const parsedUrl = url.parse(res.headers.get('location'), true) const parsedUrl = new URL(res.headers.get('location'))
expect(parsedUrl.pathname).toBe(isExport ? '//google.com' : '/google.com') expect(parsedUrl.pathname).toBe(isExport ? '//google.com' : '/google.com')
expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(parsedUrl.hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(parsedUrl.query).toEqual({}) expect(Object.fromEntries(parsedUrl.searchParams.entries())).toEqual({})
expect(await res.text()).toBe('/google.com') expect(await res.text()).toBe('/google.com')
} }
......
...@@ -54,7 +54,7 @@ describe('app edge middleware', () => { ...@@ -54,7 +54,7 @@ describe('app edge middleware', () => {
`, `,
'middleware.js': ` 'middleware.js': `
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
import { parse } from 'url'; import { URL } from 'url';
export async function middleware() { export async function middleware() {
return NextResponse.next() return NextResponse.next()
} }
......
...@@ -42,6 +42,7 @@ async function main() { ...@@ -42,6 +42,7 @@ async function main() {
await app.render(req, res, '/page-error') await app.render(req, res, '/page-error')
} else { } else {
parsedUrl.pathname = pathname parsedUrl.pathname = pathname
// TODO: Accept WHATWG URLs in Next.js request handlers
await handle(req, res, parsedUrl) await handle(req, res, parsedUrl)
} }
} catch (err) { } catch (err) {
......
import url from 'url' import url from 'url'
import Link from 'next/link' import Link from 'next/link'
console.log(url.parse('https://example.com')) // `url` is shimmed partially by Next.js in the browser so we have to test a method
// that's not deprected in Node.js but also available in the shim.
console.log(url.resolve('/one/two/three', 'four'))
export default () => ( export default () => (
<div> <div>
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import http from 'http' import http from 'http'
import url from 'url'
import { join } from 'path' import { join } from 'path'
import { getBrowserBodyText, waitFor, fetchViaHTTP } from 'next-test-utils' import { getBrowserBodyText, waitFor, fetchViaHTTP } from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
...@@ -215,9 +214,7 @@ export default (next: NextInstance) => { ...@@ -215,9 +214,7 @@ export default (next: NextInstance) => {
} }
) )
const { pathname, hostname } = url.parse( const { pathname, hostname } = new URL(res.headers.get('location') || '')
res.headers.get('location') || ''
)
expect(res.status).toBe(307) expect(res.status).toBe(307)
expect(pathname).toBe(encodeURI('/\\google.com/about')) expect(pathname).toBe(encodeURI('/\\google.com/about'))
expect(hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(hostname).toBeOneOf(['localhost', '127.0.0.1'])
...@@ -233,9 +230,7 @@ export default (next: NextInstance) => { ...@@ -233,9 +230,7 @@ export default (next: NextInstance) => {
} }
) )
const { pathname, hostname } = url.parse( const { pathname, hostname } = new URL(res.headers.get('location') || '')
res.headers.get('location') || ''
)
expect(res.status).toBe(307) expect(res.status).toBe(307)
expect(pathname).toBe('/%25google.com/about') expect(pathname).toBe('/%25google.com/about')
expect(hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(hostname).toBeOneOf(['localhost', '127.0.0.1'])
...@@ -251,14 +246,14 @@ export default (next: NextInstance) => { ...@@ -251,14 +246,14 @@ export default (next: NextInstance) => {
} }
) )
const { pathname, hostname, query } = url.parse( const { pathname, hostname, search } = new URL(
res.headers.get('location') || '' res.headers.get('location') || ''
) )
expect(res.status).toBe(308) expect(res.status).toBe(308)
expect(pathname).toBe('/trailing-redirect') expect(pathname).toBe('/trailing-redirect')
expect(hostname).toBeOneOf(['localhost', '127.0.0.1']) expect(hostname).toBeOneOf(['localhost', '127.0.0.1'])
expect(query).toBe( expect(search).toBe(
'url=https%3A%2F%2Fgoogle.com%2Fimage%3Fcrop%3Dfocalpoint%26w%3D24&w=1200&q=100' '?url=https%3A%2F%2Fgoogle.com%2Fimage%3Fcrop%3Dfocalpoint%26w%3D24&w=1200&q=100'
) )
}) })
...@@ -272,9 +267,7 @@ export default (next: NextInstance) => { ...@@ -272,9 +267,7 @@ export default (next: NextInstance) => {
} }
) )
const { pathname, hostname } = url.parse( const { pathname, hostname } = new URL(res.headers.get('location') || '')
res.headers.get('location') || ''
)
expect(res.status).toBe(307) expect(res.status).toBe(307)
expect(pathname).toBe('/%2fgoogle.com/about') expect(pathname).toBe('/%2fgoogle.com/about')
expect(hostname).not.toBe('google.com') expect(hostname).not.toBe('google.com')
...@@ -290,12 +283,12 @@ export default (next: NextInstance) => { ...@@ -290,12 +283,12 @@ export default (next: NextInstance) => {
} }
) )
const { pathname, hostname, query } = url.parse( const { pathname, hostname, search } = new URL(
res.headers.get('location') || '' res.headers.get('location') || ''
) )
expect(res.status).toBe(307) expect(res.status).toBe(307)
expect(pathname).toBe('/about') expect(pathname).toBe('/about')
expect(query).toBe('foo=%2Fgoogle.com') expect(search).toBe('?foo=%2Fgoogle.com')
expect(hostname).not.toBe('google.com') expect(hostname).not.toBe('google.com')
expect(hostname).not.toMatch(/google/) expect(hostname).not.toMatch(/google/)
}) })
...@@ -308,9 +301,7 @@ export default (next: NextInstance) => { ...@@ -308,9 +301,7 @@ export default (next: NextInstance) => {
{ redirect: 'manual' } { redirect: 'manual' }
) )
const { pathname, hostname } = url.parse( const { pathname, hostname } = new URL(res.headers.get('location') || '')
res.headers.get('location') || ''
)
expect(res.status).toBe(308) expect(res.status).toBe(308)
expect(pathname).toBe('/%2fexample.com') expect(pathname).toBe('/%2fexample.com')
expect(hostname).not.toBe('example.com') expect(hostname).not.toBe('example.com')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment