Skip to content

fix: make sure to strip also base path non pathParameter #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions __tests__/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const apiGatewayEventSource = eventSources.getEventSource({ eventSourceName: 'AW

test('getPathWithQueryStringParams: no params', () => {
const event = {
resource: '/',
path: '/foo/bar'
}
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
Expand All @@ -18,6 +19,7 @@ test('getPathWithQueryStringParams: no params', () => {

test('getPathWithQueryStringParams: 1 param', () => {
const event = {
resource: '/',
path: '/foo/bar',
multiValueQueryStringParameters: {
bizz: 'bazz'
Expand All @@ -29,6 +31,7 @@ test('getPathWithQueryStringParams: 1 param', () => {

test('getPathWithQueryStringParams: to be url-encoded param', () => {
const event = {
resource: '/',
path: '/foo/bar',
multiValueQueryStringParameters: {
redirect_uri: 'http://lvh.me:3000/cb'
Expand All @@ -40,6 +43,7 @@ test('getPathWithQueryStringParams: to be url-encoded param', () => {

test('getPathWithQueryStringParams: 2 params', () => {
const event = {
resource: '/',
path: '/foo/bar',
multiValueQueryStringParameters: {
bizz: 'bazz',
Expand All @@ -52,6 +56,7 @@ test('getPathWithQueryStringParams: 2 params', () => {

test('getPathWithQueryStringParams: array param', () => {
const event = {
resource: '/',
path: '/foo/bar',
multiValueQueryStringParameters: {
bizz: [
Expand All @@ -64,8 +69,52 @@ test('getPathWithQueryStringParams: array param', () => {
expect(pathWithQueryStringParams).toEqual('/foo/bar?bizz=bazz&bizz=buzz')
})

test('getPathWithQueryStringParams: pathParameters.proxy', () => {
const event = {
resource: '/foo/{proxy+}',
path: '/foo/123',
pathParameters: {
proxy: '123'
}
}
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
expect(pathWithQueryStringParams).toEqual('/123')
})

test('getPathWithQueryStringParams: customDomain and no path parameters', () => {
const event = {
resource: '/foo',
path: '/baz/foo',
requestContext: {
customDomain: {
basePathMatched: 'baz'
}
}
}
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
expect(pathWithQueryStringParams).toEqual('/')
})

test('getPathWithQueryStringParams: customDomain and path parameters', () => {
const event = {
resource: '/foo/{proxy+}',
path: '/baz/foo/123',
pathParameters: {
proxy: '123'
},
requestContext: {
customDomain: {
basePathMatched: 'baz'
}
}
}
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
expect(pathWithQueryStringParams).toEqual('/123')
})

function getReqRes (multiValueHeaders = {}) {
const event = {
resource: '/',
path: '/foo',
httpMethod: 'GET',
body: 'Hello serverless!',
Expand Down
11 changes: 9 additions & 2 deletions src/event-sources/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const url = require('url')

function getDefalutStripBasePath (event) {
const basePathMatched = event?.requestContext?.customDomain?.basePathMatched || ''
const resource = event?.pathParameters?.proxy ? '' : event.resource
return basePathMatched ? `/${basePathMatched}${resource}` : resource
}

function getPathWithQueryStringParams ({
event,
query = event.multiValueQueryStringParameters,
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path`
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path,
// NOTE: Strip base path for custom domains
stripBasePath = '',
stripBasePath = getDefalutStripBasePath(event),
replaceRegex = new RegExp(`^${stripBasePath}`)
}) {
const pathname = path.replace(replaceRegex, '')
return url.format({
pathname: path.replace(replaceRegex, ''),
pathname: pathname.startsWith('/') ? pathname : `/${pathname}`,
query
})
}
Expand Down