aboutsummaryrefslogtreecommitdiffstats
path: root/test/caldav/request/abstract_test.js
blob: 18d6bc3ab7a441f04ed05528aa9803b8f18fbcf3 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
requireRequest();

suite('caldav/request/abstract.js', function() {
  var subject;
  var Abstract;
  var Xhr;
  var Connection;
  var con;
  var FakeXhr;
  var SAX;
  var oldXhrClass;
  var url = 'http://google.com/';
  var options = {
    url: url,
    configOpt: true
  };

  suiteSetup(function() {
    Abstract = Caldav.require('request/abstract');
    FakeXhr = Caldav.require('support/fake_xhr');
    Xhr = Caldav.require('xhr');
    SAX = Caldav.require('sax');
    Connection = Caldav.require('connection');

    oldXhrClass = Xhr.prototype.xhrClass;
    Xhr.prototype.xhrClass = FakeXhr;
  });

  suiteTeardown(function() {
    Xhr.prototype.xhrClass = oldXhrClass;
  });

  setup(function() {
    con = new Connection({
      password: 'password',
      user: 'user'
    });
    subject = new Abstract(con, options);
    FakeXhr.instances.length = 0;
  });

  test('#_createPayload', function() {
    assert.equal(subject._createPayload(), '');
  });

  test('#initializer', function() {
    assert.instanceOf(subject.xhr, Xhr);
    assert.equal(subject.xhr.url, url);
    assert.equal(subject.configOpt, options.configOpt);
    assert.equal(subject.connection, con);
    assert.instanceOf(subject.sax, SAX);
    assert.equal(subject.xhr.headers['Content-Type'], 'text/xml');
  });

  test('xhr password options', function() {
    var subject = new Abstract(con, {
      url: url
    });

    var xhr = subject.xhr;

    assert.equal(xhr.password, 'password');
    assert.equal(xhr.user, 'user');
  });

  suite('#send', function(done) {
    var xhr;

    function getXhr() {
      return FakeXhr.instances.pop();
    }

    test('should return a Caldav.Xhr object', function() {
      var req = subject.send(function() {});
      assert.deepEqual(url, req.url)
      assert.deepEqual(con.user, req.user);
      assert.deepEqual(con.password, req.password);
    });

    suite('errors', function() {
      var calledWith;

      setup(function() {
        subject.send(function() {
          calledWith = arguments;
        });
      });

      function verifyStatusFailure(status, name) {
        test('status ' + status, function() {
          xhr = getXhr();
          xhr.respond('', status);
          assert.equal(calledWith[0].name, name);
        });
      }

      verifyStatusFailure(403, 'caldav-unknown');
      verifyStatusFailure(401, 'caldav-authentication');
      verifyStatusFailure(500, 'caldav-server-failure');
    });

    suite('success', function() {
      var calledWith;
      var xml = '<el><item>value</item></el>';

      setup(function(done) {
        subject.send(function() {
          calledWith = arguments;
          done();
        });

        xhr = getXhr();
        xhr.respond(xml, 207);
      });

      test('on response', function() {
        assert.equal(calledWith[0], null);
        assert.deepEqual(calledWith[1], {
          el: {
            item: { value: 'value' }
          }
        });
      });
    });
  });

});