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

Commit e9d579b

Browse files
committed
fix(copy): add support for copying Blob objects
Although `copy()` does not need to (and never will) support all kinds of objects, there is a (not uncommon) usecase for supporting `Blob` objects: `ngMock`'s `$httpBackend` will return a copy of the response data (so that changes in one test won't affect others). Since returning `Blob` objects in response to HTTP requests is a valid usecase and since `ngMocks`'s `$httpBackend` will use `copy()` to create a copy of that data, it is reasonable to support `Blob` objects. (I didn't run any benchmarks, but the additional check for the type of the copied element should have negligible impact, compared to the other stuff that `copy()` is doing.) Fixes #9669 Closes #14064
1 parent 1059aff commit e9d579b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Angular.js

+3
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,9 @@ function copy(source, destination) {
913913
var re = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
914914
re.lastIndex = source.lastIndex;
915915
return re;
916+
917+
case '[object Blob]':
918+
return new source.constructor([source], {type: source.type});
916919
}
917920

918921
if (isFunction(source.cloneNode)) {

test/AngularSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@ describe('angular', function() {
228228
}
229229
});
230230

231+
it('should handle Blob objects', function() {
232+
if (typeof Blob !== 'undefined') {
233+
var src = new Blob(['foo'], {type: 'bar'});
234+
var dst = copy(src);
235+
236+
expect(dst).not.toBe(src);
237+
expect(dst.size).toBe(3);
238+
expect(dst.type).toBe('bar');
239+
expect(isBlob(dst)).toBe(true);
240+
}
241+
});
242+
231243
it("should throw an exception if a Uint8Array is the destination", function() {
232244
if (typeof Uint8Array !== 'undefined') {
233245
var src = new Uint8Array();

test/ngMock/angular-mocksSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,26 @@ describe('ngMock', function() {
10111011
});
10121012

10131013

1014+
it('should be able to handle Blobs as mock data', function() {
1015+
if (typeof Blob !== 'undefined') {
1016+
var mockBlob = new Blob(['{"foo":"bar"}'], {type: 'application/json'});
1017+
1018+
hb.when('GET', '/url1').respond(200, mockBlob, {});
1019+
1020+
callback.andCallFake(function(status, response) {
1021+
expect(response).not.toBe(mockBlob);
1022+
expect(response.size).toBe(13);
1023+
expect(response.type).toBe('application/json');
1024+
expect(response.toString()).toBe('[object Blob]');
1025+
});
1026+
1027+
hb('GET', '/url1', null, callback);
1028+
hb.flush();
1029+
expect(callback).toHaveBeenCalledOnce();
1030+
}
1031+
});
1032+
1033+
10141034
it('should throw error when unexpected request', function() {
10151035
hb.when('GET', '/url1').respond(200, 'content');
10161036
expect(function() {

0 commit comments

Comments
 (0)