Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbalfour committed Oct 25, 2023
1 parent edbbc1d commit 14ba543
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/constructs/edge-api/src/lambdas/origin-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ export const handler = async (event: CloudFrontRequestEvent): Promise<CloudFront
if (!mapping) {
throw new Error(`no domain mapping found for host ${host}`)
}
const domainName = typeof mapping === 'string' ? mapping : mapping.destination
req.origin = {
custom: {
...req.origin.custom,
domainName: mapping.destination,
domainName,
},
}
req.headers['host'] = [
{
key: 'host',
value: mapping.destination,
value: domainName,
},
]

Expand Down
28 changes: 25 additions & 3 deletions packages/constructs/edge-api/tests/lambdas/origin-selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ const genEvent = (reqHost: string, env: any) => ({
})

describe('origin selector', () => {
it('should select the origin based on the req-host and env headers', async () => {
it('should select a domain-mapped origin based on the req-host and env headers', async () => {
const env = {
domainMapping: {
'example.org': {
domain: 'somewhere.org',
destination: 'somewhere.org',
},
'example.com': {
domain: 'somewhere.com',
destination: 'somewhere.com',
},
},
}
Expand All @@ -131,4 +131,26 @@ describe('origin selector', () => {
expect(result2.origin.custom?.domainName).toBe('somewhere.com')
expect(result2.headers['host'][0].value).toBe('somewhere.com')
})

it('should select a domain-mapped origin based on the req-host and env headers', async () => {
const env = {
domainMapping: {
'example.org': 'somewhere.org',
'example.com': 'somewhere.com',
},
}
const result = await handler(genEvent('example.org', env) as any)
if (!result.origin) {
throw new Error('no origin on result')
}
expect(result.origin.custom?.domainName).toBe('somewhere.org')
expect(result.headers['host'][0].value).toBe('somewhere.org')

const result2 = await handler(genEvent('example.com', env) as any)
if (!result2.origin) {
throw new Error('no origin on result')
}
expect(result2.origin.custom?.domainName).toBe('somewhere.com')
expect(result2.headers['host'][0].value).toBe('somewhere.com')
})
})

0 comments on commit 14ba543

Please sign in to comment.