light-my-request
Advanced tools
Comparing version 3.4.1 to 3.5.0
'use strict' | ||
/* eslint no-prototype-builtins: 0 */ | ||
const { Readable } = require('readable-stream') | ||
@@ -4,0 +6,0 @@ const util = require('util') |
@@ -111,2 +111,10 @@ 'use strict' | ||
// Prepare payload parsers | ||
res.json = function parseJsonPayload () { | ||
if (this.headers['content-type'].indexOf('application/json') < 0) { | ||
throw new Error('The content-type of the response is not application/json') | ||
} | ||
return JSON.parse(this.payload) | ||
} | ||
return res | ||
@@ -113,0 +121,0 @@ } |
{ | ||
"name": "light-my-request", | ||
"version": "3.4.1", | ||
"version": "3.5.0", | ||
"description": "Fake HTTP injection library", | ||
"main": "index.js", | ||
"dependencies": { | ||
"ajv": "^6.8.1", | ||
"readable-stream": "^3.1.1" | ||
"ajv": "^6.10.2", | ||
"readable-stream": "^3.4.0" | ||
}, | ||
"types": "index.d.ts", | ||
"devDependencies": { | ||
"@types/node": "^11.11.4", | ||
"form-data": "^2.3.3", | ||
"@types/node": "^11.13.22", | ||
"form-data": "^2.5.1", | ||
"pre-commit": "^1.2.2", | ||
"standard": "^12.0.0", | ||
"standard": "^14.0.2", | ||
"tap": "^12.6.6", | ||
"tsd": "^0.7.0" | ||
"tsd": "^0.7.4" | ||
}, | ||
@@ -19,0 +19,0 @@ "scripts": { |
@@ -102,3 +102,3 @@ # Light my Request | ||
- `Request` - custom light-my-request `request` object interface. Extends Node.js `stream.Readable` type | ||
- `Response` - custom light-my-re uest `response` object interface. Extends Node.js `http.ServerResponse` type | ||
- `Response` - custom light-my-request `response` object interface. Extends Node.js `http.ServerResponse` type | ||
@@ -105,0 +105,0 @@ ## API |
@@ -283,3 +283,3 @@ 'use strict' | ||
res.setHeader('Trailer', 'Test') | ||
res.addTrailers({ 'Test': 123 }) | ||
res.addTrailers({ Test: 123 }) | ||
res.end() | ||
@@ -913,2 +913,54 @@ } | ||
test('Response.json() should parse the JSON payload', (t) => { | ||
t.plan(2) | ||
const json = { | ||
a: 1, | ||
b: '2' | ||
} | ||
const dispatch = function (req, res) { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
res.end(JSON.stringify(json)) | ||
} | ||
inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => { | ||
t.error(err) | ||
t.deepEqual(res.json(), json) | ||
}) | ||
}) | ||
test('Response.json() should throw an error if content-type is not application/json', (t) => { | ||
t.plan(2) | ||
const json = { | ||
a: 1, | ||
b: '2' | ||
} | ||
const dispatch = function (req, res) { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end(JSON.stringify(json)) | ||
} | ||
inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => { | ||
t.error(err) | ||
t.throws(res.json, Error) | ||
}) | ||
}) | ||
test('Response.json() should throw an error if the payload is not of valid JSON format', (t) => { | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
res.end('notAJSON') | ||
} | ||
inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => { | ||
t.error(err) | ||
t.throws(res.json, Error) | ||
}) | ||
}) | ||
function getTestStream (encoding) { | ||
@@ -915,0 +967,0 @@ const word = 'hi' |
49103
1290
Updatedajv@^6.10.2
Updatedreadable-stream@^3.4.0