Skip to content

Commit

Permalink
close #80 with initial ALB support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Dec 26, 2018
1 parent 7d6c169 commit 776dde2
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/response.js
Expand Up @@ -456,6 +456,7 @@ class RESPONSE {
: { headers: UTILS.stringifyHeaders(this._headers) },
{
statusCode: this._statusCode,
statusDescription: this._request.interface === 'alb' ? `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` : undefined,
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body,this._serializer),
isBase64Encoded: this._isBase64
}
Expand Down
82 changes: 82 additions & 0 deletions lib/statusCodes.js
@@ -0,0 +1,82 @@
'use strict'

/**
* Lightweight web framework for your serverless applications
* @author Jeremy Daly <jeremy@jeremydaly.com>
* @license MIT
*/

// HTTP status code map (IANA Status Code Registry)

module.exports = {
// 1xx: Informational
100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
103: 'Early Hints',

// 2xx: Success
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
207: 'Multi-Status',
208: 'Already Reported',
226: 'IM Used',

// 3xx: Redirection
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
307: 'Temporary Redirect',
308: 'Permanent Redirect',

// 4xx: Client Error
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Payload Too Large',
414: 'URI Too Long',
415: 'Unsupported Media Type',
416: 'Range Not Satisfiable',
417: 'Expectation Failed',
421: 'Misdirected Request',
422: 'Unprocessable Entity',
423: 'Locked',
424: 'Failed Dependency',
425: 'Too Early',
426: 'Upgrade Required',
428: 'Precondition Required',
429: 'Too Many Requests',
431: 'Request Header Fields Too Large',
451: 'Unavailable For Legal Reasons',

// 5xx: Server Error
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
506: 'Variant Also Negotiates',
507: 'Insufficient Storage',
508: 'Loop Detected',
510: 'Not Extended',
511: 'Network Authentication Required'
}
6 changes: 6 additions & 0 deletions lib/utils.js
Expand Up @@ -95,6 +95,12 @@ exports.mimeLookup = (input,custom={}) => {
}
}

const statusCodes = require('./statusCodes.js') // MIME Map

exports.statusLookup = status => {
return status in statusCodes ? statusCodes[status] : 'Unknown'
}


// Parses routes into readable array
const extractRoutes = (routes,table=[]) => {
Expand Down
33 changes: 33 additions & 0 deletions test/requests.js
Expand Up @@ -16,6 +16,11 @@ api.get('/test/hello', function(req,res) {
res.status(200).json({ request })
})

api.get('/test/201', function(req,res) {
let request = Object.assign(req,{app:null})
res.status(201).json({ request })
})



/******************************************************************************/
Expand Down Expand Up @@ -89,6 +94,7 @@ describe('Request Tests:', function() {
let _context = require('./sample-context-alb1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(JSON.stringify(result,null,2));
expect(result.headers).to.deep.equal({ 'content-type': 'application/json', 'set-cookie': 'test2=value2; Path=/' })
expect(body).to.have.property('request')
expect(body.request.id).is.not.null
Expand Down Expand Up @@ -130,6 +136,33 @@ describe('Request Tests:', function() {
expect(body.request.multiValueHeaders['test-header']).to.deep.equal(['val1','val2'])
})


it('Alternate statuss code', async function() {
let _event = Object.assign(require('./sample-event-alb2.json'),{ path: '/test/201' })
let _context = require('./sample-context-alb1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(JSON.stringify(result,null,2));
expect(result.multiValueHeaders).to.deep.equal({ 'content-type': ['application/json'] })
expect(result.statusDescription).to.equal('201 Created')
expect(body).to.have.property('request')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('alb')
expect(body.request.userAgent).to.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('192.168.100.1')
expect(body.request.isBase64Encoded).to.equal(true)
expect(body.request.clientType).to.equal('unknown')
expect(body.request.clientCountry).to.equal('unknown')
expect(body.request.route).to.equal('/test/201')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.multiValueQuery.qs1).to.deep.equal(['foo'])
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['foo','bar','bat'])
expect(body.request.headers['test-header']).to.equal('val1,val2')
expect(body.request.multiValueHeaders['test-header']).to.deep.equal(['val1','val2'])
})

})

}) // end Request tests
37 changes: 37 additions & 0 deletions test/utils.js
Expand Up @@ -192,6 +192,43 @@ describe('Utility Function Tests:', function() {
}) // end encodeBody tests


describe('statusLookup:', function() {

it('200', function() {
expect(utils.statusLookup(200)).to.equal('OK')
}) // end it

it('201', function() {
expect(utils.statusLookup(201)).to.equal('Created')
}) // end it

it('304', function() {
expect(utils.statusLookup(304)).to.equal('Not Modified')
}) // end it

it('404', function() {
expect(utils.statusLookup(404)).to.equal('Not Found')
}) // end it

it('502', function() {
expect(utils.statusLookup(502)).to.equal('Bad Gateway')
}) // end it

it('999 Uknown', function() {
expect(utils.statusLookup(999)).to.equal('Unknown')
}) // end it

it('As string (parsable as int)', function() {
expect(utils.statusLookup('200')).to.equal('OK')
}) // end it

it('As string (not parsable as int)', function() {
expect(utils.statusLookup('foo')).to.equal('Unknown')
}) // end it

}) // end encodeBody tests


describe('extractRoutes:', function() {

it('Sample routes', function() {
Expand Down

0 comments on commit 776dde2

Please sign in to comment.