@@ -30,61 +30,65 @@ const mockPageRequire = (mockPagePath: string): void => {
30
30
} ;
31
31
32
32
describe ( "API lambda handler" , ( ) => {
33
- it ( "serves api request" , async ( ) => {
34
- const event = createCloudFrontEvent ( {
35
- uri : "/api/getCustomers" ,
36
- host : "mydistribution.cloudfront.net" ,
37
- origin : {
38
- s3 : {
39
- domainName : "my-bucket.s3.amazonaws.com"
33
+ describe ( "API routes" , ( ) => {
34
+ it ( "serves api request" , async ( ) => {
35
+ const event = createCloudFrontEvent ( {
36
+ uri : "/api/getCustomers" ,
37
+ host : "mydistribution.cloudfront.net" ,
38
+ origin : {
39
+ s3 : {
40
+ domainName : "my-bucket.s3.amazonaws.com"
41
+ }
40
42
}
41
- }
42
- } ) ;
43
+ } ) ;
43
44
44
- mockPageRequire ( "pages/api/getCustomers.js" ) ;
45
+ mockPageRequire ( "pages/api/getCustomers.js" ) ;
45
46
46
- const response = ( await handler ( event ) ) as CloudFrontResponseResult ;
47
+ const response = ( await handler ( event ) ) as CloudFrontResponseResult ;
47
48
48
- const decodedBody = new Buffer ( response . body , "base64" ) . toString ( "utf8" ) ;
49
+ const decodedBody = new Buffer ( response . body , "base64" ) . toString ( "utf8" ) ;
49
50
50
- expect ( decodedBody ) . toEqual ( "pages/api/getCustomers" ) ;
51
- expect ( response . status ) . toEqual ( 200 ) ;
52
- } ) ;
51
+ expect ( decodedBody ) . toEqual ( "pages/api/getCustomers" ) ;
52
+ expect ( response . status ) . toEqual ( 200 ) ;
53
+ } ) ;
53
54
54
- it ( "returns 404 for not-found api routes" , async ( ) => {
55
- const event = createCloudFrontEvent ( {
56
- uri : "/foo/bar" ,
57
- host : "mydistribution.cloudfront.net" ,
58
- origin : {
59
- s3 : {
60
- domainName : "my-bucket.s3.amazonaws.com"
55
+ it ( "returns 404 for not-found api routes" , async ( ) => {
56
+ const event = createCloudFrontEvent ( {
57
+ uri : "/foo/bar" ,
58
+ host : "mydistribution.cloudfront.net" ,
59
+ origin : {
60
+ s3 : {
61
+ domainName : "my-bucket.s3.amazonaws.com"
62
+ }
61
63
}
62
- }
63
- } ) ;
64
+ } ) ;
64
65
65
- mockPageRequire ( "pages/api/getCustomers.js" ) ;
66
+ mockPageRequire ( "pages/api/getCustomers.js" ) ;
66
67
67
- const response = ( await handler ( event ) ) as CloudFrontResponseResult ;
68
+ const response = ( await handler ( event ) ) as CloudFrontResponseResult ;
68
69
69
- expect ( response . status ) . toEqual ( "404" ) ;
70
+ expect ( response . status ) . toEqual ( "404" ) ;
71
+ } ) ;
70
72
} ) ;
71
73
72
- describe ( "Custom Redirects" , ( ) => {
73
- let runRedirectTest = async (
74
- path : string ,
75
- expectedRedirect : string ,
76
- statusCode : number ,
77
- querystring ?: string
78
- ) : Promise < void > => {
79
- await runRedirectTestWithHandler (
80
- handler ,
81
- path ,
82
- expectedRedirect ,
83
- statusCode ,
84
- querystring
85
- ) ;
86
- } ;
74
+ let runRedirectTest = async (
75
+ path : string ,
76
+ expectedRedirect : string ,
77
+ statusCode : number ,
78
+ querystring ?: string ,
79
+ host ?: string
80
+ ) : Promise < void > => {
81
+ await runRedirectTestWithHandler (
82
+ handler ,
83
+ path ,
84
+ expectedRedirect ,
85
+ statusCode ,
86
+ querystring ,
87
+ host
88
+ ) ;
89
+ } ;
87
90
91
+ describe ( "Custom Redirects" , ( ) => {
88
92
it . each `
89
93
path | expectedRedirect | expectedRedirectStatusCode
90
94
${ "/api/deprecated/getCustomers" } | ${ "/api/getCustomers" } | ${ 308 }
@@ -100,6 +104,31 @@ describe("API lambda handler", () => {
100
104
) ;
101
105
} ) ;
102
106
107
+ describe ( "Domain Redirects" , ( ) => {
108
+ it . each `
109
+ path | querystring | expectedRedirect | expectedRedirectStatusCode
110
+ ${ "/" } | ${ "" } | ${ "https://www.example.com/" } | ${ 308 }
111
+ ${ "/" } | ${ "a=1234" } | ${ "https://www.example.com/?a=1234" } | ${ 308 }
112
+ ${ "/terms" } | ${ "" } | ${ "https://www.example.com/terms" } | ${ 308 }
113
+ ` (
114
+ "redirects path $path to $expectedRedirect, expectedRedirectStatusCode: $expectedRedirectStatusCode" ,
115
+ async ( {
116
+ path,
117
+ querystring,
118
+ expectedRedirect,
119
+ expectedRedirectStatusCode
120
+ } ) => {
121
+ await runRedirectTest (
122
+ path ,
123
+ expectedRedirect ,
124
+ expectedRedirectStatusCode ,
125
+ querystring ,
126
+ "example.com" // Override host to test a domain redirect from host example.com -> https://www.example.com
127
+ ) ;
128
+ }
129
+ ) ;
130
+ } ) ;
131
+
103
132
describe ( "Custom Rewrites" , ( ) => {
104
133
it . each `
105
134
path | expectedJs | expectedBody | expectedStatus
0 commit comments