Skip to content

Commit 14ba543

Browse files
committed
update test
1 parent edbbc1d commit 14ba543

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

packages/constructs/edge-api/src/lambdas/origin-selector.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ export const handler = async (event: CloudFrontRequestEvent): Promise<CloudFront
3434
if (!mapping) {
3535
throw new Error(`no domain mapping found for host ${host}`)
3636
}
37+
const domainName = typeof mapping === 'string' ? mapping : mapping.destination
3738
req.origin = {
3839
custom: {
3940
...req.origin.custom,
40-
domainName: mapping.destination,
41+
domainName,
4142
},
4243
}
4344
req.headers['host'] = [
4445
{
4546
key: 'host',
46-
value: mapping.destination,
47+
value: domainName,
4748
},
4849
]
4950

packages/constructs/edge-api/tests/lambdas/origin-selector.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ const genEvent = (reqHost: string, env: any) => ({
106106
})
107107

108108
describe('origin selector', () => {
109-
it('should select the origin based on the req-host and env headers', async () => {
109+
it('should select a domain-mapped origin based on the req-host and env headers', async () => {
110110
const env = {
111111
domainMapping: {
112112
'example.org': {
113-
domain: 'somewhere.org',
113+
destination: 'somewhere.org',
114114
},
115115
'example.com': {
116-
domain: 'somewhere.com',
116+
destination: 'somewhere.com',
117117
},
118118
},
119119
}
@@ -131,4 +131,26 @@ describe('origin selector', () => {
131131
expect(result2.origin.custom?.domainName).toBe('somewhere.com')
132132
expect(result2.headers['host'][0].value).toBe('somewhere.com')
133133
})
134+
135+
it('should select a domain-mapped origin based on the req-host and env headers', async () => {
136+
const env = {
137+
domainMapping: {
138+
'example.org': 'somewhere.org',
139+
'example.com': 'somewhere.com',
140+
},
141+
}
142+
const result = await handler(genEvent('example.org', env) as any)
143+
if (!result.origin) {
144+
throw new Error('no origin on result')
145+
}
146+
expect(result.origin.custom?.domainName).toBe('somewhere.org')
147+
expect(result.headers['host'][0].value).toBe('somewhere.org')
148+
149+
const result2 = await handler(genEvent('example.com', env) as any)
150+
if (!result2.origin) {
151+
throw new Error('no origin on result')
152+
}
153+
expect(result2.origin.custom?.domainName).toBe('somewhere.com')
154+
expect(result2.headers['host'][0].value).toBe('somewhere.com')
155+
})
134156
})

0 commit comments

Comments
 (0)