UNPKG

@incdevco/framework

Version:
231 lines (147 loc) 5.75 kB
var expect = require('chai').expect; var sinon = require('sinon'); var API = require('./index'); describe('api', function () { var api, sandbox; beforeEach(function () { api = new API(); sandbox = sinon.sandbox.create(); sandbox.stub(api, 'error'); sandbox.stub(api, 'log'); }); afterEach(function () { sandbox.verifyAndRestore(); }); it('should set query correctly from querystring when query is undefined', function() { var event = { "body": "Called=%2B13853931363&ToState=UT&CallerCountry=US&Direction=inbound&CallerState=UT&ToZip=&CallSid=CA93ff1d0dbf27a655b0939e7ef4881b7d&To=%2B13853931363&CallerZip=&ToCountry=US&ApiVersion=2010-04-01&CalledZip=&CalledCity=SALT+LAKE+CITY&CallStatus=completed&From=%2B13852988363&AccountSid=AC2bc7a6b7beb9b4a5de7227bcc5825629&CalledCountry=US&CallerCity=SALT+LAKE+CITY&Caller=%2B13852988363&FromCountry=US&ToCity=SALT+LAKE+CITY&FromCity=SALT+LAKE+CITY&CalledState=UT&FromZip=&FromState=UT", "params": { "path": {}, "querystring": { "attempts": "2" }, "header": { "Cache-Control": "max-age=259200", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "US", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Host": "web-hooks.development.cn.incdev.co", "Referer": "https://web-hooks.development.cn.incdev.co/twilio/calls/input?attempts=1", "User-Agent": "TwilioProxy/1.1", "Via": "1.1 eb53c6a0d1be72f83a75c1af82e646b7.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "zpV97JhMXr6I62eSk-SeKoLh28MXGCbO459JHPMjztrl3MlIgvK5iw==", "X-Amzn-Trace-Id": "Root=1-599cb4dc-64abc464010106b046dacfcb", "X-Forwarded-For": "34.230.62.101, 54.240.144.7", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https", "X-Twilio-Signature": "xA1rIeI+02If4t6enbHl46dq8CU=" } }, "method": "POST", "requestId": "16ae1cff-878c-11e7-912b-3b044446d700", "originalPath": "/twilio/calls/input", "path": "/twilio/calls/input", "sourceIp": "", "stage": "development" }; var request = api.eventToRequest(event); expect(request.query.attempts).to.equal(event.params.querystring.attempts, 'request.query.attempts'); }); it('should route to the correct handler based on path and method', function (done) { var event = { method: 'GET', path: '/test' }; var expected = {"body":"test","headers":{},"statusCode":200}; api.register('/test', 'GET', function (request, response) { response.body = 'test'; return true; }); api.handleRequest(event) .then(function (result) { expect(result).to.deep.equal(expected, 'result'); return done(); }) .catch(done); }); it('should catch exception thrown in handler and return generic exception', function (done) { var event = { method: 'GET', path: '/test' }; var expected = 'An Error Occurred'; api.register('/test', 'GET', function (request, response) { throw new Error('Unexpected'); }); api.handleRequest(event) .then(function () { throw new Error('resolved'); }) .catch(function (exception) { expect(exception.message).to.equal(expected, 'exception.message'); return done(); }) .catch(done); }); it('should catch "Not Allowed" exception thrown in handler and rethrow exception', function (done) { var event = { method: 'GET', path: '/test' }; var expected = 'Not Allowed'; api.register('/test', 'GET', function (request, response) { throw new Error(expected); }); api.handleRequest(event) .then(function () { throw new Error('resolved'); }) .catch(function (exception) { expect(exception.message).to.equal(expected, 'exception.message'); return done(); }) .catch(done); }); it('should catch "Not Found" exception thrown in handler and rethrow exception', function (done) { var event = { method: 'GET', path: '/test' }; var expected = 'Not Found'; api.register('/test', 'GET', function (request, response) { throw new Error(expected); }); api.handleRequest(event) .then(function () { throw new Error('resolved'); }) .catch(function (exception) { expect(exception.message).to.equal(expected, 'exception.message'); return done(); }) .catch(done); }); it('should catch "Not Valid" exception thrown in handler and rethrow exception', function (done) { var event = { method: 'GET', path: '/test' }; var expected = 'Not Valid'; api.register('/test', 'GET', function (request, response) { throw new Error(expected); }); api.handleRequest(event) .then(function () { throw new Error('resolved'); }) .catch(function (exception) { expect(exception.message).to.equal(expected, 'exception.message'); return done(); }) .catch(done); }); });