Skip to content

Commit c18fe6e

Browse files
authored
Merge pull request #1 from danielcondemarin/add-cache-behavior-support
Add cache behavior support
2 parents 2cddbf2 + 42b9cf2 commit c18fe6e

17 files changed

+1223
-76
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ distribution:
5454
- https://my-bucket.s3.amazonaws.com
5555
```
5656
57+
#### Complex origin objects
58+
59+
You can extend your origins configuration by declaring them as objects. For example, to add cache behaviors:
60+
61+
```yml
62+
# serverless.yml
63+
64+
distribution:
65+
component: '@serverless/aws-cloudfront'
66+
inputs:
67+
origins:
68+
- url: https://my-assets.com
69+
pathPatterns:
70+
/static/images: # route any /static/images requests to https://my-assets.com
71+
ttl: 10
72+
```
73+
5774
### 4. Deploy
5875
5976
```console

__mocks__/aws-sdk.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const promisifyMock = (mockFn) => {
2+
const promise = jest.fn()
3+
mockFn.mockImplementation(() => ({
4+
promise
5+
}))
6+
7+
return promise
8+
}
9+
10+
const mockCreateDistribution = jest.fn()
11+
const mockCreateDistributionPromise = promisifyMock(mockCreateDistribution)
12+
13+
const mockUpdateDistribution = jest.fn()
14+
const mockUpdateDistributionPromise = promisifyMock(mockUpdateDistribution)
15+
16+
const mockGetDistributionConfig = jest.fn()
17+
const mockGetDistributionConfigPromise = promisifyMock(mockGetDistributionConfig)
18+
19+
const mockDeleteDistribution = jest.fn()
20+
const mockDeleteDistributionPromise = promisifyMock(mockDeleteDistribution)
21+
22+
module.exports = {
23+
mockCreateDistribution,
24+
mockUpdateDistribution,
25+
mockGetDistributionConfig,
26+
mockDeleteDistribution,
27+
mockCreateDistributionPromise,
28+
mockUpdateDistributionPromise,
29+
mockGetDistributionConfigPromise,
30+
mockDeleteDistributionPromise,
31+
32+
CloudFront: jest.fn(() => ({
33+
createDistribution: mockCreateDistribution,
34+
updateDistribution: mockUpdateDistribution,
35+
getDistributionConfig: mockGetDistributionConfig,
36+
deleteDistribution: mockDeleteDistribution
37+
}))
38+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Input origin as a custom url creates distribution with custom url origin 1`] = `
4+
Object {
5+
"DistributionConfig": Object {
6+
"Aliases": Object {
7+
"Items": Array [],
8+
"Quantity": 0,
9+
},
10+
"CallerReference": "1566599541192",
11+
"Comment": "",
12+
"DefaultCacheBehavior": Object {
13+
"AllowedMethods": Object {
14+
"CachedMethods": Object {
15+
"Items": Array [
16+
"HEAD",
17+
"GET",
18+
],
19+
"Quantity": 2,
20+
},
21+
"Items": Array [
22+
"HEAD",
23+
"GET",
24+
],
25+
"Quantity": 2,
26+
},
27+
"Compress": false,
28+
"DefaultTTL": 86400,
29+
"FieldLevelEncryptionId": "",
30+
"ForwardedValues": Object {
31+
"Cookies": Object {
32+
"Forward": "none",
33+
},
34+
"Headers": Object {
35+
"Items": Array [],
36+
"Quantity": 0,
37+
},
38+
"QueryString": false,
39+
"QueryStringCacheKeys": Object {
40+
"Items": Array [],
41+
"Quantity": 0,
42+
},
43+
},
44+
"LambdaFunctionAssociations": Object {
45+
"Items": Array [],
46+
"Quantity": 0,
47+
},
48+
"MaxTTL": 31536000,
49+
"MinTTL": 0,
50+
"SmoothStreaming": false,
51+
"TargetOriginId": "mycustomorigin.com",
52+
"TrustedSigners": Object {
53+
"Enabled": false,
54+
"Items": Array [],
55+
"Quantity": 0,
56+
},
57+
"ViewerProtocolPolicy": "redirect-to-https",
58+
},
59+
"Enabled": true,
60+
"HttpVersion": "http2",
61+
"Origins": Object {
62+
"Items": Array [
63+
Object {
64+
"CustomHeaders": Object {
65+
"Items": Array [],
66+
"Quantity": 0,
67+
},
68+
"CustomOriginConfig": Object {
69+
"HTTPPort": 80,
70+
"HTTPSPort": 443,
71+
"OriginKeepaliveTimeout": 5,
72+
"OriginProtocolPolicy": "https-only",
73+
"OriginReadTimeout": 30,
74+
"OriginSslProtocols": Object {
75+
"Items": Array [
76+
"TLSv1.2",
77+
],
78+
"Quantity": 1,
79+
},
80+
},
81+
"DomainName": "mycustomorigin.com",
82+
"Id": "mycustomorigin.com",
83+
"OriginPath": "",
84+
},
85+
],
86+
"Quantity": 1,
87+
},
88+
"PriceClass": "PriceClass_All",
89+
},
90+
}
91+
`;
92+
93+
exports[`Input origin as a custom url updates distribution 1`] = `
94+
Object {
95+
"DistributionConfig": Object {
96+
"DefaultCacheBehavior": Object {
97+
"AllowedMethods": Object {
98+
"CachedMethods": Object {
99+
"Items": Array [
100+
"HEAD",
101+
"GET",
102+
],
103+
"Quantity": 2,
104+
},
105+
"Items": Array [
106+
"HEAD",
107+
"GET",
108+
],
109+
"Quantity": 2,
110+
},
111+
"Compress": false,
112+
"DefaultTTL": 86400,
113+
"FieldLevelEncryptionId": "",
114+
"ForwardedValues": Object {
115+
"Cookies": Object {
116+
"Forward": "none",
117+
},
118+
"Headers": Object {
119+
"Items": Array [],
120+
"Quantity": 0,
121+
},
122+
"QueryString": false,
123+
"QueryStringCacheKeys": Object {
124+
"Items": Array [],
125+
"Quantity": 0,
126+
},
127+
},
128+
"LambdaFunctionAssociations": Object {
129+
"Items": Array [],
130+
"Quantity": 0,
131+
},
132+
"MaxTTL": 31536000,
133+
"MinTTL": 0,
134+
"SmoothStreaming": false,
135+
"TargetOriginId": "mycustomoriginupdated.com",
136+
"TrustedSigners": Object {
137+
"Enabled": false,
138+
"Items": Array [],
139+
"Quantity": 0,
140+
},
141+
"ViewerProtocolPolicy": "redirect-to-https",
142+
},
143+
"Enabled": true,
144+
"Origins": Object {
145+
"Items": Array [
146+
Object {
147+
"CustomHeaders": Object {
148+
"Items": Array [],
149+
"Quantity": 0,
150+
},
151+
"CustomOriginConfig": Object {
152+
"HTTPPort": 80,
153+
"HTTPSPort": 443,
154+
"OriginKeepaliveTimeout": 5,
155+
"OriginProtocolPolicy": "https-only",
156+
"OriginReadTimeout": 30,
157+
"OriginSslProtocols": Object {
158+
"Items": Array [
159+
"TLSv1.2",
160+
],
161+
"Quantity": 1,
162+
},
163+
},
164+
"DomainName": "mycustomoriginupdated.com",
165+
"Id": "mycustomoriginupdated.com",
166+
"OriginPath": "",
167+
},
168+
],
169+
"Quantity": 1,
170+
},
171+
},
172+
"Id": "distribution123",
173+
"IfMatch": "etag",
174+
}
175+
`;

0 commit comments

Comments
 (0)