aboutsummaryrefslogtreecommitdiffstats
path: root/test/support/fake_xhr.js
blob: 27e621b4fc071b8eac7364930087fc9f0e96e665 (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
39
40
41
42
43
(function(module) {

  function FakeXhr() {
    this.openArgs = null;
    this.sendArgs = null;
    this.headers = {};
    this.responseHeaders = {};
  }

  FakeXhr.prototype = {
    open: function() {
      this.openArgs = arguments;
    },

    getResponseHeader: function(key) {
      return this.responseHeaders[key];
    },

    setRequestHeader: function(key, value) {
      this.headers[key] = value;
    },

    send: function() {
      this.sendArgs = arguments;
    },

    respond: function(data, code) {
      this.readyState = 4;
      this.responseHeaders['content-type'] = 'application/json';
      this.responseText = JSON.stringify(data);
      this.status = code || 200;
      this.onreadystatechange();
    }
  };

  module.exports = FakeXhr;

}.apply(
  this,
  (this.Webcals) ?
    [Webcals('support/fake_xhr'), Webcals] :
    [module, require('../../lib/webcals/webcals')]
));