src/index.js
function HttpBackend() {
    this.requests = [];
    this.expectedRequests = [];
    const self = this;

    // All the promises our flush requests have returned. When all these promises are
    // resolved or rejected, there are no more flushes in progress.
    // For simplicity we never remove promises from this loop: this is a mock utility
    // for short duration tests, so this keeps it simpler.
    this._flushPromises = [];

    // the request function dependency that the SDK needs.
    this.requestFn = function(opts, callback) {
        const req = new Request(opts, callback);
        console.log(`${Date.now()} HTTP backend received request: ${req}`);
        self.requests.push(req);

        const abort = function() {
            const idx = self.requests.indexOf(req);
            if (idx >= 0) {
                console.log("Aborting HTTP request: %s %s", opts.method,
                            opts.uri);
                self.requests.splice(idx, 1);
                req.callback("aborted");
            }
        };

        return {
            abort: abort,
        };
    };

    // very simplistic mapping from the whatwg fetch interface onto the request
    // interface, so we can use the same mock backend for both.
    this.fetchFn = function(input, init) {
        init = init || {};
        const requestOpts = {
            uri: input,
            method: init.method || 'GET',
            body: init.body,
        };

        return new Promise((resolve, reject) => {
            function callback(err, response, body) {
                if (err) {
                    reject(err);
                }
                resolve({
                    ok: response.statusCode >= 200 && response.statusCode < 300,
                    json: () => JSON.parse(body),
                });
            };

            const req = new Request(requestOpts, callback);
            console.log(`HTTP backend received request: ${req}`);
            self.requests.push(req);
        });
    };
}
function HttpBackend() {
    this.requests = [];
    this.expectedRequests = [];
    const self = this;

    // All the promises our flush requests have returned. When all these promises are
    // resolved or rejected, there are no more flushes in progress.
    // For simplicity we never remove promises from this loop: this is a mock utility
    // for short duration tests, so this keeps it simpler.
    this._flushPromises = [];

    // the request function dependency that the SDK needs.
    this.requestFn = function(opts, callback) {
        const req = new Request(opts, callback);
        console.log(`${Date.now()} HTTP backend received request: ${req}`);
        self.requests.push(req);

        const abort = function() {
            const idx = self.requests.indexOf(req);
            if (idx >= 0) {
                console.log("Aborting HTTP request: %s %s", opts.method,
                            opts.uri);
                self.requests.splice(idx, 1);
                req.callback("aborted");
            }
        };

        return {
            abort: abort,
        };
    };

    // very simplistic mapping from the whatwg fetch interface onto the request
    // interface, so we can use the same mock backend for both.
    this.fetchFn = function(input, init) {
        init = init || {};
        const requestOpts = {
            uri: input,
            method: init.method || 'GET',
            body: init.body,
        };

        return new Promise((resolve, reject) => {
            function callback(err, response, body) {
                if (err) {
                    reject(err);
                }
                resolve({
                    ok: response.statusCode >= 200 && response.statusCode < 300,
                    json: () => JSON.parse(body),
                });
            };

            const req = new Request(requestOpts, callback);
            console.log(`HTTP backend received request: ${req}`);
            self.requests.push(req);
        });
    };
}
function HttpBackend ( ) { this . requests = [ ] ; this . expectedRequests = [ ] ; const self = this ; // All the promises our flush requests have returned. When all these promises are // resolved or rejected, there are no more flushes in progress. // For simplicity we never remove promises from this loop: this is a mock utility // for short duration tests, so this keeps it simpler. this . _flushPromises = [ ] ; // the request function dependency that the SDK needs. this . requestFn = function ( opts , callback ) { const req = new Request ( opts , callback ) ; console . log ( ` ${ Date . now ( ) } ${ req } ` ) ; self . requests . push ( req ) ; const abort = function ( ) { const idx = self . requests . indexOf ( req ) ; if ( idx >= 0 ) { console . log ( "Aborting HTTP request: %s %s" , opts . method , opts . uri ) ; self . requests . splice ( idx , 1 ) ; req . callback ( "aborted" ) ; } } ; return { abort : abort , } ; } ; // very simplistic mapping from the whatwg fetch interface onto the request // interface, so we can use the same mock backend for both. this . fetchFn = function ( input , init ) { init = init || { } ; const requestOpts = { uri : input , method : init . method || 'GET' , body : init . body , } ; return new Promise ( ( resolve , reject ) => { function callback ( err , response , body ) { if ( err ) { reject ( err ) ; } resolve ( { ok : response . statusCode >= 200 && response . statusCode < 300 , json : ( ) => JSON . parse ( body ) , } ) ; } ; const req = new Request ( requestOpts , callback ) ; console . log ( ` ${ req } ` ) ; self . requests . push ( req ) ; } ) ; } ; }
Construct a mock HTTP backend, heavily inspired by Angular.js.
@constructor
Construct a mock HTTP backend heavily inspired by Angular . js .