diff options
-rw-r--r-- | caldav.js | 51 | ||||
-rw-r--r-- | lib/caldav/caldav.js | 1 | ||||
-rw-r--r-- | lib/caldav/request/calendar_home.js | 10 | ||||
-rw-r--r-- | test-agent/config.json | 2 | ||||
-rw-r--r-- | test/caldav/request/calendar_home_test.js | 43 | ||||
-rw-r--r-- | test/support/mock_request.js | 72 | ||||
-rw-r--r-- | test/support/propfind.js | 0 |
7 files changed, 141 insertions, 38 deletions
@@ -1050,7 +1050,6 @@ function write (chunk) { * Maps exports to a file path. */ set exports(val) { - console.log(paths); return paths[this.path] = val; }, @@ -2003,7 +2002,6 @@ function write (chunk) { }; - console.log('!HIT!!'); module.exports = Connection; }.apply( @@ -2437,10 +2435,10 @@ function write (chunk) { /** * Creates an (Web/Cal)Dav request. * - * @param {String} url location of resource. + * @param {Caldav.Connection} connection connection details. * @param {Object} options additional options for request. */ - function Abstract(url, options) { + function Abstract(connection, options) { if (typeof(options) === 'undefined') { options = {}; } @@ -2448,22 +2446,6 @@ function write (chunk) { var key; var xhrOptions = {}; - if (typeof(url) === 'undefined' || !url) { - throw new Error('request requires a url'); - } - - xhrOptions.url = url; - - if ('password' in options) { - xhrOptions.password = options.password; - delete options.password; - } - - if ('user' in options) { - xhrOptions.user = options.user; - delete options.user; - } - this.sax = new SAX(); for (key in options) { @@ -2472,8 +2454,16 @@ function write (chunk) { } } - this.xhr = new XHR(xhrOptions); - this.xhr.headers['Content-Type'] = 'text/xml'; + if (!connection) { + throw new Error('must pass connection object'); + } + + this.connection = connection; + + this.xhr = this.connection.request({ + url: this.url, + headers: { 'Content-Type': 'text/xml' } + }); } Abstract.prototype = { @@ -2543,14 +2533,20 @@ function write (chunk) { DavResponse ); - this.xhr.headers['Depth'] = this.depth; + this.xhr.headers['Depth'] = 0; this.xhr.method = 'PROPFIND'; } Propfind.prototype = { __proto__: Abstract.prototype, - depth: 0, + get depth() { + return this.xhr.headers.Depth; + }, + + set depth(val) { + this.xhr.headers.Depth = val; + }, /** * Adds property to request. @@ -2601,10 +2597,10 @@ function write (chunk) { * * Defaults to Depth of 1. * - * @param {String} url location to make request. + * @param {CalDav.Connection} connection connection object. * @param {Object} options options for calendar query. */ - function CalendarQuery(url, options) { + function CalendarQuery(options) { Propfind.apply(this, arguments); this.xhr.headers['Depth'] = this.depth || 1; @@ -2652,7 +2648,8 @@ function write (chunk) { module.exports = { Abstract: ns.require('request/abstract'), CalendarQuery: ns.require('request/calendar_query'), - Propfind: ns.require('request/propfind') + Propfind: ns.require('request/propfind'), + CalendarHome: ns.require('request/calendar_home') }; }.apply( diff --git a/lib/caldav/caldav.js b/lib/caldav/caldav.js index f0afe0a..065b815 100644 --- a/lib/caldav/caldav.js +++ b/lib/caldav/caldav.js @@ -42,7 +42,6 @@ * Maps exports to a file path. */ set exports(val) { - console.log(paths); return paths[this.path] = val; }, diff --git a/lib/caldav/request/calendar_home.js b/lib/caldav/request/calendar_home.js index da51740..f4f80ac 100644 --- a/lib/caldav/request/calendar_home.js +++ b/lib/caldav/request/calendar_home.js @@ -1,8 +1,5 @@ (function(module, ns) { - var Propfind = ns.require('request/propfind'); - - /** * Creates a propfind request. * @@ -51,8 +48,10 @@ CalendarHome.prototype = { + Propfind: ns.require('request/propfind'), + _findPrincipal: function(url, callback) { - var find = new Propfind(this.connection, { + var find = new this.Propfind(this.connection, { url: url }); @@ -78,7 +77,7 @@ _findCalendarHome: function(url, callback) { var details = {}; - var find = new Propfind(this.connection, { + var find = new this.Propfind(this.connection, { url: url }); @@ -105,7 +104,6 @@ */ send: function(callback) { var self = this; - // find principal self._findPrincipal(self.url, function(err, url) { if (!url) { diff --git a/test-agent/config.json b/test-agent/config.json index 4f6d6b4..b8e177f 100644 --- a/test-agent/config.json +++ b/test-agent/config.json @@ -1,3 +1,3 @@ {"tests": [ -"/test/caldav/connection_test.js","/test/caldav/ical_test.js","/test/caldav/index_test.js","/test/caldav/request/abstract_test.js","/test/caldav/request/calendar_query_test.js","/test/caldav/request/propfind_test.js","/test/caldav/resource_root_test.js","/test/caldav/resources/calendar_test.js","/test/caldav/sax/base_test.js","/test/caldav/sax/dav_response_test.js","/test/caldav/sax_test.js","/test/caldav/template_test.js","/test/caldav/templates/calendar_data_test.js","/test/caldav/templates/calendar_filter_test.js","/test/caldav/xhr_test.js" +"/test/caldav/connection_test.js","/test/caldav/ical_test.js","/test/caldav/index_test.js","/test/caldav/request/abstract_test.js","/test/caldav/request/calendar_home_test.js","/test/caldav/request/calendar_query_test.js","/test/caldav/request/propfind_test.js","/test/caldav/resource_root_test.js","/test/caldav/resources/calendar_test.js","/test/caldav/sax/base_test.js","/test/caldav/sax/dav_response_test.js","/test/caldav/sax_test.js","/test/caldav/template_test.js","/test/caldav/templates/calendar_data_test.js","/test/caldav/templates/calendar_filter_test.js","/test/caldav/xhr_test.js" ]} diff --git a/test/caldav/request/calendar_home_test.js b/test/caldav/request/calendar_home_test.js index 525d407..6df65df 100644 --- a/test/caldav/request/calendar_home_test.js +++ b/test/caldav/request/calendar_home_test.js @@ -2,10 +2,12 @@ requireRequest(); testSupport.lib('request/propfind'); testSupport.lib('request/calendar_home'); +testSupport.helper('mock_request'); suite('caldav/request/propfind', function() { - var Propfind; var Connection; + var MockRequest; + var MockPropfind; var Home; var subject; var con; @@ -14,15 +16,22 @@ suite('caldav/request/propfind', function() { subject; suiteSetup(function() { - Propfind = Caldav.require('request/propfind'); Connection = Caldav.require('connection'); Home = Caldav.require('request/calendar_home'); + MockRequest = Caldav.require('support/mock_request'); + }); + + suiteSetup(function() { + MockPropfind = MockRequest.create(['prop']); }); setup(function() { + MockPropfind.reset(); + con = new Connection(); subject = new Home(con, { - url: url + url: url, + Propfind: MockPropfind }); }); @@ -31,5 +40,33 @@ suite('caldav/request/propfind', function() { assert.equal(subject.connection, con); }); + test('_findPrincipal', function() { + var err, data, response = {}; + + subject._findPrincipal(url, function() { + err = arguments[0]; + data = arguments[1]; + }); + + var req = MockPropfind.instances[0]; + assert.equal(req.options.url, url); + assert.deepEqual(req.propCalls, [ + ['current-user-principal'], + ['principal-URL'] + ]); + + response[url] = { + 'current-user-principal': { + status: '200', + value: 'foo.com/' + } + }; + + // respond to request + req.respond(null, response); + + assert.equal(data, 'foo.com/'); + }); + }); diff --git a/test/support/mock_request.js b/test/support/mock_request.js new file mode 100644 index 0000000..2141b3c --- /dev/null +++ b/test/support/mock_request.js @@ -0,0 +1,72 @@ +(function(module, ns) { + + function MockRequest(connection, options) { + this.connection = connection; + this.options = options; + + var parent = this.constructor; + + if (!parent.instances) { + parent.instances = []; + } + + parent.instances.push(this); + } + + + MockRequest.prototype = { + send: function(callback) { + this.__sendCallback = callback; + }, + + respond: function() { + this.__sendCallback.apply(this, arguments); + } + + }; + + MockRequest.reset = function() { + if (!this.instances) { + this.instances = []; + } + this.instances.length = 0; + } + + MockRequest.create = function(methods) { + var self = this; + + if (typeof(methods) === 'undefined') { + methods = []; + } + + var child = function() { + self.apply(this, arguments); + }; + + child.prototype = Object.create(self.prototype); + child.prototype.constructor = child; + + methods.forEach(function(method) { + child.prototype[method] = function() { + var savedName = method + 'Calls'; + if (!(savedName in this)) { + this[savedName] = []; + } + this[savedName].push(arguments); + } + }); + + child.create = self.create; + child.reset = self.reset; + + return child; + }; + + module.exports = MockRequest; + +}.apply( + this, + (this.Caldav) ? + [Caldav('support/mock_request'), Caldav] : + [module, require('../../lib/caldav/caldav')] +)); diff --git a/test/support/propfind.js b/test/support/propfind.js deleted file mode 100644 index e69de29..0000000 --- a/test/support/propfind.js +++ /dev/null |