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

Commit

Permalink
fix(copy): add support for copying Blob objects
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gkalpak committed Feb 17, 2016
1 parent 1059aff commit e9d579b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Angular.js
Expand Up @@ -913,6 +913,9 @@ function copy(source, destination) {
var re = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
re.lastIndex = source.lastIndex;
return re;

case '[object Blob]':
return new source.constructor([source], {type: source.type});
}

if (isFunction(source.cloneNode)) {
Expand Down
12 changes: 12 additions & 0 deletions test/AngularSpec.js
Expand Up @@ -228,6 +228,18 @@ describe('angular', function() {
}
});

it('should handle Blob objects', function() {
if (typeof Blob !== 'undefined') {
var src = new Blob(['foo'], {type: 'bar'});
var dst = copy(src);

expect(dst).not.toBe(src);
expect(dst.size).toBe(3);
expect(dst.type).toBe('bar');
expect(isBlob(dst)).toBe(true);
}
});

it("should throw an exception if a Uint8Array is the destination", function() {
if (typeof Uint8Array !== 'undefined') {
var src = new Uint8Array();
Expand Down
20 changes: 20 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Expand Up @@ -1011,6 +1011,26 @@ describe('ngMock', function() {
});


it('should be able to handle Blobs as mock data', function() {
if (typeof Blob !== 'undefined') {
var mockBlob = new Blob(['{"foo":"bar"}'], {type: 'application/json'});

hb.when('GET', '/url1').respond(200, mockBlob, {});

callback.andCallFake(function(status, response) {
expect(response).not.toBe(mockBlob);
expect(response.size).toBe(13);
expect(response.type).toBe('application/json');
expect(response.toString()).toBe('[object Blob]');
});

hb('GET', '/url1', null, callback);
hb.flush();
expect(callback).toHaveBeenCalledOnce();
}
});


it('should throw error when unexpected request', function() {
hb.when('GET', '/url1').respond(200, 'content');
expect(function() {
Expand Down

0 comments on commit e9d579b

Please sign in to comment.