Skip to content

Commit

Permalink
feat($http): support handling additional XHR events
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisirhc authored and petebacondarwin committed Apr 8, 2016
1 parent 451e0f6 commit 56c861c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ function $HttpProvider() {
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
* HTTP headers to send to the server. If the return value of a function is null, the
* header will not be sent. Functions accept a config object as an argument.
* - **events** - `{Object}` - Event listeners to be bound to the XMLHttpRequest object.
* To bind events to the XMLHttpRequest upload object, nest it under the upload key.
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
* - **transformRequest** –
Expand Down Expand Up @@ -1259,7 +1261,7 @@ function $HttpProvider() {
}

$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
config.withCredentials, config.responseType, config.events);
}

return promise;
Expand Down
16 changes: 15 additions & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function $HttpBackendProvider() {

function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, eventHandlers) {
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();

Expand Down Expand Up @@ -114,6 +114,20 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
xhr.onerror = requestError;
xhr.onabort = requestError;

if (eventHandlers) {
forEach(eventHandlers, function(value, key) {
if (key !== 'upload') {
xhr.addEventListener(key, value);
}
});

if (eventHandlers.upload) {
forEach(eventHandlers.upload, function(value, key) {
xhr.upload.addEventListener(key, value);
});
}
}

if (withCredentials) {
xhr.withCredentials = true;
}
Expand Down
11 changes: 11 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,17 @@ function MockXhr() {
};

this.abort = angular.noop;

this.$$events = {};
this.addEventListener = function(name, listener) {
if (angular.isUndefined(this.$$events[name])) this.$$events[name] = [];
this.$$events[name].push(listener);
};

this.upload = {
$$events: {},
addEventListener: this.addEventListener
};
}


Expand Down
15 changes: 15 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ describe('$httpBackend', function() {
});


it('should set up event listeners', function() {
var progressFn = function() {};
var uploadProgressFn = function() {};
$backend('GET', '/url', null, callback, {}, null, null, null, {
progress: progressFn,
upload: {
progress: uploadProgressFn
}
});
xhr = MockXhr.$$lastInstance;
expect(xhr.$$events.progress[0]).toBe(progressFn);
expect(xhr.upload.$$events.progress[0]).toBe(uploadProgressFn);
});


describe('responseType', function() {

it('should set responseType and return xhr.response', function() {
Expand Down

0 comments on commit 56c861c

Please sign in to comment.