stubborn-ws
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "stubborn-ws", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "", | ||
@@ -38,5 +38,7 @@ "homepage": "https://github.com/ybonnefond/stubborn#stubborn", | ||
"dependencies": { | ||
"accept": "^3.1.3", | ||
"body-parser": "^1.18.3", | ||
"content-type": "^1.0.4", | ||
"lodash": "^4.17.10" | ||
} | ||
} |
@@ -513,3 +513,3 @@ 'use strict'; | ||
it('should respond with provided string body', async() => { | ||
it('should respond with a json encoded body if accept header is json', async() => { | ||
sb | ||
@@ -522,2 +522,19 @@ .get('/') | ||
it('should encode JSON is response content-type header is provided', async() => { | ||
sb | ||
.get('/') | ||
.setResponseBody('toto') | ||
.setResponseHeader('Content-Type', 'application/json; charset=utf-8'); | ||
await expect(request('/', { json: false })).toReplyWith(STATUS_CODES.SUCCESS, JSON.stringify('toto')); | ||
}); | ||
it('should respond with no encodding if accept and content-type is not provided', async() => { | ||
sb | ||
.get('/') | ||
.setResponseBody('toto'); | ||
await expect(request('/', { json: false })).toReplyWith(STATUS_CODES.SUCCESS, 'toto'); | ||
}); | ||
it('should respond with provided object body', async() => { | ||
@@ -549,3 +566,3 @@ sb | ||
describe('Doc ehandle basic usage', () => { | ||
describe('Doc handle basic usage', () => { | ||
it('should respond to query', async() => { | ||
@@ -552,0 +569,0 @@ const body = { some: 'body' }; |
@@ -29,5 +29,3 @@ 'use strict'; | ||
statusCode: STATUS_CODES.SUCCESS, | ||
headers: { | ||
'Content-Type': '{{headers.accept}}' | ||
}, | ||
headers: {}, | ||
body: null | ||
@@ -34,0 +32,0 @@ }; |
@@ -9,2 +9,5 @@ 'use strict'; | ||
const accept = require('accept'); | ||
const contentType = require('content-type'); | ||
class Router { | ||
@@ -119,8 +122,39 @@ constructor(options) { | ||
function reply(route, res, req) { | ||
res.writeHead(route.response.statusCode, applyTemplate(route.response.headers, req)); | ||
const headers = applyTemplate(route.response.headers, req); | ||
const body = applyTemplate(route.response.body, req); | ||
res.write(JSON.stringify(body, null, 2)); // eslint-disable-line no-magic-numbers | ||
res.writeHead(route.response.statusCode, headers); | ||
res.write(encodeBody(req, headers, body)); // eslint-disable-line no-magic-numbers | ||
res.end(); | ||
} | ||
function encodeBody(req, headers, body) { | ||
const mediaType = findContentType(headers); | ||
const mediaTypes = null === mediaType ? findAcceptTypes(req.headers) : [mediaType]; | ||
if (mediaTypes.includes('application/json')) { | ||
return JSON.stringify(body, null, 2); // eslint-disable-line no-magic-numbers | ||
} | ||
return body; | ||
} | ||
function findAcceptTypes(headers) { | ||
const headerKey = Object.keys(headers).find((name) => new RegExp('^accept$', 'i').test(name)); | ||
return 'undefined' === typeof headerKey ? [] : accept.mediaTypes(headers[headerKey]); | ||
} | ||
function findContentType(headers) { | ||
const headerKey = Object.keys(headers).find((name) => new RegExp('^content-type$', 'i').test(name)); | ||
try { | ||
return contentType.parse(headers[headerKey]).type; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
function applyTemplate(template, req, scope) { | ||
@@ -127,0 +161,0 @@ if (null === template || 'undefined' === typeof template) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78120
1607
4
+ Addedaccept@^3.1.3
+ Addedcontent-type@^1.0.4
+ Addedaccept@3.1.3(transitive)
+ Addedboom@7.3.0(transitive)
+ Addedhoek@6.1.3(transitive)