fix(vue-app): not strip trailing slash for redirect external domain (#7533)

This commit is contained in:
Xin Du (Clark) 2020-06-18 16:32:39 +01:00 committed by GitHub
parent 39dd866b7b
commit 7f1429ebb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 22 deletions

View File

@ -597,6 +597,10 @@ function formatUrl (url, query) {
let result = (protocol ? protocol + '://' : '//') + parts.shift()
let path = parts.join('/')
if (path === '' && parts.length === 1) {
result += '/'
}
let hash
parts = path.split('#')
if (parts.length === 2) {

View File

@ -152,6 +152,18 @@ describe('basic ssr', () => {
expect(html).toContain('<h1>Index page</h1>')
})
describe('/redirect', () => {
let _headers, _status
const renderContext = {
res: {
writeHead (status, headers) {
_status = status
_headers = headers
},
end () { }
}
}
test('/redirect', async () => {
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
expect(html).toContain('<div id="__nuxt"></div>')
@ -168,21 +180,27 @@ describe('basic ssr', () => {
})
test('/redirect -> external link', async () => {
let _headers, _status
const { html } = await nuxt.server.renderRoute('/redirect-external', {
res: {
writeHead (status, headers) {
_status = status
_headers = headers
},
end () { }
}
})
const { html } = await nuxt.server.renderRoute('/redirect-external', renderContext)
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org')
expect(_headers.Location).toBe('https://nuxtjs.org/api/')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
test('/redirect -> external link without trailing slash', async () => {
const { html } = await nuxt.server.renderRoute('/redirect-external-no-slash', renderContext)
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org/api')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
test('/redirect -> external link with root domain url', async () => {
const { html } = await nuxt.server.renderRoute('/redirect-external-root', renderContext)
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org/')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
})
test('/special-state -> check window.__NUXT__.test = true', async () => {
const window = await nuxt.server.renderAndGetWindow(url('/special-state'))
expect(window.document.title).toBe('Nuxt.js')

View File

@ -261,7 +261,7 @@ describe('basic browser', () => {
await page.nuxt.navigate('/redirect-external', false)
await page.waitForFunction(
() => window.location.href === 'https://nuxtjs.org/'
() => window.location.href === 'https://nuxtjs.org/api/'
)
page.close()
})

View File

@ -0,0 +1,11 @@
<template>
<div>Redirecting...</div>
</template>
<script>
export default {
middleware ({ redirect }) {
return redirect('https://nuxtjs.org/api')
}
}
</script>

View File

@ -0,0 +1,11 @@
<template>
<div>Redirecting...</div>
</template>
<script>
export default {
middleware ({ redirect }) {
return redirect('https://nuxtjs.org/')
}
}
</script>

View File

@ -5,7 +5,7 @@
<script>
export default {
middleware ({ redirect }) {
return redirect('https://nuxtjs.org/')
return redirect('https://nuxtjs.org/api/')
}
}
</script>