@@ -25,9 +25,14 @@ const {
25
25
patchNextFiles,
26
26
unpatchNextFiles,
27
27
} = 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' )
29
33
const { dirname } = require ( 'path' )
30
34
const { getProblematicUserRewrites } = require ( '../plugin/src/helpers/verification' )
35
+ const { onPostBuild } = require ( '../plugin/lib' )
31
36
32
37
const FIXTURES_DIR = `${ __dirname } /fixtures`
33
38
const SAMPLE_PROJECT_DIR = `${ __dirname } /../demos/default`
@@ -246,11 +251,11 @@ describe('onBuild()', () => {
246
251
process . env . URL = mockSiteUrl
247
252
248
253
await moveNextDist ( )
249
-
254
+
250
255
const initialConfig = await getRequiredServerFiles ( netlifyConfig . build . publish )
251
- initialConfig . config . basePath = " /foo"
256
+ initialConfig . config . basePath = ' /foo'
252
257
await updateRequiredServerFiles ( netlifyConfig . build . publish , initialConfig )
253
-
258
+
254
259
await plugin . onBuild ( defaultArgs )
255
260
256
261
expect ( onBuildHasRun ( netlifyConfig ) ) . toBe ( true )
@@ -788,3 +793,85 @@ describe('function helpers', () => {
788
793
)
789
794
} )
790
795
} )
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