0

I'm looking for a way to mock HTTP(S) requests using protractor and aurelia. I know there are protractor plugins that do this but they seem to drop in an angular module, since i'm not using angular those don't seem to be an option.

For our use case testing against a live API is far from ideal. The tests we want to do are preferably based on a fixed json mock

Does anyone have suggestion on how to do this?

Sven van de Scheur
  • 1,809
  • 2
  • 15
  • 31

1 Answers1

0

Okay so I think I found a solution that does not require any altering to the applications code. It ain't pretty, but it seems to work:

/**
 * If you have a problem...
 * if no one else can help...
 * and if you can find them...
 * maybe you can hire...
 * fakeXHR.
 *
 * Overwrites XMLHttpRequest and auto resoles with status and response
 * @param {Number} status The statuscode to resolve with
 * @param {String} response, The response to resolve with
 */
function fakeXHR(status, response) {
    browser.executeScript(function (args) {
        var status = args[0];
        var response = args[1];

        function FakeXHR() {
            this.open = function() {}
            this.send = function() {}
            this.setRequestHeader = function() {}

            setTimeout(function() {
                this.response = response;
                this.status = status;
                this.onload();
            }.bind(this), 0);
        }

        XMLHttpRequest = FakeXHR;
    }, [status, response])
}
Sven van de Scheur
  • 1,809
  • 2
  • 15
  • 31