From 5945845c76a0a96c90aa3f05856acf87f7a513d7 Mon Sep 17 00:00:00 2001 From: "Xin Du (Clark)" Date: Mon, 2 Dec 2019 15:23:56 +0000 Subject: [PATCH] test: use `got` instead of `request` (#6740) --- package.json | 2 +- test/unit/basic.dev.test.js | 10 +-- test/unit/basic.generate.test.js | 12 +-- test/unit/basic.ssr.csp.test.js | 128 ++++++++--------------------- test/unit/basic.ssr.test.js | 14 ++-- test/unit/cli.test.js | 2 +- test/unit/custom-dirs.test.js | 4 +- test/unit/dist-options.test.js | 12 +-- test/unit/express.test.js | 2 +- test/unit/modern.client.test.js | 10 +-- test/unit/modern.server.test.js | 16 ++-- test/unit/modern.spa.test.js | 12 +-- test/unit/module.test.js | 6 +- test/unit/ssr.test.js | 2 +- test/unit/unicode-base.test.js | 2 +- test/unit/with-config.test.js | 8 +- test/utils/index.js | 2 +- yarn.lock | 136 ++++++++++++++++++++++++++++++- 18 files changed, 213 insertions(+), 167 deletions(-) diff --git a/package.json b/package.json index 65810d10de..d23021a973 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "fs-extra": "^8.1.0", "get-port": "^5.0.0", "glob": "^7.1.6", + "got": "^9.6.0", "is-wsl": "^2.1.1", "jest": "^24.9.0", "jest-junit": "^9.0.0", @@ -61,7 +62,6 @@ "node-sass": "^4.13.0", "puppeteer-core": "^2.0.0", "request": "^2.88.0", - "request-promise-native": "^1.0.8", "rimraf": "^3.0.0", "rollup": "^1.27.5", "rollup-plugin-alias": "^2.2.0", diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 30538751a1..669d499f6e 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -141,17 +141,14 @@ describe('basic dev', () => { // }) test('/__open-in-editor (open-in-editor)', async () => { - const { body } = await rp( - url('/__open-in-editor?file=pages/index.vue'), - { resolveWithFullResponse: true } - ) + const { body } = await rp(url('/__open-in-editor?file=pages/index.vue')) expect(body).toBe('') }) test('/__open-in-editor should return error (open-in-editor)', async () => { await expect(rp(url('/__open-in-editor?file='))).rejects.toMatchObject({ statusCode: 500, - error: 'launch-editor-middleware: required query param "file" is missing.' + body: 'launch-editor-middleware: required query param "file" is missing.' }) }) @@ -165,8 +162,7 @@ describe('basic dev', () => { const opts = { headers: { accept: 'application/json' - }, - resolveWithFullResponse: true + } } await expect(rp(url('/error'), opts)).rejects.toMatchObject({ statusCode: 500, diff --git a/test/unit/basic.generate.test.js b/test/unit/basic.generate.test.js index e12fedde9e..54af239338 100644 --- a/test/unit/basic.generate.test.js +++ b/test/unit/basic.generate.test.js @@ -137,7 +137,7 @@ describe('basic generate', () => { }) test('/users/1/index.html', async () => { - const html = await rp(url('/users/1/index.html')) + const { body: html } = await rp(url('/users/1/index.html')) expect(html).toContain('

User: 1

') expect( existsSync(resolve(distDir, 'users/1/index.html')) @@ -146,12 +146,12 @@ describe('basic generate', () => { }) test('/users/2', async () => { - const html = await rp(url('/users/2')) + const { body: html } = await rp(url('/users/2')) expect(html).toContain('

User: 2

') }) test('/users/3 (payload given)', async () => { - const html = await rp(url('/users/3')) + const { body: html } = await rp(url('/users/3')) expect(html).toContain('

User: 3000

') }) @@ -165,7 +165,7 @@ describe('basic generate', () => { }) test('/validate should not be server-rendered', async () => { - const html = await rp(url('/validate')) + const { body: html } = await rp(url('/validate')) expect(html).toContain('
') expect(html).toContain('serverRendered:!1') }) @@ -183,7 +183,7 @@ describe('basic generate', () => { }) test('/redirect should not be server-rendered', async () => { - const html = await rp(url('/redirect')) + const { body: html } = await rp(url('/redirect')) expect(html).toContain('
') expect(html).toContain('serverRendered:!1') }) @@ -223,7 +223,7 @@ describe('basic generate', () => { }) test('creates /200.html as fallback', async () => { - const html = await rp(url('/200.html')) + const { body: html } = await rp(url('/200.html')) expect(html.includes('

Index page

')).toBe(false) expect(html.includes('data-server-rendered')).toBe(false) expect(existsSync(resolve(distDir, '200.html'))).toBe(true) diff --git a/test/unit/basic.ssr.csp.test.js b/test/unit/basic.ssr.csp.test.js index 43eaf5677c..205cd2ab3c 100644 --- a/test/unit/basic.ssr.csp.test.js +++ b/test/unit/basic.ssr.csp.test.js @@ -34,9 +34,7 @@ describe('basic ssr csp', () => { 'Not contain Content-Security-Policy header, when csp is false', async () => { nuxt = await startCspServer(false) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[cspHeader]).toBe(undefined) } @@ -46,9 +44,7 @@ describe('basic ssr csp', () => { 'Contain Content-Security-Policy header, when csp is set', async () => { nuxt = await startCspServer(true) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[cspHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/) } @@ -58,9 +54,7 @@ describe('basic ssr csp', () => { 'Contain Content-Security-Policy-Report-Only header, when explicitly asked for', async () => { nuxt = await startCspDevServer({ reportOnly: true }) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/) } @@ -70,9 +64,7 @@ describe('basic ssr csp', () => { 'Contain only unique hashes in header when csp is set', async () => { nuxt = await startCspServer(true) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) const hashes = headers[cspHeader].split(' ').filter(s => s.startsWith('\'sha256-')) const uniqueHashes = [...new Set(hashes)] @@ -89,9 +81,7 @@ describe('basic ssr csp', () => { } nuxt = await startCspServer(cspOption) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[cspHeader]).toMatch(/^script-src 'self' 'sha256-.*'/) expect(headers[cspHeader]).toContain('https://example.com') @@ -111,9 +101,7 @@ describe('basic ssr csp', () => { } nuxt = await startCspServer(cspOption) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[cspHeader]).toMatch(/default-src 'none'/) expect(headers[cspHeader]).toMatch(/script-src 'sha256-(.*)?' 'self'/) @@ -133,9 +121,7 @@ describe('basic ssr csp', () => { } nuxt = await startCspServer(cspOption) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[cspHeader]).toMatch(/default-src 'none'/) expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self'$/) @@ -156,14 +142,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) const hashes = headers[cspHeader].split(' ').filter(s => s.startsWith('\'sha256-')) const uniqueHashes = [...new Set(hashes)] @@ -184,14 +166,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) expect(headers[cspHeader]).toMatch(/script-src 'self' 'unsafe-inline'$/) } @@ -210,14 +188,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/) } @@ -237,14 +211,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/) } @@ -256,9 +226,7 @@ describe('basic ssr csp', () => { 'Not contain Content-Security-Policy-Report-Only header, when csp is false', async () => { nuxt = await startCspDevServer(false) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[reportOnlyHeader]).toBe(undefined) } @@ -268,9 +236,7 @@ describe('basic ssr csp', () => { 'Contain Content-Security-Policy header, when explicitly asked for', async () => { nuxt = await startCspDevServer({ reportOnly: false }) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[cspHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/) } @@ -280,9 +246,7 @@ describe('basic ssr csp', () => { 'Contain Content-Security-Policy header, when csp is set', async () => { nuxt = await startCspDevServer(true) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/) } @@ -292,9 +256,7 @@ describe('basic ssr csp', () => { 'Contain only unique hashes in header when csp is set', async () => { nuxt = await startCspDevServer(true) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) const hashes = headers[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-')) const uniqueHashes = [...new Set(hashes)] @@ -311,9 +273,7 @@ describe('basic ssr csp', () => { } nuxt = await startCspDevServer(cspOption) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'/) expect(headers[reportOnlyHeader]).toContain('https://example.com') @@ -333,9 +293,7 @@ describe('basic ssr csp', () => { } nuxt = await startCspDevServer(cspOption) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[reportOnlyHeader]).toMatch(/default-src 'none'/) expect(headers[reportOnlyHeader]).toMatch(/script-src 'sha256-(.*)?' 'self'/) @@ -355,9 +313,7 @@ describe('basic ssr csp', () => { } nuxt = await startCspDevServer(cspOption) - const { headers } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateless')) expect(headers[reportOnlyHeader]).toMatch(/default-src 'none'/) expect(headers[reportOnlyHeader]).toMatch(/script-src 'sha256-.*' 'self'$/) @@ -378,14 +334,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) const hashes = headers[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-')) const uniqueHashes = [...new Set(hashes)] @@ -405,14 +357,10 @@ describe('basic ssr csp', () => { } } nuxt = await startCspDevServer(cspOption) - const { headers: user1Header } = await rp(url('/users/1'), { - resolveWithFullResponse: true - }) + const { headers: user1Header } = await rp(url('/users/1')) const user1Hashes = user1Header[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-')) - const { headers: user2Header } = await rp(url('/users/2'), { - resolveWithFullResponse: true - }) + const { headers: user2Header } = await rp(url('/users/2')) const user2Hashes = new Set(user2Header[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-'))) const intersection = new Set(user1Hashes.filter(x => user2Hashes.has(x))) @@ -432,14 +380,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) expect(headers[reportOnlyHeader]).toMatch(/script-src 'self' 'unsafe-inline'$/) } @@ -458,14 +402,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/) } @@ -485,14 +425,10 @@ describe('basic ssr csp', () => { }) for (let i = 0; i < 5; i++) { - await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + await rp(url('/stateless')) } - const { headers } = await rp(url('/stateful'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/stateful')) expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/) } diff --git a/test/unit/basic.ssr.test.js b/test/unit/basic.ssr.test.js index 43ed4c1257..ba686194fc 100644 --- a/test/unit/basic.ssr.test.js +++ b/test/unit/basic.ssr.test.js @@ -223,8 +223,7 @@ describe('basic ssr', () => { const opts = { headers: { accept: 'application/json' - }, - resolveWithFullResponse: true + } } await expect(rp(url('/error'), opts)).rejects.toMatchObject({ statusCode: 500, @@ -247,7 +246,7 @@ describe('basic ssr', () => { test('/error2 status code', async () => { await expect(rp(url('/error2'))).rejects.toMatchObject({ statusCode: 500, - message: expect.stringContaining('Custom error') + body: expect.stringContaining('Custom error') }) }) @@ -297,12 +296,11 @@ describe('basic ssr', () => { }) test('ETag Header', async () => { - const { headers: { etag } } = await rp(url('/stateless'), { - resolveWithFullResponse: true - }) + const { headers: { etag } } = await rp(url('/stateless')) + // Verify functionality - await expect(rp(url('/stateless'), { headers: { 'If-None-Match': etag } })) - .rejects.toMatchObject({ statusCode: 304 }) + const response = await rp(url('/stateless'), { headers: { 'If-None-Match': etag } }) + await expect(response).toMatchObject({ statusCode: 304 }) }) test('/_nuxt/ should return 404', async () => { diff --git a/test/unit/cli.test.js b/test/unit/cli.test.js index 6959aa0187..c87ef4a329 100644 --- a/test/unit/cli.test.js +++ b/test/unit/cli.test.js @@ -74,7 +74,7 @@ describe.posix('cli', () => { expect(error).toBe(undefined) - const html = await rp(url('/')) + const { body: html } = await rp(url('/')) expect(html).toMatch(('
CLI Test
')) await close(nuxtStart) diff --git a/test/unit/custom-dirs.test.js b/test/unit/custom-dirs.test.js index f19f408999..bd90c012c6 100644 --- a/test/unit/custom-dirs.test.js +++ b/test/unit/custom-dirs.test.js @@ -44,9 +44,7 @@ describe('custom-dirs', () => { }) test('custom static directory', async () => { - const { headers } = await rp(url('/test.txt'), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url('/test.txt')) expect(headers['cache-control']).toBe('public, max-age=0') }) diff --git a/test/unit/dist-options.test.js b/test/unit/dist-options.test.js index c0d781441d..4253306a8d 100644 --- a/test/unit/dist-options.test.js +++ b/test/unit/dist-options.test.js @@ -16,20 +16,14 @@ describe('dist options', () => { }) test('Specify maxAge/index in render.dist options', async () => { - const { body } = await rp(url('/'), { - resolveWithFullResponse: true - }) + const { body } = await rp(url('/')) try { - await rp(url('/_nuxt/'), { - resolveWithFullResponse: true - }) + await rp(url('/_nuxt/')) } catch (err) { expect(err.toString().includes('StatusCodeError')) } const distFile = body.match(/\/_nuxt\/.+?\.js/)[0] - const { headers } = await rp(url(distFile), { - resolveWithFullResponse: true - }) + const { headers } = await rp(url(distFile)) const twoYears = (((60 * 60 * 24 * 365) * 2) / 1000).toString() expect(headers['cache-control']).toContain(twoYears) }) diff --git a/test/unit/express.test.js b/test/unit/express.test.js index a586fe786d..8a1d8fb125 100644 --- a/test/unit/express.test.js +++ b/test/unit/express.test.js @@ -28,7 +28,7 @@ describe('express', () => { }) test('/stateless with express', async () => { - const html = await rp(url('/stateless')) + const { body: html } = await rp(url('/stateless')) expect(html).toContain('

My component!

') }) diff --git a/test/unit/modern.client.test.js b/test/unit/modern.client.test.js index ba2d322b4c..251085667b 100644 --- a/test/unit/modern.client.test.js +++ b/test/unit/modern.client.test.js @@ -14,25 +14,25 @@ describe('modern client mode (SSR)', () => { }) test('should contain nomodule legacy resources', async () => { - const response = await rp(url('/')) + const { body: response } = await rp(url('/')) expect(response).toContain('script nomodule crossorigin="use-credentials" src="/_nuxt/app.js') expect(response).toContain('script nomodule crossorigin="use-credentials" src="/_nuxt/commons.app.js') }) test('should contain module modern resources', async () => { - const response = await rp(url('/')) + const { body: response } = await rp(url('/')) expect(response).toContain('