-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlambda-at-edge.test.js
128 lines (117 loc) · 3.82 KB
/
lambda-at-edge.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const { createComponent, assertHasCacheBehavior } = require('../test-utils')
const { mockCreateDistribution, mockCreateDistributionPromise } = require('aws-sdk')
describe('Input origin as a custom url', () => {
let component
beforeEach(async () => {
mockCreateDistributionPromise.mockResolvedValueOnce({
Distribution: {
Id: 'distribution123'
}
})
component = await createComponent()
})
it('creates distribution with lambda associations for each event type', async () => {
await component.default({
origins: [
{
url: 'https://exampleorigin.com',
pathPatterns: {
'/some/path': {
ttl: 10,
'lambda@edge': {
'viewer-request': {
arn: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
includeBody: true
},
'origin-request': {
arn: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
includeBody: false
},
'origin-response': 'arn:aws:lambda:us-east-1:123:function:originResponseFunction',
'viewer-response': 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction'
}
}
}
}
]
})
assertHasCacheBehavior(mockCreateDistribution, {
PathPattern: '/some/path',
LambdaFunctionAssociations: {
Quantity: 4,
Items: [
{
EventType: 'viewer-request',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
IncludeBody: true
},
{
EventType: 'origin-request',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
IncludeBody: false
},
{
EventType: 'origin-response',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originResponseFunction',
IncludeBody: false
},
{
EventType: 'viewer-response',
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction',
IncludeBody: false
}
]
}
})
expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot()
})
it('throws error when event type provided is not valid', async () => {
expect.assertions(1)
try {
await component.default({
origins: [
{
url: 'https://exampleorigin.com',
pathPatterns: {
'/some/path': {
ttl: 10,
'lambda@edge': {
'invalid-eventtype': 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction'
}
}
}
}
]
})
} catch (err) {
expect(err.message).toEqual(
'"invalid-eventtype" is not a valid lambda trigger. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html for valid event types.'
)
}
})
it('throws error when includeBody given for event types other than request', async () => {
expect.assertions(1)
try {
await component.default({
origins: [
{
url: 'https://exampleorigin.com',
pathPatterns: {
'/some/path': {
ttl: 10,
'lambda@edge': {
'viewer-response': {
arn: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
includeBody: true
}
}
}
}
}
]
})
} catch (err) {
expect(err.message).toEqual('"includeBody" not allowed for viewer-response lambda triggers.')
}
})
})