0.2.0 • Published 9 years ago

superagent-httpbackend v0.2.0

Weekly downloads
41
License
Apache 2.0
Repository
github
Last release
9 years ago

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' });
  
0.2.0

9 years ago

0.1.0

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago