Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions test/axios.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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')) + ''
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>