Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): add support for blob as a response type #10190

Merged
merged 2 commits into from Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/@angular/http/src/backends/xhr_backend.ts
Expand Up @@ -120,6 +120,9 @@ export class XHRConnection implements Connection {
case ResponseContentType.Text:
_xhr.responseType = 'text';
break;
case ResponseContentType.Blob:
_xhr.responseType = 'blob';
break;
default:
throw new Error('The selected responseType is not supported');
}
Expand Down
4 changes: 2 additions & 2 deletions modules/@angular/http/src/base_response_options.ts
Expand Up @@ -46,9 +46,9 @@ import {ResponseOptionsArgs} from './interfaces';
export class ResponseOptions {
// TODO: FormData | Blob
/**
* String, Object, ArrayBuffer representing the body of the {@link Response}.
* String, Object, ArrayBuffer or Blob representing the body of the {@link Response}.
*/
body: string|Object|ArrayBuffer;
body: string|Object|ArrayBuffer|Blob;
/**
* Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code}
* associated with the response.
Expand Down
5 changes: 3 additions & 2 deletions modules/@angular/http/src/enums.ts
Expand Up @@ -67,7 +67,8 @@ export enum ContentType {
* @experimental
*/
export enum ResponseContentType {
ArrayBuffer,
Text,
Json,
Text
ArrayBuffer,
Blob
}
3 changes: 1 addition & 2 deletions modules/@angular/http/src/interfaces.ts
Expand Up @@ -67,8 +67,7 @@ export interface RequestArgs extends RequestOptionsArgs { url: string; }
* @experimental
*/
export type ResponseOptionsArgs = {
// TODO: Support Blob, JSON
body?: string | Object | FormData | ArrayBuffer; status?: number; statusText?: string;
body?: string | Object | FormData | ArrayBuffer | Blob; status?: number; statusText?: string;
headers?: Headers;
type?: ResponseType;
url?: string;
Expand Down
23 changes: 22 additions & 1 deletion modules/@angular/http/test/backends/xhr_backend_spec.ts
Expand Up @@ -20,7 +20,7 @@ import {Headers} from '../../src/headers';
import {Map} from '../../src/facade/collection';
import {RequestOptions, BaseRequestOptions} from '../../src/base_request_options';
import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options';
import {ResponseType} from '../../src/enums';
import {ResponseType, ResponseContentType} from '../../src/enums';
import {URLSearchParams} from '../../src/url_search_params';

var abortSpy: any;
Expand Down Expand Up @@ -53,6 +53,9 @@ class MockBrowserXHR extends BrowserXhr {
this.send = sendSpy = spy.spy('send');
this.open = openSpy = spy.spy('open');
this.setRequestHeader = setRequestHeaderSpy = spy.spy('setRequestHeader');
// If responseType is supported by the browser, then it should be set to an empty string.
// (https://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute)
this.responseType = '';
}

setStatusCode(status: number) { this.status = status; }
Expand Down Expand Up @@ -652,6 +655,24 @@ export function main() {
existingXHRs[0].setStatusCode(statusCode);
existingXHRs[0].dispatchEvent('load');
}));

it('should set the responseType attribute to blob when the corresponding response content type is present',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var statusCode = 200;
var base = new BaseRequestOptions();
var connection = new XHRConnection(
new Request(
base.merge(new RequestOptions({responseType: ResponseContentType.Blob}))),
new MockBrowserXHR());

connection.response.subscribe((res: Response) => {
expect(existingXHRs[0].responseType).toBe('blob');
async.done();
});

existingXHRs[0].setStatusCode(statusCode);
existingXHRs[0].dispatchEvent('load');
}));
});
});
}
9 changes: 5 additions & 4 deletions tools/public_api_guard/http/index.d.ts
Expand Up @@ -179,14 +179,15 @@ export declare class Response extends Body {

/** @experimental */
export declare enum ResponseContentType {
ArrayBuffer = 0,
Text = 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to explicitly assign value here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is generated by the command gulp public-api:update, if the values are not explicitly assigned, then any call to gulp public-api:update will put back the values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. didn't notice it was a .d.ts file before

Json = 1,
Text = 2,
ArrayBuffer = 2,
Blob = 3,
}

/** @experimental */
export declare class ResponseOptions {
body: string | Object | ArrayBuffer;
body: string | Object | ArrayBuffer | Blob;
headers: Headers;
status: number;
url: string;
Expand All @@ -196,7 +197,7 @@ export declare class ResponseOptions {

/** @experimental */
export declare type ResponseOptionsArgs = {
body?: string | Object | FormData | ArrayBuffer;
body?: string | Object | FormData | ArrayBuffer | Blob;
status?: number;
statusText?: string;
headers?: Headers;
Expand Down