-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
762 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,354 @@ | ||
import assert from 'node:assert' | ||
import { dirname, resolve } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { setup, teardown } from '../../_testHelpers/index.js' | ||
import { BASE_URL } from '../../config.js' | ||
|
||
const { stringify } = JSON | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
describe('ALB handler payload tests', function desc() { | ||
beforeEach(() => | ||
setup({ | ||
noPrependStageInUrl: false, | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
afterEach(() => teardown()) | ||
|
||
// | ||
;[ | ||
{ | ||
description: 'when handler is context.done', | ||
expected: 'foo', | ||
path: '/dev/context-done-handler', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler is context.done which is deferred', | ||
expected: 'foo', | ||
path: '/dev/context-done-handler-deferred', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler is context.succeed', | ||
expected: 'foo', | ||
path: '/dev/context-succeed-handler', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler is context.succeed which is deferred', | ||
expected: 'foo', | ||
path: '/dev/context-succeed-handler-deferred', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler is a callback', | ||
expected: 'foo', | ||
path: '/dev/callback-handler', | ||
status: 200, | ||
}, | ||
{ | ||
description: 'when handler is a callback which is deferred', | ||
expected: 'foo', | ||
path: '/dev/callback-handler-deferred', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler returns a promise', | ||
expected: 'foo', | ||
path: '/dev/promise-handler', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler a promise which is deferred', | ||
expected: 'foo', | ||
path: '/dev/promise-handler-deferred', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: 'when handler is an async function', | ||
expected: 'foo', | ||
path: '/dev/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: '/dev/promise-with-defined-callback-handler', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
description: | ||
'when handler throws an expection in promise should return 502', | ||
path: '/dev/throw-exception-in-promise-handler', | ||
status: 502, | ||
}, | ||
|
||
{ | ||
description: | ||
'when handler throws an expection before calling callback should return 502', | ||
path: '/dev/throw-exception-in-callback-handler', | ||
status: 502, | ||
}, | ||
|
||
{ | ||
description: | ||
'when handler does not return any answer in promise should return 502', | ||
path: '/dev/no-answer-in-promise-handler', | ||
status: 502, | ||
}, | ||
|
||
{ | ||
description: | ||
'when handler returns bad answer in promise should return 204', | ||
path: '/dev/bad-answer-in-promise-handler', | ||
status: 204, | ||
}, | ||
|
||
{ | ||
description: | ||
'when handler returns bad answer in callback should return 204', | ||
path: '/dev/bad-answer-in-callback-handler', | ||
status: 204, | ||
}, | ||
|
||
// TODO: reactivate! | ||
// { | ||
// description: 'when handler calls context.succeed and context.done', | ||
// expected: 'Hello Context.succeed!', | ||
// path: '/dev/context-succeed-with-context-done-handler', | ||
// }, | ||
|
||
// TODO: reactivate! | ||
// { | ||
// description: 'when handler calls callback and context.done', | ||
// expected: 'Hello Callback!', | ||
// path: '/dev/callback-with-context-done-handler', | ||
// }, | ||
|
||
// TODO: reactivate! | ||
// { | ||
// description: 'when handler calls callback and returns Promise', | ||
// expected: 'Hello Callback!', | ||
// path: '/dev/callback-with-promise-handler', | ||
// }, | ||
|
||
// TODO: reactivate! | ||
// { | ||
// description: 'when handler calls callback inside returned Promise', | ||
// expected: 'Hello Callback!', | ||
// path: '/dev/callback-inside-promise-handler', | ||
// }, | ||
].forEach(({ description, expected, path, status }) => { | ||
it(description, async () => { | ||
const url = new URL(path, BASE_URL) | ||
url.port = url.port ? '3003' : url.port | ||
|
||
const response = await fetch(url) | ||
assert.equal(response.status, status) | ||
|
||
if (expected) { | ||
const json = await response.json() | ||
assert.deepEqual(json, expected) | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
describe('ALB handler payload tests with prepend off', function desc() { | ||
beforeEach(() => | ||
setup({ | ||
args: ['--noPrependStageInUrl'], | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
afterEach(() => 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 204', | ||
path: '/bad-answer-in-promise-handler', | ||
status: 204, | ||
}, | ||
|
||
{ | ||
description: | ||
'when handler returns bad answer in callback should return 204', | ||
path: '/bad-answer-in-callback-handler', | ||
status: 204, | ||
}, | ||
].forEach(({ description, expected, path, status }) => { | ||
it(description, async () => { | ||
const url = new URL(path, BASE_URL) | ||
|
||
const response = await fetch(url) | ||
assert.equal(response.status, status) | ||
|
||
if (expected) { | ||
const json = await response.json() | ||
assert.deepEqual(json, expected) | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
describe('ALB handler payload schemas validation tests', function desc() { | ||
beforeEach(() => | ||
setup({ | ||
args: ['--noPrependStageInUrl'], | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
afterEach(() => teardown()) | ||
|
||
// | ||
;[ | ||
{ | ||
body: { | ||
foo: 'bar', | ||
}, | ||
description: 'test with valid payload', | ||
expectedBody: `{"foo":"bar"}`, | ||
path: '/test-payload-schema-validator', | ||
status: 200, | ||
}, | ||
|
||
{ | ||
body: {}, | ||
description: 'test with invalid payload', | ||
path: '/test-payload-schema-validator', | ||
status: 400, | ||
}, | ||
].forEach(({ description, expectedBody, path, body, status }) => { | ||
it(description, async () => { | ||
const url = new URL(path, BASE_URL) | ||
|
||
const response = await fetch(url, { | ||
body: stringify(body), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'post', | ||
}) | ||
|
||
assert.equal(response.status, status) | ||
|
||
if (expectedBody) { | ||
const json = await response.json() | ||
assert.deepEqual(json.body, expectedBody) | ||
} | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.