New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

superagent-httpbackend

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

superagent-httpbackend

Stub out superagent requests using AngularJS' $httpBackend syntax

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
15
150%
Maintainers
1
Weekly downloads
 
Created
Source

superagent-httpbackend

Stub out superagent requests using AngularJS' $httpBackend syntax

Examples

configure responses to requests


    const httpBackend = require('../');
    const superagent = require('superagent');

    let response = null;
    httpBackend.expect('GET', '/hello').respond({ hello: 'world' });
    superagent.get('/hello', (err, res) => {
      response = res;
    });
    assert.strictEqual(response, null);
    httpBackend.flush();
    assert.deepEqual(response.body, { hello: 'world' });
  

throws if an unexpected request gets fired


    assert.throws(function() {
      superagent.get('/hello', () => {});
    }, /Unexpected request/);
  

response errors


    httpBackend.expect('GET', '/hello').error(401, 'Not Authorized', { error: 'Fail!' });
    superagent.get('/hello', (error) => {
      assert.ok(error);
      assert.equal(error.statusCode, 401);
      assert.deepEqual(error.body, { error: 'Fail!' });
    });
    httpBackend.flush();
  

reset after each test


    let response = null;
    httpBackend.expect('GET', '/hello').respond({ hello: 'world' });
    httpBackend.reset();
    assert.throws(function() {
      superagent.get('/hello', () => {});
    }, /Unexpected request/);
  

API

expect(verb, url)


    let response = null;
    httpBackend.expect('GET', '/hello').respond({ hello: 'world' });
    superagent.get('/hello', (err, res) => {
      response = res;
    });
    assert.strictEqual(response, null);
    httpBackend.flush();
    assert.deepEqual(response.body, { hello: 'world' });
  

expectGET(url)


    let response = null;
    httpBackend.expectGET('/hello').respond({ hello: 'world' });
    superagent.get('/hello', (err, res) => {
      response = res;
    });
    assert.strictEqual(response, null);
    httpBackend.flush();
    assert.deepEqual(response.body, { hello: 'world' });
  

expectPUT(url, validateBody)


    let response = null;
    const validateBody = (body) => {
      throw new Error('Body validation failed');
    };
    httpBackend.expectPUT('/hello', validateBody).respond({ hello: 'world' });
    assert.throws(() => {
      superagent.put('/hello').send({ hello: 'world' }).end(() => {});
    }, /Body validation failed/);
  

expectPOST(url, validateBody)


    let response = null;
    const validateBody = (body) => {
      assert.deepEqual(body, { hello: 'world' });
    };
    httpBackend.expectPUT('/hello', validateBody).respond({ foo: 'bar' });
    superagent.put('/hello').send({ hello: 'world' }).end((error, res) => {
      assert.ifError(error);
      assert.deepEqual(res.body, { foo: 'bar' });
    });
    httpBackend.flush();
  

autoFlush option


    let response = null;
    httpBackend.expectGET('/hello', { autoFlush: true }).
      respond({ hello: 'world' });
    superagent.get('/hello', (err, res) => {
      response = res;
    });
    assert.deepEqual(response.body, { hello: 'world' });
  

FAQs

Package last updated on 31 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts