From d8228d83249a1dbb630ed1dfa8cf0ad2251fdf73 Mon Sep 17 00:00:00 2001 From: Angel Mendez Date: Fri, 28 Jul 2023 11:36:36 -0600 Subject: [PATCH] refactor: 130.eleventy.test.cjs (#5894) * refactor: 130.eleventy.test.cjs - convert file to ESM - replace ava with vitest - replace got with node-fetch - rename and move file Related to: #5698 * refactor: 130.eleventy.test.cjs - convert file to ESM - replace ava with vitest - replace got with node-fetch - rename and move file Related to: #5698 * fix: update cwd path on startDevServer * test: run tests concurrently * test: remove skip on test with technical debt - remove skip on test due to the fact that the issue commented in technical debt is already solved * fix: match with most of the original test headers * style: headers reorganization --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- tests/integration/130.eleventy.test.cjs | 145 --------------- .../integration/frameworks/eleventy.test.mjs | 166 ++++++++++++++++++ 2 files changed, 166 insertions(+), 145 deletions(-) delete mode 100644 tests/integration/130.eleventy.test.cjs create mode 100644 tests/integration/frameworks/eleventy.test.mjs diff --git a/tests/integration/130.eleventy.test.cjs b/tests/integration/130.eleventy.test.cjs deleted file mode 100644 index de160ff0f4a..00000000000 --- a/tests/integration/130.eleventy.test.cjs +++ /dev/null @@ -1,145 +0,0 @@ -const { Buffer } = require('buffer') -const path = require('path') - -const test = require('ava') - -const { clientIP, originalIP } = require('../lib/local-ip.cjs') - -const { startDevServer } = require('./utils/dev-server.cjs') -const got = require('./utils/got.cjs') - -test.before(async (t) => { - const server = await startDevServer({ cwd: path.join(__dirname, '__fixtures__/eleventy-site') }) - - t.context.server = server -}) - -test.after(async (t) => { - const { server } = t.context - await server.close() -}) - -test('homepage', async (t) => { - const { url } = t.context.server - const response = await got(`${url}/`).text() - - t.true(response.includes('Eleventy Site')) -}) - -test('redirect test', async (t) => { - const { url } = t.context.server - const { body, headers, statusCode } = await got(`${url}/something`, { followRedirect: false }) - - t.is(statusCode, 301) - t.is(headers.location, `/otherthing`) - t.is(body, 'Redirecting to /otherthing') -}) - -// TODO: un-skip this once https://github.com/netlify/cli/issues/1242 is fixed -test.skip('normal rewrite', async (t) => { - const { url } = t.context.server - const { body, headers, statusCode } = await got(`${url}/doesnt-exist`) - - t.is(statusCode, 200) - t.true(headers['content-type'].startsWith('text/html')) - t.true(body.includes('Eleventy Site')) -}) - -test('force rewrite', async (t) => { - const { url } = t.context.server - const { body, headers, statusCode } = await got(`${url}/force`) - - t.is(statusCode, 200) - t.true(headers['content-type'].startsWith('text/html')) - t.true(body.includes('

Test content

')) -}) - -test('functions rewrite echo without body', async (t) => { - const { host, port, url } = t.context.server - const response = await got(`${url}/api/echo?ding=dong`).json() - const { 'x-nf-request-id': requestID, ...headers } = response.headers - - t.is(response.body, undefined) - t.deepEqual(headers, { - accept: 'application/json', - 'accept-encoding': 'gzip, deflate, br', - 'client-ip': clientIP, - connection: 'close', - host: `${host}:${port}`, - 'user-agent': 'got (https://github.com/sindresorhus/got)', - 'x-forwarded-for': originalIP, - 'x-nf-account-id': '', - 'x-nf-client-connection-ip': clientIP, - 'x-nf-geo': Buffer.from( - '{"city":"San Francisco","country":{"code":"US","name":"United States"},"subdivision":{"code":"CA","name":"California"},"longitude":0,"latitude":0,"timezone":"UTC"}', - ).toString('base64'), - }) - t.is(requestID.length, 26) - t.is(response.httpMethod, 'GET') - t.is(response.isBase64Encoded, true) - t.is(response.path, '/api/echo') - t.deepEqual(response.queryStringParameters, { ding: 'dong' }) -}) - -test('functions rewrite echo with body', async (t) => { - const { host, port, url } = t.context.server - const response = await got - .post(`${url}/api/echo?ding=dong`, { - headers: { - 'content-type': 'application/x-www-form-urlencoded', - }, - body: 'some=thing', - }) - .json() - const { 'x-nf-request-id': requestID, ...headers } = response.headers - - t.is(response.body, 'some=thing') - t.deepEqual(headers, { - accept: 'application/json', - 'accept-encoding': 'gzip, deflate, br', - 'client-ip': clientIP, - connection: 'close', - host: `${host}:${port}`, - 'content-type': 'application/x-www-form-urlencoded', - 'content-length': '10', - 'user-agent': 'got (https://github.com/sindresorhus/got)', - 'x-forwarded-for': originalIP, - 'x-nf-account-id': '', - 'x-nf-client-connection-ip': clientIP, - 'x-nf-geo': Buffer.from( - '{"city":"San Francisco","country":{"code":"US","name":"United States"},"subdivision":{"code":"CA","name":"California"},"longitude":0,"latitude":0,"timezone":"UTC"}', - ).toString('base64'), - }) - t.is(requestID.length, 26) - t.is(response.httpMethod, 'POST') - t.is(response.isBase64Encoded, false) - t.is(response.path, '/api/echo') - t.deepEqual(response.queryStringParameters, { ding: 'dong' }) -}) - -test('functions echo with multiple query params', async (t) => { - const { host, port, url } = t.context.server - const response = await got(`${url}/.netlify/functions/echo?category=a&category=b`).json() - const { 'x-nf-request-id': requestID, ...headers } = response.headers - - t.deepEqual(headers, { - accept: 'application/json', - 'accept-encoding': 'gzip, deflate, br', - 'client-ip': clientIP, - connection: 'close', - host: `${host}:${port}`, - 'user-agent': 'got (https://github.com/sindresorhus/got)', - 'x-forwarded-for': originalIP, - 'x-nf-account-id': '', - 'x-nf-client-connection-ip': clientIP, - 'x-nf-geo': Buffer.from( - '{"city":"San Francisco","country":{"code":"US","name":"United States"},"subdivision":{"code":"CA","name":"California"},"longitude":0,"latitude":0,"timezone":"UTC"}', - ).toString('base64'), - }) - t.is(requestID.length, 26) - t.is(response.httpMethod, 'GET') - t.is(response.isBase64Encoded, true) - t.is(response.path, '/.netlify/functions/echo') - t.deepEqual(response.queryStringParameters, { category: 'a, b' }) - t.deepEqual(response.multiValueQueryStringParameters, { category: ['a', 'b'] }) -}) diff --git a/tests/integration/frameworks/eleventy.test.mjs b/tests/integration/frameworks/eleventy.test.mjs new file mode 100644 index 00000000000..0157e7890ff --- /dev/null +++ b/tests/integration/frameworks/eleventy.test.mjs @@ -0,0 +1,166 @@ +import { Buffer } from 'buffer' +import path from 'path' +import { fileURLToPath } from 'url' + +import fetch from 'node-fetch' +import { afterAll, beforeAll, describe, test } from 'vitest' + +import { clientIP, originalIP } from '../../lib/local-ip.cjs' +import { startDevServer } from '../utils/dev-server.cjs' + +// eslint-disable-next-line no-underscore-dangle +const __dirname = path.dirname(fileURLToPath(import.meta.url)) + +const context = {} + +beforeAll(async () => { + const server = await startDevServer({ cwd: path.join(__dirname, '../__fixtures__/eleventy-site') }) + + context.server = server +}) + +afterAll(async () => { + const { server } = context + await server.close() +}) + +describe.concurrent('eleventy', () => { + test('homepage', async (t) => { + const { url } = context.server + const response = await fetch(`${url}/`).then((res) => res.text()) + + t.expect(response.includes('Eleventy Site')).toBe(true) + }) + + test('redirect test', async (t) => { + const { url } = context.server + const response = await fetch(`${url}/something`, { + redirect: 'manual', + }) + const { headers, status } = response + + t.expect(status).toBe(301) + t.expect(headers.get('location').endsWith('/otherthing')).toBe(true) + t.expect(await response.text()).toEqual('Redirecting to /otherthing') + }) + + test('normal rewrite', async (t) => { + const { url } = context.server + const response = await fetch(`${url}/doesnt-exist`) + const { headers, status } = response + const body = await response.text() + + t.expect(status).toBe(200) + t.expect(headers.get('content-type').startsWith('text/html')).toBe(true) + t.expect(body.includes('Eleventy Site')).toBe(true) + }) + + test('force rewrite', async (t) => { + const { url } = context.server + const response = await fetch(`${url}/force`) + const { headers, status } = response + const body = await response.text() + + t.expect(status).toBe(200) + t.expect(headers.get('content-type').startsWith('text/html')).toBe(true) + t.expect(body.includes('

Test content

')).toBe(true) + }) + + test('functions rewrite echo without body', async (t) => { + const { host, port, url } = context.server + const jsonResponse = await fetch(`${url}/api/echo?ding=dong`, { + headers: { accept: 'application/json', 'accept-encoding': 'gzip, deflate, br' }, + }).then((res) => res.json()) + const { 'x-nf-request-id': requestID, ...headers } = jsonResponse.headers + + t.expect(jsonResponse.body).toBe(undefined) + t.expect(headers).toStrictEqual({ + accept: 'application/json', + 'accept-encoding': 'gzip, deflate, br', + 'client-ip': clientIP, + connection: 'close', + host: `${host}:${port}`, + 'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)', + 'x-forwarded-for': originalIP, + 'x-nf-account-id': '', + 'x-nf-client-connection-ip': clientIP, + 'x-nf-geo': Buffer.from( + '{"city":"San Francisco","country":{"code":"US","name":"United States"},"subdivision":{"code":"CA","name":"California"},"longitude":0,"latitude":0,"timezone":"UTC"}', + ).toString('base64'), + }) + t.expect(requestID.length).toBe(26) + t.expect(jsonResponse.httpMethod).toEqual('GET') + t.expect(jsonResponse.isBase64Encoded).toBe(true) + t.expect(jsonResponse.path).toEqual('/api/echo') + t.expect(jsonResponse.queryStringParameters).toStrictEqual({ ding: 'dong' }) + }) + + test('functions rewrite echo with body', async (t) => { + const { host, port, url } = context.server + const response = await fetch(`${url}/api/echo?ding=dong`, { + method: 'POST', + headers: { + accept: 'application/json', + 'accept-encoding': 'gzip, deflate, br', + 'content-type': 'application/x-www-form-urlencoded', + }, + body: 'some=thing', + }).then((res) => res.json()) + const { 'x-nf-request-id': requestID, ...headers } = response.headers + + t.expect(response.body).toEqual('some=thing') + t.expect(headers).toStrictEqual({ + accept: 'application/json', + 'accept-encoding': 'gzip, deflate, br', + 'client-ip': clientIP, + connection: 'close', + host: `${host}:${port}`, + 'content-type': 'application/x-www-form-urlencoded', + 'content-length': '10', + 'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)', + 'x-forwarded-for': originalIP, + 'x-nf-account-id': '', + 'x-nf-client-connection-ip': clientIP, + 'x-nf-geo': Buffer.from( + '{"city":"San Francisco","country":{"code":"US","name":"United States"},"subdivision":{"code":"CA","name":"California"},"longitude":0,"latitude":0,"timezone":"UTC"}', + ).toString('base64'), + }) + t.expect(requestID.length).toBe(26) + t.expect(response.httpMethod).toEqual('POST') + t.expect(response.isBase64Encoded).toBe(false) + t.expect(response.path).toEqual('/api/echo') + t.expect(response.queryStringParameters).toStrictEqual({ ding: 'dong' }) + }) + + test('functions echo with multiple query params', async (t) => { + const { host, port, url } = context.server + const response = await fetch(`${url}/.netlify/functions/echo?category=a&category=b`, { + headers: { + accept: 'application/json', + 'accept-encoding': 'gzip, deflate, br', + }, + }).then((res) => res.json()) + const { 'x-nf-request-id': requestID, ...headers } = response.headers + + t.expect(headers).toStrictEqual({ + accept: 'application/json', + 'accept-encoding': 'gzip, deflate, br', + 'client-ip': clientIP, + connection: 'close', + host: `${host}:${port}`, + 'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)', + 'x-forwarded-for': originalIP, + 'x-nf-account-id': '', + 'x-nf-client-connection-ip': clientIP, + 'x-nf-geo': Buffer.from( + '{"city":"San Francisco","country":{"code":"US","name":"United States"},"subdivision":{"code":"CA","name":"California"},"longitude":0,"latitude":0,"timezone":"UTC"}', + ).toString('base64'), + }) + t.expect(requestID.length).toBe(26) + t.expect(response.httpMethod).toEqual('GET') + t.expect(response.isBase64Encoded).toBe(true) + t.expect(response.path).toEqual('/.netlify/functions/echo') + t.expect(response.queryStringParameters).toStrictEqual({ category: 'a, b' }) + t.expect(response.multiValueQueryStringParameters).toStrictEqual({ category: ['a', 'b'] }) + }) +})