Skip to content

Commit 0cf1835

Browse files
Adding support for custom headers
1 parent 6f15e07 commit 0cf1835

File tree

4 files changed

+4674
-0
lines changed

4 files changed

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

lib/getOriginConfig.js

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ module.exports = (origin, { originAccessIdentityId = '' }) => {
2525
: ''
2626
}
2727
} else {
28+
if (origin.headers) {
29+
originConfig.CustomHeaders.Quantity = Object.keys(origin.headers).length
30+
originConfig.CustomHeaders.Items = Object.keys(origin.headers).map((key) => ({
31+
HeaderName: key,
32+
HeaderValue: origin.headers[key]
33+
}))
34+
}
35+
2836
originConfig.CustomOriginConfig = {
2937
HTTPPort: 80,
3038
HTTPSPort: 443,

0 commit comments

Comments
 (0)