A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.npmjs.com/package/superagent-httpbackend below:

superagent-httpbackend - npm

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

  


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