Skip to content

Commit 5901884

Browse files
authored
feat: handle api gateway v2 cookies (#51)
* feat: add cookies handling * ci: move to github actions
1 parent 73cf70b commit 5901884

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"afterEach": false
1010
},
1111
"rules": {
12-
"array-bracket-spacing": 0
12+
"array-bracket-spacing": 0,
13+
"dot-notation": 0
1314
}
1415
}

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- "*.md"
7+
pull_request:
8+
paths-ignore:
9+
- "*.md"
10+
11+
jobs:
12+
tests:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
node-version: [12, 14, 16]
17+
steps:
18+
- uses: actions/[email protected]
19+
- name: Use Node.js
20+
uses: actions/[email protected]
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- name: Install
24+
run: |
25+
npm install
26+
- name: Run tests
27+
run: |
28+
npm run test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
.DS_Store
33
npm-debug.log
44
package-lock.json
5-
.nyc_output
5+
.nyc_output
6+
yarn.lock

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ module.exports = (app, options) => (event, context, callback) => {
3535
headers['x-request-id'] = headers['x-request-id'] || event.requestContext.requestId
3636
}
3737

38+
// API gateway v2 cookies
39+
if (event.cookies && event.cookies.length) {
40+
headers['cookie'] = event.cookies.join(';')
41+
}
42+
3843
const prom = new Promise((resolve) => {
3944
app.inject({ method, url, query, payload, headers }, (err, res) => {
4045
if (err) {

test/basic.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ const fs = require('fs')
55
const awsLambdaFastify = require('../index')
66

77
test('GET', async (t) => {
8-
t.plan(14)
8+
t.plan(15)
99

1010
const app = fastify()
1111
app.get('/test', async (request, reply) => {
1212
t.equal(request.headers['x-my-header'], 'wuuusaaa')
13-
t.equal(request.headers['x-apigateway-event'], '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%2C%22queryStringParameters%22%3A%22%22%7D')
13+
t.equal(request.headers['cookie'], 'foo=bar')
14+
t.equal(request.headers['x-apigateway-event'], '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%2C%22cookies%22%3A%5B%22foo%3Dbar%22%5D%2C%22queryStringParameters%22%3A%22%22%7D')
1415
t.equal(request.headers['user-agent'], 'lightMyRequest')
1516
t.equal(request.headers.host, 'localhost:80')
1617
t.equal(request.headers['content-length'], '0')
@@ -25,6 +26,7 @@ test('GET', async (t) => {
2526
headers: {
2627
'X-My-Header': 'wuuusaaa'
2728
},
29+
cookies: ['foo=bar'],
2830
queryStringParameters: ''
2931
})
3032
t.equal(ret.statusCode, 200)

0 commit comments

Comments
 (0)