-
-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lowercase API gateway V2 event headers (#1288)
Co-authored-by: dnalborczyk <[email protected]>
- Loading branch information
1 parent
e1f60da
commit 9ff4cf3
Showing
7 changed files
with
113 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import lowerCaseKeys from '../lowerCaseKeys.js' | ||
|
||
describe('lowerCaseKeys', () => { | ||
test(`should handle empty object`, () => { | ||
const result = lowerCaseKeys({}) | ||
expect(result).toEqual({}) | ||
}) | ||
|
||
test(`should handle object with one key`, () => { | ||
const result = lowerCaseKeys({ 'Some-Key': 'value' }) | ||
expect(result).toEqual({ 'some-key': 'value' }) | ||
}) | ||
|
||
test(`should handle object with multiple keys`, () => { | ||
const result = lowerCaseKeys({ | ||
'Some-Key': 'value', | ||
'Another-Key': 'anotherValue', | ||
'lOts-OF-CAPitaLs': 'ButThisIsNotTouched', | ||
'already-lowercase': 'cool', | ||
}) | ||
expect(result).toEqual({ | ||
'some-key': 'value', | ||
'another-key': 'anotherValue', | ||
'lots-of-capitals': 'ButThisIsNotTouched', | ||
'already-lowercase': 'cool', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const { entries, fromEntries } = Object | ||
|
||
// (obj: { [string]: string }): { [Lowercase<string>]: string } | ||
export default function parseHeaders(obj) { | ||
return fromEntries(entries(obj).map(([k, v]) => [k.toLowerCase(), v])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
exports.echoHeaders = async function get(event) { | ||
return { | ||
body: JSON.stringify({ | ||
headersReceived: event.headers, | ||
}), | ||
statusCode: 200, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { resolve } from 'path' | ||
import fetch from 'node-fetch' | ||
import { joinUrl, setup, teardown } from '../_testHelpers/index.js' | ||
|
||
jest.setTimeout(30000) | ||
|
||
describe('HttpApi Headers Tests', () => { | ||
// init | ||
beforeAll(() => | ||
setup({ | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
// cleanup | ||
afterAll(() => teardown()) | ||
|
||
test.each(['GET', 'POST'])('%s headers', async (method) => { | ||
const url = joinUrl(TEST_BASE_URL, '/echo-headers') | ||
const options = { | ||
method, | ||
headers: { | ||
Origin: 'http://www.example.com', | ||
'X-Webhook-Signature': 'ABCDEF', | ||
}, | ||
} | ||
|
||
const response = await fetch(url, options) | ||
expect(response.status).toEqual(200) | ||
|
||
const body = await response.json() | ||
|
||
expect(body.headersReceived).toMatchObject({ | ||
origin: 'http://www.example.com', | ||
'x-webhook-signature': 'ABCDEF', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
service: httpapi-headers | ||
|
||
plugins: | ||
- ../../../ | ||
|
||
provider: | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs12.x | ||
stage: dev | ||
versionFunctions: false | ||
httpApi: | ||
payload: '2.0' | ||
|
||
functions: | ||
echoHeaders: | ||
events: | ||
- httpApi: | ||
method: get | ||
path: echo-headers | ||
- httpApi: | ||
method: post | ||
path: echo-headers | ||
handler: handler.echoHeaders |