test('server.onError EADDRINUSE', (done) => { const serverWithSameSocketPath = awsServerlessExpress.createServer(mockApp) serverWithSameSocketPath._socketPathSuffix = server._socketPathSuffix const succeed = response => { expect(response.statusCode).toBe(200) done() serverWithSameSocketPath.close() } awsServerlessExpress.proxy(serverWithSameSocketPath, makeEvent({}), { succeed }) })
server.listen(port, host, function() { server.close(cb); });
test('serverListenCallback', (done) => { const serverListenCallback = jest.fn() const serverWithCallback = awsServerlessExpress.createServer(mockApp, serverListenCallback) const succeed = response => { expect(response.statusCode).toBe(200) expect(serverListenCallback).toHaveBeenCalled() serverWithCallback.close() done() } awsServerlessExpress.proxy(serverWithCallback, makeEvent({}), { succeed }) })
function createServer (requestListener, serverListenCallback, binaryTypes) { const server = http.createServer(requestListener) server._socketPathSuffix = getRandomString() server._binaryTypes = binaryTypes ? binaryTypes.slice() : [] server.on('listening', () => { server._isListening = true if (serverListenCallback) serverListenCallback() }) server.on('close', () => { server._isListening = false }) .on('error', (error) => { /* istanbul ignore else */ if (error.code === 'EADDRINUSE') { console.warn(`WARNING: Attempting to listen on socket ${getSocketPath(server._socketPathSuffix)}, but it is already in use. This is likely as a result of a previous invocation error or timeout. Check the logs for the invocation(s) immediately prior to this for root cause, and consider increasing the timeout and/or cpu/memory allocation if this is purely as a result of a timeout. aws-serverless-express will restart the Node.js server listening on a new port and continue with this request.`) server._socketPathSuffix = getRandomString() return server.close(() => startServer(server)) } else { console.log('ERROR: server error') console.error(error) } }) return server }
test('GET JSON single (resolutionMode = PROMISE; new server)', (done) => { const succeed = response => { delete response.headers.date expect(response).toEqual(makeResponse({ 'body': '{"id":1,"name":"Joe"}', 'headers': { 'content-length': '21', 'etag': 'W/"15-rRboW+j/yFKqYqV6yklp53+fANQ"' } })) newServer.close() done() } const newServer = awsServerlessExpress.createServer(app) lambdaFunction.handler(makeEvent({ path: '/users/1', httpMethod: 'GET' }), {}, 'PROMISE', null, newServer) .promise.then(succeed) })
'isBase64Encoded': true })) serverWithBinaryTypes.close() done()
process.on("uncaughtException", function(error) { console.log(error); server.close(); process.exit(1); });
// -- RUN EVERYTHING ----------------------------------------------------------- step( function() { setTimeout(this, 100); }, // let the server start up function() { makeHttpRequest(this); }, _.bind(httpServer.close, httpServer) );
// Start an HTTP server to mock API calls (telemetry server and Bitballoon) // Tests are using child processes, so we cannot use `nock` or similar library // that relies on monkey-patching global variables. const startServer = async function(path, response = {}, { status = 200 } = {}) { const requests = [] const server = createServer((req, res) => requestHandler({ req, res, requests, response, status, path })) await promisify(server.listen.bind(server))(0) const host = getHost(server) const stopServer = promisify(server.close.bind(server)) return { scheme: 'http', host, requests, stopServer } }
// -- RUN EVERYTHING ----------------------------------------------------------- step( function() { setTimeout(this, 100); }, // let the server start up function() { makeRequest('NO FIXTURES' , false, this); }, function() { makeRequest('YES FIXTURES', true , this); }, httpServer.close.bind(httpServer) );
// support listen on unix socket file, server.close() will delete unix socket file when process exit process.on("SIGINT", function() { server.close(); process.exit(0); });
// -- RUN EVERYTHING ----------------------------------------------------------- step( function() { setTimeout(this, 100); }, // let the server start up function() { makeHttpRequest('en,sp', 'Hello', this); }, function() { makeHttpRequest('sp,en', 'Hola' , this); }, _.bind(httpServer.close, httpServer) );
// -- RUN EVERYTHING ----------------------------------------------------------- step( function() { setTimeout(this, 100); }, // let the server start up function() { makeHttpRequest(this); }, _.bind(httpServer.close, httpServer) );
// -- RUN EVERYTHING ----------------------------------------------------------- step( function() { setTimeout(this, 100); }, // let the server start up function() { makeHttpRequest(this); }, _.bind(httpServer.close, httpServer) );
// -- RUN EVERYTHING ----------------------------------------------------------- step( function() { setTimeout(this, 100); }, // let the server start up function() { makeHttpRequest(this); }, _.bind(httpServer.close, httpServer) );