Skip to content

Commit

Permalink
refactor: 130.eleventy.test.cjs (#5894)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
hereje and kodiakhq[bot] authored Jul 28, 2023
1 parent 0897962 commit d8228d8
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 145 deletions.
145 changes: 0 additions & 145 deletions tests/integration/130.eleventy.test.cjs

This file was deleted.

166 changes: 166 additions & 0 deletions tests/integration/frameworks/eleventy.test.mjs
Original file line number Diff line number Diff line change
@@ -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('<h1>Test content</h1>')).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'] })
})
})

1 comment on commit d8228d8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,303
  • Package size: 271 MB

Please sign in to comment.