aboutsummaryrefslogtreecommitdiffstats
path: root/test/caldav/request/asset_test.js
blob: f1878ba27365fed50c1602bffaa3b8e9ea821cee (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
requireRequest();
testSupport.lib('request/asset');

suite('caldav/request/asset.js', function() {

  // classes
  var Asset;
  var Xhr;
  var Errors;
  var Connection;
  var SAX;
  var FakeXhr;

  // instances
  var subject;
  var oldXhrClass;
  var con;
  var url = '/foo.ics';
  var options = {
    url: url,
    configOpt: true
  };

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

  suiteSetup(function() {
    Asset = Caldav.require('request/asset');
    FakeXhr = Caldav.require('support/fake_xhr');
    Xhr = Caldav.require('xhr');
    Connection = Caldav.require('connection');
    Errors = Caldav.require('errors');

    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 Asset(con, url);
    FakeXhr.instances.length = 0;
  });

  test('#initializer', function() {
    assert.equal(subject.connection, con);
    assert.equal(subject.url, url);
  });

  suite('#get', function() {

    test('with etag', function(done) {
      subject.get({ etag: 'foo' }, function(err, data, xhr) {
        assert.equal(xhr.headers['If-None-Match'], 'foo');
        done();
      });

      var xhr = lastXHR();
      xhr.respond('', 200);
    });

    test('no options', function(done) {
      var content = 'content';

      subject.get(function(err, data, xhr) {
        assert.ok(!err);
        assert.equal(data, content);
        assert.equal(xhr.openArgs[0], 'GET');
        assert.equal(xhr.headers['Content-Type'], subject.contentType);
        done();
      });

      var xhr = lastXHR();
      xhr.respond(content, 200);
    });

  });

  suite('#put', function() {
    test('with error', function() {
      subject.put({}, '', function(err) {
        assert.ok(err, 'returns error');
        assert.instanceOf(err, Errors.Authentication, 'assets validate http');
      });

      var xhr = lastXHR();
      xhr.respond('', 401);
    });

    test('success', function(done) {
      var content = 'foo';
      subject.put({ etag: 'x' }, content, function(err, data, xhr) {
        assert.equal(xhr.openArgs[0], 'PUT');
        assert.equal(xhr.sendArgs[0], content);
        done();
      });

      var xhr = lastXHR();
      xhr.respond('', 201);
    });
  });

  test('#delete', function(done) {
    subject.delete({ etag: 'x' }, function(err, data, xhr) {
      assert.equal(xhr.openArgs[0], 'DELETE');
      done();
    });

    var xhr = lastXHR();
    xhr.respond('', 201);
  });

});