How to use fake-xml-http-request - 10 common examples

To help you get started, we’ve selected a few fake-xml-http-request examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github folio-org / stripes-core / test / bigtest / network / patch-fake-xml-http-request.js View on Github external
this.responseText += body.substring(index, index + chunkSize);
    index += chunkSize;
  } while (index < body.length);

  var type = this.getResponseHeader("Content-Type");

  if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
    try {
      this.responseXML = parseXML(this.responseText);
    } catch (e) {
      // Unable to parse XML - no biggie
    }
  }

  if (this.async) {
    this._readyStateChange(FakeXMLHttpRequest.DONE);
  } else {
    this.readyState = FakeXMLHttpRequest.DONE;
  }
};
github folio-org / stripes-core / test / bigtest / network / patch-fake-xml-http-request.js View on Github external
FakeXMLHttpRequest.prototype._setResponseBody = function (body) {
  verifyRequestSent(this);
  verifyHeadersReceived(this);
  verifyResponseBodyType(body);

  var chunkSize = this.chunkSize || 10;
  var index = 0;
  this.responseText = "";
  delete this.response;

  do {
    if (this.async) {
      this._readyStateChange(FakeXMLHttpRequest.LOADING);
    }

    this.responseText += body.substring(index, index + chunkSize);
    index += chunkSize;
  } while (index < body.length);

  var type = this.getResponseHeader("Content-Type");

  if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
    try {
      this.responseXML = parseXML(this.responseText);
    } catch (e) {
      // Unable to parse XML - no biggie
    }
  }
github folio-org / stripes-core / test / bigtest / network / patch-fake-xml-http-request.js View on Github external
/* eslint-disable */
import FakeXMLHttpRequest from 'fake-xml-http-request';

/**
 * Monkey patch FakeXMLHttpRequest and remove `response` based on the work in:
 * https://github.com/pretenderjs/FakeXMLHttpRequest/pull/53/files
 *
 * which caused the issue described under:
 *
 * https://github.com/pretenderjs/FakeXMLHttpRequest/issues/54
 *
 */

FakeXMLHttpRequest.prototype.open = function(method, url, async, username, password) {
  this.method = method;
  this.url = url;
  this.async = typeof async == "boolean" ? async : true;
  this.username = username;
  this.password = password;
  this.responseText = null;
  this.responseXML = null;
  this.responseURL = url;
  this.requestHeaders = {};
  this.sendFlag = false;
  delete this.response;
  this._readyStateChange(FakeXMLHttpRequest.OPENED);
}

FakeXMLHttpRequest.prototype.abort = function() {
  this.aborted = true;
github TradeMe / tractor / plugins / mock-requests / src / scripts / shim-xhr.js View on Github external
let mock = tractor.getMatchingMock(method, url);

        if (!mock) {
            let message = `Unexpected "${method}" request to "${url}".`;
            /* eslint-disable no-console */
            console.error(message);
            /* eslint-enable no-console */
        }

        if (!mock || mock && mock.passThrough) {
            return originalOpen.apply(this, arguments);
        }

        let real = this;
        let fake = new FakeXMLHttpRequest();
        Object.setPrototypeOf(real, FakeXMLHttpRequest.prototype);

        real.send = function () {
            copyEvents(real, fake);
            FakeXMLHttpRequest.prototype.send.apply(this, arguments);
            fake.send.apply(fake, arguments);
            FakeXMLHttpRequest.prototype.respond.call(this, mock.status, mock.headers, mock.body);
            fake.respond.apply(fake, arguments);
        };

        copyEvents(real, fake);
        FakeXMLHttpRequest.prototype.open.apply(this, arguments);
        fake.open.apply(fake, arguments);
    };
github TradeMe / tractor / plugins / mock-requests / src / scripts / shim-xhr.js View on Github external
originalXHR.prototype.open = function (method, url) {
        let mock = tractor.getMatchingMock(method, url);

        if (!mock) {
            let message = `Unexpected "${method}" request to "${url}".`;
            /* eslint-disable no-console */
            console.error(message);
            /* eslint-enable no-console */
        }

        if (!mock || mock && mock.passThrough) {
            return originalOpen.apply(this, arguments);
        }

        let real = this;
        let fake = new FakeXMLHttpRequest();
        Object.setPrototypeOf(real, FakeXMLHttpRequest.prototype);

        real.send = function () {
            copyEvents(real, fake);
            FakeXMLHttpRequest.prototype.send.apply(this, arguments);
            fake.send.apply(fake, arguments);
            FakeXMLHttpRequest.prototype.respond.call(this, mock.status, mock.headers, mock.body);
            fake.respond.apply(fake, arguments);
        };

        copyEvents(real, fake);
        FakeXMLHttpRequest.prototype.open.apply(this, arguments);
        fake.open.apply(fake, arguments);
    };
github pretenderjs / pretender / src / index.ts View on Github external
function interceptor(ctx) {
  function FakeRequest() {
    // super()
    FakeXMLHttpRequest.call(this);
  }
  FakeRequest.prototype = Object.create(FakeXMLHttpRequest.prototype);
  FakeRequest.prototype.constructor = FakeRequest;

  // extend
  FakeRequest.prototype.send = function send() {
    this.sendArguments = arguments;
    if (!ctx.pretender.running) {
      throw new Error('You shut down a Pretender instance while there was a pending request. ' +
            'That request just tried to complete. Check to see if you accidentally shut down ' +
            'a pretender earlier than you intended to');
    }

    FakeXMLHttpRequest.prototype.send.apply(this, arguments);

    if (ctx.pretender.checkPassthrough(this)) {
      this.passthrough();
    } else {
github pretenderjs / pretender / src / pretender.es.js View on Github external
function interceptor(ctx) {
    function FakeRequest() {
        // super()
        FakeXMLHttpRequest.call(this);
    }
    FakeRequest.prototype = Object.create(FakeXMLHttpRequest.prototype);
    FakeRequest.prototype.constructor = FakeRequest;
    // extend
    FakeRequest.prototype.send = function send() {
        if (!ctx.pretender.running) {
            throw new Error('You shut down a Pretender instance while there was a pending request. ' +
                'That request just tried to complete. Check to see if you accidentally shut down ' +
                'a pretender earlier than you intended to');
        }
        FakeXMLHttpRequest.prototype.send.apply(this, arguments);
        if (ctx.pretender.checkPassthrough(this)) {
            var xhr = createPassthrough(this);
            xhr.send.apply(xhr, arguments);
        }
        else {
            ctx.pretender.handleRequest(this);
        }
github pretenderjs / pretender / src / index.ts View on Github external
function FakeRequest() {
    // super()
    FakeXMLHttpRequest.call(this);
  }
  FakeRequest.prototype = Object.create(FakeXMLHttpRequest.prototype);
github pretenderjs / pretender / src / pretender.es.js View on Github external
function FakeRequest() {
        // super()
        FakeXMLHttpRequest.call(this);
    }
    FakeRequest.prototype = Object.create(FakeXMLHttpRequest.prototype);
github folio-org / stripes-core / test / bigtest / network / patch-fake-xml-http-request.js View on Github external
FakeXMLHttpRequest.prototype.open = function(method, url, async, username, password) {
  this.method = method;
  this.url = url;
  this.async = typeof async == "boolean" ? async : true;
  this.username = username;
  this.password = password;
  this.responseText = null;
  this.responseXML = null;
  this.responseURL = url;
  this.requestHeaders = {};
  this.sendFlag = false;
  delete this.response;
  this._readyStateChange(FakeXMLHttpRequest.OPENED);
}

FakeXMLHttpRequest.prototype.abort = function() {
  this.aborted = true;
  this.responseText = null;
  delete this.response;
  this.errorFlag = true;
  this.requestHeaders = {};

  this.dispatchEvent(new _Event("abort", false, false, this));

  if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
    this._readyStateChange(FakeXMLHttpRequest.UNSENT);
    this.sendFlag = false;
  }

  if (typeof this.onerror === "function") {
    this.onerror();
  }

fake-xml-http-request

test infrastructure for a fake XMLHttpRequest object

MIT
Latest version published 4 years ago

Package Health Score

59 / 100
Full package analysis