Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #421

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [ 14.x, 16.x, 18.x ]
node-version: [ 14.x, 16.x, 18.x, 20.x ]
os: [ windows-latest, ubuntu-latest, macOS-latest ]

# Go
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint": "~8.54.0",
"mime-types": "~2.1.35",
"mock-fs": "~5.2.0",
"mock-tmp": "~0.0.2",
"nyc": "~15.1.0",
"proxyquire": "~2.1.3",
"tap-arc": "~1.2.2",
Expand Down
40 changes: 24 additions & 16 deletions test/unit/src/read/_local-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
let test = require('tape')
let mockfs = require('mock-fs')
let mockTmp = require('mock-tmp')
let proxyquire = require('proxyquire')
let { join } = require('path')
let crypto = require('crypto')
let env = process.env.ARC_ENV
let sandboxPath = join(process.cwd(), 'public')
let public = 'public'
let sandboxPath
let setSandboxPath = tmp => sandboxPath = join(tmp, public)
let isNode18 = require('../../../../src/lib/is-node-18')
if (!isNode18) {
/**
Expand Down Expand Up @@ -39,7 +41,7 @@ if (!isNode18) {
let b64 = buf => Buffer.from(buf).toString('base64')
function reset () {
process.env.ARC_ENV = env
mockfs.restore()
mockTmp.reset()
}

// File contents
Expand Down Expand Up @@ -75,9 +77,10 @@ if (!isNode18) {

test('Local proxy reader returns formatted response from text payload (200)', async t => {
t.plan(6)
mockfs({
[join(sandboxPath, imgName)]: imgContents
let tmp = mockTmp({
[join(public, imgName)]: imgContents
})
setSandboxPath(tmp)
let result = await readLocal(read())
t.equal(result.statusCode, 200, 'Returns statusCode: 200')
t.equal(result.headers['cache-control'], defaultCacheControl, 'Returns correct cache-control')
Expand All @@ -90,9 +93,10 @@ if (!isNode18) {

test('Local proxy reader returns formatted response from binary payload (200)', async t => {
t.plan(2)
mockfs({
[join(sandboxPath, imgName)]: Buffer.from(binary)
let tmp = mockTmp({
[join(public, imgName)]: Buffer.from(binary)
})
setSandboxPath(tmp)
let result = await readLocal(read())
t.equal(result.headers['etag'], hash(Buffer.from(binary)), 'Returns correct ETag')
t.equal(result.body, b64(binary), 'Returns correct body')
Expand All @@ -105,9 +109,10 @@ if (!isNode18) {
process.env.ARC_STATIC_PREFIX = 'foobar'
t.ok(process.env.ARC_STATIC_PREFIX, 'ARC_STATIC_PREFIX set')

mockfs({
[join(sandboxPath, imgName)]: imgContents
let tmp = mockTmp({
[join(public, imgName)]: imgContents
})
setSandboxPath(tmp)
let params = read({ Key: `${process.env.ARC_STATIC_PREFIX}/${imgName}` })
let result = await readLocal(params)
t.equal(result.statusCode, 200, 'Returns statusCode: 200')
Expand All @@ -122,9 +127,10 @@ if (!isNode18) {

test('Local proxy reader returns 304 (aka S3 NotModified)', async t => {
t.plan(2)
mockfs({
[join(sandboxPath, imgName)]: imgContents
let tmp = mockTmp({
[join(public, imgName)]: imgContents
})
setSandboxPath(tmp)
let params = read({ IfNoneMatch: hash(imgContents) })
let result = await readLocal(params)
t.equal(result.statusCode, 304, 'Returns statusCode of 304 if ETag matches')
Expand All @@ -136,10 +142,11 @@ if (!isNode18) {
t.plan(3)
// Tests to ensure ${ARC_STATIC('foo.gif')} doesn't use fingerprinted filenames locally
process.env.ARC_ENV = 'staging'
mockfs({
[join(sandboxPath, mdName)]: mdContents,
[join(sandboxPath, imgName)]: imgContents
let tmp = mockTmp({
[join(public, mdName)]: mdContents,
[join(public, imgName)]: imgContents
})
setSandboxPath(tmp)
let params = read({ Key: mdName, config: { assets: staticStub, sandboxPath } })
let result = await readLocal(params)
t.notEqual(result.body, b64(mdContents), `Contents containing template calls mutated: ${dec(result.body)}`)
Expand All @@ -150,9 +157,10 @@ if (!isNode18) {

test('Local proxy reader hands off to pretty URLifier if file case does not match', async t => {
t.plan(1)
mockfs({
[join(sandboxPath, mdName)]: mdContents,
let tmp = mockTmp({
[join(public, mdName)]: mdContents,
})
setSandboxPath(tmp)
let params = read({ Key: mdName.toUpperCase(), config: { sandboxPath } })
let result = await readLocal(params)
t.equal(result, 'pretty', 'File not found returns response from pretty')
Expand Down
21 changes: 10 additions & 11 deletions test/unit/src/read/_pretty-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let isNode18 = require('../../../../src/lib/is-node-18')
let test = require('tape')
let mockfs = require('mock-fs')
let mockTmp = require('mock-tmp')
let proxyquire = require('proxyquire')
let { join } = require('path')
let env = process.env.ARC_ENV
Expand Down Expand Up @@ -42,14 +42,15 @@ class S3Stub {

let reset = () => {
Key = isFolder = errorState = undefined
mockfs.restore()
mockTmp.reset()
}

let sut = join(process.cwd(), 'src', 'read', '_pretty')
let pretty

let Key
let isFolder
let tmp
let Bucket = 'a-bucket'
let headers = {}

Expand Down Expand Up @@ -105,15 +106,15 @@ if (!isNode18) {
// Local
process.env.ARC_ENV = 'testing'
let msg = 'got ok/hi/index.html from local!'
mockfs({
tmp = mockTmp({
'ok/hi/index.html': buf(msg)
})
result = await pretty({
Bucket,
Key,
headers,
isFolder,
sandboxPath: '',
sandboxPath: tmp,
})
t.equal(result.body, msg, 'Successfully peeked into a local folder without a trailing slash')
reset()
Expand Down Expand Up @@ -192,15 +193,15 @@ if (!isNode18) {

// Local
process.env.ARC_ENV = 'testing'
// Update mockfs to find a 404
// Update mockTmp to find a 404
let msg = 'got 404 from local!'
mockfs({ '404.html': buf(msg) })
let tmp = mockTmp({ '404.html': buf(msg) })
result = await pretty({
Bucket,
Key,
headers,
isFolder,
sandboxPath: '',
sandboxPath: tmp,
})
t.equal(result.statusCode, 404, 'Returns statusCode of 404 with custom 404 error from local')
t.equal(result.body, msg, 'Output is custom 404 page from local')
Expand All @@ -225,8 +226,6 @@ if (!isNode18) {

// Local
process.env.ARC_ENV = 'testing'
// Update mockfs to find a nothing
mockfs({})
Key = 'cantfindme'
errorState = 'NoSuchKey'
result = await pretty({
Expand All @@ -240,7 +239,7 @@ if (!isNode18) {
t.match(result.body, /NoSuchKey/, 'Error message included in response from local')

// Check casing
mockfs({
tmp = mockTmp({
'404.HTML': 'yo'
})
errorState = 'NoSuchKey'
Expand All @@ -249,7 +248,7 @@ if (!isNode18) {
Key,
headers,
isFolder,
sandboxPath: '',
sandboxPath: tmp,
})
t.equal(result.statusCode, 404, 'Returns statusCode of 404 if local file is not found')
t.match(result.body, /NoSuchKey/, 'Error message included in response from local')
Expand Down