-
-
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.
feat: Ensure
websocket
parity with API Gateway (#1301)
- Loading branch information
Showing
9 changed files
with
378 additions
and
34 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
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
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,24 @@ | ||
const websocketSend = (ws, data) => | ||
new Promise((res) => { | ||
ws.on('open', () => { | ||
ws.send(data, (e) => { | ||
if (e) { | ||
res({ err: e }) | ||
} | ||
}) | ||
}) | ||
ws.on('close', (c) => { | ||
res({ code: c }) | ||
}) | ||
ws.on('message', (d) => { | ||
res({ data: d }) | ||
}) | ||
ws.on('error', (e) => { | ||
res({ err: e }) | ||
}) | ||
setTimeout(() => { | ||
res({}) | ||
}, 5000) | ||
}) | ||
|
||
export default websocketSend |
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,19 @@ | ||
'use strict' | ||
|
||
exports.handler = async (event) => { | ||
const { body, requestContext } = event | ||
|
||
if ( | ||
body && | ||
JSON.parse(body).throwError && | ||
requestContext && | ||
requestContext.routeKey === '$default' | ||
) { | ||
throw new Error('Throwing error from incoming message') | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: body || undefined, | ||
} | ||
} |
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,26 @@ | ||
service: oneway-websocket-tests | ||
|
||
plugins: | ||
- ../../../ | ||
|
||
provider: | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs12.x | ||
stage: dev | ||
versionFunctions: false | ||
|
||
functions: | ||
handler: | ||
handler: handler.handler | ||
events: | ||
- http: | ||
path: echo | ||
method: get | ||
- websocket: | ||
route: $connect | ||
- websocket: | ||
route: $disconnect | ||
- websocket: | ||
route: $default |
55 changes: 55 additions & 0 deletions
55
tests/integration/websocket-oneway/websocket-oneway.test.js
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,55 @@ | ||
import { resolve } from 'path' | ||
import WebSocket from 'ws' | ||
import { joinUrl, setup, teardown } from '../_testHelpers/index.js' | ||
import websocketSend from '../_testHelpers/websocketPromise.js' | ||
|
||
jest.setTimeout(30000) | ||
|
||
describe('one way websocket tests', () => { | ||
// init | ||
beforeAll(() => | ||
setup({ | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
// cleanup | ||
afterAll(() => teardown()) | ||
|
||
test('websocket echos nothing', async () => { | ||
const url = new URL(joinUrl(TEST_BASE_URL, '/dev')) | ||
url.port = url.port ? '3001' : url.port | ||
url.protocol = 'ws' | ||
|
||
const payload = JSON.stringify({ | ||
hello: 'world', | ||
now: new Date().toISOString(), | ||
}) | ||
|
||
const ws = new WebSocket(url.toString()) | ||
const { data, code, err } = await websocketSend(ws, payload) | ||
|
||
expect(code).toBeUndefined() | ||
expect(err).toBeUndefined() | ||
expect(data).toBeUndefined() | ||
}) | ||
|
||
test('execution error emits Internal Server Error', async () => { | ||
const url = new URL(joinUrl(TEST_BASE_URL, '/dev')) | ||
url.port = url.port ? '3001' : url.port | ||
url.protocol = 'ws' | ||
|
||
const payload = JSON.stringify({ | ||
hello: 'world', | ||
now: new Date().toISOString(), | ||
throwError: true, | ||
}) | ||
|
||
const ws = new WebSocket(url.toString()) | ||
const { data, code, err } = await websocketSend(ws, payload) | ||
|
||
expect(code).toBeUndefined() | ||
expect(err).toBeUndefined() | ||
expect(JSON.parse(data).message).toEqual('Internal server error') | ||
}) | ||
}) |
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,32 @@ | ||
'use strict' | ||
|
||
exports.handler = async (event) => { | ||
const { body, queryStringParameters, requestContext } = event | ||
const statusCode = | ||
queryStringParameters && queryStringParameters.statusCode | ||
? Number(queryStringParameters.statusCode) | ||
: 200 | ||
|
||
if ( | ||
queryStringParameters && | ||
queryStringParameters.throwError && | ||
requestContext && | ||
requestContext.routeKey === '$connect' | ||
) { | ||
throw new Error('Throwing error during connect phase') | ||
} | ||
|
||
if ( | ||
body && | ||
JSON.parse(body).throwError && | ||
requestContext && | ||
requestContext.routeKey === '$default' | ||
) { | ||
throw new Error('Throwing error from incoming message') | ||
} | ||
|
||
return { | ||
statusCode, | ||
body: body || undefined, | ||
} | ||
} |
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,28 @@ | ||
service: twoway-websocket-tests | ||
|
||
plugins: | ||
- ../../../ | ||
|
||
provider: | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs12.x | ||
stage: dev | ||
versionFunctions: false | ||
|
||
functions: | ||
handler: | ||
handler: handler.handler | ||
events: | ||
- http: | ||
path: echo | ||
method: get | ||
- websocket: | ||
route: $connect | ||
- websocket: | ||
route: $disconnect | ||
- websocket: | ||
route: $default | ||
# Enable 2-way comms | ||
routeResponseSelectionExpression: $default |
Oops, something went wrong.