Method, URL, Headers and Body
- Easy to read HTTP requets.
- Run over promises.
- Built-in assertion functionality.
- Send many request over pooling strategy.
- NO external dependencies
Install with: npm i muhb.
Getting NodeJS homepage:
constmuhb=require('muhb');var{ status, headers, body }=awaitmuhb('get','https://nodejs.org/en/');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 }=awaitpost('https://nodejs.org/en/','key=value&key=value');Sending headers:
const{ put }=require('muhb');var{ status, headers, body }=awaitput('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 }=awaitput('https://nodejs.org/en/',{'no-auto': true,myHeader: 'example'},'key=value&key=value');Having all available muhb methods:
constmuhb=require('muhb');muhb.get//=> [function]muhb.post//=> [function]muhb.patch//=> [function]muhb.delete//=> [function]muhb.put//=> [function]muhb.head//=> [function]muhb.options//=> [function]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');letres=awaitget('https://nodejs.org/en/');Testing response data:
var{ assert }=awaitget('https://example.com');// Assert about your reposnse body.assert.body.exactly('foobar');assert.body.contains('oba');assert.body.match(/oo.a/);// Mostly chainable.assert.body.type('application/json').length(23);// Test JSON bodies.assert.body.json.hasKey('foo').match('foo','bar').empty();// test for {}// Test JSON array.assert.body.json.array.match(1,'bar').includes('foo').length(2).empty();// Assert about response status codeassert.status.is(200);assert.status.not(400);assert.status.in([200,203,404]);assert.status.notIn([500,403,201]);assert.status.type(2);// Test for 2xxassert.status.notType(5)// Test for NOT 5xx// Assert about response headersassert.headers.has('authorization').match('connection','close');As of now the only auth method supported is MD5 Digest.
You must ensure your server responds with 401 and a WWW-Authenticate header
so muhb knows to perform the auth.
Then just send your credentials in the headers object as follows:
let{ body }=awaitpost('http://example.com',{auth: {username: 'my-user',password: 'my-pass'}});Or use the user and password syntax (they will be stripped from the URL before being sent).
let{ body }=awaitpost('http://my-user:my-pass@example.com');Define a pool with a max size of 10 requests and a timeout of 2 seconds:
const{ Pool }=require('muhb');letmyPool=newPool({size: 10,timeout: 2000});Then run the pool over an array of say request bodies:
letbodies='abcdefghijklmnopqrstuvwxyz'.split('');bodies.forEach(body=>myPool.post('http://localhost/fail',body));Wait until all requests recive responses.
// With promisesletresponses=awaitmyPool.done();// With eventsmyPool.on('finish',responses=>console.log(responses));Wait for a single request in the pool to finish.
letres=awaitmyPool.post('http://localhost/fail');Act on every request that is responded.
myPool.on('response',(req,res)=>{console.log(res.status);});