light-my-request
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -73,5 +73,5 @@ 'use strict' | ||
} else { | ||
return new Promise((resolve) => { | ||
return new Promise((resolve, reject) => { | ||
const req = new Request(options) | ||
const res = new Response(req, resolve) | ||
const res = new Response(req, resolve, reject) | ||
@@ -78,0 +78,0 @@ req.prepare(() => dispatchFunc.call(server, req, res)) |
@@ -5,6 +5,6 @@ 'use strict' | ||
const http = require('http') | ||
const Stream = require('stream') | ||
const stream = require('readable-stream') | ||
const util = require('util') | ||
function Response (req, onEnd) { | ||
function Response (req, onEnd, reject) { | ||
http.ServerResponse.call(this, { | ||
@@ -21,7 +21,27 @@ method: req.method, | ||
this._promiseCallback = typeof reject === 'function' | ||
const onEndSuccess = (payload) => { | ||
if (this._promiseCallback) { | ||
return process.nextTick(() => onEnd(payload)) | ||
} | ||
process.nextTick(() => onEnd(null, payload)) | ||
} | ||
const onEndFailure = (err) => { | ||
if (this._promiseCallback) { | ||
return process.nextTick(() => reject(err)) | ||
} | ||
process.nextTick(() => onEnd(err, null)) | ||
} | ||
this.once('finish', () => { | ||
const res = generatePayload(this) | ||
res.raw.req = req | ||
process.nextTick(() => onEnd(res)) | ||
onEndSuccess(res) | ||
}) | ||
this.connection.once('error', onEndFailure) | ||
this.once('error', onEndFailure) | ||
} | ||
@@ -95,3 +115,3 @@ | ||
function getNullSocket () { | ||
return new Stream.Writable({ | ||
return new stream.Writable({ | ||
write (chunk, encoding, callback) { | ||
@@ -98,0 +118,0 @@ setImmediate(callback) |
{ | ||
"name": "light-my-request", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Fake HTTP injection library", | ||
"main": "index.js", | ||
"dependencies": { | ||
"ajv": "^5.2.3", | ||
"ajv": "^5.5.2", | ||
"readable-stream": "^2.3.3", | ||
"safe-buffer": "^5.1.1" | ||
@@ -13,3 +14,3 @@ }, | ||
"standard": "^10.0.3", | ||
"tap": "^10.7.2" | ||
"tap": "^11.0.1" | ||
}, | ||
@@ -16,0 +17,0 @@ "scripts": { |
@@ -22,3 +22,3 @@ # Light my Request | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
console.log(res.payload) | ||
@@ -34,6 +34,11 @@ }) | ||
.then(res => console.log(res.payload)) | ||
.catch(console.log) | ||
// async-await | ||
const res = await inject(dispatch, { method: 'get', url: '/' }) | ||
console.log(res.payload) | ||
try { | ||
const res = await inject(dispatch, { method: 'get', url: '/' }) | ||
console.log(res.payload) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
``` | ||
@@ -65,3 +70,4 @@ | ||
- `server` - Optional http server. It is used for binding the `dispatchFunc`. | ||
- `callback` - the callback function using the signature `function (res)` where: | ||
- `callback` - the callback function using the signature `function (err, res)` where: | ||
- `err` - error object | ||
- `res` - a response object where: | ||
@@ -80,3 +86,3 @@ - `raw` - an object containing the raw request and response objects where: | ||
Checks if given object `obj` is a Shot `Request` object. | ||
Checks if given object `obj` is a *light-my-request* `Request` object. | ||
@@ -83,0 +89,0 @@ ## Acknowledgements |
'use strict' | ||
async function asyncAwaitTest (t, inject) { | ||
t.plan(1) | ||
const dispatch = function (req, res) { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end('hello') | ||
} | ||
function asyncAwaitTest (t, inject) { | ||
t.plan(2) | ||
const res = await inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }) | ||
t.equal(res.payload, 'hello') | ||
t.test('basic async await', async t => { | ||
const dispatch = function (req, res) { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end('hello') | ||
} | ||
try { | ||
const res = await inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }) | ||
t.equal(res.payload, 'hello') | ||
} catch (err) { | ||
t.fail(err) | ||
} | ||
}) | ||
t.test('basic async await (errored)', async t => { | ||
const dispatch = function (req, res) { | ||
res.connection.destroy(new Error('kaboom')) | ||
} | ||
try { | ||
await inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }) | ||
t.fail('should throw') | ||
} catch (err) { | ||
t.ok(err) | ||
} | ||
}) | ||
} | ||
module.exports = asyncAwaitTest |
224
test/test.js
@@ -13,3 +13,3 @@ 'use strict' | ||
test('returns non-chunked payload', (t) => { | ||
t.plan(6) | ||
t.plan(7) | ||
const output = 'example.com:8080|/hello' | ||
@@ -24,3 +24,4 @@ | ||
inject(dispatch, 'http://example.com:8080/hello', (res) => { | ||
inject(dispatch, 'http://example.com:8080/hello', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
@@ -42,3 +43,3 @@ t.equal(res.statusMessage, 'Super') | ||
test('returns single buffer payload', (t) => { | ||
t.plan(5) | ||
t.plan(6) | ||
const dispatch = function (req, res) { | ||
@@ -49,3 +50,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { url: 'http://example.com:8080/hello' }, (res) => { | ||
inject(dispatch, { url: 'http://example.com:8080/hello' }, (err, res) => { | ||
t.error(err) | ||
t.ok(res.headers.date) | ||
@@ -60,3 +62,3 @@ t.ok(res.headers.connection) | ||
test('passes headers', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -67,3 +69,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { Super: 'duper' } }, (res) => { | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { Super: 'duper' } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'duper') | ||
@@ -74,3 +77,3 @@ }) | ||
test('passes remote address', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -81,3 +84,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', remoteAddress: '1.2.3.4' }, (res) => { | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', remoteAddress: '1.2.3.4' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '1.2.3.4') | ||
@@ -88,3 +92,3 @@ }) | ||
test('passes localhost as default remote address', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -95,3 +99,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }, (res) => { | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '127.0.0.1') | ||
@@ -102,3 +107,3 @@ }) | ||
test('passes host option as host header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -109,3 +114,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: '/hello', headers: { host: 'test.example.com' } }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/hello', headers: { host: 'test.example.com' } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'test.example.com') | ||
@@ -116,3 +122,3 @@ }) | ||
test('passes localhost as default host header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -123,3 +129,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: '/hello' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/hello' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'localhost:80') | ||
@@ -130,3 +137,3 @@ }) | ||
test('passes authority as host header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -137,3 +144,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: '/hello', authority: 'something' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/hello', authority: 'something' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'something') | ||
@@ -144,3 +152,3 @@ }) | ||
test('passes uri host as host header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -151,3 +159,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }, (res) => { | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'example.com:8080') | ||
@@ -158,3 +167,3 @@ }) | ||
test('includes default http port in host header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -165,3 +174,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, 'http://example.com', (res) => { | ||
inject(dispatch, 'http://example.com', (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'example.com:80') | ||
@@ -172,3 +182,3 @@ }) | ||
test('includes default https port in host header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -179,3 +189,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, 'https://example.com', (res) => { | ||
inject(dispatch, 'https://example.com', (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'example.com:443') | ||
@@ -186,3 +197,3 @@ }) | ||
test('optionally accepts an object as url', (t) => { | ||
t.plan(4) | ||
t.plan(5) | ||
const output = 'example.com:8080|/hello?test=1234' | ||
@@ -205,3 +216,4 @@ | ||
inject(dispatch, { url }, (res) => { | ||
inject(dispatch, { url }, (err, res) => { | ||
t.error(err) | ||
t.ok(res.headers.date) | ||
@@ -215,3 +227,3 @@ t.ok(res.headers.connection) | ||
test('leaves user-agent unmodified', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -222,3 +234,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { 'user-agent': 'duper' } }, (res) => { | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello', headers: { 'user-agent': 'duper' } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'duper') | ||
@@ -229,3 +242,3 @@ }) | ||
test('returns chunked payload', (t) => { | ||
t.plan(4) | ||
t.plan(5) | ||
const dispatch = function (req, res) { | ||
@@ -238,3 +251,4 @@ res.writeHead(200, 'OK') | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.ok(res.headers.date) | ||
@@ -248,3 +262,3 @@ t.ok(res.headers.connection) | ||
test('sets trailers in response object', (t) => { | ||
t.plan(3) | ||
t.plan(4) | ||
const dispatch = function (req, res) { | ||
@@ -256,3 +270,4 @@ res.setHeader('Trailer', 'Test') | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.headers.trailer, 'Test') | ||
@@ -265,3 +280,3 @@ t.equal(res.headers.test, undefined) | ||
test('parses zipped payload', (t) => { | ||
t.plan(3) | ||
t.plan(4) | ||
const dispatch = function (req, res) { | ||
@@ -273,3 +288,4 @@ res.writeHead(200, 'OK') | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
fs.readFile('./package.json', { encoding: 'utf-8' }, (err, file) => { | ||
@@ -287,3 +303,3 @@ t.error(err) | ||
test('returns multi buffer payload', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -296,3 +312,4 @@ res.writeHead(200) | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'ab') | ||
@@ -303,3 +320,3 @@ }) | ||
test('returns null payload', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -310,3 +327,4 @@ res.writeHead(200, { 'Content-Length': 0 }) | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '') | ||
@@ -317,3 +335,3 @@ }) | ||
test('allows ending twice', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -325,3 +343,4 @@ res.writeHead(200, { 'Content-Length': 0 }) | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '') | ||
@@ -332,3 +351,3 @@ }) | ||
test('identifies injection object', (t) => { | ||
t.plan(2) | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
@@ -342,7 +361,9 @@ t.equal(inject.isInjection(req), true) | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => {}) | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
}) | ||
}) | ||
test('pipes response', (t) => { | ||
t.plan(2) | ||
t.plan(3) | ||
let finished = false | ||
@@ -360,3 +381,4 @@ const dispatch = function (req, res) { | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(finished, true) | ||
@@ -368,3 +390,3 @@ t.equal(res.payload, 'hi') | ||
test('pipes response with old stream', (t) => { | ||
t.plan(2) | ||
t.plan(3) | ||
let finished = false | ||
@@ -385,3 +407,4 @@ const dispatch = function (req, res) { | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(finished, true) | ||
@@ -393,3 +416,3 @@ t.equal(res.payload, 'hi') | ||
test('echos object payload', (t) => { | ||
t.plan(2) | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
@@ -400,3 +423,4 @@ res.writeHead(200, { 'content-type': req.headers['content-type'] }) | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 } }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.headers['content-type'], 'application/json') | ||
@@ -408,3 +432,3 @@ t.equal(res.payload, '{"a":1}') | ||
test('echos buffer payload', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -415,3 +439,4 @@ res.writeHead(200) | ||
inject(dispatch, { method: 'post', url: '/test', payload: Buffer.from('test!') }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test', payload: Buffer.from('test!') }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'test!') | ||
@@ -422,3 +447,3 @@ }) | ||
test('echos object payload with non-english utf-8 string', (t) => { | ||
t.plan(2) | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
@@ -429,3 +454,4 @@ res.writeHead(200, { 'content-type': req.headers['content-type'] }) | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: '½½א' } }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: '½½א' } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.headers['content-type'], 'application/json') | ||
@@ -437,3 +463,3 @@ t.equal(res.payload, '{"a":"½½א"}') | ||
test('echos object payload without payload', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -444,3 +470,4 @@ res.writeHead(200) | ||
inject(dispatch, { method: 'post', url: '/test' }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '') | ||
@@ -451,3 +478,3 @@ }) | ||
test('retains content-type header', (t) => { | ||
t.plan(2) | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
@@ -458,3 +485,4 @@ res.writeHead(200, { 'content-type': req.headers['content-type'] }) | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 }, headers: { 'content-type': 'something' } }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 }, headers: { 'content-type': 'something' } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.headers['content-type'], 'something') | ||
@@ -466,3 +494,3 @@ t.equal(res.payload, '{"a":1}') | ||
test('adds a content-length header if none set when payload specified', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -473,3 +501,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 } }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test', payload: { a: 1 } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '{"a":1}'.length.toString()) | ||
@@ -480,3 +509,3 @@ }) | ||
test('retains a content-length header when payload specified', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -487,3 +516,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'post', url: '/test', payload: '', headers: { 'content-length': '10' } }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/test', payload: '', headers: { 'content-length': '10' } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '10') | ||
@@ -494,3 +524,3 @@ }) | ||
test('can handle a stream payload', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -503,3 +533,4 @@ readStream(req, (buff) => { | ||
inject(dispatch, { method: 'post', url: '/', payload: getTestStream() }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/', payload: getTestStream() }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'hi') | ||
@@ -510,3 +541,3 @@ }) | ||
test('can handle a stream payload of utf-8 strings', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -519,3 +550,4 @@ readStream(req, (buff) => { | ||
inject(dispatch, { method: 'post', url: '/', payload: getTestStream('utf8') }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/', payload: getTestStream('utf8') }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'hi') | ||
@@ -526,3 +558,3 @@ }) | ||
test('can override stream payload content-length header', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -535,3 +567,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
inject(dispatch, { method: 'post', url: '/', payload: getTestStream(), headers }, (res) => { | ||
inject(dispatch, { method: 'post', url: '/', payload: getTestStream(), headers }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, '100') | ||
@@ -552,3 +585,3 @@ }) | ||
test('writeHead returns single buffer payload', (t) => { | ||
t.plan(3) | ||
t.plan(4) | ||
const reply = 'Hello World' | ||
@@ -562,3 +595,4 @@ const statusCode = 200 | ||
inject(dispatch, { method: 'get', url: '/' }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, statusCode) | ||
@@ -571,3 +605,3 @@ t.equal(res.statusMessage, statusMessage) | ||
test('_read() plays payload', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -590,3 +624,4 @@ let buffer = '' | ||
const body = 'something special just for you' | ||
inject(dispatch, { method: 'get', url: '/', payload: body }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/', payload: body }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, body) | ||
@@ -597,3 +632,3 @@ }) | ||
test('simulates split', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -616,3 +651,4 @@ let buffer = '' | ||
const body = 'something special just for you' | ||
inject(dispatch, { method: 'get', url: '/', payload: body, simulate: { split: true } }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/', payload: body, simulate: { split: true } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, body) | ||
@@ -623,3 +659,3 @@ }) | ||
test('simulates error', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -636,3 +672,4 @@ req.on('readable', () => { | ||
const body = 'something special just for you' | ||
inject(dispatch, { method: 'get', url: '/', payload: body, simulate: { error: true } }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/', payload: body, simulate: { error: true } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'error') | ||
@@ -653,3 +690,3 @@ }) | ||
let replied = false | ||
inject(dispatch, { method: 'get', url: '/', simulate: { end: false } }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/', simulate: { end: false } }, (notHandledErr, res) => { | ||
replied = true | ||
@@ -675,3 +712,3 @@ }) | ||
let replied = false | ||
inject(dispatch, { method: 'get', url: '/', payload: '1234567', simulate: { end: false } }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/', payload: '1234567', simulate: { end: false } }, (notHandledErr, res) => { | ||
replied = true | ||
@@ -687,3 +724,3 @@ }) | ||
test('simulates close', (t) => { | ||
t.plan(1) | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
@@ -705,3 +742,4 @@ let buffer = '' | ||
const body = 'something special just for you' | ||
inject(dispatch, { method: 'get', url: '/', payload: body, simulate: { close: true } }, (res) => { | ||
inject(dispatch, { method: 'get', url: '/', payload: body, simulate: { close: true } }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'close') | ||
@@ -714,3 +752,3 @@ }) | ||
try { | ||
inject({}, {}, (res) => {}) | ||
inject({}, {}, () => {}) | ||
t.fail('This should throw') | ||
@@ -725,3 +763,3 @@ } catch (err) { | ||
try { | ||
inject((req, res) => {}, {}, (res) => {}) | ||
inject((req, res) => {}, {}, () => {}) | ||
} catch (err) { | ||
@@ -735,3 +773,3 @@ t.ok(err) | ||
try { | ||
inject((req, res) => {}, { url: '/', simulate: 'sample string' }, (res) => {}) | ||
inject((req, res) => {}, { url: '/', simulate: 'sample string' }, () => {}) | ||
} catch (err) { | ||
@@ -745,3 +783,3 @@ t.ok(err) | ||
try { | ||
inject((req, res) => { }, { url: '/', simulate: 'sample string', validate: false }, (res) => { }) | ||
inject((req, res) => { }, { url: '/', simulate: 'sample string', validate: false }, () => { }) | ||
t.pass() | ||
@@ -756,3 +794,3 @@ } catch (err) { | ||
try { | ||
inject((req, res) => {}, { url: '/', simulate: { end: 'wrong input' } }, (res) => {}) | ||
inject((req, res) => {}, { url: '/', simulate: { end: 'wrong input' } }, () => {}) | ||
} catch (err) { | ||
@@ -772,2 +810,3 @@ t.ok(err) | ||
.then(res => t.equal(res.payload, 'hello')) | ||
.catch(err => t.fail(err)) | ||
}) | ||
@@ -796,4 +835,27 @@ | ||
.then(res => t.equal(res.statusCode, 200)) | ||
.catch(err => t.fail(err)) | ||
}) | ||
test('should handle response errors', (t) => { | ||
t.plan(1) | ||
const dispatch = function (req, res) { | ||
res.connection.destroy(new Error('kaboom')) | ||
} | ||
inject(dispatch, 'http://example.com:8080/hello', (err, res) => { | ||
t.ok(err) | ||
}) | ||
}) | ||
test('should handle response errors (promises)', (t) => { | ||
t.plan(1) | ||
const dispatch = function (req, res) { | ||
res.connection.destroy(new Error('kaboom')) | ||
} | ||
inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }) | ||
.then(res => t.fail('should throw')) | ||
.catch(err => t.ok(err)) | ||
}) | ||
function getTestStream (encoding) { | ||
@@ -800,0 +862,0 @@ const Read = function () { |
37015
983
93
3
+ Addedreadable-stream@^2.3.3
+ Addedcore-util-is@1.0.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
Updatedajv@^5.5.2