Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

light-my-request

Package Overview
Dependencies
Maintainers
11
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

light-my-request - npm Package Compare versions

Comparing version 6.3.0 to 6.4.0

eslint.config.js

4

benchmark/benchmark.js

@@ -8,4 +8,4 @@ 'use strict'

const parseURL = require('../lib/parse-url')
const { Readable } = require('stream')
const { assert } = require('console')
const { Readable } = require('node:stream')
const { assert } = require('node:console')
const { Bench } = require('tinybench')

@@ -12,0 +12,0 @@

@@ -19,2 +19,27 @@ 'use strict'

function supportStream1 (req, next) {
const payload = req._lightMyRequest.payload
if (!payload || payload._readableState || typeof payload.resume !== 'function') { // does quack like a modern stream
return next()
}
// This is a non-compliant stream
const chunks = []
// We are accumulating because Readable.wrap() does not really work as expected
// in this case.
payload.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
payload.on('end', () => {
const payload = Buffer.concat(chunks)
req.headers['content-length'] = req.headers['content-length'] || ('' + payload.length)
delete req.headers['transfer-encoding']
req._lightMyRequest.payload = payload
return next()
})
// Force to resume the stream. Needed for Stream 1
payload.resume()
}
function makeRequest (dispatchFunc, server, req, res) {

@@ -26,6 +51,8 @@ req.once('error', function (err) {

req.once('close', function () {
if (this.destroyed && !this._error) res.destroy()
if (this.destroyed && !this._error) {
res.destroy()
}
})
return req.prepare(() => dispatchFunc.call(server, req, res))
return supportStream1(req, () => dispatchFunc.call(server, req, res))
}

@@ -32,0 +59,0 @@

@@ -196,30 +196,42 @@ 'use strict'

return this
}
{
const payload = this._lightMyRequest.payload
if (payload && payload._readableState) { // does quack like a modern stream
this._read = readStream
util.inherits(Request, Readable)
util.inherits(CustomRequest, Request)
payload.on('error', (err) => {
this.destroy(err)
})
Request.prototype.prepare = function (next) {
const payload = this._lightMyRequest.payload
if (!payload || typeof payload.resume !== 'function') { // does not quack like a stream
return next()
payload.on('end', () => {
this.push(null)
})
} else {
// Stream v1 are handled in index.js asynchronously
this._read = readEverythingElse
}
}
const chunks = []
return this
}
payload.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
function readStream (size) {
const payload = this._lightMyRequest.payload
payload.on('end', () => {
const payload = Buffer.concat(chunks)
this.headers['content-length'] = this.headers['content-length'] || ('' + payload.length)
this._lightMyRequest.payload = payload
return next()
})
let more = true
let pushed = false
let chunk
while (more && (chunk = payload.read())) {
pushed = true
more = this.push(chunk)
}
// Force to resume the stream. Needed for Stream 1
payload.resume()
// We set up a recursive 'readable' event only if we didn't read anything.
// Otheriwse, the stream machinery will call _read() for us.
if (more && !pushed) {
this._lightMyRequest.payload.once('readable', this._read.bind(this))
}
}
Request.prototype._read = function (size) {
function readEverythingElse (size) {
setImmediate(() => {

@@ -261,2 +273,5 @@ if (this._lightMyRequest.isDone) {

util.inherits(Request, Readable)
util.inherits(CustomRequest, Request)
Request.prototype.destroy = function (error) {

@@ -263,0 +278,0 @@ if (this.destroyed || this._lightMyRequest.isDone) return

@@ -31,3 +31,3 @@ 'use strict'

const onEndSuccess = (payload) => {
// no need to early-return if already called because this handler is bound `once`
if (called) return
called = true

@@ -41,18 +41,24 @@ if (this._promiseCallback) {

let finished = false
const onEndFailure = (err) => {
if (called) return
called = true
if (this._lightMyRequest.stream) {
const res = generatePayload(this)
res.raw.req = req
this._lightMyRequest.stream._read = function () {
this.destroy(err || new Error('premature close'))
if (called) {
if (this._lightMyRequest.stream && !finished) {
if (!err) {
err = new Error('response destroyed before completion')
err.code = 'LIGHT_ECONNRESET'
}
this._lightMyRequest.stream.destroy(err)
this._lightMyRequest.stream.on('error', () => {})
}
onEndSuccess(res)
} else {
if (this._promiseCallback) {
return process.nextTick(() => reject(err))
}
process.nextTick(() => onEnd(err, null))
return
}
called = true
if (!err) {
err = new Error('response destroyed before completion')
err.code = 'LIGHT_ECONNRESET'
}
if (this._promiseCallback) {
return process.nextTick(() => reject(err))
}
process.nextTick(() => onEnd(err, null))
}

@@ -62,2 +68,3 @@

this.once('finish', () => {
finished = true
this._lightMyRequest.stream.push(null)

@@ -64,0 +71,0 @@ })

{
"name": "light-my-request",
"version": "6.3.0",
"version": "6.4.0",
"description": "Fake HTTP injection library",

@@ -17,3 +17,5 @@ "main": "index.js",

"@types/node": "^22.7.7",
"c8": "^10.1.2",
"end-of-stream": "^1.4.4",
"eslint": "^9.17.0",
"express": "^4.19.2",

@@ -23,7 +25,6 @@ "form-auto-content": "^3.2.1",

"formdata-node": "^6.0.3",
"standard": "^17.1.0",
"tap": "^18.7.2",
"tinybench": "^2.7.0",
"neostandard": "^0.12.0",
"tinybench": "^3.0.0",
"tsd": "^0.31.0",
"undici": "^6.13.0"
"undici": "^7.0.0"
},

@@ -33,6 +34,7 @@ "scripts": {

"coverage": "npm run unit -- --cov --coverage-report=html",
"lint": "standard",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run lint && npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},

@@ -55,8 +57,3 @@ "repository": {

},
"homepage": "https://github.com/fastify/light-my-request/blob/master/README.md",
"standard": {
"ignore": [
"test/benchmark.js"
]
}
"homepage": "https://github.com/fastify/light-my-request/blob/master/README.md"
}
# Light my Request
![CI](https://github.com/fastify/light-my-request/workflows/CI/badge.svg)
[![CI](https://github.com/fastify/light-my-request/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/light-my-request/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/light-my-request.svg?style=flat)](https://www.npmjs.com/package/light-my-request)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

@@ -7,0 +7,0 @@ Injects a fake HTTP request/response into a node HTTP server for simulating server logic, writing tests, or debugging.

'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const inject = require('../index')

@@ -14,5 +14,5 @@

const res = await inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' })
t.equal(res.payload, 'hello')
t.assert.strictEqual(res.payload, 'hello')
} catch (err) {
t.fail(err)
t.assert.fail(err)
}

@@ -26,3 +26,3 @@ })

await t.rejects(inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }))
await t.assert.rejects(() => inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }), Error)
})

@@ -39,5 +39,5 @@

const res = await chain.end()
t.equal(res.payload, 'hello')
t.assert.strictEqual(res.payload, 'hello')
} catch (err) {
t.fail(err)
t.assert.fail(err)
}

@@ -54,6 +54,6 @@ })

const res = await inject(dispatch).get('http://example.com:8080/hello')
t.equal(res.payload, 'hello')
t.assert.strictEqual(res.payload, 'hello')
} catch (err) {
t.fail(err)
t.assert.fail(err)
}
})
'use strict'
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const { Readable, finished, pipeline } = require('node:stream')

@@ -29,3 +28,3 @@ const qs = require('node:querystring')

test('returns non-chunked payload', (t) => {
test('returns non-chunked payload', (t, done) => {
t.plan(7)

@@ -42,7 +41,7 @@ const output = 'example.com:8080|/hello'

inject(dispatch, 'http://example.com:8080/hello', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.statusMessage, 'Super')
t.ok(res.headers.date)
t.strictSame(res.headers, {
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.statusMessage, 'Super')
t.assert.ok(res.headers.date)
t.assert.deepStrictEqual(res.headers, {
date: res.headers.date,

@@ -54,8 +53,9 @@ connection: 'keep-alive',

})
t.equal(res.payload, output)
t.equal(res.rawPayload.toString(), 'example.com:8080|/hello')
t.assert.strictEqual(res.payload, output)
t.assert.strictEqual(res.rawPayload.toString(), 'example.com:8080|/hello')
done()
})
})
test('returns single buffer payload', (t) => {
test('returns single buffer payload', (t, done) => {
t.plan(6)

@@ -68,12 +68,13 @@ const dispatch = function (req, res) {

inject(dispatch, { url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.equal(res.headers['transfer-encoding'], 'chunked')
t.equal(res.payload, 'example.com:8080|/hello')
t.equal(res.rawPayload.toString(), 'example.com:8080|/hello')
t.assert.ifError(err)
t.assert.ok(res.headers.date)
t.assert.ok(res.headers.connection)
t.assert.strictEqual(res.headers['transfer-encoding'], 'chunked')
t.assert.strictEqual(res.payload, 'example.com:8080|/hello')
t.assert.strictEqual(res.rawPayload.toString(), 'example.com:8080|/hello')
done()
})
})
test('passes headers', (t) => {
test('passes headers', (t, done) => {
t.plan(2)

@@ -86,12 +87,13 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', headers: { Super: 'duper' } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'duper')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'duper')
done()
})
})
test('request has rawHeaders', (t) => {
test('request has rawHeaders', (t, done) => {
t.plan(3)
const dispatch = function (req, res) {
t.ok(Array.isArray(req.rawHeaders))
t.match(req.rawHeaders, ['super', 'duper', 'user-agent', 'lightMyRequest', 'host', 'example.com:8080'])
t.assert.ok(Array.isArray(req.rawHeaders))
t.assert.deepStrictEqual(req.rawHeaders, ['super', 'duper', 'user-agent', 'lightMyRequest', 'host', 'example.com:8080'])
res.writeHead(200)

@@ -102,10 +104,11 @@ res.end()

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', headers: { Super: 'duper' } }, (err, res) => {
t.error(err)
t.assert.ifError(err)
done()
})
})
test('request inherits from custom class', (t) => {
test('request inherits from custom class', (t, done) => {
t.plan(2)
const dispatch = function (req, res) {
t.ok(req instanceof http.IncomingMessage)
t.assert.ok(req instanceof http.IncomingMessage)
res.writeHead(200)

@@ -116,10 +119,11 @@ res.end()

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', Request: http.IncomingMessage }, (err, res) => {
t.error(err)
t.assert.ifError(err)
done()
})
})
test('request with custom class preserves stream data', (t) => {
test('request with custom class preserves stream data', (t, done) => {
t.plan(2)
const dispatch = function (req, res) {
t.ok(req._readableState)
t.assert.ok(req._readableState)
res.writeHead(200)

@@ -130,3 +134,4 @@ res.end()

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', Request: http.IncomingMessage }, (err, res) => {
t.error(err)
t.assert.ifError(err)
done()
})

@@ -138,3 +143,3 @@ })

const dispatch = function (req, res) {
t.error('should not get here')
t.assert.ifError('should not get here')
res.writeHead(500)

@@ -146,12 +151,12 @@ res.end()

t.throws(() => {
t.assert.throws(() => {
inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', Request: MyInvalidRequest }, () => {})
}, {})
}, Error)
t.throws(() => {
t.assert.throws(() => {
inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', Request: 'InvalidRequest' }, () => {})
}, {})
}, Error)
})
test('passes remote address', (t) => {
test('passes remote address', (t, done) => {
t.plan(2)

@@ -164,8 +169,9 @@ const dispatch = function (req, 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')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '1.2.3.4')
done()
})
})
test('passes a socket which emits events like a normal one does', (t) => {
test('passes a socket which emits events like a normal one does', (t, done) => {
t.plan(2)

@@ -179,8 +185,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'added')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'added')
done()
})
})
test('includes deprecated connection on request', (t) => {
test('includes deprecated connection on request', (t, done) => {
t.plan(3)

@@ -190,7 +197,7 @@ const warnings = process.listeners('warning')

function onWarning (err) {
t.equal(err.code, 'FST_LIGHTMYREQUEST_DEP01')
t.assert.strictEqual(err.code, 'FST_LIGHTMYREQUEST_DEP01')
return false
}
process.on('warning', onWarning)
t.teardown(() => {
t.after(() => {
process.removeListener('warning', onWarning)

@@ -207,4 +214,5 @@ for (const fn of warnings) {

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')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '1.2.3.4')
done()
})

@@ -218,3 +226,3 @@ })

test('passes query', (t) => {
test('passes query', (t, done) => {
t.plan(2)

@@ -233,8 +241,9 @@

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', query }, (err, res) => {
t.error(err)
t.same(parseQuery(res.payload), query)
t.assert.ifError(err)
t.assert.deepEqual(parseQuery(res.payload), query)
done()
})
})
test('query will be merged into that in url', (t) => {
test('query will be merged into that in url', (t, done) => {
t.plan(2)

@@ -252,8 +261,9 @@

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello?message=OK', query }, (err, res) => {
t.error(err)
t.same(parseQuery(res.payload), Object.assign({ message: 'OK' }, query))
t.assert.ifError(err)
t.assert.deepEqual(parseQuery(res.payload), Object.assign({ message: 'OK' }, query))
done()
})
})
test('passes query as a string', (t) => {
test('passes query as a string', (t, done) => {
t.plan(2)

@@ -269,11 +279,12 @@

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', query }, (err, res) => {
t.error(err)
t.same(parseQuery(res.payload), {
t.assert.ifError(err)
t.assert.deepEqual(parseQuery(res.payload), {
message: 'OK',
xs: ['foo', 'bar']
})
done()
})
})
test('query as a string will be merged into that in url', (t) => {
test('query as a string will be merged into that in url', (t, done) => {
t.plan(2)

@@ -289,10 +300,11 @@

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello?message=OK', query }, (err, res) => {
t.error(err)
t.same(parseQuery(res.payload), Object.assign({ message: 'OK' }, {
t.assert.ifError(err)
t.assert.deepEqual(parseQuery(res.payload), Object.assign({ message: 'OK' }, {
xs: ['foo', 'bar']
}))
done()
})
})
test('passes localhost as default remote address', (t) => {
test('passes localhost as default remote address', (t, done) => {
t.plan(2)

@@ -305,8 +317,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.payload, '127.0.0.1')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '127.0.0.1')
done()
})
})
test('passes host option as host header', (t) => {
test('passes host option as host header', (t, done) => {
t.plan(2)

@@ -319,8 +332,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/hello', headers: { host: 'test.example.com' } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'test.example.com')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'test.example.com')
done()
})
})
test('passes localhost as default host header', (t) => {
test('passes localhost as default host header', (t, done) => {
t.plan(2)

@@ -333,8 +347,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/hello' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'localhost:80')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'localhost:80')
done()
})
})
test('passes authority as host header', (t) => {
test('passes authority as host header', (t, done) => {
t.plan(2)

@@ -347,8 +362,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/hello', authority: 'something' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'something')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'something')
done()
})
})
test('passes uri host as host header', (t) => {
test('passes uri host as host header', (t, done) => {
t.plan(2)

@@ -361,8 +377,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:8080')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:8080')
done()
})
})
test('includes default http port in host header', (t) => {
test('includes default http port in host header', (t, done) => {
t.plan(2)

@@ -375,8 +392,9 @@ const dispatch = function (req, res) {

inject(dispatch, 'http://example.com', (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:80')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:80')
done()
})
})
test('includes default https port in host header', (t) => {
test('includes default https port in host header', (t, done) => {
t.plan(2)

@@ -389,8 +407,9 @@ const dispatch = function (req, res) {

inject(dispatch, 'https://example.com', (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:443')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:443')
done()
})
})
test('optionally accepts an object as url', (t) => {
test('optionally accepts an object as url', (t, done) => {
t.plan(5)

@@ -415,11 +434,12 @@ const output = 'example.com:8080|/hello?test=1234'

inject(dispatch, { url }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.notOk(res.headers['transfer-encoding'])
t.equal(res.payload, output)
t.assert.ifError(err)
t.assert.ok(res.headers.date)
t.assert.ok(res.headers.connection)
t.assert.ifError(res.headers['transfer-encoding'])
t.assert.strictEqual(res.payload, output)
done()
})
})
test('leaves user-agent unmodified', (t) => {
test('leaves user-agent unmodified', (t, done) => {
t.plan(2)

@@ -432,8 +452,9 @@ const dispatch = function (req, 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')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'duper')
done()
})
})
test('returns chunked payload', (t) => {
test('returns chunked payload', (t, done) => {
t.plan(5)

@@ -448,11 +469,12 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.equal(res.headers['transfer-encoding'], 'chunked')
t.equal(res.payload, 'ab')
t.assert.ifError(err)
t.assert.ok(res.headers.date)
t.assert.ok(res.headers.connection)
t.assert.strictEqual(res.headers['transfer-encoding'], 'chunked')
t.assert.strictEqual(res.payload, 'ab')
done()
})
})
test('sets trailers in response object', (t) => {
test('sets trailers in response object', (t, done) => {
t.plan(4)

@@ -466,10 +488,11 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.headers.trailer, 'Test')
t.equal(res.headers.test, undefined)
t.equal(res.trailers.test, '123')
t.assert.ifError(err)
t.assert.strictEqual(res.headers.trailer, 'Test')
t.assert.strictEqual(res.headers.test, undefined)
t.assert.strictEqual(res.trailers.test, '123')
done()
})
})
test('parses zipped payload', (t) => {
test('parses zipped payload', (t, done) => {
t.plan(4)

@@ -483,9 +506,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.assert.ifError(err)
fs.readFile('./package.json', { encoding: 'utf-8' }, (err, file) => {
t.error(err)
t.assert.ifError(err)
zlib.unzip(res.rawPayload, (err, unzipped) => {
t.error(err)
t.equal(unzipped.toString('utf-8'), file)
t.assert.ifError(err)
t.assert.strictEqual(unzipped.toString('utf-8'), file)
done()
})

@@ -496,3 +520,3 @@ })

test('returns multi buffer payload', (t) => {
test('returns multi buffer payload', (t, done) => {
t.plan(2)

@@ -507,8 +531,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'ab')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'ab')
done()
})
})
test('returns null payload', (t) => {
test('returns null payload', (t, done) => {
t.plan(2)

@@ -521,8 +546,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.payload, '')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '')
done()
})
})
test('allows ending twice', (t) => {
test('allows ending twice', (t, done) => {
t.plan(2)

@@ -536,12 +562,13 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.payload, '')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '')
done()
})
})
test('identifies injection object', (t) => {
test('identifies injection object', (t, done) => {
t.plan(6)
const dispatchRequest = function (req, res) {
t.equal(inject.isInjection(req), true)
t.equal(inject.isInjection(res), true)
t.assert.strictEqual(inject.isInjection(req), true)
t.assert.strictEqual(inject.isInjection(res), true)

@@ -553,4 +580,4 @@ res.writeHead(200, { 'Content-Length': 0 })

const dispatchCustomRequest = function (req, res) {
t.equal(inject.isInjection(req), true)
t.equal(inject.isInjection(res), true)
t.assert.strictEqual(inject.isInjection(req), true)
t.assert.strictEqual(inject.isInjection(res), true)

@@ -562,9 +589,13 @@ res.writeHead(200, { 'Content-Length': 0 })

const options = { method: 'GET', url: '/' }
const cb = (err, res) => { t.error(err) }
const cb = (err, res) => { t.assert.ifError(err) }
const cbDone = (err, res) => {
t.assert.ifError(err)
done()
}
inject(dispatchRequest, options, cb)
inject(dispatchCustomRequest, { ...options, Request: http.IncomingMessage }, cb)
inject(dispatchCustomRequest, { ...options, Request: http.IncomingMessage }, cbDone)
})
test('pipes response', (t) => {
test('pipes response', (t, done) => {
t.plan(3)

@@ -584,9 +615,10 @@ let finished = false

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(finished, true)
t.equal(res.payload, 'hi')
t.assert.ifError(err)
t.assert.strictEqual(finished, true)
t.assert.strictEqual(res.payload, 'hi')
done()
})
})
test('pipes response with old stream', (t) => {
test('pipes response with old stream', (t, done) => {
t.plan(3)

@@ -609,9 +641,10 @@ let finished = false

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(finished, true)
t.equal(res.payload, 'hi')
t.assert.ifError(err)
t.assert.strictEqual(finished, true)
t.assert.strictEqual(res.payload, 'hi')
done()
})
})
test('echos object payload', (t) => {
test('echos object payload', (t, done) => {
t.plan(3)

@@ -624,9 +657,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test', payload: { a: 1 } }, (err, res) => {
t.error(err)
t.equal(res.headers['content-type'], 'application/json')
t.equal(res.payload, '{"a":1}')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-type'], 'application/json')
t.assert.strictEqual(res.payload, '{"a":1}')
done()
})
})
test('supports body option in Request and property in Response', (t) => {
test('supports body option in Request and property in Response', (t, done) => {
t.plan(3)

@@ -639,9 +673,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test', body: { a: 1 } }, (err, res) => {
t.error(err)
t.equal(res.headers['content-type'], 'application/json')
t.equal(res.body, '{"a":1}')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-type'], 'application/json')
t.assert.strictEqual(res.body, '{"a":1}')
done()
})
})
test('echos buffer payload', (t) => {
test('echos buffer payload', (t, done) => {
t.plan(2)

@@ -654,8 +689,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test', payload: Buffer.from('test!') }, (err, res) => {
t.error(err)
t.equal(res.payload, 'test!')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'test!')
done()
})
})
test('echos object payload with non-english utf-8 string', (t) => {
test('echos object payload with non-english utf-8 string', (t, done) => {
t.plan(3)

@@ -668,9 +704,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test', payload: { a: '½½א' } }, (err, res) => {
t.error(err)
t.equal(res.headers['content-type'], 'application/json')
t.equal(res.payload, '{"a":"½½א"}')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-type'], 'application/json')
t.assert.strictEqual(res.payload, '{"a":"½½א"}')
done()
})
})
test('echos object payload without payload', (t) => {
test('echos object payload without payload', (t, done) => {
t.plan(2)

@@ -683,8 +720,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test' }, (err, res) => {
t.error(err)
t.equal(res.payload, '')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '')
done()
})
})
test('retains content-type header', (t) => {
test('retains content-type header', (t, done) => {
t.plan(3)

@@ -697,9 +735,10 @@ const dispatch = function (req, 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')
t.equal(res.payload, '{"a":1}')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-type'], 'something')
t.assert.strictEqual(res.payload, '{"a":1}')
done()
})
})
test('adds a content-length header if none set when payload specified', (t) => {
test('adds a content-length header if none set when payload specified', (t, done) => {
t.plan(2)

@@ -712,8 +751,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test', payload: { a: 1 } }, (err, res) => {
t.error(err)
t.equal(res.payload, '{"a":1}'.length.toString())
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '{"a":1}'.length.toString())
done()
})
})
test('retains a content-length header when payload specified', (t) => {
test('retains a content-length header when payload specified', (t, done) => {
t.plan(2)

@@ -726,8 +766,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/test', payload: '', headers: { 'content-length': '10' } }, (err, res) => {
t.error(err)
t.equal(res.payload, '10')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '10')
done()
})
})
test('can handle a stream payload', (t) => {
test('can handle a stream payload', (t, done) => {
t.plan(2)

@@ -742,10 +783,30 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/', payload: getTestStream() }, (err, res) => {
t.error(err)
t.equal(res.payload, 'hi')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'hi')
done()
})
})
test('can handle a stream payload of utf-8 strings', (t) => {
test('can handle a stream payload that errors', (t, done) => {
t.plan(2)
const dispatch = function (req, res) {
req.resume()
}
const payload = new Readable({
read () {
this.destroy(new Error('kaboom'))
}
})
inject(dispatch, { method: 'POST', url: '/', payload }, (err, res) => {
t.assert.ok(err)
t.assert.equal(err.message, 'kaboom')
done()
})
})
test('can handle a stream payload of utf-8 strings', (t, done) => {
t.plan(2)
const dispatch = function (req, res) {
readStream(req, (buff) => {

@@ -758,8 +819,9 @@ res.writeHead(200, { 'Content-Type': 'text/plain' })

inject(dispatch, { method: 'POST', url: '/', payload: getTestStream('utf8') }, (err, res) => {
t.error(err)
t.equal(res.payload, 'hi')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'hi')
done()
})
})
test('can override stream payload content-length header', (t) => {
test('can override stream payload content-length header', (t, done) => {
t.plan(2)

@@ -774,18 +836,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'POST', url: '/', payload: getTestStream(), headers }, (err, res) => {
t.error(err)
t.equal(res.payload, '100')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '100')
done()
})
})
test('can override stream payload content-length header without request content-length', (t) => {
t.plan(1)
const dispatch = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
t.equal(req.headers['content-length'], '2')
}
inject(dispatch, { method: 'POST', url: '/', payload: getTestStream() }, () => {})
})
test('writeHead returns single buffer payload', (t) => {
test('writeHead returns single buffer payload', (t, done) => {
t.plan(4)

@@ -801,10 +854,11 @@ const reply = 'Hello World'

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.statusCode, statusCode)
t.equal(res.statusMessage, statusMessage)
t.equal(res.payload, reply)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, statusCode)
t.assert.strictEqual(res.statusMessage, statusMessage)
t.assert.strictEqual(res.payload, reply)
done()
})
})
test('_read() plays payload', (t) => {
test('_read() plays payload', (t, done) => {
t.plan(2)

@@ -829,8 +883,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payload: body }, (err, res) => {
t.error(err)
t.equal(res.payload, body)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, body)
done()
})
})
test('simulates split', (t) => {
test('simulates split', (t, done) => {
t.plan(2)

@@ -855,8 +910,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payload: body, simulate: { split: true } }, (err, res) => {
t.error(err)
t.equal(res.payload, body)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, body)
done()
})
})
test('simulates error', (t) => {
test('simulates error', (t, done) => {
t.plan(2)

@@ -875,8 +931,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payload: body, simulate: { error: true } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'error')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'error')
done()
})
})
test('simulates no end without payload', (t) => {
test('simulates no end without payload', (t, done) => {
t.plan(2)

@@ -897,8 +954,9 @@ let end = false

setTimeout(() => {
t.equal(end, false)
t.equal(replied, false)
t.assert.strictEqual(end, false)
t.assert.strictEqual(replied, false)
done()
}, 10)
})
test('simulates no end with payload', (t) => {
test('simulates no end with payload', (t, done) => {
t.plan(2)

@@ -919,8 +977,9 @@ let end = false

setTimeout(() => {
t.equal(end, false)
t.equal(replied, false)
t.assert.strictEqual(end, false)
t.assert.strictEqual(replied, false)
done()
}, 10)
})
test('simulates close', (t) => {
test('simulates close', (t, done) => {
t.plan(2)

@@ -944,4 +1003,5 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payload: body, simulate: { close: true } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'close')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'close')
done()
})

@@ -953,3 +1013,6 @@ })

t.throws(() => inject({}, {}, () => {}), new Error('dispatchFunc should be a function'))
t.assert.throws(
() => inject({}, {}, () => {}),
{ name: 'AssertionError', message: 'dispatchFunc should be a function' }
)
})

@@ -960,3 +1023,6 @@

t.throws(() => inject((req, res) => {}, {}, () => {}), /must have required property 'url'/)
t.assert.throws(
() => inject((req, res) => {}, {}, () => {}),
{ message: /must have required property 'url'/ }
)
})

@@ -967,3 +1033,6 @@

t.throws(() => inject((req, res) => {}, { url: '/', simulate: 'sample string' }, () => {}), /^must be object$/)
t.assert.throws(
() => inject((req, res) => {}, { url: '/', simulate: 'sample string' }, () => {}),
{ message: /^must be object$/ }
)
})

@@ -974,3 +1043,3 @@

t.doesNotThrow(() => inject((req, res) => { }, { url: '/', simulate: 'sample string', validate: false }, () => { }))
t.assert.doesNotThrow(() => inject((req, res) => { }, { url: '/', simulate: 'sample string', validate: false }, () => { }))
})

@@ -981,6 +1050,9 @@

t.throws(() => inject((req, res) => {}, { url: '/', simulate: { end: 'wrong input' } }, () => {}), /^must be boolean$/)
t.assert.throws(
() => inject((req, res) => {}, { url: '/', simulate: { end: 'wrong input' } }, () => {}),
{ message: /^must be boolean$/ }
)
})
test('promises support', (t) => {
test('promises support', (t, done) => {
t.plan(1)

@@ -993,7 +1065,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' })
.then(res => t.equal(res.payload, 'hello'))
.catch(t.fail)
.then(res => {
t.assert.strictEqual(res.payload, 'hello')
done()
})
.catch(t.assert.fail)
})
test('this should be the server instance', t => {
test('this should be the server instance', (t, done) => {
t.plan(2)

@@ -1004,3 +1079,3 @@

const dispatch = function (req, res) {
t.equal(this, server)
t.assert.strictEqual(this, server)
res.end('hello')

@@ -1010,7 +1085,8 @@ }

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', server })
.then(res => t.equal(res.statusCode, 200))
.catch(t.fail)
.then(res => t.assert.strictEqual(res.statusCode, 200))
.catch(t.assert.fail)
.finally(done)
})
test('should handle response errors', (t) => {
test('should handle response errors', (t, done) => {
t.plan(1)

@@ -1022,3 +1098,4 @@ const dispatch = function (req, res) {

inject(dispatch, 'http://example.com:8080/hello', (err, res) => {
t.ok(err)
t.assert.ok(err)
done()
})

@@ -1033,6 +1110,9 @@ })

await t.rejects(() => inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }), new Error('kaboom'))
await t.assert.rejects(
() => inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }),
{ name: 'Error', message: 'kaboom' }
)
})
test('should handle response timeout handler', (t) => {
test('should handle response timeout handler', (t, done) => {
t.plan(3)

@@ -1050,8 +1130,9 @@ const dispatch = function (req, res) {

res.on('timeout', () => {
t.ok(true, 'Response timeout event not emitted')
t.assert.ok(true, 'Response timeout event not emitted')
})
}
inject(dispatch, { method: 'GET', url: '/test' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'correct')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'correct')
done()
})

@@ -1064,4 +1145,4 @@ })

t.throws(() => inject(dispatch, { method: 'UNKNOWN_METHOD', url: 'http://example.com:8080/hello' }, (err, res) => {
t.ok(err)
t.assert.throws(() => inject(dispatch, { method: 'UNKNOWN_METHOD', url: 'http://example.com:8080/hello' }, (err, res) => {
t.assert.ok(err)
}), Error)

@@ -1074,7 +1155,7 @@ })

t.throws(() => inject(dispatch, { method: 'UNKNOWN_METHOD', url: 'http://example.com:8080/hello' })
t.assert.throws(() => inject(dispatch, { method: 'UNKNOWN_METHOD', url: 'http://example.com:8080/hello' })
.then(res => {}), Error)
})
test('HTTP method is case insensitive', (t) => {
test('HTTP method is case insensitive', (t, done) => {
t.plan(3)

@@ -1087,12 +1168,14 @@

inject(dispatch, { method: 'get', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, 'Hi!')
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.payload, 'Hi!')
done()
})
})
test('form-data should be handled correctly', (t) => {
t.plan(3)
test('form-data should be handled correctly', (t, done) => {
t.plan(4)
const dispatch = function (req, res) {
t.assert.strictEqual(req.headers['transfer-encoding'], undefined)
let body = ''

@@ -1113,11 +1196,16 @@ req.on('data', d => {

url: 'http://example.com:8080/hello',
headers: {
// Transfer-encoding is automatically deleted if Stream1 is used
'transfer-encoding': 'chunked'
},
payload: form
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.ok(/--.+\r\nContent-Disposition: form-data; name="my_field"\r\n\r\nmy value\r\n--.+--\r\n/.test(res.payload))
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
t.assert.ok(/--.+\r\nContent-Disposition: form-data; name="my_field"\r\n\r\nmy value\r\n--.+--\r\n/.test(res.payload))
done()
})
})
test('path as alias to url', (t) => {
test('path as alias to url', (t, done) => {
t.plan(2)

@@ -1131,4 +1219,5 @@

inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.payload, '/hello')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '/hello')
done()
})

@@ -1140,6 +1229,9 @@ })

t.throws(() => inject(() => {}, { method: 'GET' }, () => {}), /must have required property 'url',must have required property 'path'/)
t.assert.throws(
() => inject(() => {}, { method: 'GET' }, () => {}),
{ message: /must have required property 'url',must have required property 'path'/ }
)
})
test('chainable api: backwards compatibility for promise (then)', (t) => {
test('chainable api: backwards compatibility for promise (then)', (t, done) => {
t.plan(1)

@@ -1154,7 +1246,8 @@

.get('/')
.then(res => t.equal(res.payload, 'hello'))
.catch(t.fail)
.then(res => t.assert.strictEqual(res.payload, 'hello'))
.catch(t.assert.fail)
.finally(done)
})
test('chainable api: backwards compatibility for promise (catch)', (t) => {
test('chainable api: backwards compatibility for promise (catch)', (t, done) => {
t.plan(1)

@@ -1168,6 +1261,7 @@

.get('/')
.catch(err => t.ok(err))
.catch(err => t.assert.ok(err))
.finally(done)
})
test('chainable api: multiple call of then should return the same promise', (t) => {
test('chainable api: multiple call of then should return the same promise', (t, done) => {
t.plan(2)

@@ -1179,3 +1273,3 @@ let id = 0

++id
t.pass('request id incremented')
t.assert.ok('request id incremented')
res.end('hello')

@@ -1187,3 +1281,4 @@ }

chain.then(rep => {
t.equal(res.headers['request-id'], rep.headers['request-id'])
t.assert.strictEqual(res.headers['request-id'], rep.headers['request-id'])
done()
})

@@ -1193,3 +1288,3 @@ })

test('chainable api: http methods should work correctly', (t) => {
test('chainable api: http methods should work correctly', (t, done) => {
t.plan(16)

@@ -1202,7 +1297,10 @@

httpMethods.forEach(method => {
httpMethods.forEach((method, index) => {
inject(dispatch)[method]('http://example.com:8080/hello')
.end((err, res) => {
t.error(err)
t.equal(res.body, method.toUpperCase())
t.assert.ifError(err)
t.assert.strictEqual(res.body, method.toUpperCase())
if (index === httpMethods.length - 1) {
done()
}
})

@@ -1212,3 +1310,3 @@ })

test('chainable api: http methods should throw if already invoked', (t) => {
test('chainable api: http methods should throw if already invoked', (t, done) => {
t.plan(8)

@@ -1221,10 +1319,13 @@

httpMethods.forEach(method => {
httpMethods.forEach((method, index) => {
const chain = inject(dispatch)[method]('http://example.com:8080/hello')
chain.end()
t.throws(() => chain[method]('/'), Error)
t.assert.throws(() => chain[method]('/'), Error)
if (index === httpMethods.length - 1) {
done()
}
})
})
test('chainable api: body method should work correctly', (t) => {
test('chainable api: body method should work correctly', (t, done) => {
t.plan(2)

@@ -1241,8 +1342,9 @@

.end((err, res) => {
t.error(err)
t.equal(res.body, 'test')
t.assert.ifError(err)
t.assert.strictEqual(res.body, 'test')
done()
})
})
test('chainable api: cookie', (t) => {
test('chainable api: cookie', (t, done) => {
t.plan(2)

@@ -1260,4 +1362,5 @@

.end((err, res) => {
t.error(err)
t.equal(res.body, 'hello=world; fastify=rulez')
t.assert.ifError(err)
t.assert.strictEqual(res.body, 'hello=world; fastify=rulez')
done()
})

@@ -1278,6 +1381,6 @@ })

.end()
t.throws(() => chain.body('test'), Error)
t.assert.throws(() => chain.body('test'), Error)
})
test('chainable api: headers method should work correctly', (t) => {
test('chainable api: headers method should work correctly', (t, done) => {
t.plan(2)

@@ -1294,4 +1397,5 @@

.end((err, res) => {
t.error(err)
t.equal(res.payload, 'bar')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'bar')
done()
})

@@ -1312,6 +1416,6 @@ })

.end()
t.throws(() => chain.headers({ foo: 'bar' }), Error)
t.assert.throws(() => chain.headers({ foo: 'bar' }), Error)
})
test('chainable api: payload method should work correctly', (t) => {
test('chainable api: payload method should work correctly', (t, done) => {
t.plan(2)

@@ -1328,4 +1432,5 @@

.end((err, res) => {
t.error(err)
t.equal(res.payload, 'payload')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'payload')
done()
})

@@ -1346,6 +1451,6 @@ })

.end()
t.throws(() => chain.payload('payload'), Error)
t.assert.throws(() => chain.payload('payload'), Error)
})
test('chainable api: query method should work correctly', (t) => {
test('chainable api: query method should work correctly', (t, done) => {
t.plan(2)

@@ -1367,4 +1472,5 @@

.end((err, res) => {
t.error(err)
t.same(parseQuery(res.payload), query)
t.assert.ifError(err)
t.assert.deepEqual(parseQuery(res.payload), query)
done()
})

@@ -1385,3 +1491,3 @@ })

.end()
t.throws(() => chain.query({ foo: 'bar' }), Error)
t.assert.throws(() => chain.query({ foo: 'bar' }), Error)
})

@@ -1400,6 +1506,6 @@

chain.then()
t.throws(() => chain.end(), Error)
t.assert.throws(() => chain.end(), Error)
})
test('chainable api: invoking promise method after end method with a callback function should throw', (t) => {
test('chainable api: invoking promise method after end method with a callback function should throw', (t, done) => {
t.plan(2)

@@ -1415,8 +1521,9 @@

chain.end((err, res) => {
t.error(err)
t.assert.ifError(err)
done()
})
t.throws(() => chain.then(), Error)
t.assert.throws(() => chain.then(), Error)
})
test('chainable api: invoking promise method after end method without a callback function should work properly', (t) => {
test('chainable api: invoking promise method after end method without a callback function should work properly', (t, done) => {
t.plan(1)

@@ -1432,3 +1539,4 @@

.end()
.then(res => t.equal(res.payload, 'hello'))
.then(res => t.assert.strictEqual(res.payload, 'hello'))
.finally(done)
})

@@ -1447,6 +1555,6 @@

chain.end()
t.throws(() => chain.end(), Error)
t.assert.throws(() => chain.end(), Error)
})
test('chainable api: string url', (t) => {
test('chainable api: string url', (t, done) => {
t.plan(2)

@@ -1457,3 +1565,3 @@

res.end()
t.pass()
t.assert.ok('pass')
}

@@ -1463,6 +1571,6 @@

chain.then(() => t.pass())
chain.then(() => t.assert.ok('pass')).finally(done)
})
test('Response.json() should parse the JSON payload', (t) => {
test('Response.json() should parse the JSON payload', (t, done) => {
t.plan(2)

@@ -1481,9 +1589,10 @@

inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.assert.ifError(err)
const { json } = res
t.same(json(), jsonData)
t.assert.deepStrictEqual(json(), jsonData)
done()
})
})
test('Response.json() should not throw an error if content-type is not application/json', (t) => {
test('Response.json() should not throw an error if content-type is not application/json', (t, done) => {
t.plan(2)

@@ -1502,9 +1611,10 @@

inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.assert.ifError(err)
const { json } = res
t.same(json(), jsonData)
t.assert.deepStrictEqual(json(), jsonData)
done()
})
})
test('Response.json() should throw an error if the payload is not of valid JSON format', (t) => {
test('Response.json() should throw an error if the payload is not of valid JSON format', (t, done) => {
t.plan(2)

@@ -1518,8 +1628,9 @@

inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.throws(res.json, Error)
t.assert.ifError(err)
t.assert.throws(res.json, Error)
done()
})
})
test('Response.stream() should provide a Readable stream', (t) => {
test('Response.stream() should provide a Readable stream', (t, done) => {
const lines = [

@@ -1541,6 +1652,6 @@ JSON.stringify({ foo: 'bar' }),

inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.assert.ifError(err)
const readable = res.stream()
const payload = []
t.equal(readable instanceof Readable, true)
t.assert.strictEqual(readable instanceof Readable, true)
readable.on('data', function (chunk) {

@@ -1551,4 +1662,5 @@ payload.push(chunk)

for (let i = 0; i < lines.length; i++) {
t.equal(lines[i], payload[i].toString())
t.assert.strictEqual(lines[i], payload[i].toString())
}
done()
})

@@ -1558,7 +1670,7 @@ })

test('promise api should auto start (fire and forget)', (t) => {
test('promise api should auto start (fire and forget)', (t, done) => {
t.plan(1)
function dispatch (req, res) {
t.pass('dispatch called')
t.assert.ok('dispatch called')
res.writeHead(200, { 'Content-Type': 'text/plain' })

@@ -1569,5 +1681,6 @@ res.end()

inject(dispatch, 'http://example.com:8080/hello')
process.nextTick(done)
})
test('disabling autostart', (t) => {
test('disabling autostart', (t, done) => {
t.plan(3)

@@ -1578,6 +1691,7 @@

function dispatch (req, res) {
t.pass('dispatch called')
t.assert.ok('dispatch called')
called = true
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end()
done()
}

@@ -1591,5 +1705,5 @@

setImmediate(() => {
t.equal(called, false)
t.assert.strictEqual(called, false)
p.then(() => {
t.equal(called, true)
t.assert.strictEqual(called, true)
})

@@ -1626,3 +1740,3 @@ })

test('send cookie', (t) => {
test('send cookie', (t, done) => {
t.plan(3)

@@ -1635,9 +1749,10 @@ const dispatch = function (req, res) {

inject(dispatch, { url: 'http://example.com:8080/hello', cookies: { foo: 'bar', grass: 'àìùòlé' } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:8080|foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
t.equal(res.rawPayload.toString(), 'example.com:8080|foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:8080|foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
t.assert.strictEqual(res.rawPayload.toString(), 'example.com:8080|foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
done()
})
})
test('send cookie with header already set', (t) => {
test('send cookie with header already set', (t, done) => {
t.plan(3)

@@ -1654,9 +1769,10 @@ const dispatch = function (req, res) {

}, (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:8080|custom=one; foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
t.equal(res.rawPayload.toString(), 'example.com:8080|custom=one; foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:8080|custom=one; foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
t.assert.strictEqual(res.rawPayload.toString(), 'example.com:8080|custom=one; foo=bar; grass=%C3%A0%C3%AC%C3%B9%C3%B2l%C3%A9')
done()
})
})
test('read cookie', (t) => {
test('read cookie', (t, done) => {
t.plan(3)

@@ -1673,5 +1789,5 @@ const dispatch = function (req, res) {

inject(dispatch, { url: 'http://example.com:8080/hello', cookies: { foo: 'bar' } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:8080|foo=bar')
t.strictSame(res.cookies, [
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:8080|foo=bar')
t.assert.deepStrictEqual(res.cookies, [
{ name: 'type', value: 'ninja' },

@@ -1690,6 +1806,7 @@ {

])
done()
})
})
test('correctly handles no string headers', (t) => {
test('correctly handles no string headers', (t, done) => {
t.plan(3)

@@ -1727,5 +1844,5 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', headers }, (err, res) => {
t.error(err)
t.assert.ifError(err)
t.strictSame(res.headers, {
t.assert.deepStrictEqual(res.headers, {
integer: '12',

@@ -1745,3 +1862,3 @@ float: '3.14',

t.strictSame(JSON.parse(res.payload), {
t.assert.deepStrictEqual(JSON.parse(res.payload), {
integer: '12',

@@ -1759,6 +1876,7 @@ float: '3.14',

})
done()
})
})
test('errors for invalid undefined header value', (t) => {
test('errors for invalid undefined header value', (t, done) => {
t.plan(1)

@@ -1768,7 +1886,8 @@ try {

} catch (err) {
t.ok(err)
t.assert.ok(err)
done()
}
})
test('example with form-auto-content', (t) => {
test('example with form-auto-content', (t, done) => {
t.plan(4)

@@ -1796,10 +1915,11 @@ const dispatch = function (req, res) {

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.ok(/--.+\r\nContent-Disposition: form-data; name="myField"\r\n\r\nmy value\r\n--.*/.test(res.payload))
t.ok(/--.+\r\nContent-Disposition: form-data; name="myFile"; filename="LICENSE"\r\n.*/.test(res.payload))
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
t.assert.ok(/--.+\r\nContent-Disposition: form-data; name="myField"\r\n\r\nmy value\r\n--.*/.test(res.payload))
t.assert.ok(/--.+\r\nContent-Disposition: form-data; name="myFile"; filename="LICENSE"\r\n.*/.test(res.payload))
done()
})
})
test('simulate invalid alter _lightMyRequest.isDone with end', (t) => {
test('simulate invalid alter _lightMyRequest.isDone with end', (t, done) => {
const dispatch = function (req, res) {

@@ -1809,4 +1929,4 @@ req.resume()

req.on('end', () => {
t.pass('should have end event')
t.end()
t.assert.ok('should have end event')
done()
})

@@ -1816,7 +1936,7 @@ }

inject(dispatch, { method: 'GET', url: '/', simulate: { end: true } }, (notHandledErr, res) => {
t.fail('should not have reply')
t.assert.fail('should not have reply')
})
})
test('simulate invalid alter _lightMyRequest.isDone without end', (t) => {
test('simulate invalid alter _lightMyRequest.isDone without end', (t, done) => {
const dispatch = function (req, res) {

@@ -1826,15 +1946,14 @@ req.resume()

req.on('end', () => {
t.fail('should not have end event')
t.assert.fail('should not have end event')
})
done()
}
inject(dispatch, { method: 'GET', url: '/', simulate: { end: false } }, (notHandledErr, res) => {
t.fail('should not have reply')
t.assert.fail('should not have reply')
})
t.end()
})
test('no error for response destory', (t) => {
t.plan(1)
test('no error for response destroy', (t, done) => {
t.plan(2)

@@ -1846,7 +1965,9 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.assert.equal(res, null)
t.assert.equal(err.code, 'LIGHT_ECONNRESET')
done()
})
})
test('request destory without error', (t) => {
test('request destory without.assert.ifError', (t, done) => {
t.plan(2)

@@ -1859,8 +1980,9 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res, null)
t.assert.equal(err.code, 'LIGHT_ECONNRESET')
t.assert.equal(res, null)
done()
})
})
test('request destory with error', (t) => {
test('request destory with error', (t, done) => {
t.plan(2)

@@ -1875,8 +1997,9 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.equal(err, fakeError)
t.equal(res, null)
t.assert.strictEqual(err, fakeError)
t.assert.strictEqual(res, null)
done()
})
})
test('compatible with stream.finished', (t) => {
test('compatible with stream.finished', (t, done) => {
t.plan(3)

@@ -1886,3 +2009,3 @@

finished(res, (err) => {
t.ok(err instanceof Error)
t.assert.ok(err instanceof Error)
})

@@ -1894,13 +2017,14 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res, null)
t.assert.equal(err.code, 'LIGHT_ECONNRESET')
t.assert.equal(res, null)
done()
})
})
test('compatible with eos', (t) => {
t.plan(3)
test('compatible with eos', (t, done) => {
t.plan(4)
const dispatch = function (req, res) {
eos(res, (err) => {
t.ok(err instanceof Error)
t.assert.ok(err instanceof Error)
})

@@ -1912,8 +2036,10 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res, null)
t.assert.ok(err)
t.assert.equal(err.code, 'LIGHT_ECONNRESET')
t.assert.equal(res, null)
done()
})
})
test('compatible with stream.finished pipe a Stream', (t) => {
test('compatible with stream.finished pipe a Stream', (t, done) => {
t.plan(3)

@@ -1923,3 +2049,3 @@

finished(res, (err) => {
t.error(err)
t.assert.ifError(err)
})

@@ -1936,8 +2062,9 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.error(err)
t.equal(res.body, 'hello world')
t.assert.ifError(err)
t.assert.strictEqual(res.body, 'hello world')
done()
})
})
test('compatible with eos, passes error correctly', (t) => {
test('compatible with eos, passes error correctly', (t, done) => {
t.plan(3)

@@ -1949,3 +2076,3 @@

eos(res, (err) => {
t.equal(err, fakeError)
t.assert.strictEqual(err, fakeError)
})

@@ -1957,8 +2084,9 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.equal(err, fakeError)
t.equal(res, null)
t.assert.strictEqual(err, fakeError)
t.assert.strictEqual(res, null)
done()
})
})
test('multiple calls to req.destroy should not be called', (t) => {
test('multiple calls to req.destroy should not be called', (t, done) => {
t.plan(2)

@@ -1972,8 +2100,9 @@

inject(dispatch, { method: 'GET', url: '/' }, (err, res) => {
t.equal(err)
t.equal(res, null)
t.assert.equal(res, null)
t.assert.equal(err.code, 'LIGHT_ECONNRESET')
done()
})
})
test('passes headers when using an express app', (t) => {
test('passes headers when using an express app', (t, done) => {
t.plan(2)

@@ -1989,8 +2118,9 @@

inject(app, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.headers['some-fancy-header'], 'a very cool value')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['some-fancy-header'], 'a very cool value')
done()
})
})
test('value of request url when using inject should not differ', (t) => {
test('value of request url when using inject should not differ', (t, done) => {
t.plan(1)

@@ -2005,4 +2135,5 @@

inject(dispatch, { method: 'GET', url: 'http://example.com:8080//hello', server })
.then(res => { t.equal(res.body, '//hello') })
.catch(err => t.error(err))
.then(res => { t.assert.strictEqual(res.body, '//hello') })
.catch(err => t.assert.ifError(err))
.finally(done)
})

@@ -2013,3 +2144,3 @@

const parsedURL = parseURL('/test', undefined)
t.equal(parsedURL.href, 'http://localhost/test')
t.assert.strictEqual(parsedURL.href, 'http://localhost/test')
})

@@ -2020,3 +2151,3 @@

const parsedURL = parseURL('//test', undefined)
t.equal(parsedURL.href, 'http://localhost//test')
t.assert.strictEqual(parsedURL.href, 'http://localhost//test')
})

@@ -2027,3 +2158,3 @@

const parsedURL = parseURL('https://example.com//test', undefined)
t.equal(parsedURL.href, 'https://example.com//test')
t.assert.strictEqual(parsedURL.href, 'https://example.com//test')
})

@@ -2034,3 +2165,3 @@

const parsedURL = parseURL('https://example.com/test', undefined)
t.equal(parsedURL.href, 'https://example.com/test')
t.assert.strictEqual(parsedURL.href, 'https://example.com/test')
})

@@ -2052,8 +2183,8 @@

wanted.name = 'AbortError'
t.rejects(promise, wanted)
t.assert.rejects(promise, wanted)
}, { skip: globalThis.AbortController == null })
test('should pass req to ServerResponse', (t) => {
test('should pass req to ServerResponse', (t, done) => {
if (parseInt(process.versions.node.split('.', 1)[0], 10) < 16) {
t.pass('Skip because Node version < 16')
t.assert.ok('Skip because Node version < 16')
t.end()

@@ -2070,11 +2201,12 @@ return

inject(dispatch, 'http://example.com:8080/hello', (err, res) => {
t.error(err)
t.ok(res.raw.req === res.raw.res.req)
t.ok(res.raw.res.req.removeListener)
t.equal(res.payload, 'example.com:8080|/hello')
t.equal(res.rawPayload.toString(), 'example.com:8080|/hello')
t.assert.ifError(err)
t.assert.ok(res.raw.req === res.raw.res.req)
t.assert.ok(res.raw.res.req.removeListener)
t.assert.strictEqual(res.payload, 'example.com:8080|/hello')
t.assert.strictEqual(res.rawPayload.toString(), 'example.com:8080|/hello')
done()
})
})
test('should work with pipeline', (t) => {
test('should work with pipeline', (t, done) => {
t.plan(3)

@@ -2087,15 +2219,16 @@ const dispatch = function (req, res) {

inject(dispatch, 'http://example.com:8080/hello', (err, res) => {
t.error(err)
t.equal(res.payload, 'example.com:8080|/hello')
t.equal(res.rawPayload.toString(), 'example.com:8080|/hello')
t.assert.ifError(err)
t.assert.strictEqual(res.payload, 'example.com:8080|/hello')
t.assert.strictEqual(res.rawPayload.toString(), 'example.com:8080|/hello')
done()
})
})
test('should leave the headers user-agent and content-type undefined when the headers are explicitly set to undefined in the inject', (t) => {
test('should leave the headers user-agent and content-type undefined when the headers are explicitly set to undefined in the inject', (t, done) => {
t.plan(5)
const dispatch = function (req, res) {
t.ok(Array.isArray(req.rawHeaders))
t.equal(req.headers['user-agent'], undefined)
t.equal(req.headers['content-type'], undefined)
t.equal(req.headers['x-foo'], 'bar')
t.assert.ok(Array.isArray(req.rawHeaders))
t.assert.strictEqual(req.headers['user-agent'], undefined)
t.assert.strictEqual(req.headers['content-type'], undefined)
t.assert.strictEqual(req.headers['x-foo'], 'bar')
res.writeHead(200, { 'Content-Type': 'text/plain' })

@@ -2115,7 +2248,8 @@ res.end('Ok')

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
done()
})
})
test("passes payload when using express' send", (t) => {
test("passes payload when using express' send", (t, done) => {
t.plan(3)

@@ -2130,9 +2264,10 @@

inject(app, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.headers['content-length'], '9')
t.equal(res.payload, 'some text')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-length'], '9')
t.assert.strictEqual(res.payload, 'some text')
done()
})
})
test('request that is destroyed does not error', (t) => {
test('request that is destroyed errors', (t, done) => {
t.plan(2)

@@ -2144,3 +2279,3 @@ const dispatch = function (req, res) {

res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(buff)
res.end('hi')
})

@@ -2153,4 +2288,5 @@ })

inject(dispatch, { method: 'POST', url: '/', payload }, (err, res) => {
t.error(err)
t.equal(res.payload, 'hi')
t.assert.equal(res, null)
t.assert.equal(err.code, 'LIGHT_ECONNRESET')
done()
})

@@ -2160,3 +2296,3 @@ })

function runFormDataUnitTest (name, { FormData, Blob }) {
test(`${name} - form-data should be handled correctly`, (t) => {
test(`${name} - form-data should be handled correctly`, (t, done) => {
t.plan(23)

@@ -2166,3 +2302,3 @@

let body = ''
t.ok(/multipart\/form-data; boundary=----formdata-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(--)?$/.test(req.headers['content-type']), 'proper Content-Type provided')
t.assert.ok(/multipart\/form-data; boundary=----formdata-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(--)?$/.test(req.headers['content-type']), 'proper Content-Type provided')
req.on('data', d => {

@@ -2188,4 +2324,4 @@ body += d

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)

@@ -2210,3 +2346,3 @@ const regexp = [

// header
t.ok(regexp[0].test(chunk), 'correct header')
t.assert.ok(regexp[0].test(chunk), 'correct header')
break

@@ -2219,3 +2355,3 @@ }

// content-disposition
t.ok(regexp[1].test(chunk), 'correct content-disposition')
t.assert.ok(regexp[1].test(chunk), 'correct content-disposition')
break

@@ -2227,3 +2363,3 @@ }

// content-type
t.ok(regexp[2].test(chunk), 'correct content-type')
t.assert.ok(regexp[2].test(chunk), 'correct content-type')
break

@@ -2236,3 +2372,3 @@ }

// empty
t.equal(chunk, '', 'correct space')
t.assert.strictEqual(chunk, '', 'correct space')
break

@@ -2245,3 +2381,3 @@ }

// value
t.equal(chunk, 'value', 'correct value')
t.assert.strictEqual(chunk, 'value', 'correct value')
break

@@ -2252,2 +2388,3 @@ }

})
done()
})

@@ -2264,3 +2401,3 @@ }, { skip: FormData == null || Blob == null })

test('QUERY method works', (t) => {
test('QUERY method works', (t, done) => {
t.plan(3)

@@ -2273,9 +2410,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'QUERY', url: '/test', payload: { a: 1 } }, (err, res) => {
t.error(err)
t.equal(res.headers['content-type'], 'application/json')
t.equal(res.payload, '{"a":1}')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-type'], 'application/json')
t.assert.strictEqual(res.payload, '{"a":1}')
done()
})
})
test('query method works', (t) => {
test('query method works', (t, done) => {
t.plan(3)

@@ -2288,6 +2426,7 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'query', url: '/test', payload: { a: 1 } }, (err, res) => {
t.error(err)
t.equal(res.headers['content-type'], 'application/json')
t.equal(res.payload, '{"a":1}')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-type'], 'application/json')
t.assert.strictEqual(res.payload, '{"a":1}')
done()
})
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')

@@ -15,3 +15,3 @@ const Request = require('../lib/request')

t.same(req.aborted, false)
t.assert.strictEqual(req.aborted, false)
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Response = require('../lib/response')
test('multiple calls to res.destroy should not be called', (t) => {
t.plan(1)
test('multiple calls to res.destroy should not be called', (t, done) => {
t.plan(2)
const mockReq = {}
const res = new Response(mockReq, (err, response) => {
t.error(err)
t.assert.ok(err)
t.assert.strictEqual(err.code, 'LIGHT_ECONNRESET')
done()
})

@@ -14,0 +16,0 @@

'use strict'
const t = require('tap')
const t = require('node:test')
const fs = require('node:fs')

@@ -22,3 +22,3 @@ const test = t.test

test('stream mode - non-chunked payload', (t) => {
test('stream mode - non-chunked payload', (t, done) => {
t.plan(9)

@@ -38,7 +38,7 @@ const output = 'example.com:8080|/hello'

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.statusMessage, 'Super')
t.ok(res.headers.date)
t.strictSame(res.headers, {
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.statusMessage, 'Super')
t.assert.ok(res.headers.date)
t.assert.deepStrictEqual(res.headers, {
date: res.headers.date,

@@ -50,8 +50,9 @@ connection: 'keep-alive',

})
t.equal(res.payload, undefined)
t.equal(res.rawPayload, undefined)
t.assert.strictEqual(res.payload, undefined)
t.assert.strictEqual(res.rawPayload, undefined)
accumulate(res.stream(), (err, payload) => {
t.error(err)
t.equal(payload.toString(), 'example.com:8080|/hello')
t.assert.ifError(err)
t.assert.strictEqual(payload.toString(), 'example.com:8080|/hello')
done()
})

@@ -61,3 +62,3 @@ })

test('stream mode - passes headers', (t) => {
test('stream mode - passes headers', (t, done) => {
t.plan(3)

@@ -75,6 +76,7 @@ const dispatch = function (req, res) {

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
accumulate(res.stream(), (err, payload) => {
t.error(err)
t.equal(payload.toString(), 'duper')
t.assert.ifError(err)
t.assert.strictEqual(payload.toString(), 'duper')
done()
})

@@ -84,3 +86,3 @@ })

test('stream mode - returns chunked payload', (t) => {
test('stream mode - returns chunked payload', (t, done) => {
t.plan(6)

@@ -95,9 +97,10 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
t.ok(res.headers.date)
t.ok(res.headers.connection)
t.equal(res.headers['transfer-encoding'], 'chunked')
t.assert.ifError(err)
t.assert.ok(res.headers.date)
t.assert.ok(res.headers.connection)
t.assert.strictEqual(res.headers['transfer-encoding'], 'chunked')
accumulate(res.stream(), (err, payload) => {
t.error(err)
t.equal(payload.toString(), 'ab')
t.assert.ifError(err)
t.assert.strictEqual(payload.toString(), 'ab')
done()
})

@@ -107,3 +110,3 @@ })

test('stream mode - sets trailers in response object', (t) => {
test('stream mode - sets trailers in response object', (t, done) => {
t.plan(4)

@@ -117,10 +120,11 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
t.equal(res.headers.trailer, 'Test')
t.equal(res.headers.test, undefined)
t.equal(res.trailers.test, '123')
t.assert.ifError(err)
t.assert.strictEqual(res.headers.trailer, 'Test')
t.assert.strictEqual(res.headers.test, undefined)
t.assert.strictEqual(res.trailers.test, '123')
done()
})
})
test('stream mode - parses zipped payload', (t) => {
test('stream mode - parses zipped payload', (t, done) => {
t.plan(5)

@@ -134,12 +138,13 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
t.assert.ifError(err)
fs.readFile('./package.json', { encoding: 'utf-8' }, (err, file) => {
t.error(err)
t.assert.ifError(err)
accumulate(res.stream(), (err, payload) => {
t.error(err)
t.assert.ifError(err)
zlib.unzip(payload, (err, unzipped) => {
t.error(err)
t.equal(unzipped.toString('utf-8'), file)
t.assert.ifError(err)
t.assert.strictEqual(unzipped.toString('utf-8'), file)
done()
})

@@ -151,3 +156,3 @@ })

test('stream mode - returns multi buffer payload', (t) => {
test('stream mode - returns multi buffer payload', (t, done) => {
t.plan(3)

@@ -162,3 +167,3 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
t.assert.ifError(err)

@@ -172,4 +177,5 @@ const chunks = []

stream.on('end', () => {
t.equal(chunks.length, 2)
t.equal(Buffer.concat(chunks).toString(), 'ab')
t.assert.strictEqual(chunks.length, 2)
t.assert.strictEqual(Buffer.concat(chunks).toString(), 'ab')
done()
})

@@ -179,3 +185,3 @@ })

test('stream mode - returns null payload', (t) => {
test('stream mode - returns null payload', (t, done) => {
t.plan(4)

@@ -188,7 +194,8 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
t.equal(res.payload, undefined)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, undefined)
accumulate(res.stream(), (err, payload) => {
t.error(err)
t.equal(payload.toString(), '')
t.assert.ifError(err)
t.assert.strictEqual(payload.toString(), '')
done()
})

@@ -198,3 +205,3 @@ })

test('stream mode - simulates error', (t) => {
test('stream mode - simulates error', (t, done) => {
t.plan(3)

@@ -213,6 +220,7 @@ const dispatch = function (req, res) {

inject(dispatch, { method: 'GET', url: '/', payload: body, simulate: { error: true }, payloadAsStream: true }, (err, res) => {
t.error(err)
t.assert.ifError(err)
accumulate(res.stream(), (err, payload) => {
t.error(err)
t.equal(payload.toString(), 'error')
t.assert.ifError(err)
t.assert.strictEqual(payload.toString(), 'error')
done()
})

@@ -222,3 +230,3 @@ })

test('stream mode - promises support', (t) => {
test('stream mode - promises support', (t, done) => {
t.plan(1)

@@ -241,7 +249,8 @@ const dispatch = function (req, res) {

})
.then(payload => t.equal(payload.toString(), 'hello'))
.catch(t.fail)
.then(payload => t.assert.strictEqual(payload.toString(), 'hello'))
.catch(t.assert.fail)
.finally(done)
})
test('stream mode - Response.json() should throw', (t) => {
test('stream mode - Response.json() should throw', (t, done) => {
t.plan(2)

@@ -260,19 +269,24 @@

inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello', payloadAsStream: true }, (err, res) => {
t.error(err)
t.assert.ifError(err)
const { json } = res
t.throws(json)
t.assert.throws(json, Error)
done()
})
})
test('stream mode - error for response destroy', (t) => {
test('stream mode - error for response destroy', (t, done) => {
t.plan(2)
const dispatch = function (req, res) {
res.destroy()
res.writeHead(200)
setImmediate(() => {
res.destroy()
})
}
inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
t.assert.ifError(err)
accumulate(res.stream(), (err) => {
t.ok(err)
t.assert.ok(err)
done()
})

@@ -282,4 +296,4 @@ })

test('stream mode - request destory with error', (t) => {
t.plan(2)
test('stream mode - request destroy with error', (t, done) => {
t.plan(3)

@@ -293,6 +307,6 @@ const fakeError = new Error('some-err')

inject(dispatch, { method: 'GET', url: '/', payloadAsStream: true }, (err, res) => {
t.error(err)
accumulate(res.stream(), (err, res) => {
t.equal(err, fakeError)
})
t.assert.ok(err)
t.assert.strictEqual(err, fakeError)
t.assert.strictEqual(res, null)
done()
})

@@ -315,10 +329,10 @@ })

await t.rejects(async () => {
await t.assert.rejects(async () => {
for await (const c of res.stream()) {
t.fail(`should not loop, got ${c.toString()}`)
t.assert.fail(`should not loop, got ${c.toString()}`)
}
})
}, Error)
}, { skip: globalThis.AbortController == null })
test("stream mode - passes payload when using express' send", (t) => {
test("stream mode - passes payload when using express' send", (t, done) => {
t.plan(4)

@@ -333,9 +347,10 @@

inject(app, { method: 'GET', url: 'http://example.com:8080/hello', payloadAsStream: true }, (err, res) => {
t.error(err)
t.equal(res.headers['content-length'], '9')
t.assert.ifError(err)
t.assert.strictEqual(res.headers['content-length'], '9')
accumulate(res.stream(), function (err, payload) {
t.error(err)
t.equal(payload.toString(), 'some text')
t.assert.ifError(err)
t.assert.strictEqual(payload.toString(), 'some text')
done()
})
})
})

@@ -1,3 +0,3 @@

import * as http from 'http'
import { Readable } from 'stream'
import * as http from 'node:http'
import { Readable } from 'node:stream'

@@ -108,3 +108,3 @@ type HTTPMethods = 'DELETE' | 'delete' |

payload: (payload: InjectPayload) => Chain
query: (query: string | { [k: string]: string | string[] } ) => Chain
query: (query: string | { [k: string]: string | string[] }) => Chain
cookies: (query: object) => Chain

@@ -111,0 +111,0 @@ end(): Promise<Response>

@@ -1,5 +0,5 @@

import * as http from 'http'
import * as http from 'node:http'
import { inject, isInjection, Response, DispatchFunc, InjectOptions, Chain } from '..'
import { expectType, expectAssignable, expectNotAssignable } from 'tsd'
import { Readable } from 'stream'
import { Readable } from 'node:stream'

@@ -25,3 +25,3 @@ expectAssignable<InjectOptions>({ url: '/' })

if (!res) {
return;
return
}

@@ -28,0 +28,0 @@ expectType<Response>(res)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc