Skip to content
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

noPrependStageInUrl false should have a leading slash #946

Merged
merged 5 commits into from Apr 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/events/http/HttpServer.js
Expand Up @@ -347,7 +347,7 @@ export default class HttpServer {
}

const requestPath = request.path.substr(
this.#options.noPrependStageInUrl ? 1 : `/${stage}`.length,
this.#options.noPrependStageInUrl ? 0 : `/${stage}`.length,
)

if (request.auth.credentials && request.auth.strategy) {
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/handler/handler.js
Expand Up @@ -195,3 +195,10 @@ exports.BadAnswerInPromiseHandler = async () => {
exports.BadAnswerInCallbackHandler = (event, context, callback) => {
callback(null, {})
}

exports.TestPathVariable = (event, context, callback) => {
callback(null, {
statusCode: 200,
body: stringify(event.path),
})
}
150 changes: 150 additions & 0 deletions tests/integration/handler/handlerPayload.test.js
Expand Up @@ -9,6 +9,7 @@ describe('handler payload tests', () => {
beforeAll(() =>
setup({
servicePath: resolve(__dirname),
noPrependStageInUrl: false,
dl748 marked this conversation as resolved.
Show resolved Hide resolved
}),
)

Expand Down Expand Up @@ -124,6 +125,13 @@ describe('handler payload tests', () => {
status: 200,
},

{
description: 'test path variable with Prepend',
expected: '/test-path-variable-handler',
path: '/dev/test-path-variable-handler',
status: 200,
},

// TODO: reactivate!
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you uncomment these lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want me to remove the comments?

// {
// description: 'when handler calls context.succeed and context.done',
Expand Down Expand Up @@ -165,3 +173,145 @@ describe('handler payload tests', () => {
})
})
})

describe('handler payload tests with prepend off', () => {
// init
beforeAll(() =>
setup({
servicePath: resolve(__dirname),
args: ['--noPrependStageInUrl'],
}),
)

// cleanup
afterAll(() => teardown())

//
;[
{
description: 'when handler is context.done',
expected: 'foo',
path: '/context-done-handler',
status: 200,
},

{
description: 'when handler is context.done which is deferred',
expected: 'foo',
path: '/context-done-handler-deferred',
status: 200,
},

{
description: 'when handler is context.succeed',
expected: 'foo',
path: '/context-succeed-handler',
status: 200,
},

{
description: 'when handler is context.succeed which is deferred',
expected: 'foo',
path: '/context-succeed-handler-deferred',
status: 200,
},

{
description: 'when handler is a callback',
expected: 'foo',
path: '/callback-handler',
status: 200,
},
{
description: 'when handler is a callback which is deferred',
expected: 'foo',
path: '/callback-handler-deferred',
status: 200,
},

{
description: 'when handler returns a promise',
expected: 'foo',
path: '/promise-handler',
status: 200,
},

{
description: 'when handler a promise which is deferred',
expected: 'foo',
path: '/promise-handler-deferred',
status: 200,
},

{
description: 'when handler is an async function',
expected: 'foo',
path: '/async-function-handler',
status: 200,
},

// NOTE: mix and matching of callbacks and promises is not recommended,
// nonetheless, we test some of the behaviour to match AWS execution precedence
{
description:
'when handler returns a callback but defines a callback parameter',
expected: 'Hello Promise!',
path: '/promise-with-defined-callback-handler',
status: 200,
},

{
description:
'when handler throws an expection in promise should return 502',
path: '/throw-exception-in-promise-handler',
status: 502,
},

{
description:
'when handler throws an expection before calling callback should return 502',
path: '/throw-exception-in-callback-handler',
status: 502,
},

{
description:
'when handler does not return any answer in promise should return 502',
path: '/no-answer-in-promise-handler',
status: 502,
},

{
description:
'when handler returns bad answer in promise should return 200',
path: '/bad-answer-in-promise-handler',
status: 200,
},

{
description:
'when handler returns bad answer in callback should return 200',
path: '/bad-answer-in-callback-handler',
status: 200,
},

{
description: 'test path variable with Prepend',
expected: '/test-path-variable-handler',
path: '/test-path-variable-handler',
status: 200,
},
].forEach(({ description, expected, path, status }) => {
test(description, async () => {
const url = joinUrl(TEST_BASE_URL, path)

const response = await fetch(url)
expect(response.status).toEqual(status)

if (expected) {
const json = await response.json()
expect(json).toEqual(expected)
}
})
})
})
7 changes: 7 additions & 0 deletions tests/integration/handler/serverless.yml
Expand Up @@ -148,3 +148,10 @@ functions:
method: get
path: bad-answer-in-callback-handler
handler: handler.BadAnswerInCallbackHandler

TestPathVariable:
events:
- http:
method: get
path: test-path-variable-handler
handler: handler.TestPathVariable