Skip to content

Commit cc82ac5

Browse files
committed
test: added some tests for custom headers
1 parent 9d83815 commit cc82ac5

File tree

2 files changed

+133
-4
lines changed

2 files changed

+133
-4
lines changed

test/__snapshots__/index.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`custom headers appends custom headers in the Netlify configuration 1`] = `
4+
Array [
5+
Object {
6+
"for": "/",
7+
"values": Object {
8+
"X-Existing-Header": "true",
9+
},
10+
},
11+
Object {
12+
"for": "/",
13+
"values": Object {
14+
"X-Unit-Test": "true",
15+
},
16+
},
17+
Object {
18+
"for": "/unit-test",
19+
"values": Object {
20+
"X-Another-Unit-Test": "true",
21+
"X-Another-Unit-Test-Again": "true",
22+
},
23+
},
24+
]
25+
`;
26+
27+
exports[`custom headers sets custom headers in the Netlify configuration 1`] = `
28+
Array [
29+
Object {
30+
"for": "/",
31+
"values": Object {
32+
"X-Unit-Test": "true",
33+
},
34+
},
35+
Object {
36+
"for": "/unit-test",
37+
"values": Object {
38+
"X-Another-Unit-Test": "true",
39+
"X-Another-Unit-Test-Again": "true",
40+
},
41+
},
42+
]
43+
`;
44+
345
exports[`onBuild() generates a file referencing all page sources 1`] = `
446
"// This file is purely to allow nft to know about these pages. It should be temporary.
547
exports.resolvePages = () => {

test/index.js

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ const {
2525
patchNextFiles,
2626
unpatchNextFiles,
2727
} = require('../plugin/src/helpers/files')
28-
const { getRequiredServerFiles, updateRequiredServerFiles } = require('../plugin/src/helpers/config')
28+
const {
29+
getRequiredServerFiles,
30+
updateRequiredServerFiles,
31+
generateCustomHeaders,
32+
} = require('../plugin/src/helpers/config')
2933
const { dirname } = require('path')
3034
const { getProblematicUserRewrites } = require('../plugin/src/helpers/verification')
35+
const { onPostBuild } = require('../plugin/lib')
3136

3237
const FIXTURES_DIR = `${__dirname}/fixtures`
3338
const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default`
@@ -246,11 +251,11 @@ describe('onBuild()', () => {
246251
process.env.URL = mockSiteUrl
247252

248253
await moveNextDist()
249-
254+
250255
const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish)
251-
initialConfig.config.basePath = "/foo"
256+
initialConfig.config.basePath = '/foo'
252257
await updateRequiredServerFiles(netlifyConfig.build.publish, initialConfig)
253-
258+
254259
await plugin.onBuild(defaultArgs)
255260

256261
expect(onBuildHasRun(netlifyConfig)).toBe(true)
@@ -788,3 +793,85 @@ describe('function helpers', () => {
788793
)
789794
})
790795
})
796+
797+
describe.only('custom headers', () => {
798+
it('sets custom headers in the Netlify configuration', async () => {
799+
netlifyConfig.headers = []
800+
const headers = [
801+
// single header for a route
802+
{
803+
source: '/',
804+
headers: [
805+
{
806+
key: 'X-Unit-Test',
807+
value: 'true',
808+
},
809+
],
810+
regex: '^/(?:/)?$',
811+
},
812+
// multiple headers for a route
813+
{
814+
source: '/unit-test',
815+
headers: [
816+
{
817+
key: 'X-Another-Unit-Test',
818+
value: 'true',
819+
},
820+
{
821+
key: 'X-Another-Unit-Test-Again',
822+
value: 'true',
823+
},
824+
],
825+
regex: '^/(?:/)?$',
826+
},
827+
]
828+
829+
generateCustomHeaders(headers, netlifyConfig.headers)
830+
831+
expect(netlifyConfig.headers).toMatchSnapshot()
832+
})
833+
834+
it('appends custom headers in the Netlify configuration', async () => {
835+
netlifyConfig.headers = [
836+
{
837+
for: '/',
838+
values: {
839+
'X-Existing-Header': 'true',
840+
},
841+
},
842+
]
843+
844+
const headers = [
845+
// single header for a route
846+
{
847+
source: '/',
848+
headers: [
849+
{
850+
key: 'X-Unit-Test',
851+
value: 'true',
852+
},
853+
],
854+
regex: '^/(?:/)?$',
855+
},
856+
// multiple headers for a route
857+
{
858+
source: '/unit-test',
859+
headers: [
860+
{
861+
key: 'X-Another-Unit-Test',
862+
value: 'true',
863+
},
864+
{
865+
key: 'X-Another-Unit-Test-Again',
866+
value: 'true',
867+
},
868+
],
869+
regex: '^/(?:/)?$',
870+
},
871+
]
872+
873+
generateCustomHeaders(headers, netlifyConfig.headers)
874+
875+
expect(netlifyConfig.headers).toMatchSnapshot()
876+
})
877+
})

0 commit comments

Comments
 (0)