http/test/module.test.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-03-24 12:39:48 +00:00
const fetch = require('node-fetch')
2020-04-13 18:58:21 +00:00
const { createBrowser } = require('tib')
2019-03-18 21:21:48 +00:00
const { setupNuxt } = require('./_utils')
const url = path => `http://localhost:3000${path}`
describe('module', () => {
2020-04-13 18:58:21 +00:00
let nuxt, browser
2019-03-18 21:21:48 +00:00
2019-04-09 10:29:39 +00:00
beforeAll(async () => {
2019-03-18 21:21:48 +00:00
nuxt = await setupNuxt()
2019-03-24 11:59:15 +00:00
await nuxt.builder.build()
2019-03-24 12:39:48 +00:00
await nuxt.listen(3000)
2020-04-13 18:58:21 +00:00
browser = await createBrowser('puppeteer')
2019-04-09 10:29:39 +00:00
}, 60000)
2019-03-18 21:21:48 +00:00
afterAll(async () => {
await nuxt.close()
2020-04-13 18:58:21 +00:00
await browser.close()
2019-03-18 21:21:48 +00:00
})
test('asyncData', async () => {
2019-03-24 12:39:48 +00:00
const html = await fetch(url('/asyncData')).then(r => r.text())
2019-03-18 21:21:48 +00:00
expect(html).toContain('foo/bar')
})
test('mounted', async () => {
2020-04-13 18:58:21 +00:00
const page = await browser.page(url('/mounted'))
const html = await page.getHtml()
expect(html).toContain('foo/bar')
2019-03-18 21:21:48 +00:00
})
2020-04-13 18:58:21 +00:00
test('defaults', async () => {
const page = await browser.page(url('/mounted'))
const defaults = await page.runScript(() => window.$nuxt.$http._defaults)
expect(defaults.headers.xsrfHeaderName).toBe('X-CSRF-TOKEN')
2019-03-18 21:21:48 +00:00
})
test('error', async () => {
const html = await fetch(url('/error')).then(r => r.text())
expect(html).toMatch('res:{statusCode:418,message:"Detailed error message"}')
})
2019-03-18 21:21:48 +00:00
test('ssr', async () => {
2019-03-24 12:39:48 +00:00
const makeReq = login => fetch(url('/ssr' + (login ? '?login' : '')))
2019-03-18 21:21:48 +00:00
.then(r => r.text())
.then(h => /session-[0-9]+/.exec(h))
.then(m => (m && m[0] ? m[0] : null))
const a = await makeReq()
const b = await makeReq(true)
const c = await makeReq()
const d = await makeReq(true)
expect(a).toBeNull()
expect(b).not.toBeNull()
expect(c).toBeNull() // Important!
expect(d).not.toBeNull()
expect(b).not.toBe(d)
})
test('ssr no brotli', async () => {
2019-03-24 12:39:48 +00:00
const makeReq = login => fetch(url('/ssr' + (login ? '?login' : '')))
2019-03-18 21:21:48 +00:00
.then(r => r.text())
.then(h => /encoding-\$(.*)\$/.exec(h))
.then(m => (m && m[1] ? m[1] : null))
const result = await makeReq()
expect(result).toBe('gzip, deflate')
})
test('instance', async () => {
const html = await fetch(url('/instance')).then(r => r.text())
expect(html).toContain('prefixUrl:https://jsonplaceholder.typicode.com/')
expect(html).toContain('testing:oui')
})
2019-03-18 21:21:48 +00:00
})