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

Commit 863a423

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 4a39ad4 commit 863a423

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Angular.js

+2
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@ function copy(source, destination) {
894894
} else if (isRegExp(source)) {
895895
destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
896896
destination.lastIndex = source.lastIndex;
897+
} else if (isBlob(source)) {
898+
destination = new source.constructor([source], {type: source.type});
897899
} else if (isFunction(source.cloneNode)) {
898900
destination = source.cloneNode(true);
899901
} else {

test/AngularSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ describe('angular', function() {
177177
}
178178
});
179179

180+
it('should handle Blob objects', function() {
181+
if (typeof Blob !== 'undefined') {
182+
var src = new Blob(['foo'], {type: 'bar'});
183+
var dst = copy(src);
184+
185+
expect(dst).not.toBe(src);
186+
expect(dst.size).toBe(3);
187+
expect(dst.type).toBe('bar');
188+
expect(isBlob(dst)).toBe(true);
189+
}
190+
});
191+
180192
it("should throw an exception if a Uint8Array is the destination", function() {
181193
if (typeof Uint8Array !== 'undefined') {
182194
var src = new Uint8Array();

test/ngMock/angular-mocksSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,26 @@ describe('ngMock', function() {
993993
});
994994

995995

996+
it('should be able to handle Blobs as mock data', function() {
997+
if (typeof Blob !== 'undefined') {
998+
var mockBlob = new Blob(['{"foo":"bar"}'], {type: 'application/json'});
999+
1000+
hb.when('GET', '/url1').respond(200, mockBlob, {});
1001+
1002+
callback.andCallFake(function(status, response) {
1003+
expect(response).not.toBe(mockBlob);
1004+
expect(response.size).toBe(13);
1005+
expect(response.type).toBe('application/json');
1006+
expect(response.toString()).toBe('[object Blob]');
1007+
});
1008+
1009+
hb('GET', '/url1', null, callback);
1010+
hb.flush();
1011+
expect(callback).toHaveBeenCalledOnce();
1012+
}
1013+
});
1014+
1015+
9961016
it('should throw error when unexpected request', function() {
9971017
hb.when('GET', '/url1').respond(200, 'content');
9981018
expect(function() {

0 commit comments

Comments
 (0)