Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit dd2fa6e

Browse files
authored
feat(aws-cloudfront): add support for custom headers in custom origins (#1448)
1 parent bcc9f73 commit dd2fa6e

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Input origin with custom header creates distribution with custom url origin 1`] = `
4+
Object {
5+
"DistributionConfig": Object {
6+
"Aliases": Object {
7+
"Items": Array [],
8+
"Quantity": 0,
9+
},
10+
"CacheBehaviors": Object {
11+
"Items": Array [
12+
Object {
13+
"AllowedMethods": Object {
14+
"CachedMethods": Object {
15+
"Items": Array [
16+
"GET",
17+
"HEAD",
18+
],
19+
"Quantity": 2,
20+
},
21+
"Items": Array [
22+
"GET",
23+
"HEAD",
24+
"POST",
25+
],
26+
"Quantity": 3,
27+
},
28+
"Compress": true,
29+
"DefaultTTL": 10,
30+
"FieldLevelEncryptionId": "",
31+
"ForwardedValues": Object {
32+
"Cookies": Object {
33+
"Forward": "all",
34+
},
35+
"Headers": Object {
36+
"Items": Array [],
37+
"Quantity": 0,
38+
},
39+
"QueryString": true,
40+
"QueryStringCacheKeys": Object {
41+
"Items": Array [],
42+
"Quantity": 0,
43+
},
44+
},
45+
"LambdaFunctionAssociations": Object {
46+
"Items": Array [],
47+
"Quantity": 0,
48+
},
49+
"MaxTTL": 10,
50+
"MinTTL": 10,
51+
"PathPattern": "/some/path",
52+
"SmoothStreaming": false,
53+
"TargetOriginId": "exampleorigin.com",
54+
"TrustedSigners": Object {
55+
"Enabled": false,
56+
"Quantity": 0,
57+
},
58+
"ViewerProtocolPolicy": "https-only",
59+
},
60+
],
61+
"Quantity": 1,
62+
},
63+
"CallerReference": "1566599541192",
64+
"Comment": "",
65+
"CustomErrorResponses": Object {
66+
"Items": Array [],
67+
"Quantity": 0,
68+
},
69+
"DefaultCacheBehavior": Object {
70+
"AllowedMethods": Object {
71+
"CachedMethods": Object {
72+
"Items": Array [
73+
"HEAD",
74+
"GET",
75+
],
76+
"Quantity": 2,
77+
},
78+
"Items": Array [
79+
"HEAD",
80+
"GET",
81+
],
82+
"Quantity": 2,
83+
},
84+
"Compress": false,
85+
"DefaultTTL": 86400,
86+
"FieldLevelEncryptionId": "",
87+
"ForwardedValues": Object {
88+
"Cookies": Object {
89+
"Forward": "none",
90+
},
91+
"Headers": Object {
92+
"Items": Array [],
93+
"Quantity": 0,
94+
},
95+
"QueryString": false,
96+
"QueryStringCacheKeys": Object {
97+
"Items": Array [],
98+
"Quantity": 0,
99+
},
100+
},
101+
"LambdaFunctionAssociations": Object {
102+
"Items": Array [],
103+
"Quantity": 0,
104+
},
105+
"MaxTTL": 31536000,
106+
"MinTTL": 0,
107+
"SmoothStreaming": false,
108+
"TargetOriginId": "exampleorigin.com",
109+
"TrustedSigners": Object {
110+
"Enabled": false,
111+
"Items": Array [],
112+
"Quantity": 0,
113+
},
114+
"ViewerProtocolPolicy": "redirect-to-https",
115+
},
116+
"Enabled": true,
117+
"HttpVersion": "http2",
118+
"Origins": Object {
119+
"Items": Array [
120+
Object {
121+
"CustomHeaders": Object {
122+
"Items": Array [
123+
Object {
124+
"HeaderName": "x-api-key",
125+
"HeaderValue": "test",
126+
},
127+
],
128+
"Quantity": 1,
129+
},
130+
"CustomOriginConfig": Object {
131+
"HTTPPort": 80,
132+
"HTTPSPort": 443,
133+
"OriginKeepaliveTimeout": 5,
134+
"OriginProtocolPolicy": "https-only",
135+
"OriginReadTimeout": 30,
136+
"OriginSslProtocols": Object {
137+
"Items": Array [
138+
"TLSv1.2",
139+
],
140+
"Quantity": 1,
141+
},
142+
},
143+
"DomainName": "exampleorigin.com",
144+
"Id": "exampleorigin.com",
145+
"OriginPath": "",
146+
},
147+
],
148+
"Quantity": 1,
149+
},
150+
"PriceClass": "PriceClass_All",
151+
},
152+
}
153+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const {
2+
createComponent,
3+
assertHasCacheBehavior,
4+
assertHasOrigin
5+
} = require("../test-utils");
6+
7+
import {
8+
mockCreateDistribution,
9+
mockCreateDistributionPromise
10+
} from "../__mocks__/aws-sdk.mock";
11+
12+
jest.mock("aws-sdk", () => require("../__mocks__/aws-sdk.mock"));
13+
14+
describe("Input origin with custom header", () => {
15+
let component;
16+
17+
beforeEach(async () => {
18+
mockCreateDistributionPromise.mockResolvedValueOnce({
19+
Distribution: {
20+
Id: "xyz"
21+
}
22+
});
23+
24+
component = await createComponent();
25+
});
26+
27+
it("creates distribution with custom url origin", async () => {
28+
await component.default({
29+
origins: [
30+
{
31+
url: "https://exampleorigin.com",
32+
pathPatterns: {
33+
"/some/path": {
34+
minTTL: 10,
35+
defaultTTL: 10,
36+
maxTTL: 10,
37+
allowedHttpMethods: ["GET", "HEAD", "POST"]
38+
}
39+
},
40+
headers: {
41+
"x-api-key": "test"
42+
}
43+
}
44+
]
45+
});
46+
47+
assertHasOrigin(mockCreateDistribution, {
48+
Id: "exampleorigin.com",
49+
DomainName: "exampleorigin.com",
50+
CustomHeaders: {
51+
Quantity: 1,
52+
Items: [{ HeaderName: "x-api-key", HeaderValue: "test" }]
53+
}
54+
});
55+
56+
assertHasCacheBehavior(mockCreateDistribution, {
57+
PathPattern: "/some/path",
58+
MinTTL: 10,
59+
TargetOriginId: "exampleorigin.com"
60+
});
61+
62+
expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot();
63+
});
64+
});

packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type Origin =
2525
protocolPolicy: string;
2626
url: string;
2727
pathPatterns: Record<string, unknown>;
28+
headers: Record<string, string>;
2829
};
2930

3031
export const getOriginConfig = (
@@ -55,6 +56,15 @@ export const getOriginConfig = (
5556
: ""
5657
};
5758
} else {
59+
if (typeof origin === "object" && origin.headers) {
60+
originConfig.CustomHeaders.Quantity = Object.keys(origin.headers).length;
61+
originConfig.CustomHeaders.Items = Object.keys(origin.headers).map(
62+
(key) => ({
63+
HeaderName: key,
64+
HeaderValue: origin.headers[key]
65+
})
66+
);
67+
}
5868
originConfig.CustomOriginConfig = {
5969
HTTPPort: 80,
6070
HTTPSPort: 443,

0 commit comments

Comments
 (0)