Skip to content

Commit 6354a58

Browse files
authored
Merge pull request #42 from panva/refactor/get-path-with-query
Use native url.format; test for re-encoding in getPathWithQueryStringParams
2 parents 28d9e35 + de2ec33 commit 6354a58

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

__tests__/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ test('getPathWithQueryStringParams: 1 param', () => {
2020
expect(pathWithQueryStringParams).toEqual('/foo/bar?bizz=bazz')
2121
})
2222

23+
test('getPathWithQueryStringParams: to be url-encoded param', () => {
24+
const event = {
25+
path: '/foo/bar',
26+
queryStringParameters: {
27+
'redirect_uri': 'http://lvh.me:3000/cb'
28+
}
29+
}
30+
const pathWithQueryStringParams = awsServerlessExpress.getPathWithQueryStringParams(event)
31+
expect(pathWithQueryStringParams).toEqual('/foo/bar?redirect_uri=http%3A%2F%2Flvh.me%3A3000%2Fcb')
32+
})
33+
2334
test('getPathWithQueryStringParams: 2 params', () => {
2435
const event = {
2536
path: '/foo/bar',

index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@
1414
*/
1515
'use strict'
1616
const http = require('http')
17+
const url = require('url')
1718
const binarycase = require('binary-case')
1819

1920
function getPathWithQueryStringParams(event) {
20-
const queryStringKeys = Object.keys(event.queryStringParameters || {})
21-
22-
if (queryStringKeys.length === 0) return event.path
23-
24-
const queryStringParams = queryStringKeys.map(queryStringKey => `${queryStringKey}=${encodeURIComponent(event.queryStringParameters[queryStringKey])}`).join('&')
25-
26-
return `${event.path}?${queryStringParams}`
21+
return url.format({ pathname: event.path, query: event.queryStringParameters })
2722
}
2823

2924
function mapApiGatewayEventToHttpRequest(event, context, socketPath) {

0 commit comments

Comments
 (0)