From 7f30ad85d5415e8fb3bbbf729ac090d453205372 Mon Sep 17 00:00:00 2001 From: James Lal Date: Fri, 6 Jul 2012 14:10:11 -0700 Subject: Adding connection --- Makefile | 1 + caldav.js | 104 ++++++++++++++++++++++++++++++++++++++++- lib/caldav/caldav.js | 1 + lib/caldav/connection.js | 99 +++++++++++++++++++++++++++++++++++++++ lib/caldav/index.js | 1 + test-agent/config.json | 2 +- test/caldav/connection_test.js | 69 +++++++++++++++++++++++++++ test/caldav/index_test.js | 1 + test/helper.js | 3 ++ 9 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 lib/caldav/connection.js create mode 100644 test/caldav/connection_test.js diff --git a/Makefile b/Makefile index e5af129..a16e8c4 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ package: test-agent-config cat $(LIB_ROOT)/sax.js >> $(WEB_FILE) cat $(LIB_ROOT)/template.js >> $(WEB_FILE) cat $(LIB_ROOT)/xhr.js >> $(WEB_FILE) + cat $(LIB_ROOT)/connection.js >> $(WEB_FILE) cat $(LIB_ROOT)/templates/calendar_data.js >> $(WEB_FILE) cat $(LIB_ROOT)/templates/calendar_filter.js >> $(WEB_FILE) cat $(LIB_ROOT)/sax/base.js >> $(WEB_FILE) diff --git a/caldav.js b/caldav.js index c7d8e04..0d15595 100644 --- a/caldav.js +++ b/caldav.js @@ -1050,6 +1050,7 @@ function write (chunk) { * Maps exports to a file path. */ set exports(val) { + console.log(paths); return paths[this.path] = val; }, @@ -1250,7 +1251,6 @@ function write (chunk) { @namespace */ (function(module, ns) { - console.log(ns); /** * Constructor * @@ -1913,6 +1913,105 @@ function write (chunk) { [Caldav('xhr'), Caldav] : [module, require('./caldav')] )); +(function(module, ns) { + + var XHR = ns.require('xhr'); + + /** + * Connection objects contain + * general information to be reused + * across XHR requests. + * + * Also handles normalization of path details. + */ + function Connection(options) { + if (typeof(options) === 'undefined') { + options = {}; + } + + var key; + + for (key in options) { + if (options.hasOwnProperty(key)) { + this[key] = options[key]; + } + } + + var domain = options.domain; + + if (domain) { + if (domain.substr(-1) === '/') { + this.domain = domain.substr(0, domain.length - 1); + } + } + + } + + Connection.prototype = { + /** + * Default username for requests. + */ + user: '', + + /** + * Default passwords for requests. + */ + password: '', + + /** + * Default domain for requests. + */ + domain: '', + + /** + * Creates new XHR request based on default + * options for connection. + * + * @return {Caldav.Xhr} http request set with default options. + */ + request: function(options) { + if (typeof(options) === 'undefined') { + options = {}; + } + + var copy = {}; + var key; + // copy options + + for (key in options) { + copy[key] = options[key]; + } + + if (!copy.user) { + copy.user = this.user; + } + + if (!copy.password) { + copy.password = this.password; + } + + if (copy.url && copy.url.indexOf('http') !== 0) { + var url = copy.url; + if (url.substr(0, 1) !== '/') { + url = '/' + url; + } + copy.url = this.domain + url; + } + + return new XHR(copy); + } + + }; + + console.log('!HIT!!'); + module.exports = Connection; + +}.apply( + this, + (this.Caldav) ? + [Caldav('connection'), Caldav] : + [module, require('./caldav')] +)); (function(module, ns) { function CalendarData() { @@ -2249,6 +2348,7 @@ function write (chunk) { 'DAV:/status': HttpStatusHandler, 'DAV:/resourcetype': ArrayHandler, 'DAV:/principal-URL': HrefHandler, + 'DAV:/current-user-principal': HrefHandler, 'urn:ietf:params:xml:ns:caldav/calendar-data': CalendarDataHandler, 'DAV:/value': TextHandler, 'urn:ietf:params:xml:ns:caldav/calendar-home-set': HrefHandler, @@ -2373,6 +2473,7 @@ function write (chunk) { } this.xhr = new XHR(xhrOptions); + this.xhr.headers['Content-Type'] = 'text/xml'; } Abstract.prototype = { @@ -2598,6 +2699,7 @@ function write (chunk) { exports.Xhr = ns.require('xhr'); exports.Request = ns.require('request'); exports.Templates = ns.require('templates'); + exports.Connection = ns.require('connection'); }.apply( this, diff --git a/lib/caldav/caldav.js b/lib/caldav/caldav.js index 065b815..f0afe0a 100644 --- a/lib/caldav/caldav.js +++ b/lib/caldav/caldav.js @@ -42,6 +42,7 @@ * Maps exports to a file path. */ set exports(val) { + console.log(paths); return paths[this.path] = val; }, diff --git a/lib/caldav/connection.js b/lib/caldav/connection.js new file mode 100644 index 0000000..953d405 --- /dev/null +++ b/lib/caldav/connection.js @@ -0,0 +1,99 @@ +(function(module, ns) { + + var XHR = ns.require('xhr'); + + /** + * Connection objects contain + * general information to be reused + * across XHR requests. + * + * Also handles normalization of path details. + */ + function Connection(options) { + if (typeof(options) === 'undefined') { + options = {}; + } + + var key; + + for (key in options) { + if (options.hasOwnProperty(key)) { + this[key] = options[key]; + } + } + + var domain = options.domain; + + if (domain) { + if (domain.substr(-1) === '/') { + this.domain = domain.substr(0, domain.length - 1); + } + } + + } + + Connection.prototype = { + /** + * Default username for requests. + */ + user: '', + + /** + * Default passwords for requests. + */ + password: '', + + /** + * Default domain for requests. + */ + domain: '', + + /** + * Creates new XHR request based on default + * options for connection. + * + * @return {Caldav.Xhr} http request set with default options. + */ + request: function(options) { + if (typeof(options) === 'undefined') { + options = {}; + } + + var copy = {}; + var key; + // copy options + + for (key in options) { + copy[key] = options[key]; + } + + if (!copy.user) { + copy.user = this.user; + } + + if (!copy.password) { + copy.password = this.password; + } + + if (copy.url && copy.url.indexOf('http') !== 0) { + var url = copy.url; + if (url.substr(0, 1) !== '/') { + url = '/' + url; + } + copy.url = this.domain + url; + } + + return new XHR(copy); + } + + }; + + console.log('!HIT!!'); + module.exports = Connection; + +}.apply( + this, + (this.Caldav) ? + [Caldav('connection'), Caldav] : + [module, require('./caldav')] +)); diff --git a/lib/caldav/index.js b/lib/caldav/index.js index b54a9ac..df7dfa5 100644 --- a/lib/caldav/index.js +++ b/lib/caldav/index.js @@ -9,6 +9,7 @@ exports.Xhr = ns.require('xhr'); exports.Request = ns.require('request'); exports.Templates = ns.require('templates'); + exports.Connection = ns.require('connection'); }.apply( this, diff --git a/test-agent/config.json b/test-agent/config.json index a0e6506..4f6d6b4 100644 --- a/test-agent/config.json +++ b/test-agent/config.json @@ -1,3 +1,3 @@ {"tests": [ -"/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/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_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/connection_test.js b/test/caldav/connection_test.js new file mode 100644 index 0000000..fb32e8e --- /dev/null +++ b/test/caldav/connection_test.js @@ -0,0 +1,69 @@ +testSupport.lib('xhr'); +testSupport.lib('connection'); + +suite('caldav/connection', function() { + + var subject; + var Connection; + var XHR; + var user = 'foo'; + var password = 'bar'; + var domain = 'http://foo.com'; + + suiteSetup(function() { + Connection = Caldav.require('connection'); + XHR = Caldav.require('xhr'); + }); + + setup(function() { + subject = new Connection({ + user: user, + password: password, + domain: domain + }); + }); + + suite('initialization', function() { + + test('assignment', function() { + assert.equal(subject.user, user); + assert.equal(subject.password, password); + assert.equal(subject.domain, domain); + }); + + test('when domain has trailing slash', function() { + subject = new Connection({ + domain: domain + '/' + }); + + assert.equal(subject.domain, domain, 'should remove trailing slash'); + }); + + }); + + + suite('request', function() { + + test('credentails', function() { + var result = subject.request({ + url: domain + }); + + assert.instanceOf(result, XHR); + assert.equal(result.url, domain); + assert.equal(result.password, password); + assert.equal(result.user, user); + }); + + test('url without domain', function() { + var request = subject.request({ + url: 'bar.json' + }); + + // we add slash + assert.equal(request.url, domain + '/bar.json'); + }); + + }); + +}); diff --git a/test/caldav/index_test.js b/test/caldav/index_test.js index 4fd13f8..24e45f2 100644 --- a/test/caldav/index_test.js +++ b/test/caldav/index_test.js @@ -28,6 +28,7 @@ suite('caldav', function() { assert.ok(root.Request); assert.ok(root.Templates); assert.ok(root.Xhr); + assert.ok(root.Connection); }); }); diff --git a/test/helper.js b/test/helper.js index db3e162..07ab837 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,5 +1,7 @@ (function() { + // lazy defined navigator causes global leak warnings... + var requireBak; var specialRequires = { 'chai': requireChai @@ -13,6 +15,7 @@ /* stream hack for SAX */ if (!testSupport.isNode) { + window.navigator; requireBak = require; require = function require_shim(type) { -- cgit