Skip to content

Commit

Permalink
test: add failing test for #411
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 10, 2020
1 parent bec998c commit 1509987
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 47 deletions.
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.

6 changes: 6 additions & 0 deletions test/fixture/api/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default (req, res) => {
const reqValue = (new URLSearchParams(req.headers.cookie || '').get('mycookie') || '').split(';')[0].trim()
const newValue = Math.round(Math.random() * 1000) + ''
res.setHeader('Set-Cookie', `mycookie=${newValue}; path=/`)
res.end(JSON.stringify([reqValue, newValue], null, 2))
}
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>
21 changes: 21 additions & 0 deletions test/fixture/pages/cookie-client.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template>
<div>
<p>Client proxy pass: {{ proxyWorks }}</p>
</div>
</template>

<script>
export default {
data () {
return {
proxyWorks: '?'
}
},
async mounted () {
this.clientCookie = new URLSearchParams(document.cookie).get('mycookie')
const a = await this.$axios.$get('/cookie')
const b = await this.$axios.$get('/cookie')
this.proxyWorks = a[1] === b[0]
}
}
</script>
26 changes: 26 additions & 0 deletions test/fixture/pages/cookie-server.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<div>
<p>SSRCookie: {{ ssrCookie }}</p>
<p>ClientCookie: {{ clientCookie }}</p>
<p>SSR proxy pass: {{ clientCookie === ssrCookie }}</p>
</div>
</template>

<script>
export default {
async asyncData ({ app }) {
const [_, ssrCookie] = await app.$axios.$get('/cookie')
return {
ssrCookie
}
},
data () {
return {
clientCookie: ''
}
},
mounted () {
this.clientCookie = new URLSearchParams(document.cookie).get('mycookie')
}
}
</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>

0 comments on commit 1509987

Please sign in to comment.