responselike
Advanced tools
Comparing version 0.2.0 to 1.0.0
{ | ||
"name": "responselike", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "A response-like object for mocking a Node.js HTTP response stream", | ||
@@ -31,5 +31,9 @@ "main": "src/index.js", | ||
"eslint-config-xo-lukechilds": "^1.0.0", | ||
"get-stream": "^3.0.0", | ||
"nyc": "^10.3.2", | ||
"xo": "^0.19.0" | ||
}, | ||
"dependencies": { | ||
"lowercase-keys": "^1.0.0" | ||
} | ||
} |
@@ -7,2 +7,4 @@ # responselike | ||
Returns a streamable response object similar to a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage). Useful for formatting cached responses so they can be consumed by code expecting a real response. | ||
## Install | ||
@@ -20,4 +22,55 @@ | ||
## Usage | ||
```js | ||
const Response = require('responselike'); | ||
const response = new Response(200, { foo: 'bar' }, Buffer.from('Hi!'), 'https://example.com'); | ||
response.statusCode; | ||
// 200 | ||
response.headers; | ||
// { foo: 'bar' } | ||
response.body; | ||
// <Buffer 48 69 21> | ||
response.url; | ||
// 'https://example.com' | ||
response.pipe(process.stdout); | ||
// Hi! | ||
``` | ||
## API | ||
### new Response(statusCode, headers, body, url) | ||
Returns a streamable response object similar to a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage). | ||
#### statusCode | ||
Type: `number` | ||
HTTP response status code. | ||
#### headers | ||
Type: `object` | ||
HTTP headers object. Keys will be automatically lowercased. | ||
#### body | ||
Type: `buffer` | ||
A Buffer containing the response body. The Buffer contents will be streamable but is also exposed directly as `response.body`. | ||
#### url | ||
Type: `string` | ||
Request URL string. | ||
## License | ||
MIT © Luke Childs |
'use strict'; | ||
const Readable = require('stream').Readable; | ||
const lowercaseKeys = require('lowercase-keys'); | ||
class Response extends Readable { | ||
constructor(statusCode, headers, body, url) { | ||
if (typeof statusCode !== 'number') { | ||
throw new TypeError('Argument `statusCode` should be a number'); | ||
} | ||
if (typeof headers !== 'object') { | ||
throw new TypeError('Argument `headers` should be an object'); | ||
} | ||
if (!(body instanceof Buffer)) { | ||
throw new TypeError('Argument `body` should be a buffer'); | ||
} | ||
if (typeof url !== 'string') { | ||
throw new TypeError('Argument `url` should be a string'); | ||
} | ||
super(); | ||
this.statusCode = statusCode; | ||
this.headers = headers; | ||
this.headers = lowercaseKeys(headers); | ||
this.body = body; | ||
@@ -11,0 +25,0 @@ this.url = url; |
import test from 'ava'; | ||
import lowercaseKeys from 'lowercase-keys'; | ||
import getStream from 'get-stream'; | ||
import Response from '../'; | ||
import f from './fixtures'; | ||
@@ -7,1 +10,46 @@ test('Response is a function', t => { | ||
}); | ||
test('Response cannot be invoked without \'new\'', t => { | ||
t.throws(() => Response(f.statusCode, f.headers, f.body, f.url)); // eslint-disable-line new-cap | ||
t.notThrows(() => new Response(f.statusCode, f.headers, f.body, f.url)); | ||
}); | ||
test('new Response() throws on invalid statusCode', t => { | ||
const error = t.throws(() => new Response(undefined, f.headers, f.body, f.url)); | ||
t.is(error.message, 'Argument `statusCode` should be a number'); | ||
}); | ||
test('new Response() throws on invalid headers', t => { | ||
const error = t.throws(() => new Response(f.statusCode, undefined, f.body, f.url)); | ||
t.is(error.message, 'Argument `headers` should be an object'); | ||
}); | ||
test('new Response() throws on invalid body', t => { | ||
const error = t.throws(() => new Response(f.statusCode, f.headers, undefined, f.url)); | ||
t.is(error.message, 'Argument `body` should be a buffer'); | ||
}); | ||
test('new Response() throws on invalid url', t => { | ||
const error = t.throws(() => new Response(f.statusCode, f.headers, f.body, undefined)); | ||
t.is(error.message, 'Argument `url` should be a string'); | ||
}); | ||
test('response has expected properties', t => { | ||
const response = new Response(f.statusCode, f.headers, f.body, f.url); | ||
t.is(response.statusCode, f.statusCode); | ||
t.deepEqual(response.headers, lowercaseKeys(f.headers)); | ||
t.is(response.body, f.body); | ||
t.is(response.url, f.url); | ||
}); | ||
test('response headers have lowercase keys', t => { | ||
const response = new Response(f.statusCode, f.headers, f.body, f.url); | ||
t.not(JSON.stringify(f.headers), response.headers); | ||
t.deepEqual(response.headers, lowercaseKeys(f.headers)); | ||
}); | ||
test('response streams body', async t => { | ||
const response = new Response(f.statusCode, f.headers, f.body, f.url); | ||
const responseStream = await getStream(response); | ||
t.is(responseStream, f.bodyText); | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7970
8
86
0
75
1
6
+ Addedlowercase-keys@^1.0.0
+ Addedlowercase-keys@1.0.1(transitive)