Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($http): upload and download event #7995

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ function $HttpProvider() {
</example>
*/
function $http(requestConfig) {
var config = {
method: 'get',
transformRequest: defaults.transformRequest,
transformResponse: defaults.transformResponse,
progress: null
};
var headers = mergeHeaders(requestConfig);

if (!angular.isObject(requestConfig)) {
throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig);
Expand Down Expand Up @@ -1067,7 +1074,7 @@ function $HttpProvider() {
}

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

return promise;
Expand Down
9 changes: 8 additions & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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, progressCallback) {
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();

Expand Down Expand Up @@ -109,6 +109,13 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
}
}

if (typeof progressCallback === "function") {
xhr.onprogress = progressCallback;
if ("upload" in xhr) {
xhr.upload.onprogress = progressCallback;
}
}

xhr.send(post || null);
}

Expand Down
6 changes: 4 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
}

// TODO(vojta): change params to: method, url, data, headers, callback
function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) {
function $httpBackend(method, url, data, callback, headers, timeout, withCredentials,
progressCallback) {
var xhr = new MockXhr(),
expectation = expectations[0],
wasExpected = false;
Expand Down Expand Up @@ -1209,7 +1210,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
// if $browser specified, we do auto flush all requests
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
} else if (definition.passThrough) {
$delegate(method, url, data, callback, headers, timeout, withCredentials);
$delegate(method, url, data, callback, headers, timeout, withCredentials,
progressCallback);
} else throw new Error('No response defined !');
return;
}
Expand Down
19 changes: 19 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ describe('$httpBackend', function() {
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
});

describe('upload and download events', function() {

it('should call progress callback on POST requests', function() {
$backend('POST', '/whatever', null, noop, {}, null, null, 'blob', callback);

MockXhr.$$lastInstance.onprogress();

expect(callback).toHaveBeenCalled();
});

it('should call progress callback on GET requests', function() {
$backend('GET', '/whatever', null, noop, {}, null, null, 'blob', callback);

MockXhr.$$lastInstance.onprogress();

expect(callback).toHaveBeenCalled();
});
});


describe('responseType', function() {

Expand Down
4 changes: 2 additions & 2 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,10 +1741,10 @@ describe('ngMockE2E', function() {
describe('passThrough()', function() {
it('should delegate requests to the real backend when passThrough is invoked', function() {
hb.when('GET', /\/passThrough\/.*/).passThrough();
hb('GET', '/passThrough/23', null, callback, {}, null, true);
hb('GET', '/passThrough/23', null, callback, {}, null, true, null);

expect(realHttpBackend).toHaveBeenCalledOnceWith(
'GET', '/passThrough/23', null, callback, {}, null, true);
'GET', '/passThrough/23', null, callback, {}, null, true, null);
});

it('should be able to override a respond definition with passThrough', function() {
Expand Down