Stub out superagent requests using AngularJS' $httpBackend syntax
Examples configure responses to requeststhrows if an unexpected request gets firedÂ
    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' });
 Â
response errorsÂ
    assert.throws(function() {
      superagent.get('/hello', () => {});
    }, /Unexpected request/);
 Â
reset after each testÂ
    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();
 Â
API expect(verb, url)Â
    let response = null;
    httpBackend.expect('GET', '/hello').respond({ hello: 'world' });
    httpBackend.reset();
    assert.throws(function() {
      superagent.get('/hello', () => {});
    }, /Unexpected request/);
 Â
expectGET(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' });
 Â
expectPUT(url, validateBody)Â
    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' });
 Â
expectPOST(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/);
 Â
autoFlush optionÂ
    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();
 Â
Â
    let response = null;
    httpBackend.expectGET('/hello', { autoFlush: true }).
      respond({ hello: 'world' });
    superagent.get('/hello', (err, res) => {
      response = res;
    });
    assert.deepEqual(response.body, { hello: 'world' });
 Â
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4