From d64a1c54d6098596af1ed68272c78975ec56d0b7 Mon Sep 17 00:00:00 2001 From: James Lal Date: Mon, 7 Jan 2013 08:26:13 +0100 Subject: support for chunked responses --- caldav.js | 25 ++++++++++++++++++++ lib/caldav/xhr.js | 25 ++++++++++++++++++++ test/caldav/sax/dav_response_test.js | 6 +++-- test/caldav/xhr_test.js | 45 +++++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/caldav.js b/caldav.js index a37263e..75b9cdd 100644 --- a/caldav.js +++ b/caldav.js @@ -2096,10 +2096,35 @@ function write (chunk) { } } + + var hasProgressEvents = false; + + // check for progress event support. + if ('onprogress' in xhr) { + hasProgressEvents = true; + var last = 0; + xhr.onprogress = function onProgress(event) { + var chunk = xhr.responseText.substr(last, event.loaded); + last = event.loaded; + if (this.ondata) { + this.ondata(chunk); + } + }.bind(this); + } + xhr.onreadystatechange = function onReadyStateChange() { var data; if (xhr.readyState === 4) { data = xhr.responseText; + + // emulate progress events for node... + // this really lame we should probably just + // use a real http request for node but this + // will let us do some testing via node for now. + if (!hasProgressEvents && this.ondata) { + this.ondata(data); + } + this.waiting = false; callback(null, xhr); } diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js index 4ed7ec6..abeae79 100644 --- a/lib/caldav/xhr.js +++ b/lib/caldav/xhr.js @@ -118,10 +118,35 @@ } } + + var hasProgressEvents = false; + + // check for progress event support. + if ('onprogress' in xhr) { + hasProgressEvents = true; + var last = 0; + xhr.onprogress = function onProgress(event) { + var chunk = xhr.responseText.substr(last, event.loaded); + last = event.loaded; + if (this.ondata) { + this.ondata(chunk); + } + }.bind(this); + } + xhr.onreadystatechange = function onReadyStateChange() { var data; if (xhr.readyState === 4) { data = xhr.responseText; + + // emulate progress events for node... + // this really lame we should probably just + // use a real http request for node but this + // will let us do some testing via node for now. + if (!hasProgressEvents && this.ondata) { + this.ondata(data); + } + this.waiting = false; callback(null, xhr); } diff --git a/test/caldav/sax/dav_response_test.js b/test/caldav/sax/dav_response_test.js index d435ef8..3825043 100644 --- a/test/caldav/sax/dav_response_test.js +++ b/test/caldav/sax/dav_response_test.js @@ -64,8 +64,10 @@ suite('caldav/sax/dav_response', function() { var event = response['event.ics']; assert.ok(event); - assert.ok(event['calendar-data'].value.name, 'name'); - assert.ok(event['calendar-data'].value.value, 'value'); + assert.ok(event['calendar-data'], 'has calendar data'); + assert.ok( + event['calendar-data'].value instanceof Array, 'ical is parsed' + ); done(); }); diff --git a/test/caldav/xhr_test.js b/test/caldav/xhr_test.js index 0d6d7fb..fdcbe37 100644 --- a/test/caldav/xhr_test.js +++ b/test/caldav/xhr_test.js @@ -202,6 +202,49 @@ suite('webacls/xhr', function() { }); -}); + suite('requests real files', function() { + function request(path) { + path = 'fixtures/' + path; + + if (typeof(__dirname) !== 'undefined') { + path = 'file://' + __dirname + '/' + path; + } else { + path = '/test/caldav/' + path; + } + + return new Xhr({ url: path }); + } + + test('get', function(done) { + subject = request('file.txt'); + subject.send(function(err, xhr) { + var data = xhr.responseText; + assert.equal(data.trim(), 'file'); + done(); + }); + }); + + test('.ondata', function(done) { + var subject = request('long.txt'); + var gotData = ''; + subject.ondata = function(chunk) { + gotData += chunk; + }; + subject.send(function(err, xhr) { + var data = xhr.responseText; + + assert.equal( + data.trim(), + gotData.trim(), + 'sends ondata' + ); + + done(); + }); + }); + + }); + +}); -- cgit