mirror of https://github.com/sundowndev/http.git
test(module): increase coverage (#213)
parent
bcfeda3191
commit
c63d5ef139
|
@ -2,39 +2,34 @@ jest.setTimeout(60000)
|
|||
|
||||
const { Nuxt, Builder } = require('nuxt-edge')
|
||||
const axios = require('axios')
|
||||
|
||||
const config = require('./fixture/nuxt.config')
|
||||
|
||||
let nuxt, addTemplate
|
||||
|
||||
const url = path => `http://localhost:3000${path}`
|
||||
|
||||
describe('axios module', () => {
|
||||
let nuxt
|
||||
let addTemplate
|
||||
const setupNuxt = async (config) => {
|
||||
nuxt = new Nuxt(config)
|
||||
|
||||
beforeAll(async () => {
|
||||
nuxt = new Nuxt(config)
|
||||
// Spy addTemplate
|
||||
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
|
||||
nuxt.moduleContainer.addTemplate
|
||||
)
|
||||
|
||||
// Spy addTemplate
|
||||
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
|
||||
nuxt.moduleContainer.addTemplate
|
||||
)
|
||||
const build = new Builder(nuxt)
|
||||
|
||||
await new Builder(nuxt).build()
|
||||
await nuxt.listen(3000)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
})
|
||||
await build.validatePages()
|
||||
await build.generateRoutesAndFiles()
|
||||
await nuxt.listen(3000)
|
||||
}
|
||||
|
||||
const testSuite = () => {
|
||||
test('baseURL', () => {
|
||||
expect(addTemplate).toBeDefined()
|
||||
const call = addTemplate.mock.calls.find(args =>
|
||||
args[0].src.includes('plugin.js')
|
||||
)
|
||||
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/test_api`
|
||||
)
|
||||
expect(options.baseURL.toString()).toBe('http://localhost:3000/test_api')
|
||||
expect(options.browserBaseURL.toString()).toBe('/test_api')
|
||||
})
|
||||
|
||||
|
@ -60,12 +55,11 @@ describe('axios module', () => {
|
|||
})
|
||||
|
||||
test('ssr', async () => {
|
||||
const makeReq = login =>
|
||||
axios
|
||||
.get(url('/ssr' + (login ? '?login' : '')))
|
||||
.then(r => r.data)
|
||||
.then(h => /session-[0-9]+/.exec(h))
|
||||
.then(m => (m && m[0] ? m[0] : null))
|
||||
const makeReq = login => axios
|
||||
.get(url('/ssr' + (login ? '?login' : '')))
|
||||
.then(r => r.data)
|
||||
.then(h => /session-[0-9]+/.exec(h))
|
||||
.then(m => (m && m[0] ? m[0] : null))
|
||||
|
||||
const a = await makeReq()
|
||||
const b = await makeReq(true)
|
||||
|
@ -80,15 +74,96 @@ describe('axios module', () => {
|
|||
})
|
||||
|
||||
test('ssr no brotli', async () => {
|
||||
const makeReq = login =>
|
||||
axios
|
||||
.get(url('/ssr' + (login ? '?login' : '')))
|
||||
.then(r => r.data)
|
||||
.then(h => /encoding-\$(.*)\$/.exec(h))
|
||||
.then(m => (m && m[1] ? m[1] : null))
|
||||
const makeReq = login => axios
|
||||
.get(url('/ssr' + (login ? '?login' : '')))
|
||||
.then(r => r.data)
|
||||
.then(h => /encoding-\$(.*)\$/.exec(h))
|
||||
.then(m => (m && m[1] ? m[1] : null))
|
||||
|
||||
const result = await makeReq()
|
||||
|
||||
expect(result).toBe('gzip, deflate')
|
||||
})
|
||||
}
|
||||
|
||||
describe('module', () => {
|
||||
beforeAll(async () => {
|
||||
nuxt = new Nuxt(config)
|
||||
|
||||
// Spy addTemplate
|
||||
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
|
||||
nuxt.moduleContainer.addTemplate
|
||||
)
|
||||
|
||||
await new Builder(nuxt).build()
|
||||
await nuxt.listen(3000)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
})
|
||||
|
||||
testSuite()
|
||||
})
|
||||
|
||||
describe('other options', () => {
|
||||
beforeAll(async () => {
|
||||
config.axios = {
|
||||
prefix: '/test_api',
|
||||
proxy: {},
|
||||
credentials: true,
|
||||
https: true,
|
||||
retry: false
|
||||
}
|
||||
|
||||
await setupNuxt(config)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
})
|
||||
|
||||
testSuite()
|
||||
})
|
||||
|
||||
describe('browserBaseURL', () => {
|
||||
beforeAll(async () => {
|
||||
config.axios = {
|
||||
browserBaseURL: '/test_api'
|
||||
}
|
||||
|
||||
await setupNuxt(config)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
})
|
||||
|
||||
test('custom', () => {
|
||||
expect(addTemplate).toBeDefined()
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
||||
describe('empty config', () => {
|
||||
beforeAll(async () => {
|
||||
config.axios = {}
|
||||
|
||||
await setupNuxt(config)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
})
|
||||
|
||||
test('preset baseURL and browserBaseURL', () => {
|
||||
expect(addTemplate).toBeDefined()
|
||||
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('http://localhost:3000/')
|
||||
})
|
||||
})
|
||||
|
|
|
@ -2,8 +2,8 @@ const { resolve } = require('path')
|
|||
|
||||
module.exports = {
|
||||
rootDir: resolve(__dirname, '../..'),
|
||||
buildDir: resolve(__dirname, '.nuxt'),
|
||||
srcDir: __dirname,
|
||||
dev: false,
|
||||
render: {
|
||||
resourceHints: false
|
||||
},
|
||||
|
@ -12,13 +12,11 @@ module.exports = {
|
|||
],
|
||||
serverMiddleware: ['~/api.js'],
|
||||
axios: {
|
||||
prefix: `/test_api`,
|
||||
prefix: '/test_api',
|
||||
proxy: true,
|
||||
credentials: true,
|
||||
debug: true,
|
||||
retry: {
|
||||
retries: 3
|
||||
}
|
||||
retry: true
|
||||
},
|
||||
plugins: ['~/plugins/axios']
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<template>
|
||||
<div>
|
||||
<div>
|
||||
Response: {{ res }}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
async asyncData ({ app }) {
|
||||
async asyncData({ app }) {
|
||||
let res = await app.$axios.$get('foo/bar')
|
||||
return {
|
||||
res
|
||||
|
|
|
@ -6,12 +6,13 @@
|
|||
|
||||
<script>
|
||||
export default {
|
||||
data () {
|
||||
data() {
|
||||
return {
|
||||
res: ''
|
||||
}
|
||||
},
|
||||
async mounted () {
|
||||
|
||||
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')
|
||||
}
|
||||
|
|
|
@ -10,17 +10,18 @@
|
|||
let reqCtr = 1
|
||||
|
||||
export default {
|
||||
async fetch ({app, route}) {
|
||||
async fetch({app, route}) {
|
||||
let doLogin = route.query.login !== undefined
|
||||
if (doLogin) {
|
||||
app.$axios.setHeader('sessionId', reqCtr++)
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
axiosSessionId () {
|
||||
axiosSessionId() {
|
||||
return this.$axios.defaults.headers.common.sessionId
|
||||
},
|
||||
axiosEncoding () {
|
||||
|
||||
axiosEncoding() {
|
||||
return this.$axios.defaults.headers.common['Accept-Encoding']
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue