aboutsummaryrefslogtreecommitdiffstats
path: root/test/caldav/sax/dav_response_test.js
blob: 38250434a47d722286e0db5ac01e01add8c14861 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
testSupport.helper('ical');
testSupport.lib('responder');
testSupport.lib('sax');
testSupport.lib('sax/base');
testSupport.lib('sax/calendar_data_handler');
testSupport.lib('sax/dav_response');

suite('caldav/sax/dav_response', function() {

  var Parse;
  var Response;
  var Base;
  var CalendarDataHandler;

  var originalHandler;

  suiteSetup(function() {
    Parse = Caldav.require('sax');
    Base = Caldav.require('sax/base');
    Response = Caldav.require('sax/dav_response');
  });

  var subject;
  var data;
  var handler;
  var parser;

  setup(function() {
    //we omit the option to pass base parser
    //because we are the base parser
    subject = new Parse();
    subject.registerHandler('DAV:/response', Response);

    // HACK to get CalendarDataHandler
    var handleNS = 'urn:ietf:params:xml:ns:caldav/calendar-data';

    CalendarDataHandler = subject.handles['DAV:/response'];
    CalendarDataHandler = CalendarDataHandler.handles['DAV:/propstat'];
    CalendarDataHandler = CalendarDataHandler.handles[handleNS];

    if (!originalHandler) {
      originalHandler = CalendarDataHandler.parseICAL;
    }

    // XXX: this may change later if ICAL is no longer
    //      exposed directly.
    CalendarDataHandler.parseICAL = ICAL.parse;
  });

  teardown(function() {
    CalendarDataHandler.parseICAL = originalHandler;
  });

  suite('calendar-query', function() {
    var xml;

    testSupport.defineSample('xml/calendar_query_single.xml', function(data) {
      xml = data;
    });

    test('result', function(done) {
      subject.once('complete', function(data) {
        var response = data.multistatus;
        var event = response['event.ics'];
        assert.ok(event);

        assert.ok(event['calendar-data'], 'has calendar data');
        assert.ok(
          event['calendar-data'].value instanceof Array, 'ical is parsed'
        );
        done();
      });

      subject.write(xml).close();
    });
  });

  suite('propget', function() {
    var xml;

    testSupport.defineSample('xml/propget.xml', function(data) {
      xml = data;
    });

    expected = {
      '/calendar/user/': {

        'principal-URL': {
          status: '200',
          value: {
            href: '/calendar/pinc/'
          }
        },

        resourcetype: {
          status: '200',
          value: [
            'principal',
            'collection'
          ]
        },

        'current-user-principal': {
          status: '404',
          value: {}
        }
      },

      '/calendar/other': {
        missing: {
          status: '200',
          value: {}
        }
      }

    };

    test('output', function(done) {
      var response = [];

      subject.on('DAV:/response', function(url, data) {
        response.push([url, data]);
      });


      subject.once('complete', function(data) {
        assert.deepEqual(
          data.multistatus, expected,
          "expected \n '" + JSON.stringify(data.multistatus) +
          "'\n to equal \n '" + JSON.stringify(expected) + '\n"'
        );

        assert.deepEqual(
          [
            '/calendar/user/',
            expected['/calendar/user/']
          ],
          response[0],
          '/calendar/user/ response'
        );

        assert.deepEqual(
          [
            '/calendar/other',
            expected['/calendar/other']
          ],
          response[1],
          '/calendar/other/ response'
        );

        done();
      });
      subject.write(xml).close();
    });
  });

});