Skip to content

Commit b50d92c

Browse files
committed
test: added onPostBuild test to check for header generation
1 parent 0d35e36 commit b50d92c

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

test/index.js

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const useFixture = async function (fixtureName) {
138138
await cpy('**', process.cwd(), { cwd: fixtureDir, parents: true, overwrite: true, dot: true })
139139
}
140140

141-
const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, redirects: [] }
141+
const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, redirects: [], headers: [] }
142142
const defaultArgs = {
143143
netlifyConfig,
144144
utils,
@@ -159,6 +159,7 @@ beforeEach(async () => {
159159
netlifyConfig.build.environment = {}
160160

161161
netlifyConfig.redirects = []
162+
netlifyConfig.headers = []
162163
netlifyConfig.functions[HANDLER_FUNCTION_NAME] && (netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files = [])
163164
netlifyConfig.functions[ODB_FUNCTION_NAME] && (netlifyConfig.functions[ODB_FUNCTION_NAME].included_files = [])
164165
await useFixture('serverless_next_config')
@@ -632,6 +633,93 @@ describe('onPostBuild', () => {
632633
},
633634
])
634635
})
636+
637+
test('adds headers to Netlify configuration', async () => {
638+
await moveNextDist()
639+
640+
const show = jest.fn()
641+
642+
await plugin.onPostBuild({
643+
...defaultArgs,
644+
645+
utils: { ...defaultArgs.utils, status: { show }, functions: { list: jest.fn().mockResolvedValue([]) } },
646+
})
647+
648+
expect(netlifyConfig.headers).toEqual([
649+
{
650+
for: '/',
651+
values: {
652+
'x-custom-header': 'my custom header value',
653+
},
654+
},
655+
{
656+
for: '/en/',
657+
values: {
658+
'x-custom-header': 'my custom header value',
659+
},
660+
},
661+
{
662+
for: '/es/',
663+
values: {
664+
'x-custom-header': 'my custom header value',
665+
},
666+
},
667+
{
668+
for: '/fr/',
669+
values: {
670+
'x-custom-header': 'my custom header value',
671+
},
672+
},
673+
{
674+
for: '/api/*',
675+
values: {
676+
'x-custom-api-header': 'my custom api header value',
677+
},
678+
},
679+
{
680+
for: '/en/api/*',
681+
values: {
682+
'x-custom-api-header': 'my custom api header value',
683+
},
684+
},
685+
{
686+
for: '/es/api/*',
687+
values: {
688+
'x-custom-api-header': 'my custom api header value',
689+
},
690+
},
691+
{
692+
for: '/fr/api/*',
693+
values: {
694+
'x-custom-api-header': 'my custom api header value',
695+
},
696+
},
697+
{
698+
for: '/*',
699+
values: {
700+
'x-custom-header-for-everything': 'my custom header for everything value',
701+
},
702+
},
703+
{
704+
for: '/en/*',
705+
values: {
706+
'x-custom-header-for-everything': 'my custom header for everything value',
707+
},
708+
},
709+
{
710+
for: '/es/*',
711+
values: {
712+
'x-custom-header-for-everything': 'my custom header for everything value',
713+
},
714+
},
715+
{
716+
for: '/fr/*',
717+
values: {
718+
'x-custom-header-for-everything': 'my custom header for everything value',
719+
},
720+
},
721+
])
722+
})
635723
})
636724

637725
describe('utility functions', () => {
@@ -822,8 +910,6 @@ describe('function helpers', () => {
822910
// in test data.
823911

824912
it('sets custom headers in the Netlify configuration', () => {
825-
netlifyConfig.headers = []
826-
827913
const nextConfig = {
828914
routesManifest: {
829915
headers: [

0 commit comments

Comments
 (0)