aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-06-26 07:21:43 +0200
committerJames Lal <james@lightsofapollo.com>2012-06-26 07:21:43 +0200
commit646e0b6991c9e85de9b8eee20e601079fb128429 (patch)
treecdd2a21d7816936556eab9f99dd32e8914c2a426
parent96683161e43fc0101c74f0875d1a2d445afe7e8d (diff)
downloadjsCalDAV-646e0b6991c9e85de9b8eee20e601079fb128429.tar.gz
abstract request
-rw-r--r--Makefile1
-rw-r--r--lib/webcals/request/abstract.js65
-rw-r--r--lib/webcals/xhr.js2
-rw-r--r--test/support/fake_xhr.js8
-rw-r--r--test/webcals/request/abstract_test.js92
-rw-r--r--test/webcals/sax/base_test.js4
-rw-r--r--test/webcals/sax/dav_response_test.js3
-rw-r--r--test/webcals/xhr_test.js6
8 files changed, 168 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index 10e47df..ca50ad0 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,7 @@ test:
--reporter $(REPORTER) \
--growl test/helper.js \
test/webcals/sax/*_test.js \
+ test/webcals/request/*_test.js \
test/webcals/*_test.js
.PHONY: watch
diff --git a/lib/webcals/request/abstract.js b/lib/webcals/request/abstract.js
index 7d31540..fe47de9 100644
--- a/lib/webcals/request/abstract.js
+++ b/lib/webcals/request/abstract.js
@@ -1,9 +1,72 @@
(function(module, ns) {
- function Abstract() {
+ var SAX = ns.require('sax');
+ var XHR = ns.require('xhr');
+
+ /**
+ * Creates an (Web/Cal)Dav request.
+ *
+ * @param {String} url location of resource.
+ * @param {Object} options additional options for request.
+ */
+ function Abstract(url, options) {
+ var key;
+
+ if (typeof(url) === 'undefined' || !url) {
+ throw new Error('request requires a url');
+ }
+
+ this.url = url;
+ this.sax = new SAX();
+
+ for (key in options) {
+ if (options.hasOwnProperty(key)) {
+ this[key] = options[key];
+ }
+ }
}
+ Abstract.prototype = {
+
+ _createXhr: function() {
+ return new XHR({
+ url: this.url
+ });
+ },
+
+ _createPayload: function() {
+ return '';
+ },
+
+ /**
+ * Sends request to server.
+ *
+ * @param {Function} callback node style callback.
+ * Receives three arguments
+ * error, parsedData, xhr.
+ */
+ send: function(callback) {
+ var self = this;
+ var req = this.xhr = this._createXhr();
+ req.data = this._createPayload();
+
+ // in the future we may stream data somehow
+ req.send(function xhrResult() {
+ var xhr = req.xhr;
+ if (xhr.status > 199 && xhr.status < 300) {
+ // success
+ self.sax.write(xhr.responseText).close();
+
+ callback(null, self.sax.root, req);
+ } else {
+ // fail
+ callback(new Error('http error code: ' + xhr.status));
+ }
+ });
+ }
+ };
+
module.exports = Abstract;
}.apply(
diff --git a/lib/webcals/xhr.js b/lib/webcals/xhr.js
index f049c3f..f20415f 100644
--- a/lib/webcals/xhr.js
+++ b/lib/webcals/xhr.js
@@ -92,7 +92,7 @@
if (xhr.readyState === 4) {
data = xhr.responseText;
this.waiting = false;
- callback(data, xhr);
+ callback(null, xhr);
}
}.bind(this);
diff --git a/test/support/fake_xhr.js b/test/support/fake_xhr.js
index 27e621b..6c4976d 100644
--- a/test/support/fake_xhr.js
+++ b/test/support/fake_xhr.js
@@ -5,8 +5,12 @@
this.sendArgs = null;
this.headers = {};
this.responseHeaders = {};
+
+ FakeXhr.instances.push(this);
}
+ FakeXhr.instances = [];
+
FakeXhr.prototype = {
open: function() {
this.openArgs = arguments;
@@ -26,8 +30,8 @@
respond: function(data, code) {
this.readyState = 4;
- this.responseHeaders['content-type'] = 'application/json';
- this.responseText = JSON.stringify(data);
+ this.responseHeaders['content-type'] = 'text/xml';
+ this.responseText = data;
this.status = code || 200;
this.onreadystatechange();
}
diff --git a/test/webcals/request/abstract_test.js b/test/webcals/request/abstract_test.js
index 08c806c..23bfa8c 100644
--- a/test/webcals/request/abstract_test.js
+++ b/test/webcals/request/abstract_test.js
@@ -1,13 +1,105 @@
requireLib('xhr');
+requireLib('sax');
requireLib('request/abstract');
+requireSupport('fake_xhr');
suite('webcals/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 = Webcals.require('request/abstract');
+ FakeXhr = Webcals.require('support/fake_xhr');
+ Xhr = Webcals.require('xhr');
+ SAX = Webcals.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('#_createXhr', function() {
+ var result = subject._createXhr();
+ assert.instanceOf(result, Xhr);
+ assert.equal(result.url, url);
+ });
+
+ test('#_createPayload', function() {
+ assert.equal(subject._createPayload(), '');
+ });
+
+ test('#initializer', function() {
+ assert.equal(subject.url, url);
+ assert.equal(subject.configOpt, options.configOpt);
+ assert.instanceOf(subject.sax, SAX);
});
+ 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, 200);
+ });
+
+ 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/webcals/sax/base_test.js b/test/webcals/sax/base_test.js
index 9ad0bdc..04d8fda 100644
--- a/test/webcals/sax/base_test.js
+++ b/test/webcals/sax/base_test.js
@@ -56,7 +56,7 @@ suite('webcals/sax/base', function() {
});
suite('base parser', function() {
- var xml;
+ var xml, data;
defineSample('xml/simple.xml', function(data) {
xml = data;
@@ -64,7 +64,7 @@ suite('webcals/sax/base', function() {
//base baser does not
//care about attrs at this point
- expected = {
+ var expected = {
simple: {
a: [
{ value: 'Foo' },
diff --git a/test/webcals/sax/dav_response_test.js b/test/webcals/sax/dav_response_test.js
index 36616b6..1279b84 100644
--- a/test/webcals/sax/dav_response_test.js
+++ b/test/webcals/sax/dav_response_test.js
@@ -2,7 +2,7 @@ requireLib('sax');
requireLib('sax/base');
requireLib('sax/dav_response');
-suite('webcals/sax/base', function() {
+suite('webcals/sax/dav_response', function() {
var data,
subject,
@@ -65,7 +65,6 @@ suite('webcals/sax/base', function() {
test('output', function(done) {
subject.once('complete', function(data) {
- console.log(JSON.stringify(data.multistatus));
assert.deepEqual(
data.multistatus, expected,
"expected \n '" + JSON.stringify(data.multistatus) +
diff --git a/test/webcals/xhr_test.js b/test/webcals/xhr_test.js
index 2ac2dbb..33f692d 100644
--- a/test/webcals/xhr_test.js
+++ b/test/webcals/xhr_test.js
@@ -58,12 +58,10 @@ suite('webacls/xhr', function() {
var data = '<html></html>',
url = 'http://foo',
xhr,
- responseData,
responseXhr;
function callback(done, data, xhr) {
responseXhr = xhr;
- responseData = data;
done();
}
@@ -98,10 +96,9 @@ suite('webacls/xhr', function() {
setup(function() {
responseXhr = null;
- responseData = null;
});
- suite('when xhr is a success and responds /w json', function() {
+ suite('when xhr is a success and responds /w data', function() {
var response = '<html></html>', cb;
setup(function(done) {
@@ -129,7 +126,6 @@ suite('webacls/xhr', function() {
test('should send callback parsed data and xhr', function() {
assert.equal(responseXhr, subject.xhr);
- assert.deepEqual(responseData, response);
});
opensXHR();