Skip to content

Commit 913c4fd

Browse files
authored
Merge pull request #8 from Pavel910/master
Expose more cache behavior parameters.
2 parents 8763348 + c139592 commit 913c4fd

6 files changed

+442
-45
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,25 @@ distribution:
5353
defaults: # optional
5454
ttl: 15
5555
allowedHttpMethods: ['HEAD', 'GET']
56+
forward: # optional
57+
# array of header names, 'none' or 'all'
58+
headers: ['Accept', 'Accept-Language']
59+
# array of cookie names, 'none' or 'all'
60+
cookies: ['my-cookie]
61+
queryString: true
62+
queryStringCacheKeys: ['queryKey']
63+
viewerProtocolPolicy: 'https-only' # optional
64+
smoothStreaming: true # optional
65+
compress: true # optional
66+
fieldLevelEncryptionId: '123' # optional
5667
lambda@edge: # added to cloudfront default cache behavior
5768
viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version
5869
origins:
5970
- https://my-bucket.s3.amazonaws.com
6071
```
6172
6273
#### Custom cache behavior
63-
74+
Custom cache behaviors support the same config parameters as the default cache behavior (see the example above).
6475
```yml
6576
# serverless.yml
6677

@@ -73,6 +84,12 @@ distribution:
7384
/static/images: # route any /static/images requests to https://my-assets.com
7485
ttl: 10
7586
allowedHttpMethods: ['GET', 'HEAD'] # optional
87+
forward: # optional
88+
headers: 'all'
89+
cookies: ['auth-token']
90+
queryString: true
91+
compress: false # optional
92+
# ...
7693
```
7794

7895
#### Lambda@Edge
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Input origin as a custom url creates distribution with custom behavior options 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+
],
25+
"Quantity": 2,
26+
},
27+
"Compress": false,
28+
"DefaultTTL": 0,
29+
"FieldLevelEncryptionId": "321",
30+
"ForwardedValues": Object {
31+
"Cookies": Object {
32+
"Forward": "whitelist",
33+
"WhitelistedNames": Object {
34+
"Items": Array [
35+
"auth-token",
36+
],
37+
"Quantity": 1,
38+
},
39+
},
40+
"Headers": Object {
41+
"Items": Array [
42+
"*",
43+
],
44+
"Quantity": 1,
45+
},
46+
"QueryString": true,
47+
"QueryStringCacheKeys": Object {
48+
"Items": Array [],
49+
"Quantity": 0,
50+
},
51+
},
52+
"LambdaFunctionAssociations": Object {
53+
"Items": Array [],
54+
"Quantity": 0,
55+
},
56+
"MaxTTL": 0,
57+
"MinTTL": 0,
58+
"PathPattern": "/sample/path",
59+
"SmoothStreaming": false,
60+
"TargetOriginId": "mycustomorigin.com",
61+
"TrustedSigners": Object {
62+
"Enabled": false,
63+
"Quantity": 0,
64+
},
65+
"ViewerProtocolPolicy": "redirect-to-https",
66+
},
67+
],
68+
"Quantity": 1,
69+
},
70+
"CallerReference": "1566599541192",
71+
"Comment": "",
72+
"DefaultCacheBehavior": Object {
73+
"AllowedMethods": Object {
74+
"CachedMethods": Object {
75+
"Items": Array [
76+
"HEAD",
77+
"GET",
78+
],
79+
"Quantity": 2,
80+
},
81+
"Items": Array [
82+
"HEAD",
83+
"GET",
84+
],
85+
"Quantity": 2,
86+
},
87+
"Compress": false,
88+
"DefaultTTL": 0,
89+
"FieldLevelEncryptionId": "",
90+
"ForwardedValues": Object {
91+
"Cookies": Object {
92+
"Forward": "none",
93+
},
94+
"Headers": Object {
95+
"Items": Array [],
96+
"Quantity": 0,
97+
},
98+
"QueryString": false,
99+
"QueryStringCacheKeys": Object {
100+
"Items": Array [],
101+
"Quantity": 0,
102+
},
103+
},
104+
"LambdaFunctionAssociations": Object {
105+
"Items": Array [],
106+
"Quantity": 0,
107+
},
108+
"MaxTTL": 31536000,
109+
"MinTTL": 0,
110+
"SmoothStreaming": false,
111+
"TargetOriginId": "mycustomorigin.com",
112+
"TrustedSigners": Object {
113+
"Enabled": false,
114+
"Items": Array [],
115+
"Quantity": 0,
116+
},
117+
"ViewerProtocolPolicy": "redirect-to-https",
118+
},
119+
"Enabled": true,
120+
"HttpVersion": "http2",
121+
"Origins": Object {
122+
"Items": Array [
123+
Object {
124+
"CustomHeaders": Object {
125+
"Items": Array [],
126+
"Quantity": 0,
127+
},
128+
"CustomOriginConfig": Object {
129+
"HTTPPort": 80,
130+
"HTTPSPort": 443,
131+
"OriginKeepaliveTimeout": 5,
132+
"OriginProtocolPolicy": "https-only",
133+
"OriginReadTimeout": 30,
134+
"OriginSslProtocols": Object {
135+
"Items": Array [
136+
"TLSv1.2",
137+
],
138+
"Quantity": 1,
139+
},
140+
},
141+
"DomainName": "mycustomorigin.com",
142+
"Id": "mycustomorigin.com",
143+
"OriginPath": "",
144+
},
145+
],
146+
"Quantity": 1,
147+
},
148+
"PriceClass": "PriceClass_All",
149+
},
150+
}
151+
`;
152+
153+
exports[`Input origin as a custom url creates distribution with custom default behavior options 1`] = `
154+
Object {
155+
"DistributionConfig": Object {
156+
"Aliases": Object {
157+
"Items": Array [],
158+
"Quantity": 0,
159+
},
160+
"CacheBehaviors": Object {
161+
"Items": Array [],
162+
"Quantity": 0,
163+
},
164+
"CallerReference": "1566599541192",
165+
"Comment": "",
166+
"DefaultCacheBehavior": Object {
167+
"AllowedMethods": Object {
168+
"CachedMethods": Object {
169+
"Items": Array [
170+
"HEAD",
171+
"GET",
172+
],
173+
"Quantity": 2,
174+
},
175+
"Items": Array [
176+
"GET",
177+
"HEAD",
178+
"OPTIONS",
179+
"PUT",
180+
"POST",
181+
"PATCH",
182+
"DELETE",
183+
],
184+
"Quantity": 7,
185+
},
186+
"Compress": true,
187+
"DefaultTTL": 0,
188+
"FieldLevelEncryptionId": "123",
189+
"ForwardedValues": Object {
190+
"Cookies": Object {
191+
"Forward": "all",
192+
},
193+
"Headers": Object {
194+
"Items": Array [
195+
"Accept",
196+
"Accept-Language",
197+
],
198+
"Quantity": 2,
199+
},
200+
"QueryString": true,
201+
"QueryStringCacheKeys": Object {
202+
"Items": Array [
203+
"query",
204+
],
205+
"Quantity": 1,
206+
},
207+
},
208+
"LambdaFunctionAssociations": Object {
209+
"Items": Array [],
210+
"Quantity": 0,
211+
},
212+
"MaxTTL": 31536000,
213+
"MinTTL": 0,
214+
"SmoothStreaming": true,
215+
"TargetOriginId": "mycustomorigin.com",
216+
"TrustedSigners": Object {
217+
"Enabled": false,
218+
"Items": Array [],
219+
"Quantity": 0,
220+
},
221+
"ViewerProtocolPolicy": "https-only",
222+
},
223+
"Enabled": true,
224+
"HttpVersion": "http2",
225+
"Origins": Object {
226+
"Items": Array [
227+
Object {
228+
"CustomHeaders": Object {
229+
"Items": Array [],
230+
"Quantity": 0,
231+
},
232+
"CustomOriginConfig": Object {
233+
"HTTPPort": 80,
234+
"HTTPSPort": 443,
235+
"OriginKeepaliveTimeout": 5,
236+
"OriginProtocolPolicy": "https-only",
237+
"OriginReadTimeout": 30,
238+
"OriginSslProtocols": Object {
239+
"Items": Array [
240+
"TLSv1.2",
241+
],
242+
"Quantity": 1,
243+
},
244+
},
245+
"DomainName": "mycustomorigin.com",
246+
"Id": "mycustomorigin.com",
247+
"OriginPath": "",
248+
},
249+
],
250+
"Quantity": 1,
251+
},
252+
"PriceClass": "PriceClass_All",
253+
},
254+
}
255+
`;
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const { createComponent } = require('../test-utils')
2+
3+
const { mockCreateDistribution, mockCreateDistributionPromise } = require('aws-sdk')
4+
5+
describe('Input origin as a custom url', () => {
6+
let component
7+
8+
beforeEach(async () => {
9+
mockCreateDistributionPromise.mockResolvedValueOnce({
10+
Distribution: {
11+
Id: 'distribution123'
12+
}
13+
})
14+
15+
component = await createComponent()
16+
})
17+
18+
it('creates distribution with custom default behavior options', async () => {
19+
await component.default({
20+
defaults: {
21+
ttl: 0,
22+
forward: {
23+
headers: ['Accept', 'Accept-Language'],
24+
cookies: 'all',
25+
queryString: true,
26+
queryStringCacheKeys: ['query']
27+
},
28+
allowedHttpMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'],
29+
viewerProtocolPolicy: 'https-only',
30+
smoothStreaming: true,
31+
compress: true,
32+
fieldLevelEncryptionId: '123'
33+
},
34+
origins: ['https://mycustomorigin.com']
35+
})
36+
37+
expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot()
38+
})
39+
40+
it('creates distribution with custom behavior options', async () => {
41+
await component.default({
42+
defaults: {
43+
ttl: 0
44+
},
45+
origins: [
46+
{
47+
url: 'https://mycustomorigin.com',
48+
pathPatterns: {
49+
'/sample/path': {
50+
ttl: 0,
51+
forward: {
52+
headers: 'all',
53+
cookies: ['auth-token'],
54+
queryString: true
55+
},
56+
allowedHttpMethods: ['GET', 'HEAD'],
57+
viewerProtocolPolicy: 'redirect-to-https',
58+
compress: false,
59+
fieldLevelEncryptionId: '321'
60+
}
61+
}
62+
}
63+
]
64+
})
65+
66+
expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot()
67+
})
68+
})

0 commit comments

Comments
 (0)