Skip to content

Commit

Permalink
fix(command-dev): match root path to /* headers (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
evans authored Dec 3, 2020
1 parent 7090638 commit 4b991db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/utils/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const matchPaths = function (rulePath, targetPath) {
const rulePathParts = rulePath.split('/').filter(Boolean)
const targetPathParts = targetPath.split('/').filter(Boolean)

if (rulePathParts.length === 0 && targetPathParts.length === 0) {
if (
targetPathParts.length === 0 &&
(rulePathParts.length === 0 || (rulePathParts.length === 1 && rulePathParts[0] === '*'))
) {
return true
}

Expand Down
21 changes: 14 additions & 7 deletions src/utils/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const headers = [
{ path: '/', headers: ['X-Frame-Options: SAMEORIGIN'] },
{ path: '/*', headers: ['X-Frame-Thing: SAMEORIGIN'] },
{
path: '/something/*',
path: '/static-path/*',
headers: [
'X-Frame-Options: DENY',
'X-XSS-Protection: 1; mode=block',
Expand All @@ -20,7 +20,7 @@ const headers = [
'cache-control: must-revalidate',
],
},
{ path: '/:ding/index.html', headers: ['X-Frame-Options: SAMEORIGIN'] },
{ path: '/:placeholder/index.html', headers: ['X-Frame-Options: SAMEORIGIN'] },
]

test.before(async (t) => {
Expand All @@ -47,12 +47,12 @@ test('_headers: validate correct parsing', (t) => {
'/*': {
'X-Frame-Thing': ['SAMEORIGIN'],
},
'/something/*': {
'/static-path/*': {
'X-Frame-Options': ['DENY'],
'X-XSS-Protection': ['1; mode=block'],
'cache-control': ['max-age=0', 'no-cache', 'no-store', 'must-revalidate'],
},
'/:ding/index.html': {
'/:placeholder/index.html': {
'X-Frame-Options': ['SAMEORIGIN'],
},
})
Expand All @@ -62,17 +62,24 @@ test('_headers: rulesForPath testing', (t) => {
const rules = parseHeadersFile(path.resolve(t.context.builder.directory, '_headers'))
t.deepEqual(objectForPath(rules, '/'), {
'X-Frame-Options': ['SAMEORIGIN'],
'X-Frame-Thing': ['SAMEORIGIN'],
})
t.deepEqual(objectForPath(rules, '/placeholder'), {
'X-Frame-Thing': ['SAMEORIGIN'],
})
t.deepEqual(objectForPath(rules, '/ding'), {
t.deepEqual(objectForPath(rules, '/static-path/placeholder'), {
'X-Frame-Thing': ['SAMEORIGIN'],
'X-Frame-Options': ['DENY'],
'X-XSS-Protection': ['1; mode=block'],
'cache-control': ['max-age=0', 'no-cache', 'no-store', 'must-revalidate'],
})
t.deepEqual(objectForPath(rules, '/something/ding'), {
t.deepEqual(objectForPath(rules, '/static-path'), {
'X-Frame-Thing': ['SAMEORIGIN'],
'X-Frame-Options': ['DENY'],
'X-XSS-Protection': ['1; mode=block'],
'cache-control': ['max-age=0', 'no-cache', 'no-store', 'must-revalidate'],
})
t.deepEqual(objectForPath(rules, '/ding/index.html'), {
t.deepEqual(objectForPath(rules, '/placeholder/index.html'), {
'X-Frame-Options': ['SAMEORIGIN'],
'X-Frame-Thing': ['SAMEORIGIN'],
})
Expand Down
40 changes: 40 additions & 0 deletions tests/command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ testMatrix.forEach(({ args }) => {
})
})

test(testName('should return user defined headers when / is accessed', args), async (t) => {
await withSiteBuilder('site-with-index-file', async (builder) => {
builder.withContentFile({
path: 'index.html',
content: '<h1>⊂◉‿◉つ</h1>',
})

const headerName = 'X-Frame-Options'
const headerValue = 'SAMEORIGIN'
builder.withHeadersFile({ headers: [{ path: '/*', headers: [`${headerName}: ${headerValue}`] }] })

await builder.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async (server) => {
const responseHeaders = await fetch(server.url).then((res) => res.headers)
t.is(responseHeaders.get(headerName), headerValue)
})
})
})

test(testName('should return user defined headers when non-root path is accessed', args), async (t) => {
await withSiteBuilder('site-with-index-file', async (builder) => {
builder.withContentFile({
path: 'foo/index.html',
content: '<h1>⊂◉‿◉つ</h1>',
})

const headerName = 'X-Frame-Options'
const headerValue = 'SAMEORIGIN'
builder.withHeadersFile({ headers: [{ path: '/*', headers: [`${headerName}: ${headerValue}`] }] })

await builder.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async (server) => {
const responseHeaders = await fetch(`${server.url}/foo`).then((res) => res.headers)
t.is(responseHeaders.get(headerName), headerValue)
})
})
})

test(testName('should return response from a function with setTimeout', args), async (t) => {
await withSiteBuilder('site-with-set-timeout-function', async (builder) => {
builder.withNetlifyToml({ config: { build: { functions: 'functions' } } }).withFunction({
Expand Down

0 comments on commit 4b991db

Please sign in to comment.