diff options
author | James Lal <james@lightsofapollo.com> | 2012-07-06 08:13:15 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2012-07-06 08:13:15 -0700 |
commit | 20711f003e97a4a57d1e2838ba78a994c5faf6c0 (patch) | |
tree | b060e5de05a9ffefc2ec836469c087fffa99dcef /test/caldav/request | |
parent | c8e85fe1e0a1d2e15df580068244324ff536a23a (diff) | |
download | jsCalDAV-20711f003e97a4a57d1e2838ba78a994c5faf6c0.tar.gz |
webcals -> caldav rename (yes, back again)
Diffstat (limited to 'test/caldav/request')
-rw-r--r-- | test/caldav/request/abstract_test.js | 109 | ||||
-rw-r--r-- | test/caldav/request/calendar_query_test.js | 116 | ||||
-rw-r--r-- | test/caldav/request/propfind_test.js | 123 |
3 files changed, 348 insertions, 0 deletions
diff --git a/test/caldav/request/abstract_test.js b/test/caldav/request/abstract_test.js new file mode 100644 index 0000000..b63c355 --- /dev/null +++ b/test/caldav/request/abstract_test.js @@ -0,0 +1,109 @@ +requireRequest(); + +suite('caldav/request/abstract.js', function() { + var subject; + var Abstract; + var Xhr; + var FakeXhr; + var SAX; + var oldXhrClass; + var url = 'http://google.com/'; + var options = { + configOpt: true + }; + + suiteSetup(function() { + Abstract = Caldav.require('request/abstract'); + FakeXhr = Caldav.require('support/fake_xhr'); + Xhr = Caldav.require('xhr'); + SAX = Caldav.require('sax'); + + oldXhrClass = Xhr.prototype.xhrClass; + Xhr.prototype.xhrClass = FakeXhr; + }); + + suiteTeardown(function() { + Xhr.prototype.xhrClass = oldXhrClass; + }); + + setup(function() { + subject = new Abstract(url, 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.instanceOf(subject.sax, SAX); + }); + + test('xhr password options', function() { + var subject = new Abstract(url, { + password: 'password', + user: 'user' + }); + + 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(); + } + + suite('error', function() { + var calledWith; + + setup(function(done) { + subject.send(function() { + calledWith = arguments; + done(); + }); + + + xhr = getXhr(); + xhr.respond('NOT XML <div>', 500); + }); + + test('on response', function() { + assert.match(calledWith[0].message, /http error/); + }); + }); + + 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' } + } + }); + assert.equal(calledWith[2].xhr, xhr); + }); + }); + }); + +}); diff --git a/test/caldav/request/calendar_query_test.js b/test/caldav/request/calendar_query_test.js new file mode 100644 index 0000000..96a95af --- /dev/null +++ b/test/caldav/request/calendar_query_test.js @@ -0,0 +1,116 @@ +requireRequest(); +testSupport.lib('request/propfind'); +testSupport.lib('request/calendar_query'); + +suite('caldav/request/calendar_query', function() { + var Propfind, + FakeXhr, + CalendarData, + CalendarQuery, + CalendarFilter, + Xhr, + Template, + oldXhrClass, + SaxResponse; + + var url = 'http://google.com', + subject; + + suiteSetup(function() { + Propfind = Caldav.require('request/propfind'); + CalendarData = Caldav.require('templates/calendar_data'); + CalendarFilter = Caldav.require('templates/calendar_filter'); + CalendarQuery = Caldav.require('request/calendar_query'); + SaxResponse = Caldav.require('sax/dav_response'); + FakeXhr = Caldav.require('support/fake_xhr'); + Template = Caldav.require('template'); + Xhr = Caldav.require('xhr'); + + oldXhrClass = Xhr.prototype.xhrClass; + Xhr.prototype.xhrClass = FakeXhr; + }); + + suiteTeardown(function() { + Xhr.prototype.xhrClass = oldXhrClass; + }); + + setup(function() { + subject = new CalendarQuery(url); + FakeXhr.instances.length = 0; + }); + + test('initializer', function() { + assert.instanceOf(subject, Propfind); + assert.deepEqual(subject._props, []); + assert.equal(subject.xhr.headers['Depth'], 1); + assert.equal(subject.xhr.method, 'REPORT'); + + assert.instanceOf(subject.fields, CalendarData); + assert.instanceOf(subject.filters, CalendarFilter); + }); + + test('#_createPayload', function() { + subject.prop('getetag'); + subject.fields.select('VEVENT', ['NAME']); + subject.filters.add('VEVENT', true); + + var props = [ + '<N0:getetag />', + '<N1:calendar-data>', + '<N1:comp name="VCALENDAR">', + '<N1:comp name="VEVENT">', + '<N1:prop name="NAME" />', + '</N1:comp>', + '</N1:comp>', + '</N1:calendar-data>' + ].join(''); + + var filter = [ + '<N1:comp-filter name="VCALENDAR">', + '<N1:comp-filter name="VEVENT" />', + '</N1:comp-filter>' + ].join(''); + + var expected = [ + subject.template.doctype, + '<N1:calendar-query xmlns:N0="DAV:" ', + 'xmlns:N1="urn:ietf:params:xml:ns:caldav">', + '<N0:prop>', props, '</N0:prop>', + '<N1:filter>', filter, '</N1:filter>', + '</N1:calendar-query>' + ].join(''); + + assert.equal(subject._createPayload(), expected); + }); + + suite('integration', function() { + return; + var xml, + data, + result, + xhr, + calledWith; + + testSupport.defineSample('xml/propget.xml', function(data) { + xml = data; + }); + + setup(function(done) { + subject.prop('foo'); + + subject.send(function(err, tree) { + data = tree; + done(); + }); + + xhr = FakeXhr.instances.pop(); + xhr.respond(xml, 207); + }); + + test('simple tree', function() { + assert.ok(data['/calendar/user/']); + }); + }); + +}); + diff --git a/test/caldav/request/propfind_test.js b/test/caldav/request/propfind_test.js new file mode 100644 index 0000000..069aaf3 --- /dev/null +++ b/test/caldav/request/propfind_test.js @@ -0,0 +1,123 @@ +requireRequest(); +testSupport.lib('request/propfind'); + +suite('caldav/request/propfind', function() { + var Abstract, + Propfind, + FakeXhr, + Xhr, + Template, + oldXhrClass, + SaxResponse; + + var url = 'http://google.com', + subject; + + suiteSetup(function() { + Abstract = Caldav.require('request/abstract'); + Propfind = Caldav.require('request/propfind'); + SaxResponse = Caldav.require('sax/dav_response'); + FakeXhr = Caldav.require('support/fake_xhr'); + Template = Caldav.require('template'); + Xhr = Caldav.require('xhr'); + + oldXhrClass = Xhr.prototype.xhrClass; + Xhr.prototype.xhrClass = FakeXhr; + }); + + suiteTeardown(function() { + Xhr.prototype.xhrClass = oldXhrClass; + }); + + setup(function() { + subject = new Propfind(url); + FakeXhr.instances.length = 0; + }); + + test('initializer', function() { + assert.instanceOf(subject, Abstract); + assert.instanceOf(subject.template, Template); + assert.deepEqual(subject._props, []); + assert.equal( + subject.sax.handles['DAV:/response'], + SaxResponse + ); + + assert.equal(subject.xhr.headers['Depth'], 0); + assert.equal(subject.xhr.method, 'PROPFIND'); + }); + + test('#prop', function() { + var expected = subject.template.tag('test'); + + subject.prop('test'); + + assert.deepEqual(subject._props, [expected]); + }); + + test('#_createPayload', function() { + subject.prop('foo'); + subject.prop('bar'); + + var tags = [ + '<N0:foo />', + '<N0:bar />' + ].join(''); + + var expected = [ + subject.template.doctype, + '<N0:propfind xmlns:N0="DAV:">', + '<N0:prop>', tags, '</N0:prop>', + '</N0:propfind>' + ].join(''); + + var result = subject._createPayload(); + + assert.equal(subject._createPayload(), expected); + }); + + test('#_processResult', function(done) { + var inner = {}, + req = {}; + + subject.sax.root = { + multistatus: inner + }; + + subject._processResult(req, function(err, obj, xhr) { + assert.equal(xhr, req); + assert.equal(obj, inner); + assert.equal(err, null); + done(); + }); + }); + + suite('integration', function() { + var xml, + data, + result, + xhr, + calledWith; + + testSupport.defineSample('xml/propget.xml', function(data) { + xml = data; + }); + + setup(function(done) { + subject.prop('foo'); + + subject.send(function(err, tree) { + data = tree; + done(); + }); + + xhr = FakeXhr.instances.pop(); + xhr.respond(xml, 207); + }); + + test('simple tree', function() { + assert.ok(data['/calendar/user/']); + }); + }); + +}); |