aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/FakeXMLHttpRequest.js
blob: 6dc35b98a7015e0915edd30e7b1a103fa9170622 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var chai = require('chai');
var assert = chai.assert;

var FakeXMLHttpRequest = function () {
    this.responseText = "{}";
    this.readyState = 4;
    this.onreadystatechange = null;
    this.async = false;
    this.headers = {
        'Content-Type': 'text/plain'
    };
};

FakeXMLHttpRequest.prototype.open = function (method, host, async) {
    assert.equal(method, 'POST');
    assert.notEqual(host, null);
    assert.equal(async === false || async === true, true);
    this.async = async;
};

FakeXMLHttpRequest.prototype.setRequestHeader = function(name, value) {
    this.headers[name] = value;
};

FakeXMLHttpRequest.prototype.send = function (payload) {
    assert.equal(typeof payload, 'string');
    if (this.async) {
        assert.equal(typeof this.onreadystatechange, 'function');
        this.onreadystatechange();
        return;
    }
    return this.responseText;
};

module.exports = { 
    XMLHttpRequest: FakeXMLHttpRequest
};