Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve default headers with custom headers #452

Merged
merged 8 commits into from Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions test/axios.test.js
Expand Up @@ -32,8 +32,8 @@ const testSuite = () => {
const call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
const options = call[0].options
const proto = options.https ? 'https' : 'http'
expect(options.baseURL.toString()).toBe(`${proto}://localhost:3000/test_api`)
expect(options.browserBaseURL.toString()).toBe('/test_api')
expect(options.baseURL.toString()).toBe(`${proto}://localhost:3000/api/test`)
expect(options.browserBaseURL.toString()).toBe('/api/test')
})

test('asyncData', async () => {
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('module', () => {
describe('other options', () => {
beforeAll(async () => {
config.axios = {
prefix: '/test_api',
prefix: '/api/test',
proxy: {},
credentials: true,
https: true,
Expand All @@ -141,7 +141,7 @@ describe('other options', () => {
describe('browserBaseURL', () => {
beforeAll(async () => {
config.axios = {
browserBaseURL: '/test_api'
browserBaseURL: '/api/test'
}

await setupNuxt(config)
Expand All @@ -156,7 +156,7 @@ describe('browserBaseURL', () => {
const call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
const options = call[0].options
expect(options.baseURL.toString()).toBe('http://localhost:3000/')
expect(options.browserBaseURL.toString()).toBe('/test_api')
expect(options.browserBaseURL.toString()).toBe('/api/test')
})
})

Expand Down
20 changes: 0 additions & 20 deletions test/fixture/api.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/fixture/api/cookie.js
@@ -0,0 +1,4 @@
export default (req, res) => {
const reqCookie = (new URLSearchParams(req.headers.cookie || '').get('mycookie') || '').split(';')[0].trim()
res.end(reqCookie || '')
}
17 changes: 17 additions & 0 deletions test/fixture/api/test.js
@@ -0,0 +1,17 @@
export default async (req, res) => {
const query = new URL(req.url, 'http://localhost:3000').query
if (query && query.delay) {
await sleep(query.delay)
}

res.end(JSON.stringify({
url: req.url,
method: req.method
}))
}

function sleep (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
7 changes: 5 additions & 2 deletions test/fixture/nuxt.config.js
Expand Up @@ -10,9 +10,12 @@ module.exports = {
modules: [
{ handler: require('../../') }
],
serverMiddleware: ['~/api.js'],
serverMiddleware: {
'/api/test': '~/api/test',
'/api/cookie': '~/api/cookie'
},
axios: {
prefix: '/test_api',
prefix: '/api',
proxy: true,
credentials: true,
debug: true,
Expand Down
28 changes: 15 additions & 13 deletions test/fixture/pages/cancelToken.vue
@@ -1,33 +1,35 @@
<template>
<div>
there should be no loading bar left over:
<button @click="test">Fake Request</button>
<button @click="test">
Fake Request
</button>
</div>
</template>

<script>
export default {
methods: {
test() {
const source = this.$axios.CancelToken.source();
test () {
const source = this.$axios.CancelToken.source()
this.$axios
.$post(
"http://localhost:3000/test_api/foo/bar?delay=1000",
{ data: "test" },
'http://localhost:3000/api/test/foo/bar?delay=1000',
{ data: 'test' },
{
cancelToken: source.token,
cancelToken: source.token
}
)
.catch((err) => {
if (this.$axios.isCancel(err)) {
console.log("request canceled");
console.log('request canceled')
}
});
})

setTimeout(function () {
source.cancel();
}, 500);
},
},
};
source.cancel()
}, 500)
}
}
}
</script>
33 changes: 33 additions & 0 deletions test/fixture/pages/cookie.vue
@@ -0,0 +1,33 @@
<template>
<div>
<pre style="display: none">_req:{{ reqCookie }}</pre>
<p>Pass: {{ pass }}</p>
</div>
</template>

<script>
export default {
async asyncData ({ app }) {
const reqCookie = (await app.$axios.$get('/cookie')) + ''
pi0 marked this conversation as resolved.
Show resolved Hide resolved
return {
reqCookie
}
},
data () {
return {
pass: '?'
}
},
async mounted () {
const randomValue = Math.round(Math.random() * 1000) + ''
document.cookie = `mycookie=${randomValue}; path=/`

// Render page with server-side, expecting to be rendered with same new cookie
const html = await this.$axios.$get(window.location.href)
const m = html.match(/_req:(\w+)/)
const profifiedSSRCookie = m && m[1]

this.pass = randomValue === profifiedSSRCookie
}
}
</script>
2 changes: 1 addition & 1 deletion test/fixture/pages/mounted.vue
Expand Up @@ -14,7 +14,7 @@ export default {

async mounted () {
// Request with full url becasue we are in JSDom env
this.res = await this.$axios.$get('http://localhost:3000/test_api/foo/bar')
this.res = await this.$axios.$get('http://localhost:3000/api/test/foo/bar')
}
}
</script>
12 changes: 6 additions & 6 deletions test/fixture/pages/ssr.vue
Expand Up @@ -12,6 +12,12 @@
let reqCtr = 1

export default {
fetch ({ app, route }) {
const doLogin = route.query.login !== undefined
if (doLogin) {
app.$axios.setHeader('SessionId', reqCtr++)
}
},
computed: {
axiosSessionId () {
return this.$axios.defaults.headers.common.SessionId
Expand All @@ -32,12 +38,6 @@ export default {
'X-Requested-With': 'XMLHttpRequest'
}
})
},
fetch ({ app, route }) {
const doLogin = route.query.login !== undefined
if (doLogin) {
app.$axios.setHeader('SessionId', reqCtr++)
}
}
}
</script>