2020-11-14 22:10:16 +00:00
|
|
|
import http from 'http'
|
2023-02-02 15:13:55 +00:00
|
|
|
import { join, resolve } from 'upath'
|
2020-11-14 22:10:16 +00:00
|
|
|
import serveStatic from 'serve-static'
|
|
|
|
import finalhandler from 'finalhandler'
|
2021-02-02 12:38:54 +00:00
|
|
|
import glob from 'glob'
|
2020-11-14 22:10:16 +00:00
|
|
|
import { Builder, Generator, getPort, loadFixture, Nuxt, rp } from '../utils'
|
|
|
|
|
|
|
|
let port
|
2023-10-16 21:23:38 +00:00
|
|
|
const url = route => 'http://127.0.0.1:' + port + route
|
2020-11-14 22:10:16 +00:00
|
|
|
const rootDir = resolve(__dirname, '..', 'fixtures/full-static')
|
|
|
|
const distDir = resolve(rootDir, '.nuxt-generate')
|
|
|
|
|
|
|
|
let builder
|
|
|
|
let server = null
|
|
|
|
let generator = null
|
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
const generateAndStartServer = async (overrides) => {
|
|
|
|
const config = await loadFixture('full-static', {
|
|
|
|
generate: {
|
|
|
|
static: false,
|
|
|
|
dir: '.nuxt-generate'
|
|
|
|
},
|
|
|
|
...(overrides || {})
|
|
|
|
})
|
|
|
|
const nuxt = new Nuxt(config)
|
|
|
|
await nuxt.ready()
|
|
|
|
|
|
|
|
builder = new Builder(nuxt)
|
|
|
|
builder.build = jest.fn()
|
|
|
|
generator = new Generator(nuxt, builder)
|
2020-11-14 22:10:16 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
await generator.generate()
|
2020-11-14 22:10:16 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
const serve = serveStatic(distDir)
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
serve(req, res, finalhandler(req, res))
|
|
|
|
})
|
|
|
|
|
|
|
|
port = await getPort()
|
|
|
|
server.listen(port)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('full-static', () => {
|
|
|
|
describe('with scripts', () => {
|
|
|
|
beforeAll(async () => await generateAndStartServer())
|
2020-11-14 22:10:16 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
test('/payload (custom build.publicPath)', async () => {
|
|
|
|
const { body: html } = await rp(url('/payload'))
|
|
|
|
|
|
|
|
expect(html).toContain('<script src="https://cdn.nuxtjs.org/test/')
|
|
|
|
expect(html).toContain(
|
|
|
|
'<link rel="preload" href="https://cdn.nuxtjs.org/test/_nuxt/static/'
|
|
|
|
)
|
2020-11-14 22:10:16 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
test('/encoding/中文', async () => {
|
|
|
|
const { body: html } = await rp(url('/encoding/中文'))
|
2020-11-14 22:10:16 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
const paths = ['encoding/中文/state.js', 'encoding/中文/payload.js']
|
|
|
|
|
|
|
|
paths.forEach((path) => {
|
|
|
|
const files = glob.sync(join(distDir, '**', path))
|
|
|
|
expect(html).toContain(encodeURI(path))
|
|
|
|
expect(files).toContainEqual(expect.stringContaining(path))
|
|
|
|
})
|
|
|
|
})
|
2020-11-14 22:10:16 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
|
|
|
afterAll(async () => {
|
|
|
|
await server.close()
|
|
|
|
})
|
2021-02-02 12:38:54 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
describe('without scripts', () => {
|
|
|
|
beforeAll(async () => await generateAndStartServer({ render: { injectScripts: false } }))
|
2021-02-02 12:38:54 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
test('should not inject scripts', async () => {
|
|
|
|
const { body: html } = await rp(url('/payload'))
|
2021-02-02 12:38:54 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
expect(html).not.toContain('<script')
|
|
|
|
expect(html).not.toContain('<link')
|
2021-02-02 12:38:54 +00:00
|
|
|
})
|
2020-11-14 22:10:16 +00:00
|
|
|
|
2021-03-01 20:29:51 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
|
|
|
afterAll(async () => {
|
|
|
|
await server.close()
|
|
|
|
})
|
2020-11-14 22:10:16 +00:00
|
|
|
})
|
|
|
|
})
|