HTTP Backend Mock for Protractor

2.0.0 · abandoned · verified Wed Apr 22

httpbackend is an npm module designed to mock HTTP requests specifically within an AngularJS application being tested by Protractor. Its primary function is to intercept `XMLHttpRequest` and `$http` requests made by the AngularJS application, allowing developers to define custom responses for specific URLs and methods. This facilitates isolated and deterministic testing of frontend logic without relying on a live backend server. The package is currently at version 2.0.0, last published in June 2017. Due to its tight coupling with AngularJS and Protractor, both of which are considered legacy technologies, the package is no longer actively maintained. Its release cadence was sporadic and has ceased, making it unsuitable for modern web development workflows involving current Angular versions (2+), React, Vue, or contemporary testing frameworks like Cypress or Playwright. Its key differentiator was its direct integration with Protractor's `browser` object for injecting mock modules into AngularJS applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `httpbackend` with Protractor to mock both GET and POST requests, providing predefined responses for isolated testing of an AngularJS application's frontend logic.

const HttpBackend = require('httpbackend');
const protractor = require('protractor');
const browser = protractor.browser;
const element = protractor.element;
const by = protractor.by;

describe('HTTP Backend Mock Example', function() {
    let backend = null;

    beforeAll(function() {
        // Assuming 'browser' is globally available in Protractor tests or injected.
        backend = new HttpBackend(browser);
    });

    afterEach(function() {
        // Clears all defined fixtures after each test to prevent side effects.
        backend.clear();
    });

    it('should mock a GET request with a string response', function() {
        // Define a mock response for any GET request to '/api/data'
        backend.whenGET('/api/data').respond('Mocked Data');

        // Navigate to an application URL that triggers a GET request to '/api/data'
        // Ensure your Angular app on 127.0.0.1:8080 makes this request and displays the result.
        browser.get('http://127.0.0.1:8080/some-page-that-fetches-data');

        // Example: Assert on an element that displays the fetched data
        // (Assuming an element with binding 'dataResult' displays the response)
        const resultElement = element(by.binding('dataResult'));
        expect(resultElement.getText()).toEqual('Mocked Data');
    });

    it('should mock a POST request with a function response mirroring input', function() {
        // Define a mock response for POST requests, returning the request data back.
        backend.whenPOST('/api/submit').respond(function(method, url, data) {
            return [200, { receivedData: JSON.parse(data) }];
        });

        browser.get('http://127.0.0.1:8080/form-page');

        // Simulate user interaction that triggers a POST request
        element(by.model('formData.name')).sendKeys('Test User');
        element(by.css('#submitButton')).click();

        // Assert that the application processed the mocked response correctly
        const responseDisplay = element(by.binding('postResponse.receivedData.name'));
        expect(responseDisplay.getText()).toEqual('Test User');
    });
});

view raw JSON →