MUHB
Method, URL, Headers and Body
- Easy to read HTTP requets.
- Run over promises.
- Built-in assertion functionality.
- Send request bulks with pooling strategy.
Usage
Install with: npm i muhb
.
Getting NodeJS homepage:
const muhb = require('muhb');
var { status, headers, body } = await muhb('get', 'https://nodejs.org/en/');
Shorthands
MUHB exposes a short signature for all HTTP verbs:
method ( String url [, Object headers] [, String body] )
Posting a random form:
const { post } = require('muhb');
var { status, headers, body } = await post('https://nodejs.org/en/', 'key=value&key=value');
Sending headers:
const { put } = require('muhb');
var { status, headers, body } = await put(
'https://nodejs.org/en/',
{ myHeader: 'example' },
'key=value&key=value'
);
If you would like MUHB to not generate automatic content and date headers, send
a ghost parameter like this:
const { put } = require('muhb');
var { status, headers, body } = await put(
'https://nodejs.org/en/',
{ '--no-auto': true, myHeader: 'example' },
'key=value&key=value'
);
Having all available muhb methods:
const muhb = require('muhb');
muhb.get
muhb.post
muhb.patch
muhb.delete
muhb.put
muhb.head
muhb.options
If you need to access the nodejs res
object, all muhb methods return it modified
to have our status
and body
keys.
const { get } = require('muhb');
let res = await get('https://nodejs.org/en/');
Assertions
Testing response data:
var { assert } = await get('https://example.com');
assert.body.exactly('foobar');
assert.body.contains('oba');
assert.body.match(/oo.a/);
assert.body.type('application/json').length(23);
assert.body.json
.hasKey('foo')
.match('foo', 'bar')
.empty();
assert.body.json.array
.match(1, 'bar')
.includes('foo')
.empty();
assert.status.is(200);
assert.status.not(400);
assert.status.in([ 200, 203, 404 ]);
assert.status.notIn([ 500, 403, 201 ]);
assert.status.type(2);
assert.status.notType(5)
assert.headers
.has('authorization')
.match('connection', 'close');
Pooling
Define a pool with a max size of 10 and a timeout of 2 seconds:
const { pool } = require('muhb');
let myPool = pool(10, 2000);
Then run the pool over an array of say request bodies:
let bodies = 'abcdefghijklmnopqrstuvwxyz'.split('');
myPool.post(bodies, 'http://localhost/fail', letter => letter);
The first argument must be an array which will be the bulk subject. Next three
parameters are either functions or values for respectively url, headers
(optional) and body (optional).
Contributing
We will be delighted to receive your issues
and MRs.