Skip to content

Commit

Permalink
Merge pull request #946 from dl748/master
Browse files Browse the repository at this point in the history
noPrependStageInUrl false should have a leading slash
  • Loading branch information
dherault committed Apr 1, 2020
2 parents 287c265 + 8533205 commit 36414e3
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
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,
}),
)

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!
// {
// 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

0 comments on commit 36414e3

Please sign in to comment.