2016-12-09 17:51:19 +00:00
|
|
|
import test from 'ava'
|
|
|
|
import { resolve } from 'path'
|
2016-12-21 14:03:37 +00:00
|
|
|
import rp from 'request-promise-native'
|
2017-12-12 09:42:29 +00:00
|
|
|
import { Nuxt, Builder } from '..'
|
2017-12-17 19:30:26 +00:00
|
|
|
import { interceptLog, interceptError, release } from './helpers/console'
|
2017-05-16 13:12:30 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
const port = 4004
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2016-12-09 17:51:19 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
|
|
|
|
2017-10-28 20:42:51 +00:00
|
|
|
// Init nuxt.js and create server listening on localhost:4003
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('Init Nuxt.js', async t => {
|
2016-12-09 17:51:19 +00:00
|
|
|
const options = {
|
2016-12-20 19:44:42 +00:00
|
|
|
rootDir: resolve(__dirname, 'fixtures/basic'),
|
2017-12-17 19:30:26 +00:00
|
|
|
buildDir: '.nuxt-ssr',
|
2017-10-28 21:35:10 +00:00
|
|
|
dev: false,
|
|
|
|
head: {
|
2017-10-30 16:51:11 +00:00
|
|
|
titleTemplate(titleChunk) {
|
2017-10-28 21:35:10 +00:00
|
|
|
return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js'
|
|
|
|
}
|
2017-12-17 19:30:26 +00:00
|
|
|
},
|
|
|
|
build: {
|
|
|
|
stats: false
|
2018-01-10 06:36:34 +00:00
|
|
|
},
|
|
|
|
render: {
|
2018-01-16 15:10:10 +00:00
|
|
|
csp: {
|
2018-01-16 15:32:31 +00:00
|
|
|
enabled: true,
|
2018-01-16 15:10:10 +00:00
|
|
|
allowedSources: ['https://example.com', 'https://example.io']
|
|
|
|
}
|
2017-10-28 21:35:10 +00:00
|
|
|
}
|
2016-12-09 17:51:19 +00:00
|
|
|
}
|
2017-06-20 11:44:47 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
const logSpy = await interceptLog(async () => {
|
2017-11-27 22:35:42 +00:00
|
|
|
nuxt = new Nuxt(options)
|
2017-12-17 19:30:26 +00:00
|
|
|
const builder = await new Builder(nuxt)
|
|
|
|
await builder.build()
|
|
|
|
await nuxt.listen(port, '0.0.0.0')
|
2017-11-27 22:35:42 +00:00
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
|
|
|
t.true(logSpy.calledWithMatch('DONE'))
|
|
|
|
t.true(logSpy.calledWithMatch('OPEN'))
|
2016-12-09 17:51:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('/stateless', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/stateless')
|
|
|
|
t.true(html.includes('<h1>My component!</h1>'))
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Example of testing via dom checking
|
|
|
|
*/
|
|
|
|
test('/css', async t => {
|
2016-12-20 16:56:06 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/css'))
|
2017-12-29 08:33:13 +00:00
|
|
|
|
|
|
|
const headHtml = window.document.head.innerHTML
|
|
|
|
t.true(headHtml.includes('color:red'))
|
|
|
|
|
2016-12-09 17:51:19 +00:00
|
|
|
const element = window.document.querySelector('.red')
|
|
|
|
t.not(element, null)
|
|
|
|
t.is(element.textContent, 'This is red')
|
|
|
|
t.is(element.className, 'red')
|
2017-12-29 08:33:13 +00:00
|
|
|
// t.is(window.getComputedStyle(element).color, 'red')
|
2016-12-09 17:51:19 +00:00
|
|
|
})
|
|
|
|
|
2017-11-21 07:38:12 +00:00
|
|
|
test('/postcss', async t => {
|
|
|
|
const window = await nuxt.renderAndGetWindow(url('/css'))
|
2017-12-29 08:33:13 +00:00
|
|
|
|
|
|
|
const headHtml = window.document.head.innerHTML
|
|
|
|
t.true(headHtml.includes('background-color:blue'))
|
|
|
|
|
|
|
|
// const element = window.document.querySelector('div.red')
|
|
|
|
// t.is(window.getComputedStyle(element)['background-color'], 'blue')
|
2017-11-21 07:38:12 +00:00
|
|
|
})
|
|
|
|
|
2016-12-09 17:51:19 +00:00
|
|
|
test('/stateful', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/stateful')
|
|
|
|
t.true(html.includes('<div><p>The answer is 42</p></div>'))
|
|
|
|
})
|
|
|
|
|
2017-03-25 23:53:50 +00:00
|
|
|
test('/store', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/store')
|
|
|
|
t.true(html.includes('<h1>Vuex Nested Modules</h1>'))
|
2017-05-13 17:32:35 +00:00
|
|
|
t.true(html.includes('<p>1</p>'))
|
2017-03-25 23:53:50 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/head', async t => {
|
|
|
|
const logSpy = await interceptLog()
|
2018-01-13 05:22:11 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/head'), {
|
|
|
|
virtualConsole: false
|
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
t.is(window.document.title, 'My title - Nuxt.js')
|
2017-11-27 22:35:42 +00:00
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
|
2018-01-13 05:22:11 +00:00
|
|
|
t.true(
|
|
|
|
html.includes('<script data-n-head="true" src="/body.js" data-body="true">')
|
|
|
|
)
|
2017-12-17 19:30:26 +00:00
|
|
|
|
|
|
|
const metas = window.document.getElementsByTagName('meta')
|
|
|
|
t.is(metas[0].getAttribute('content'), 'my meta')
|
|
|
|
release()
|
2017-11-27 22:35:42 +00:00
|
|
|
|
|
|
|
t.true(logSpy.calledOnce)
|
|
|
|
t.is(logSpy.args[0][0], 'Body script!')
|
2016-12-09 17:51:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('/async-data', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/async-data')
|
|
|
|
t.true(html.includes('<p>Nuxt.js</p>'))
|
|
|
|
})
|
|
|
|
|
2016-12-27 15:31:25 +00:00
|
|
|
test('/await-async-data', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/await-async-data')
|
|
|
|
t.true(html.includes('<p>Await Nuxt.js</p>'))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/callback-async-data', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/callback-async-data')
|
|
|
|
t.true(html.includes('<p>Callback Nuxt.js</p>'))
|
|
|
|
})
|
|
|
|
|
2016-12-21 19:51:43 +00:00
|
|
|
test('/users/1', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/users/1')
|
|
|
|
t.true(html.includes('<h1>User: 1</h1>'))
|
|
|
|
})
|
|
|
|
|
2016-12-20 17:26:46 +00:00
|
|
|
test('/validate should display a 404', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/validate')
|
|
|
|
t.true(html.includes('This page could not be found'))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/validate?valid=true', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/validate?valid=true')
|
|
|
|
t.true(html.includes('<h1>I am valid</h1>'))
|
|
|
|
})
|
|
|
|
|
2016-12-20 18:25:51 +00:00
|
|
|
test('/redirect', async t => {
|
2016-12-21 14:03:37 +00:00
|
|
|
const { html, redirected } = await nuxt.renderRoute('/redirect')
|
2016-12-20 18:25:51 +00:00
|
|
|
t.true(html.includes('<div id="__nuxt"></div>'))
|
2016-12-21 14:03:37 +00:00
|
|
|
t.true(redirected.path === '/')
|
|
|
|
t.true(redirected.status === 302)
|
2016-12-20 18:25:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('/redirect -> check redirected source', async t => {
|
|
|
|
const window = await nuxt.renderAndGetWindow(url('/redirect'))
|
|
|
|
const html = window.document.body.innerHTML
|
|
|
|
t.true(html.includes('<h1>Index page</h1>'))
|
|
|
|
})
|
|
|
|
|
2017-11-28 09:10:44 +00:00
|
|
|
test('/redirect -> external link', async t => {
|
2018-01-15 11:22:51 +00:00
|
|
|
let _headers, _status
|
2017-12-21 04:55:32 +00:00
|
|
|
const { html } = await nuxt.renderRoute('/redirect-external', {
|
2017-11-28 09:10:44 +00:00
|
|
|
res: {
|
2018-01-15 11:22:51 +00:00
|
|
|
writeHead(status, headers) {
|
|
|
|
_status = status
|
|
|
|
_headers = headers
|
|
|
|
},
|
|
|
|
end() {}
|
2017-11-28 09:10:44 +00:00
|
|
|
}
|
|
|
|
})
|
2018-01-15 11:22:51 +00:00
|
|
|
t.is(_status, 302)
|
|
|
|
t.is(_headers.Location, 'https://nuxtjs.org')
|
|
|
|
t.true(html.includes('<div data-server-rendered="true"></div>'))
|
2017-11-28 09:10:44 +00:00
|
|
|
})
|
|
|
|
|
2017-07-27 14:51:17 +00:00
|
|
|
test('/special-state -> check window.__NUXT__.test = true', async t => {
|
2017-07-27 14:50:24 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/special-state'))
|
2017-10-28 21:35:10 +00:00
|
|
|
t.is(window.document.title, 'Nuxt.js')
|
2017-07-27 14:50:24 +00:00
|
|
|
t.is(window.__NUXT__.test, true)
|
|
|
|
})
|
|
|
|
|
2016-12-20 18:25:51 +00:00
|
|
|
test('/error', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
const err = await t.throws(nuxt.renderRoute('/error', { req: {}, res: {} }))
|
|
|
|
t.true(err.message.includes('Error mouahahah'))
|
2016-12-21 14:03:37 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/error status code', async t => {
|
|
|
|
const errorSpy = await interceptError()
|
|
|
|
const err = await t.throws(rp(url('/error')))
|
|
|
|
t.true(err.statusCode === 500)
|
2018-01-13 05:22:11 +00:00
|
|
|
t.true(
|
|
|
|
err.response.body.includes(
|
|
|
|
'An error occurred in the application and your page could not be served'
|
|
|
|
)
|
|
|
|
)
|
2017-12-17 19:30:26 +00:00
|
|
|
release()
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(errorSpy.calledOnce)
|
|
|
|
t.true(errorSpy.args[0][0].message.includes('Error mouahahah'))
|
2016-12-21 14:03:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('/error2', async t => {
|
|
|
|
const { html, error } = await nuxt.renderRoute('/error2')
|
|
|
|
t.true(html.includes('Custom error'))
|
|
|
|
t.true(error.message.includes('Custom error'))
|
|
|
|
t.true(error.statusCode === undefined)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/error2 status code', async t => {
|
2017-12-17 19:30:26 +00:00
|
|
|
const error = await t.throws(rp(url('/error2')))
|
|
|
|
t.is(error.statusCode, 500)
|
|
|
|
t.true(error.response.body.includes('Custom error'))
|
2016-12-20 18:25:51 +00:00
|
|
|
})
|
|
|
|
|
2017-12-17 19:30:26 +00:00
|
|
|
test.serial('/error-midd', async t => {
|
|
|
|
const errorSpy = await interceptError()
|
|
|
|
const err = await t.throws(rp(url('/error-midd')))
|
|
|
|
t.is(err.statusCode, 505)
|
|
|
|
t.true(err.response.body.includes('Middleware Error'))
|
|
|
|
release()
|
2017-11-27 22:35:42 +00:00
|
|
|
// Don't display error since redirect returns a noopApp
|
|
|
|
t.true(errorSpy.notCalled)
|
2017-11-02 16:48:20 +00:00
|
|
|
})
|
|
|
|
|
2017-12-21 04:55:32 +00:00
|
|
|
test.serial('/redirect-middleware', async t => {
|
2017-12-17 19:30:26 +00:00
|
|
|
const errorSpy = await interceptError()
|
2017-12-21 04:55:32 +00:00
|
|
|
await rp(url('/redirect-middleware')) // Should not console.error
|
2017-12-17 19:30:26 +00:00
|
|
|
release()
|
2017-06-05 09:35:10 +00:00
|
|
|
// Don't display error since redirect returns a noopApp
|
2017-11-27 22:35:42 +00:00
|
|
|
t.true(errorSpy.notCalled)
|
2017-05-16 13:12:30 +00:00
|
|
|
})
|
|
|
|
|
2017-12-21 04:55:32 +00:00
|
|
|
test('/redirect-name', async t => {
|
|
|
|
const { html, redirected } = await nuxt.renderRoute('/redirect-name')
|
|
|
|
t.true(html.includes('<div id="__nuxt"></div>'))
|
|
|
|
t.true(redirected.path === '/stateless')
|
|
|
|
t.true(redirected.status === 302)
|
|
|
|
})
|
|
|
|
|
2017-08-24 10:46:30 +00:00
|
|
|
test('/no-ssr', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/no-ssr')
|
2018-01-13 05:22:11 +00:00
|
|
|
t.true(
|
|
|
|
html.includes(
|
|
|
|
'<div class="no-ssr-placeholder"><p>Loading...</p></div>'
|
|
|
|
)
|
|
|
|
)
|
2017-08-24 10:46:30 +00:00
|
|
|
})
|
|
|
|
|
2017-10-06 20:35:14 +00:00
|
|
|
test('/no-ssr (client-side)', async t => {
|
2017-08-24 10:46:30 +00:00
|
|
|
const window = await nuxt.renderAndGetWindow(url('/no-ssr'))
|
|
|
|
const html = window.document.body.innerHTML
|
2017-10-06 20:35:14 +00:00
|
|
|
t.true(html.includes('Displayed only on client-side</h1>'))
|
2017-08-24 10:46:30 +00:00
|
|
|
})
|
|
|
|
|
2017-05-21 16:42:38 +00:00
|
|
|
test('ETag Header', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const { headers: { etag } } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
// Verify functionality
|
2018-01-13 05:22:11 +00:00
|
|
|
const error = await t.throws(
|
|
|
|
rp(url('/stateless'), { headers: { 'If-None-Match': etag } })
|
|
|
|
)
|
2017-12-17 19:30:26 +00:00
|
|
|
t.is(error.statusCode, 304)
|
2017-05-21 16:42:38 +00:00
|
|
|
})
|
|
|
|
|
2018-01-10 06:36:34 +00:00
|
|
|
test('Content-Security-Policy Header', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2018-01-10 06:36:34 +00:00
|
|
|
// Verify functionality
|
2018-01-15 10:12:08 +00:00
|
|
|
t.regex(headers['content-security-policy'], /script-src 'self' 'sha256-.*'/)
|
2018-01-16 15:10:10 +00:00
|
|
|
t.true(headers['content-security-policy'].includes('https://example.com'))
|
|
|
|
t.true(headers['content-security-policy'].includes('https://example.io'))
|
2018-01-10 06:36:34 +00:00
|
|
|
})
|
|
|
|
|
2017-06-20 14:12:55 +00:00
|
|
|
test('/_nuxt/server-bundle.json should return 404', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const err = await t.throws(
|
|
|
|
rp(url('/_nuxt/server-bundle.json'), { resolveWithFullResponse: true })
|
|
|
|
)
|
2017-06-20 14:12:55 +00:00
|
|
|
t.is(err.statusCode, 404)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/_nuxt/ should return 404', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const err = await t.throws(
|
|
|
|
rp(url('/_nuxt/'), { resolveWithFullResponse: true })
|
|
|
|
)
|
2017-06-20 14:12:55 +00:00
|
|
|
t.is(err.statusCode, 404)
|
|
|
|
})
|
|
|
|
|
2017-11-02 17:07:33 +00:00
|
|
|
test('/meta', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/meta')
|
|
|
|
t.true(html.includes('"meta":[{"works":true}]'))
|
|
|
|
})
|
|
|
|
|
2017-11-03 16:14:05 +00:00
|
|
|
test('/fn-midd', async t => {
|
2018-01-13 05:22:11 +00:00
|
|
|
const err = await t.throws(
|
|
|
|
rp(url('/fn-midd'), { resolveWithFullResponse: true })
|
|
|
|
)
|
2017-11-03 16:14:05 +00:00
|
|
|
t.is(err.statusCode, 403)
|
|
|
|
t.true(err.response.body.includes('You need to ask the permission'))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/fn-midd?please=true', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/fn-midd?please=true')
|
|
|
|
t.true(html.includes('<h1>Date:'))
|
|
|
|
})
|
|
|
|
|
2017-12-01 09:25:21 +00:00
|
|
|
test('/router-guard', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/router-guard')
|
|
|
|
t.true(html.includes('<p>Nuxt.js</p>'))
|
|
|
|
t.false(html.includes('Router Guard'))
|
|
|
|
})
|
|
|
|
|
2017-12-11 08:18:28 +00:00
|
|
|
test('/jsx', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/jsx')
|
|
|
|
t.true(html.includes('<h1>JSX Page</h1>'))
|
|
|
|
})
|
|
|
|
|
2018-01-18 12:10:23 +00:00
|
|
|
test('/jsx-link', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/jsx-link')
|
|
|
|
t.true(html.includes('<h1>JSX Link Page</h1>'))
|
|
|
|
})
|
|
|
|
|
2017-12-12 13:32:45 +00:00
|
|
|
test('/js-link', async t => {
|
|
|
|
const { html } = await nuxt.renderRoute('/js-link')
|
|
|
|
t.true(html.includes('<h1>vue file is first-class</h1>'))
|
|
|
|
})
|
|
|
|
|
2016-12-09 17:51:19 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2017-12-17 19:30:26 +00:00
|
|
|
test.after.always('Closing server and nuxt.js', async t => {
|
2017-11-27 22:35:42 +00:00
|
|
|
await nuxt.close()
|
2016-12-09 17:51:19 +00:00
|
|
|
})
|