refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
14
packages/http/.babelrc
Normal file
14
packages/http/.babelrc
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"presets": ["es2015"],
|
||||
"plugins": [["transform-es2015-modules-umd", {
|
||||
"globals": {
|
||||
"@angular/core": "ng.core",
|
||||
"@angular/platform-browser": "ng.platformBrowser",
|
||||
"@angular/http": "ng.http",
|
||||
"rxjs/Observable": "Rx",
|
||||
"rxjs/Subject": "Rx"
|
||||
},
|
||||
"exactGlobals": true
|
||||
}]],
|
||||
"moduleId": "@angular/http"
|
||||
}
|
17
packages/http/.babelrc-testing
Normal file
17
packages/http/.babelrc-testing
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"presets": ["es2015"],
|
||||
"plugins": [["transform-es2015-modules-umd", {
|
||||
"globals": {
|
||||
"@angular/core": "ng.core",
|
||||
"@angular/platform-browser": "ng.platformBrowser",
|
||||
"@angular/http": "ng.http",
|
||||
"@angular/http/testing": "ng.http.testing",
|
||||
"rxjs/Observable": "Rx",
|
||||
"rxjs/ReplaySubject": "Rx",
|
||||
"rxjs/Subject": "Rx",
|
||||
"rxjs/operator/take": "Rx.Observable.prototype"
|
||||
},
|
||||
"exactGlobals": true
|
||||
}]],
|
||||
"moduleId": "@angular/http/testing"
|
||||
}
|
14
packages/http/index.ts
Normal file
14
packages/http/index.ts
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// This file is not used to build this module. It is only used during editing
|
||||
// by the TypeScript language service and during build for verification. `ngc`
|
||||
// replaces this file with production index.ts when it rewrites private symbol
|
||||
// names.
|
||||
|
||||
export * from './public_api';
|
20
packages/http/package.json
Normal file
20
packages/http/package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@angular/http",
|
||||
"version": "0.0.0-PLACEHOLDER",
|
||||
"description": "Angular - the http service",
|
||||
"main": "./bundles/http.umd.js",
|
||||
"module": "./@angular/http.es5.js",
|
||||
"es2015": "./@angular/http.js",
|
||||
"typings": "./typings/http.d.ts",
|
||||
"author": "angular",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"rxjs": "^5.0.1",
|
||||
"@angular/core": "0.0.0-PLACEHOLDER",
|
||||
"@angular/platform-browser": "0.0.0-PLACEHOLDER"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/angular/angular.git"
|
||||
}
|
||||
}
|
16
packages/http/public_api.ts
Normal file
16
packages/http/public_api.ts
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* Entry point for all public APIs of the http package.
|
||||
*/
|
||||
export * from './src/index';
|
||||
|
||||
// This file only reexports content of the `src` folder. Keep it that way.
|
56
packages/http/src/backends/browser_jsonp.ts
Normal file
56
packages/http/src/backends/browser_jsonp.ts
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
let _nextRequestId = 0;
|
||||
export const JSONP_HOME = '__ng_jsonp__';
|
||||
let _jsonpConnections: {[key: string]: any} = null;
|
||||
|
||||
function _getJsonpConnections(): {[key: string]: any} {
|
||||
const w: {[key: string]: any} = typeof window == 'object' ? window : {};
|
||||
if (_jsonpConnections === null) {
|
||||
_jsonpConnections = w[JSONP_HOME] = {};
|
||||
}
|
||||
return _jsonpConnections;
|
||||
}
|
||||
|
||||
// Make sure not to evaluate this in a non-browser environment!
|
||||
@Injectable()
|
||||
export class BrowserJsonp {
|
||||
// Construct a <script> element with the specified URL
|
||||
build(url: string): any {
|
||||
const node = document.createElement('script');
|
||||
node.src = url;
|
||||
return node;
|
||||
}
|
||||
|
||||
nextRequestID(): string { return `__req${_nextRequestId++}`; }
|
||||
|
||||
requestCallback(id: string): string { return `${JSONP_HOME}.${id}.finished`; }
|
||||
|
||||
exposeConnection(id: string, connection: any) {
|
||||
const connections = _getJsonpConnections();
|
||||
connections[id] = connection;
|
||||
}
|
||||
|
||||
removeConnection(id: string) {
|
||||
const connections = _getJsonpConnections();
|
||||
connections[id] = null;
|
||||
}
|
||||
|
||||
// Attach the <script> element to the DOM
|
||||
send(node: any) { document.body.appendChild(<Node>(node)); }
|
||||
|
||||
// Remove <script> element from the DOM
|
||||
cleanup(node: any) {
|
||||
if (node.parentNode) {
|
||||
node.parentNode.removeChild(<Node>(node));
|
||||
}
|
||||
}
|
||||
}
|
22
packages/http/src/backends/browser_xhr.ts
Normal file
22
packages/http/src/backends/browser_xhr.ts
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
/**
|
||||
* A backend for http that uses the `XMLHttpRequest` browser API.
|
||||
*
|
||||
* Take care not to evaluate this in non-browser contexts.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class BrowserXhr {
|
||||
constructor() {}
|
||||
build(): any { return <any>(new XMLHttpRequest()); }
|
||||
}
|
157
packages/http/src/backends/jsonp_backend.ts
Normal file
157
packages/http/src/backends/jsonp_backend.ts
Normal file
@ -0,0 +1,157 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {Observer} from 'rxjs/Observer';
|
||||
|
||||
import {ResponseOptions} from '../base_response_options';
|
||||
import {ReadyState, RequestMethod, ResponseType} from '../enums';
|
||||
import {Connection, ConnectionBackend} from '../interfaces';
|
||||
import {Request} from '../static_request';
|
||||
import {Response} from '../static_response';
|
||||
|
||||
import {BrowserJsonp} from './browser_jsonp';
|
||||
|
||||
const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
|
||||
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
|
||||
|
||||
/**
|
||||
* Abstract base class for an in-flight JSONP request.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export abstract class JSONPConnection implements Connection {
|
||||
/**
|
||||
* The {@link ReadyState} of this request.
|
||||
*/
|
||||
readyState: ReadyState;
|
||||
|
||||
/**
|
||||
* The outgoing HTTP request.
|
||||
*/
|
||||
request: Request;
|
||||
|
||||
/**
|
||||
* An observable that completes with the response, when the request is finished.
|
||||
*/
|
||||
response: Observable<Response>;
|
||||
|
||||
/**
|
||||
* Callback called when the JSONP request completes, to notify the application
|
||||
* of the new data.
|
||||
*/
|
||||
abstract finished(data?: any): void;
|
||||
}
|
||||
|
||||
export class JSONPConnection_ extends JSONPConnection {
|
||||
private _id: string;
|
||||
private _script: Element;
|
||||
private _responseData: any;
|
||||
private _finished: boolean = false;
|
||||
|
||||
constructor(
|
||||
req: Request, private _dom: BrowserJsonp, private baseResponseOptions?: ResponseOptions) {
|
||||
super();
|
||||
if (req.method !== RequestMethod.Get) {
|
||||
throw new TypeError(JSONP_ERR_WRONG_METHOD);
|
||||
}
|
||||
this.request = req;
|
||||
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
|
||||
|
||||
this.readyState = ReadyState.Loading;
|
||||
const id = this._id = _dom.nextRequestID();
|
||||
|
||||
_dom.exposeConnection(id, this);
|
||||
|
||||
// Workaround Dart
|
||||
// url = url.replace(/=JSONP_CALLBACK(&|$)/, `generated method`);
|
||||
const callback = _dom.requestCallback(this._id);
|
||||
let url: string = req.url;
|
||||
if (url.indexOf('=JSONP_CALLBACK&') > -1) {
|
||||
url = url.replace('=JSONP_CALLBACK&', `=${callback}&`);
|
||||
} else if (url.lastIndexOf('=JSONP_CALLBACK') === url.length - '=JSONP_CALLBACK'.length) {
|
||||
url = url.substring(0, url.length - '=JSONP_CALLBACK'.length) + `=${callback}`;
|
||||
}
|
||||
|
||||
const script = this._script = _dom.build(url);
|
||||
|
||||
const onLoad = (event: Event) => {
|
||||
if (this.readyState === ReadyState.Cancelled) return;
|
||||
this.readyState = ReadyState.Done;
|
||||
_dom.cleanup(script);
|
||||
if (!this._finished) {
|
||||
let responseOptions =
|
||||
new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url});
|
||||
if (baseResponseOptions) {
|
||||
responseOptions = baseResponseOptions.merge(responseOptions);
|
||||
}
|
||||
responseObserver.error(new Response(responseOptions));
|
||||
return;
|
||||
}
|
||||
|
||||
let responseOptions = new ResponseOptions({body: this._responseData, url});
|
||||
if (this.baseResponseOptions) {
|
||||
responseOptions = this.baseResponseOptions.merge(responseOptions);
|
||||
}
|
||||
|
||||
responseObserver.next(new Response(responseOptions));
|
||||
responseObserver.complete();
|
||||
};
|
||||
|
||||
const onError = (error: Error) => {
|
||||
if (this.readyState === ReadyState.Cancelled) return;
|
||||
this.readyState = ReadyState.Done;
|
||||
_dom.cleanup(script);
|
||||
let responseOptions = new ResponseOptions({body: error.message, type: ResponseType.Error});
|
||||
if (baseResponseOptions) {
|
||||
responseOptions = baseResponseOptions.merge(responseOptions);
|
||||
}
|
||||
responseObserver.error(new Response(responseOptions));
|
||||
};
|
||||
|
||||
script.addEventListener('load', onLoad);
|
||||
script.addEventListener('error', onError);
|
||||
|
||||
_dom.send(script);
|
||||
|
||||
return () => {
|
||||
this.readyState = ReadyState.Cancelled;
|
||||
script.removeEventListener('load', onLoad);
|
||||
script.removeEventListener('error', onError);
|
||||
this._dom.cleanup(script);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
finished(data?: any) {
|
||||
// Don't leak connections
|
||||
this._finished = true;
|
||||
this._dom.removeConnection(this._id);
|
||||
if (this.readyState === ReadyState.Cancelled) return;
|
||||
this._responseData = data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link ConnectionBackend} that uses the JSONP strategy of making requests.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export abstract class JSONPBackend extends ConnectionBackend {}
|
||||
|
||||
@Injectable()
|
||||
export class JSONPBackend_ extends JSONPBackend {
|
||||
constructor(private _browserJSONP: BrowserJsonp, private _baseResponseOptions: ResponseOptions) {
|
||||
super();
|
||||
}
|
||||
|
||||
createConnection(request: Request): JSONPConnection {
|
||||
return new JSONPConnection_(request, this._browserJSONP, this._baseResponseOptions);
|
||||
}
|
||||
}
|
241
packages/http/src/backends/xhr_backend.ts
Normal file
241
packages/http/src/backends/xhr_backend.ts
Normal file
@ -0,0 +1,241 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
import {ɵgetDOM as getDOM} from '@angular/platform-browser';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {Observer} from 'rxjs/Observer';
|
||||
import {ResponseOptions} from '../base_response_options';
|
||||
import {ContentType, ReadyState, RequestMethod, ResponseContentType, ResponseType} from '../enums';
|
||||
import {Headers} from '../headers';
|
||||
import {getResponseURL, isSuccess} from '../http_utils';
|
||||
import {Connection, ConnectionBackend, XSRFStrategy} from '../interfaces';
|
||||
import {Request} from '../static_request';
|
||||
import {Response} from '../static_response';
|
||||
import {BrowserXhr} from './browser_xhr';
|
||||
|
||||
const XSSI_PREFIX = /^\)\]\}',?\n/;
|
||||
|
||||
/**
|
||||
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
|
||||
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
|
||||
* request.
|
||||
*
|
||||
* This class would typically not be created or interacted with directly inside applications, though
|
||||
* the {@link MockConnection} may be interacted with in tests.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class XHRConnection implements Connection {
|
||||
request: Request;
|
||||
/**
|
||||
* Response {@link EventEmitter} which emits a single {@link Response} value on load event of
|
||||
* `XMLHttpRequest`.
|
||||
*/
|
||||
response: Observable<Response>;
|
||||
readyState: ReadyState;
|
||||
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
|
||||
this.request = req;
|
||||
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
|
||||
const _xhr: XMLHttpRequest = browserXHR.build();
|
||||
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
|
||||
if (req.withCredentials != null) {
|
||||
_xhr.withCredentials = req.withCredentials;
|
||||
}
|
||||
// load event handler
|
||||
const onLoad = () => {
|
||||
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
||||
let status: number = _xhr.status === 1223 ? 204 : _xhr.status;
|
||||
|
||||
let body: any = null;
|
||||
|
||||
// HTTP 204 means no content
|
||||
if (status !== 204) {
|
||||
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
||||
// response/responseType properties were introduced in ResourceLoader Level2 spec
|
||||
// (supported by IE10)
|
||||
body = (typeof _xhr.response === 'undefined') ? _xhr.responseText : _xhr.response;
|
||||
|
||||
// Implicitly strip a potential XSSI prefix.
|
||||
if (typeof body === 'string') {
|
||||
body = body.replace(XSSI_PREFIX, '');
|
||||
}
|
||||
}
|
||||
|
||||
// fix status code when it is 0 (0 status is undocumented).
|
||||
// Occurs when accessing file resources or on Android 4.1 stock browser
|
||||
// while retrieving files from application cache.
|
||||
if (status === 0) {
|
||||
status = body ? 200 : 0;
|
||||
}
|
||||
|
||||
const headers: Headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());
|
||||
// IE 9 does not provide the way to get URL of response
|
||||
const url = getResponseURL(_xhr) || req.url;
|
||||
const statusText: string = _xhr.statusText || 'OK';
|
||||
|
||||
let responseOptions = new ResponseOptions({body, status, headers, statusText, url});
|
||||
if (baseResponseOptions != null) {
|
||||
responseOptions = baseResponseOptions.merge(responseOptions);
|
||||
}
|
||||
const response = new Response(responseOptions);
|
||||
response.ok = isSuccess(status);
|
||||
if (response.ok) {
|
||||
responseObserver.next(response);
|
||||
// TODO(gdi2290): defer complete if array buffer until done
|
||||
responseObserver.complete();
|
||||
return;
|
||||
}
|
||||
responseObserver.error(response);
|
||||
};
|
||||
// error event handler
|
||||
const onError = (err: ErrorEvent) => {
|
||||
let responseOptions = new ResponseOptions({
|
||||
body: err,
|
||||
type: ResponseType.Error,
|
||||
status: _xhr.status,
|
||||
statusText: _xhr.statusText,
|
||||
});
|
||||
if (baseResponseOptions != null) {
|
||||
responseOptions = baseResponseOptions.merge(responseOptions);
|
||||
}
|
||||
responseObserver.error(new Response(responseOptions));
|
||||
};
|
||||
|
||||
this.setDetectedContentType(req, _xhr);
|
||||
|
||||
if (req.headers == null) {
|
||||
req.headers = new Headers();
|
||||
}
|
||||
if (!req.headers.has('Accept')) {
|
||||
req.headers.append('Accept', 'application/json, text/plain, */*');
|
||||
}
|
||||
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
|
||||
|
||||
// Select the correct buffer type to store the response
|
||||
if (req.responseType != null && _xhr.responseType != null) {
|
||||
switch (req.responseType) {
|
||||
case ResponseContentType.ArrayBuffer:
|
||||
_xhr.responseType = 'arraybuffer';
|
||||
break;
|
||||
case ResponseContentType.Json:
|
||||
_xhr.responseType = 'json';
|
||||
break;
|
||||
case ResponseContentType.Text:
|
||||
_xhr.responseType = 'text';
|
||||
break;
|
||||
case ResponseContentType.Blob:
|
||||
_xhr.responseType = 'blob';
|
||||
break;
|
||||
default:
|
||||
throw new Error('The selected responseType is not supported');
|
||||
}
|
||||
}
|
||||
|
||||
_xhr.addEventListener('load', onLoad);
|
||||
_xhr.addEventListener('error', onError);
|
||||
|
||||
_xhr.send(this.request.getBody());
|
||||
|
||||
return () => {
|
||||
_xhr.removeEventListener('load', onLoad);
|
||||
_xhr.removeEventListener('error', onError);
|
||||
_xhr.abort();
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
setDetectedContentType(req: any /** TODO Request */, _xhr: any /** XMLHttpRequest */) {
|
||||
// Skip if a custom Content-Type header is provided
|
||||
if (req.headers != null && req.headers.get('Content-Type') != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the detected content type
|
||||
switch (req.contentType) {
|
||||
case ContentType.NONE:
|
||||
break;
|
||||
case ContentType.JSON:
|
||||
_xhr.setRequestHeader('content-type', 'application/json');
|
||||
break;
|
||||
case ContentType.FORM:
|
||||
_xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
|
||||
break;
|
||||
case ContentType.TEXT:
|
||||
_xhr.setRequestHeader('content-type', 'text/plain');
|
||||
break;
|
||||
case ContentType.BLOB:
|
||||
const blob = req.blob();
|
||||
if (blob.type) {
|
||||
_xhr.setRequestHeader('content-type', blob.type);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `XSRFConfiguration` sets up Cross Site Request Forgery (XSRF) protection for the application
|
||||
* using a cookie. See https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
|
||||
* for more information on XSRF.
|
||||
*
|
||||
* Applications can configure custom cookie and header names by binding an instance of this class
|
||||
* with different `cookieName` and `headerName` values. See the main HTTP documentation for more
|
||||
* details.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class CookieXSRFStrategy implements XSRFStrategy {
|
||||
constructor(
|
||||
private _cookieName: string = 'XSRF-TOKEN', private _headerName: string = 'X-XSRF-TOKEN') {}
|
||||
|
||||
configureRequest(req: Request): void {
|
||||
const xsrfToken = getDOM().getCookie(this._cookieName);
|
||||
if (xsrfToken) {
|
||||
req.headers.set(this._headerName, xsrfToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link XHRConnection} instances.
|
||||
*
|
||||
* This class would typically not be used by end users, but could be
|
||||
* overridden if a different backend implementation should be used,
|
||||
* such as in a node backend.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {Http, MyNodeBackend, HTTP_PROVIDERS, BaseRequestOptions} from '@angular/http';
|
||||
* @Component({
|
||||
* viewProviders: [
|
||||
* HTTP_PROVIDERS,
|
||||
* {provide: Http, useFactory: (backend, options) => {
|
||||
* return new Http(backend, options);
|
||||
* }, deps: [MyNodeBackend, BaseRequestOptions]}]
|
||||
* })
|
||||
* class MyComponent {
|
||||
* constructor(http:Http) {
|
||||
* http.request('people.json').subscribe(res => this.people = res.json());
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class XHRBackend implements ConnectionBackend {
|
||||
constructor(
|
||||
private _browserXHR: BrowserXhr, private _baseResponseOptions: ResponseOptions,
|
||||
private _xsrfStrategy: XSRFStrategy) {}
|
||||
|
||||
createConnection(request: Request): XHRConnection {
|
||||
this._xsrfStrategy.configureRequest(request);
|
||||
return new XHRConnection(request, this._browserXHR, this._baseResponseOptions);
|
||||
}
|
||||
}
|
220
packages/http/src/base_request_options.ts
Normal file
220
packages/http/src/base_request_options.ts
Normal file
@ -0,0 +1,220 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
import {RequestMethod, ResponseContentType} from './enums';
|
||||
import {Headers} from './headers';
|
||||
import {normalizeMethodName} from './http_utils';
|
||||
import {RequestOptionsArgs} from './interfaces';
|
||||
import {URLSearchParams} from './url_search_params';
|
||||
|
||||
|
||||
/**
|
||||
* Creates a request options object to be optionally provided when instantiating a
|
||||
* {@link Request}.
|
||||
*
|
||||
* This class is based on the `RequestInit` description in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#requestinit).
|
||||
*
|
||||
* All values are null by default. Typical defaults can be found in the {@link BaseRequestOptions}
|
||||
* class, which sub-classes `RequestOptions`.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/7Wvi3lfLq41aQPKlxB4O?p=preview))
|
||||
*
|
||||
* ```typescript
|
||||
* import {RequestOptions, Request, RequestMethod} from '@angular/http';
|
||||
*
|
||||
* var options = new RequestOptions({
|
||||
* method: RequestMethod.Post,
|
||||
* url: 'https://google.com'
|
||||
* });
|
||||
* var req = new Request(options);
|
||||
* console.log('req.method:', RequestMethod[req.method]); // Post
|
||||
* console.log('options.url:', options.url); // https://google.com
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class RequestOptions {
|
||||
/**
|
||||
* Http method with which to execute a {@link Request}.
|
||||
* Acceptable methods are defined in the {@link RequestMethod} enum.
|
||||
*/
|
||||
method: RequestMethod|string;
|
||||
/**
|
||||
* {@link Headers} to be attached to a {@link Request}.
|
||||
*/
|
||||
headers: Headers;
|
||||
/**
|
||||
* Body to be used when creating a {@link Request}.
|
||||
*/
|
||||
body: any;
|
||||
/**
|
||||
* Url with which to perform a {@link Request}.
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* Search parameters to be included in a {@link Request}.
|
||||
*/
|
||||
params: URLSearchParams;
|
||||
/**
|
||||
* @deprecated from 4.0.0. Use params instead.
|
||||
*/
|
||||
get search(): URLSearchParams { return this.params; }
|
||||
/**
|
||||
* @deprecated from 4.0.0. Use params instead.
|
||||
*/
|
||||
set search(params: URLSearchParams) { this.params = params; }
|
||||
/**
|
||||
* Enable use credentials for a {@link Request}.
|
||||
*/
|
||||
withCredentials: boolean;
|
||||
/*
|
||||
* Select a buffer to store the response, such as ArrayBuffer, Blob, Json (or Document)
|
||||
*/
|
||||
responseType: ResponseContentType;
|
||||
|
||||
// TODO(Dzmitry): remove search when this.search is removed
|
||||
constructor(
|
||||
{method, headers, body, url, search, params, withCredentials,
|
||||
responseType}: RequestOptionsArgs = {}) {
|
||||
this.method = method != null ? normalizeMethodName(method) : null;
|
||||
this.headers = headers != null ? headers : null;
|
||||
this.body = body != null ? body : null;
|
||||
this.url = url != null ? url : null;
|
||||
this.params = this._mergeSearchParams(params || search);
|
||||
this.withCredentials = withCredentials != null ? withCredentials : null;
|
||||
this.responseType = responseType != null ? responseType : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the `RequestOptions` instance, using the optional input as values to override
|
||||
* existing values. This method will not change the values of the instance on which it is being
|
||||
* called.
|
||||
*
|
||||
* Note that `headers` and `search` will override existing values completely if present in
|
||||
* the `options` object. If these values should be merged, it should be done prior to calling
|
||||
* `merge` on the `RequestOptions` instance.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/6w8XA8YTkDRcPYpdB9dk?p=preview))
|
||||
*
|
||||
* ```typescript
|
||||
* import {RequestOptions, Request, RequestMethod} from '@angular/http';
|
||||
*
|
||||
* var options = new RequestOptions({
|
||||
* method: RequestMethod.Post
|
||||
* });
|
||||
* var req = new Request(options.merge({
|
||||
* url: 'https://google.com'
|
||||
* }));
|
||||
* console.log('req.method:', RequestMethod[req.method]); // Post
|
||||
* console.log('options.url:', options.url); // null
|
||||
* console.log('req.url:', req.url); // https://google.com
|
||||
* ```
|
||||
*/
|
||||
merge(options?: RequestOptionsArgs): RequestOptions {
|
||||
return new RequestOptions({
|
||||
method: options && options.method != null ? options.method : this.method,
|
||||
headers: options && options.headers != null ? options.headers : new Headers(this.headers),
|
||||
body: options && options.body != null ? options.body : this.body,
|
||||
url: options && options.url != null ? options.url : this.url,
|
||||
params: options && this._mergeSearchParams(options.params || options.search),
|
||||
withCredentials: options && options.withCredentials != null ? options.withCredentials :
|
||||
this.withCredentials,
|
||||
responseType: options && options.responseType != null ? options.responseType :
|
||||
this.responseType
|
||||
});
|
||||
}
|
||||
|
||||
private _mergeSearchParams(params: string|URLSearchParams|
|
||||
{[key: string]: any | any[]}): URLSearchParams {
|
||||
if (!params) return this.params;
|
||||
|
||||
if (params instanceof URLSearchParams) {
|
||||
return params.clone();
|
||||
}
|
||||
|
||||
if (typeof params === 'string') {
|
||||
return new URLSearchParams(params);
|
||||
}
|
||||
|
||||
return this._parseParams(params);
|
||||
}
|
||||
|
||||
private _parseParams(objParams: {[key: string]: any | any[]} = {}): URLSearchParams {
|
||||
const params = new URLSearchParams();
|
||||
Object.keys(objParams).forEach((key: string) => {
|
||||
const value: any|any[] = objParams[key];
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item: any) => this._appendParam(key, item, params));
|
||||
} else {
|
||||
this._appendParam(key, value, params);
|
||||
}
|
||||
});
|
||||
return params;
|
||||
}
|
||||
|
||||
private _appendParam(key: string, value: any, params: URLSearchParams): void {
|
||||
if (typeof value !== 'string') {
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
params.append(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclass of {@link RequestOptions}, with default values.
|
||||
*
|
||||
* Default values:
|
||||
* * method: {@link RequestMethod RequestMethod.Get}
|
||||
* * headers: empty {@link Headers} object
|
||||
*
|
||||
* This class could be extended and bound to the {@link RequestOptions} class
|
||||
* when configuring an {@link Injector}, in order to override the default options
|
||||
* used by {@link Http} to create and send {@link Request Requests}.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/LEKVSx?p=preview))
|
||||
*
|
||||
* ```typescript
|
||||
* import {provide} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {HTTP_PROVIDERS, Http, BaseRequestOptions, RequestOptions} from '@angular/http';
|
||||
* import {App} from './myapp';
|
||||
*
|
||||
* class MyOptions extends BaseRequestOptions {
|
||||
* search: string = 'coreTeam=true';
|
||||
* }
|
||||
*
|
||||
* bootstrap(App, [HTTP_PROVIDERS, {provide: RequestOptions, useClass: MyOptions}]);
|
||||
* ```
|
||||
*
|
||||
* The options could also be extended when manually creating a {@link Request}
|
||||
* object.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/oyBoEvNtDhOSfi9YxaVb?p=preview))
|
||||
*
|
||||
* ```
|
||||
* import {BaseRequestOptions, Request, RequestMethod} from '@angular/http';
|
||||
*
|
||||
* var options = new BaseRequestOptions();
|
||||
* var req = new Request(options.merge({
|
||||
* method: RequestMethod.Post,
|
||||
* url: 'https://google.com'
|
||||
* }));
|
||||
* console.log('req.method:', RequestMethod[req.method]); // Post
|
||||
* console.log('options.url:', options.url); // null
|
||||
* console.log('req.url:', req.url); // https://google.com
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class BaseRequestOptions extends RequestOptions {
|
||||
constructor() { super({method: RequestMethod.Get, headers: new Headers()}); }
|
||||
}
|
165
packages/http/src/base_response_options.ts
Normal file
165
packages/http/src/base_response_options.ts
Normal file
@ -0,0 +1,165 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
import {ResponseType} from './enums';
|
||||
import {Headers} from './headers';
|
||||
import {ResponseOptionsArgs} from './interfaces';
|
||||
|
||||
|
||||
/**
|
||||
* Creates a response options object to be optionally provided when instantiating a
|
||||
* {@link Response}.
|
||||
*
|
||||
* This class is based on the `ResponseInit` description in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#responseinit).
|
||||
*
|
||||
* All values are null by default. Typical defaults can be found in the
|
||||
* {@link BaseResponseOptions} class, which sub-classes `ResponseOptions`.
|
||||
*
|
||||
* This class may be used in tests to build {@link Response Responses} for
|
||||
* mock responses (see {@link MockBackend}).
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/P9Jkk8e8cz6NVzbcxEsD?p=preview))
|
||||
*
|
||||
* ```typescript
|
||||
* import {ResponseOptions, Response} from '@angular/http';
|
||||
*
|
||||
* var options = new ResponseOptions({
|
||||
* body: '{"name":"Jeff"}'
|
||||
* });
|
||||
* var res = new Response(options);
|
||||
*
|
||||
* console.log('res.json():', res.json()); // Object {name: "Jeff"}
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class ResponseOptions {
|
||||
// TODO: FormData | Blob
|
||||
/**
|
||||
* String, Object, ArrayBuffer or Blob representing the body of the {@link Response}.
|
||||
*/
|
||||
body: string|Object|ArrayBuffer|Blob;
|
||||
/**
|
||||
* Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code}
|
||||
* associated with the response.
|
||||
*/
|
||||
status: number;
|
||||
/**
|
||||
* Response {@link Headers headers}
|
||||
*/
|
||||
headers: Headers;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
statusText: string;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
type: ResponseType;
|
||||
url: string;
|
||||
constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) {
|
||||
this.body = body != null ? body : null;
|
||||
this.status = status != null ? status : null;
|
||||
this.headers = headers != null ? headers : null;
|
||||
this.statusText = statusText != null ? statusText : null;
|
||||
this.type = type != null ? type : null;
|
||||
this.url = url != null ? url : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the `ResponseOptions` instance, using the optional input as values to
|
||||
* override
|
||||
* existing values. This method will not change the values of the instance on which it is being
|
||||
* called.
|
||||
*
|
||||
* This may be useful when sharing a base `ResponseOptions` object inside tests,
|
||||
* where certain properties may change from test to test.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/1lXquqFfgduTFBWjNoRE?p=preview))
|
||||
*
|
||||
* ```typescript
|
||||
* import {ResponseOptions, Response} from '@angular/http';
|
||||
*
|
||||
* var options = new ResponseOptions({
|
||||
* body: {name: 'Jeff'}
|
||||
* });
|
||||
* var res = new Response(options.merge({
|
||||
* url: 'https://google.com'
|
||||
* }));
|
||||
* console.log('options.url:', options.url); // null
|
||||
* console.log('res.json():', res.json()); // Object {name: "Jeff"}
|
||||
* console.log('res.url:', res.url); // https://google.com
|
||||
* ```
|
||||
*/
|
||||
merge(options?: ResponseOptionsArgs): ResponseOptions {
|
||||
return new ResponseOptions({
|
||||
body: options && options.body != null ? options.body : this.body,
|
||||
status: options && options.status != null ? options.status : this.status,
|
||||
headers: options && options.headers != null ? options.headers : this.headers,
|
||||
statusText: options && options.statusText != null ? options.statusText : this.statusText,
|
||||
type: options && options.type != null ? options.type : this.type,
|
||||
url: options && options.url != null ? options.url : this.url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclass of {@link ResponseOptions}, with default values.
|
||||
*
|
||||
* Default values:
|
||||
* * status: 200
|
||||
* * headers: empty {@link Headers} object
|
||||
*
|
||||
* This class could be extended and bound to the {@link ResponseOptions} class
|
||||
* when configuring an {@link Injector}, in order to override the default options
|
||||
* used by {@link Http} to create {@link Response Responses}.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/qv8DLT?p=preview))
|
||||
*
|
||||
* ```typescript
|
||||
* import {provide} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {HTTP_PROVIDERS, Headers, Http, BaseResponseOptions, ResponseOptions} from
|
||||
* '@angular/http';
|
||||
* import {App} from './myapp';
|
||||
*
|
||||
* class MyOptions extends BaseResponseOptions {
|
||||
* headers:Headers = new Headers({network: 'github'});
|
||||
* }
|
||||
*
|
||||
* bootstrap(App, [HTTP_PROVIDERS, {provide: ResponseOptions, useClass: MyOptions}]);
|
||||
* ```
|
||||
*
|
||||
* The options could also be extended when manually creating a {@link Response}
|
||||
* object.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/VngosOWiaExEtbstDoix?p=preview))
|
||||
*
|
||||
* ```
|
||||
* import {BaseResponseOptions, Response} from '@angular/http';
|
||||
*
|
||||
* var options = new BaseResponseOptions();
|
||||
* var res = new Response(options.merge({
|
||||
* body: 'Angular',
|
||||
* headers: new Headers({framework: 'angular'})
|
||||
* }));
|
||||
* console.log('res.headers.get("framework"):', res.headers.get('framework')); // angular
|
||||
* console.log('res.text():', res.text()); // Angular;
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class BaseResponseOptions extends ResponseOptions {
|
||||
constructor() {
|
||||
super({status: 200, statusText: 'Ok', type: ResponseType.Default, headers: new Headers()});
|
||||
}
|
||||
}
|
86
packages/http/src/body.ts
Normal file
86
packages/http/src/body.ts
Normal file
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {stringToArrayBuffer} from './http_utils';
|
||||
import {URLSearchParams} from './url_search_params';
|
||||
|
||||
|
||||
/**
|
||||
* HTTP request body used by both {@link Request} and {@link Response}
|
||||
* https://fetch.spec.whatwg.org/#body
|
||||
*/
|
||||
export abstract class Body {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected _body: any;
|
||||
|
||||
/**
|
||||
* Attempts to return body as parsed `JSON` object, or raises an exception.
|
||||
*/
|
||||
json(): any {
|
||||
if (typeof this._body === 'string') {
|
||||
return JSON.parse(<string>this._body);
|
||||
}
|
||||
|
||||
if (this._body instanceof ArrayBuffer) {
|
||||
return JSON.parse(this.text());
|
||||
}
|
||||
|
||||
return this._body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the body as a string, presuming `toString()` can be called on the response body.
|
||||
*/
|
||||
text(): string {
|
||||
if (this._body instanceof URLSearchParams) {
|
||||
return this._body.toString();
|
||||
}
|
||||
|
||||
if (this._body instanceof ArrayBuffer) {
|
||||
return String.fromCharCode.apply(null, new Uint16Array(<ArrayBuffer>this._body));
|
||||
}
|
||||
|
||||
if (this._body == null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof this._body === 'object') {
|
||||
return JSON.stringify(this._body, null, 2);
|
||||
}
|
||||
|
||||
return this._body.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the body as an ArrayBuffer
|
||||
*/
|
||||
arrayBuffer(): ArrayBuffer {
|
||||
if (this._body instanceof ArrayBuffer) {
|
||||
return <ArrayBuffer>this._body;
|
||||
}
|
||||
|
||||
return stringToArrayBuffer(this.text());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request's body as a Blob, assuming that body exists.
|
||||
*/
|
||||
blob(): Blob {
|
||||
if (this._body instanceof Blob) {
|
||||
return <Blob>this._body;
|
||||
}
|
||||
|
||||
if (this._body instanceof ArrayBuffer) {
|
||||
return new Blob([this._body]);
|
||||
}
|
||||
|
||||
throw new Error('The request body isn\'t either a blob or an array buffer');
|
||||
}
|
||||
}
|
74
packages/http/src/enums.ts
Normal file
74
packages/http/src/enums.ts
Normal file
@ -0,0 +1,74 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* Supported http methods.
|
||||
* @experimental
|
||||
*/
|
||||
export enum RequestMethod {
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Options,
|
||||
Head,
|
||||
Patch
|
||||
}
|
||||
|
||||
/**
|
||||
* All possible states in which a connection can be, based on
|
||||
* [States](http://www.w3.org/TR/XMLHttpRequest/#states) from the `XMLHttpRequest` spec, but with an
|
||||
* additional "CANCELLED" state.
|
||||
* @experimental
|
||||
*/
|
||||
export enum ReadyState {
|
||||
Unsent,
|
||||
Open,
|
||||
HeadersReceived,
|
||||
Loading,
|
||||
Done,
|
||||
Cancelled
|
||||
}
|
||||
|
||||
/**
|
||||
* Acceptable response types to be associated with a {@link Response}, based on
|
||||
* [ResponseType](https://fetch.spec.whatwg.org/#responsetype) from the Fetch spec.
|
||||
* @experimental
|
||||
*/
|
||||
export enum ResponseType {
|
||||
Basic,
|
||||
Cors,
|
||||
Default,
|
||||
Error,
|
||||
Opaque
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported content type to be automatically associated with a {@link Request}.
|
||||
* @experimental
|
||||
*/
|
||||
export enum ContentType {
|
||||
NONE,
|
||||
JSON,
|
||||
FORM,
|
||||
FORM_DATA,
|
||||
TEXT,
|
||||
BLOB,
|
||||
ARRAY_BUFFER
|
||||
}
|
||||
|
||||
/**
|
||||
* Define which buffer to use to store the response
|
||||
* @experimental
|
||||
*/
|
||||
export enum ResponseContentType {
|
||||
Text,
|
||||
Json,
|
||||
ArrayBuffer,
|
||||
Blob
|
||||
}
|
185
packages/http/src/headers.ts
Normal file
185
packages/http/src/headers.ts
Normal file
@ -0,0 +1,185 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as
|
||||
* specified in the [Fetch Spec](https://fetch.spec.whatwg.org/#headers-class).
|
||||
*
|
||||
* The only known difference between this `Headers` implementation and the spec is the
|
||||
* lack of an `entries` method.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {Headers} from '@angular/http';
|
||||
*
|
||||
* var firstHeaders = new Headers();
|
||||
* firstHeaders.append('Content-Type', 'image/jpeg');
|
||||
* console.log(firstHeaders.get('Content-Type')) //'image/jpeg'
|
||||
*
|
||||
* // Create headers from Plain Old JavaScript Object
|
||||
* var secondHeaders = new Headers({
|
||||
* 'X-My-Custom-Header': 'Angular'
|
||||
* });
|
||||
* console.log(secondHeaders.get('X-My-Custom-Header')); //'Angular'
|
||||
*
|
||||
* var thirdHeaders = new Headers(secondHeaders);
|
||||
* console.log(thirdHeaders.get('X-My-Custom-Header')); //'Angular'
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class Headers {
|
||||
/** @internal header names are lower case */
|
||||
_headers: Map<string, string[]> = new Map();
|
||||
/** @internal map lower case names to actual names */
|
||||
_normalizedNames: Map<string, string> = new Map();
|
||||
|
||||
// TODO(vicb): any -> string|string[]
|
||||
constructor(headers?: Headers|{[name: string]: any}) {
|
||||
if (!headers) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (headers instanceof Headers) {
|
||||
headers.forEach((values: string[], name: string) => {
|
||||
values.forEach(value => this.append(name, value));
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Object.keys(headers).forEach((name: string) => {
|
||||
const values: string[] = Array.isArray(headers[name]) ? headers[name] : [headers[name]];
|
||||
this.delete(name);
|
||||
values.forEach(value => this.append(name, value));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Headers instance from the given DOMString of Response Headers
|
||||
*/
|
||||
static fromResponseHeaderString(headersString: string): Headers {
|
||||
const headers = new Headers();
|
||||
|
||||
headersString.split('\n').forEach(line => {
|
||||
const index = line.indexOf(':');
|
||||
if (index > 0) {
|
||||
const name = line.slice(0, index);
|
||||
const value = line.slice(index + 1).trim();
|
||||
headers.set(name, value);
|
||||
}
|
||||
});
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a header to existing list of header values for a given header name.
|
||||
*/
|
||||
append(name: string, value: string): void {
|
||||
const values = this.getAll(name);
|
||||
|
||||
if (values === null) {
|
||||
this.set(name, value);
|
||||
} else {
|
||||
values.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all header values for the given name.
|
||||
*/
|
||||
delete (name: string): void {
|
||||
const lcName = name.toLowerCase();
|
||||
this._normalizedNames.delete(lcName);
|
||||
this._headers.delete(lcName);
|
||||
}
|
||||
|
||||
forEach(fn: (values: string[], name: string, headers: Map<string, string[]>) => void): void {
|
||||
this._headers.forEach(
|
||||
(values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first header that matches given name.
|
||||
*/
|
||||
get(name: string): string {
|
||||
const values = this.getAll(name);
|
||||
|
||||
if (values === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return values.length > 0 ? values[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for existence of header by given name.
|
||||
*/
|
||||
has(name: string): boolean { return this._headers.has(name.toLowerCase()); }
|
||||
|
||||
/**
|
||||
* Returns the names of the headers
|
||||
*/
|
||||
keys(): string[] { return Array.from(this._normalizedNames.values()); }
|
||||
|
||||
/**
|
||||
* Sets or overrides header value for given name.
|
||||
*/
|
||||
set(name: string, value: string|string[]): void {
|
||||
if (Array.isArray(value)) {
|
||||
if (value.length) {
|
||||
this._headers.set(name.toLowerCase(), [value.join(',')]);
|
||||
}
|
||||
} else {
|
||||
this._headers.set(name.toLowerCase(), [value]);
|
||||
}
|
||||
this.mayBeSetNormalizedName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns values of all headers.
|
||||
*/
|
||||
values(): string[][] { return Array.from(this._headers.values()); }
|
||||
|
||||
/**
|
||||
* Returns string of all headers.
|
||||
*/
|
||||
// TODO(vicb): returns {[name: string]: string[]}
|
||||
toJSON(): {[name: string]: any} {
|
||||
const serialized: {[name: string]: string[]} = {};
|
||||
|
||||
this._headers.forEach((values: string[], name: string) => {
|
||||
const split: string[] = [];
|
||||
values.forEach(v => split.push(...v.split(',')));
|
||||
serialized[this._normalizedNames.get(name)] = split;
|
||||
});
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns list of header values for a given name.
|
||||
*/
|
||||
getAll(name: string): string[] {
|
||||
return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is not implemented.
|
||||
*/
|
||||
entries() { throw new Error('"entries" method is not implemented on Headers class'); }
|
||||
|
||||
private mayBeSetNormalizedName(name: string): void {
|
||||
const lcName = name.toLowerCase();
|
||||
|
||||
if (!this._normalizedNames.has(lcName)) {
|
||||
this._normalizedNames.set(lcName, name);
|
||||
}
|
||||
}
|
||||
}
|
226
packages/http/src/http.ts
Normal file
226
packages/http/src/http.ts
Normal file
@ -0,0 +1,226 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {BaseRequestOptions, RequestOptions} from './base_request_options';
|
||||
import {RequestMethod} from './enums';
|
||||
import {ConnectionBackend, RequestOptionsArgs} from './interfaces';
|
||||
import {Request} from './static_request';
|
||||
import {Response} from './static_response';
|
||||
|
||||
function httpRequest(backend: ConnectionBackend, request: Request): Observable<Response> {
|
||||
return backend.createConnection(request).response;
|
||||
}
|
||||
|
||||
function mergeOptions(
|
||||
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod,
|
||||
url: string): RequestOptions {
|
||||
const newOptions = defaultOpts;
|
||||
if (providedOpts) {
|
||||
// Hack so Dart can used named parameters
|
||||
return newOptions.merge(new RequestOptions({
|
||||
method: providedOpts.method || method,
|
||||
url: providedOpts.url || url,
|
||||
search: providedOpts.search,
|
||||
params: providedOpts.params,
|
||||
headers: providedOpts.headers,
|
||||
body: providedOpts.body,
|
||||
withCredentials: providedOpts.withCredentials,
|
||||
responseType: providedOpts.responseType
|
||||
}));
|
||||
}
|
||||
|
||||
return newOptions.merge(new RequestOptions({method, url}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs http requests using `XMLHttpRequest` as the default backend.
|
||||
*
|
||||
* `Http` is available as an injectable class, with methods to perform http requests. Calling
|
||||
* `request` returns an `Observable` which will emit a single {@link Response} when a
|
||||
* response is received.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```typescript
|
||||
* import {Http, HTTP_PROVIDERS} from '@angular/http';
|
||||
* import 'rxjs/add/operator/map'
|
||||
* @Component({
|
||||
* selector: 'http-app',
|
||||
* viewProviders: [HTTP_PROVIDERS],
|
||||
* templateUrl: 'people.html'
|
||||
* })
|
||||
* class PeopleComponent {
|
||||
* constructor(http: Http) {
|
||||
* http.get('people.json')
|
||||
* // Call map on the response observable to get the parsed people object
|
||||
* .map(res => res.json())
|
||||
* // Subscribe to the observable to get the parsed people object and attach it to the
|
||||
* // component
|
||||
* .subscribe(people => this.people = people);
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* http.get('people.json').subscribe((res:Response) => this.people = res.json());
|
||||
* ```
|
||||
*
|
||||
* The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
|
||||
* {@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
|
||||
* the {@link XHRBackend} provider, as in the following example:
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```typescript
|
||||
* import {BaseRequestOptions, Http} from '@angular/http';
|
||||
* import {MockBackend} from '@angular/http/testing';
|
||||
* var injector = Injector.resolveAndCreate([
|
||||
* BaseRequestOptions,
|
||||
* MockBackend,
|
||||
* {provide: Http, useFactory:
|
||||
* function(backend, defaultOptions) {
|
||||
* return new Http(backend, defaultOptions);
|
||||
* },
|
||||
* deps: [MockBackend, BaseRequestOptions]}
|
||||
* ]);
|
||||
* var http = injector.get(Http);
|
||||
* http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class Http {
|
||||
constructor(protected _backend: ConnectionBackend, protected _defaultOptions: RequestOptions) {}
|
||||
|
||||
/**
|
||||
* Performs any type of http request. First argument is required, and can either be a url or
|
||||
* a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
|
||||
* object can be provided as the 2nd argument. The options object will be merged with the values
|
||||
* of {@link BaseRequestOptions} before performing the request.
|
||||
*/
|
||||
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
|
||||
let responseObservable: any;
|
||||
if (typeof url === 'string') {
|
||||
responseObservable = httpRequest(
|
||||
this._backend,
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)));
|
||||
} else if (url instanceof Request) {
|
||||
responseObservable = httpRequest(this._backend, url);
|
||||
} else {
|
||||
throw new Error('First argument must be a url string or Request instance.');
|
||||
}
|
||||
return responseObservable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `get` http method.
|
||||
*/
|
||||
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `post` http method.
|
||||
*/
|
||||
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Post,
|
||||
url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `put` http method.
|
||||
*/
|
||||
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Put,
|
||||
url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `delete` http method.
|
||||
*/
|
||||
delete (url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `patch` http method.
|
||||
*/
|
||||
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Patch,
|
||||
url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `head` http method.
|
||||
*/
|
||||
head(url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `options` http method.
|
||||
*/
|
||||
options(url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class Jsonp extends Http {
|
||||
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
|
||||
super(backend, defaultOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs any type of http request. First argument is required, and can either be a url or
|
||||
* a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
|
||||
* object can be provided as the 2nd argument. The options object will be merged with the values
|
||||
* of {@link BaseRequestOptions} before performing the request.
|
||||
*
|
||||
* @security Regular XHR is the safest alternative to JSONP for most applications, and is
|
||||
* supported by all current browsers. Because JSONP creates a `<script>` element with
|
||||
* contents retrieved from a remote source, attacker-controlled data introduced by an untrusted
|
||||
* source could expose your application to XSS risks. Data exposed by JSONP may also be
|
||||
* readable by malicious third-party websites. In addition, JSONP introduces potential risk for
|
||||
* future security issues (e.g. content sniffing). For more detail, see the
|
||||
* [Security Guide](http://g.co/ng/security).
|
||||
*/
|
||||
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
|
||||
let responseObservable: any;
|
||||
if (typeof url === 'string') {
|
||||
url =
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url));
|
||||
}
|
||||
if (url instanceof Request) {
|
||||
if (url.method !== RequestMethod.Get) {
|
||||
throw new Error('JSONP requests must use GET request method.');
|
||||
}
|
||||
responseObservable = httpRequest(this._backend, url);
|
||||
} else {
|
||||
throw new Error('First argument must be a url string or Request instance.');
|
||||
}
|
||||
return responseObservable;
|
||||
}
|
||||
}
|
77
packages/http/src/http_module.ts
Normal file
77
packages/http/src/http_module.ts
Normal file
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* The http module provides services to perform http requests. To get started, see the {@link Http}
|
||||
* class.
|
||||
*/
|
||||
import {NgModule} from '@angular/core';
|
||||
|
||||
import {BrowserJsonp} from './backends/browser_jsonp';
|
||||
import {BrowserXhr} from './backends/browser_xhr';
|
||||
import {JSONPBackend, JSONPBackend_} from './backends/jsonp_backend';
|
||||
import {CookieXSRFStrategy, XHRBackend} from './backends/xhr_backend';
|
||||
import {BaseRequestOptions, RequestOptions} from './base_request_options';
|
||||
import {BaseResponseOptions, ResponseOptions} from './base_response_options';
|
||||
import {Http, Jsonp} from './http';
|
||||
import {XSRFStrategy} from './interfaces';
|
||||
|
||||
|
||||
export function _createDefaultCookieXSRFStrategy() {
|
||||
return new CookieXSRFStrategy();
|
||||
}
|
||||
|
||||
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions): Http {
|
||||
return new Http(xhrBackend, requestOptions);
|
||||
}
|
||||
|
||||
export function jsonpFactory(jsonpBackend: JSONPBackend, requestOptions: RequestOptions): Jsonp {
|
||||
return new Jsonp(jsonpBackend, requestOptions);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The module that includes http's providers
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@NgModule({
|
||||
providers: [
|
||||
// TODO(pascal): use factory type annotations once supported in DI
|
||||
// issue: https://github.com/angular/angular/issues/3183
|
||||
{provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions]},
|
||||
BrowserXhr,
|
||||
{provide: RequestOptions, useClass: BaseRequestOptions},
|
||||
{provide: ResponseOptions, useClass: BaseResponseOptions},
|
||||
XHRBackend,
|
||||
{provide: XSRFStrategy, useFactory: _createDefaultCookieXSRFStrategy},
|
||||
],
|
||||
})
|
||||
export class HttpModule {
|
||||
}
|
||||
|
||||
/**
|
||||
* The module that includes jsonp's providers
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@NgModule({
|
||||
providers: [
|
||||
// TODO(pascal): use factory type annotations once supported in DI
|
||||
// issue: https://github.com/angular/angular/issues/3183
|
||||
{provide: Jsonp, useFactory: jsonpFactory, deps: [JSONPBackend, RequestOptions]},
|
||||
BrowserJsonp,
|
||||
{provide: RequestOptions, useClass: BaseRequestOptions},
|
||||
{provide: ResponseOptions, useClass: BaseResponseOptions},
|
||||
{provide: JSONPBackend, useClass: JSONPBackend_},
|
||||
],
|
||||
})
|
||||
export class JsonpModule {
|
||||
}
|
51
packages/http/src/http_utils.ts
Normal file
51
packages/http/src/http_utils.ts
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {RequestMethod} from './enums';
|
||||
|
||||
export function normalizeMethodName(method: string | RequestMethod): RequestMethod {
|
||||
if (typeof method !== 'string') return method;
|
||||
|
||||
switch (method.toUpperCase()) {
|
||||
case 'GET':
|
||||
return RequestMethod.Get;
|
||||
case 'POST':
|
||||
return RequestMethod.Post;
|
||||
case 'PUT':
|
||||
return RequestMethod.Put;
|
||||
case 'DELETE':
|
||||
return RequestMethod.Delete;
|
||||
case 'OPTIONS':
|
||||
return RequestMethod.Options;
|
||||
case 'HEAD':
|
||||
return RequestMethod.Head;
|
||||
case 'PATCH':
|
||||
return RequestMethod.Patch;
|
||||
}
|
||||
throw new Error(`Invalid request method. The method "${method}" is not supported.`);
|
||||
}
|
||||
|
||||
export const isSuccess = (status: number): boolean => (status >= 200 && status < 300);
|
||||
|
||||
export function getResponseURL(xhr: any): string {
|
||||
if ('responseURL' in xhr) {
|
||||
return xhr.responseURL;
|
||||
}
|
||||
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
|
||||
return xhr.getResponseHeader('X-Request-URL');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
export function stringToArrayBuffer(input: String): ArrayBuffer {
|
||||
const view = new Uint16Array(input.length);
|
||||
for (let i = 0, strLen = input.length; i < strLen; i++) {
|
||||
view[i] = input.charCodeAt(i);
|
||||
}
|
||||
return view.buffer;
|
||||
}
|
22
packages/http/src/index.ts
Normal file
22
packages/http/src/index.ts
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
export {BrowserXhr} from './backends/browser_xhr';
|
||||
export {JSONPBackend, JSONPConnection} from './backends/jsonp_backend';
|
||||
export {CookieXSRFStrategy, XHRBackend, XHRConnection} from './backends/xhr_backend';
|
||||
export {BaseRequestOptions, RequestOptions} from './base_request_options';
|
||||
export {BaseResponseOptions, ResponseOptions} from './base_response_options';
|
||||
export {ReadyState, RequestMethod, ResponseContentType, ResponseType} from './enums';
|
||||
export {Headers} from './headers';
|
||||
export {Http, Jsonp} from './http';
|
||||
export {HttpModule, JsonpModule} from './http_module';
|
||||
export {Connection, ConnectionBackend, RequestOptionsArgs, ResponseOptionsArgs, XSRFStrategy} from './interfaces';
|
||||
export {Request} from './static_request';
|
||||
export {Response} from './static_response';
|
||||
export {QueryEncoder, URLSearchParams} from './url_search_params';
|
||||
export {VERSION} from './version';
|
76
packages/http/src/interfaces.ts
Normal file
76
packages/http/src/interfaces.ts
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ReadyState, RequestMethod, ResponseContentType, ResponseType} from './enums';
|
||||
import {Headers} from './headers';
|
||||
import {Request} from './static_request';
|
||||
import {URLSearchParams} from './url_search_params';
|
||||
|
||||
/**
|
||||
* Abstract class from which real backends are derived.
|
||||
*
|
||||
* The primary purpose of a `ConnectionBackend` is to create new connections to fulfill a given
|
||||
* {@link Request}.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export abstract class ConnectionBackend { abstract createConnection(request: any): Connection; }
|
||||
|
||||
/**
|
||||
* Abstract class from which real connections are derived.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export abstract class Connection {
|
||||
readyState: ReadyState;
|
||||
request: Request;
|
||||
response: any; // TODO: generic of <Response>;
|
||||
}
|
||||
|
||||
/**
|
||||
* An XSRFStrategy configures XSRF protection (e.g. via headers) on an HTTP request.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export abstract class XSRFStrategy { abstract configureRequest(req: Request): void; }
|
||||
|
||||
/**
|
||||
* Interface for options to construct a RequestOptions, based on
|
||||
* [RequestInit](https://fetch.spec.whatwg.org/#requestinit) from the Fetch spec.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export interface RequestOptionsArgs {
|
||||
url?: string;
|
||||
method?: string|RequestMethod;
|
||||
/** @deprecated from 4.0.0. Use params instead. */
|
||||
search?: string|URLSearchParams|{[key: string]: any | any[]};
|
||||
params?: string|URLSearchParams|{[key: string]: any | any[]};
|
||||
headers?: Headers;
|
||||
body?: any;
|
||||
withCredentials?: boolean;
|
||||
responseType?: ResponseContentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Required structure when constructing new Request();
|
||||
*/
|
||||
export interface RequestArgs extends RequestOptionsArgs { url: string; }
|
||||
|
||||
/**
|
||||
* Interface for options to construct a Response, based on
|
||||
* [ResponseInit](https://fetch.spec.whatwg.org/#responseinit) from the Fetch spec.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export type ResponseOptionsArgs = {
|
||||
body?: string | Object | FormData | ArrayBuffer | Blob; status?: number; statusText?: string;
|
||||
headers?: Headers;
|
||||
type?: ResponseType;
|
||||
url?: string;
|
||||
};
|
170
packages/http/src/static_request.ts
Normal file
170
packages/http/src/static_request.ts
Normal file
@ -0,0 +1,170 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Body} from './body';
|
||||
import {ContentType, RequestMethod, ResponseContentType} from './enums';
|
||||
import {Headers} from './headers';
|
||||
import {normalizeMethodName} from './http_utils';
|
||||
import {RequestArgs} from './interfaces';
|
||||
import {URLSearchParams} from './url_search_params';
|
||||
|
||||
|
||||
// TODO(jeffbcross): properly implement body accessors
|
||||
/**
|
||||
* Creates `Request` instances from provided values.
|
||||
*
|
||||
* The Request's interface is inspired by the Request constructor defined in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#request-class),
|
||||
* but is considered a static value whose body can be accessed many times. There are other
|
||||
* differences in the implementation, but this is the most significant.
|
||||
*
|
||||
* `Request` instances are typically created by higher-level classes, like {@link Http} and
|
||||
* {@link Jsonp}, but it may occasionally be useful to explicitly create `Request` instances.
|
||||
* One such example is when creating services that wrap higher-level services, like {@link Http},
|
||||
* where it may be useful to generate a `Request` with arbitrary headers and search params.
|
||||
*
|
||||
* ```typescript
|
||||
* import {Injectable, Injector} from '@angular/core';
|
||||
* import {HTTP_PROVIDERS, Http, Request, RequestMethod} from '@angular/http';
|
||||
*
|
||||
* @Injectable()
|
||||
* class AutoAuthenticator {
|
||||
* constructor(public http:Http) {}
|
||||
* request(url:string) {
|
||||
* return this.http.request(new Request({
|
||||
* method: RequestMethod.Get,
|
||||
* url: url,
|
||||
* search: 'password=123'
|
||||
* }));
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]);
|
||||
* var authenticator = injector.get(AutoAuthenticator);
|
||||
* authenticator.request('people.json').subscribe(res => {
|
||||
* //URL should have included '?password=123'
|
||||
* console.log('people', res.json());
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class Request extends Body {
|
||||
/**
|
||||
* Http method with which to perform the request.
|
||||
*/
|
||||
method: RequestMethod;
|
||||
/**
|
||||
* {@link Headers} instance
|
||||
*/
|
||||
headers: Headers;
|
||||
/** Url of the remote resource */
|
||||
url: string;
|
||||
/** Type of the request body **/
|
||||
private contentType: ContentType;
|
||||
/** Enable use credentials */
|
||||
withCredentials: boolean;
|
||||
/** Buffer to store the response */
|
||||
responseType: ResponseContentType;
|
||||
constructor(requestOptions: RequestArgs) {
|
||||
super();
|
||||
// TODO: assert that url is present
|
||||
const url = requestOptions.url;
|
||||
this.url = requestOptions.url;
|
||||
if (requestOptions.params) {
|
||||
const params = requestOptions.params.toString();
|
||||
if (params.length > 0) {
|
||||
let prefix = '?';
|
||||
if (this.url.indexOf('?') != -1) {
|
||||
prefix = (this.url[this.url.length - 1] == '&') ? '' : '&';
|
||||
}
|
||||
// TODO: just delete search-query-looking string in url?
|
||||
this.url = url + prefix + params;
|
||||
}
|
||||
}
|
||||
this._body = requestOptions.body;
|
||||
this.method = normalizeMethodName(requestOptions.method);
|
||||
// TODO(jeffbcross): implement behavior
|
||||
// Defaults to 'omit', consistent with browser
|
||||
this.headers = new Headers(requestOptions.headers);
|
||||
this.contentType = this.detectContentType();
|
||||
this.withCredentials = requestOptions.withCredentials;
|
||||
this.responseType = requestOptions.responseType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content type enum based on header options.
|
||||
*/
|
||||
detectContentType(): ContentType {
|
||||
switch (this.headers.get('content-type')) {
|
||||
case 'application/json':
|
||||
return ContentType.JSON;
|
||||
case 'application/x-www-form-urlencoded':
|
||||
return ContentType.FORM;
|
||||
case 'multipart/form-data':
|
||||
return ContentType.FORM_DATA;
|
||||
case 'text/plain':
|
||||
case 'text/html':
|
||||
return ContentType.TEXT;
|
||||
case 'application/octet-stream':
|
||||
return this._body instanceof ArrayBuffer ? ContentType.ARRAY_BUFFER : ContentType.BLOB;
|
||||
default:
|
||||
return this.detectContentTypeFromBody();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content type of request's body based on its type.
|
||||
*/
|
||||
detectContentTypeFromBody(): ContentType {
|
||||
if (this._body == null) {
|
||||
return ContentType.NONE;
|
||||
} else if (this._body instanceof URLSearchParams) {
|
||||
return ContentType.FORM;
|
||||
} else if (this._body instanceof FormData) {
|
||||
return ContentType.FORM_DATA;
|
||||
} else if (this._body instanceof Blob) {
|
||||
return ContentType.BLOB;
|
||||
} else if (this._body instanceof ArrayBuffer) {
|
||||
return ContentType.ARRAY_BUFFER;
|
||||
} else if (this._body && typeof this._body === 'object') {
|
||||
return ContentType.JSON;
|
||||
} else {
|
||||
return ContentType.TEXT;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request's body according to its type. If body is undefined, return
|
||||
* null.
|
||||
*/
|
||||
getBody(): any {
|
||||
switch (this.contentType) {
|
||||
case ContentType.JSON:
|
||||
return this.text();
|
||||
case ContentType.FORM:
|
||||
return this.text();
|
||||
case ContentType.FORM_DATA:
|
||||
return this._body;
|
||||
case ContentType.TEXT:
|
||||
return this.text();
|
||||
case ContentType.BLOB:
|
||||
return this.blob();
|
||||
case ContentType.ARRAY_BUFFER:
|
||||
return this.arrayBuffer();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const noop = function() {};
|
||||
const w = typeof window == 'object' ? window : noop;
|
||||
const FormData = (w as any /** TODO #9100 */)['FormData'] || noop;
|
||||
const Blob = (w as any /** TODO #9100 */)['Blob'] || noop;
|
||||
export const ArrayBuffer = (w as any /** TODO #9100 */)['ArrayBuffer'] || noop;
|
100
packages/http/src/static_response.ts
Normal file
100
packages/http/src/static_response.ts
Normal file
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
|
||||
|
||||
import {ResponseOptions} from './base_response_options';
|
||||
import {Body} from './body';
|
||||
import {ResponseType} from './enums';
|
||||
import {Headers} from './headers';
|
||||
|
||||
|
||||
/**
|
||||
* Creates `Response` instances from provided values.
|
||||
*
|
||||
* Though this object isn't
|
||||
* usually instantiated by end-users, it is the primary object interacted with when it comes time to
|
||||
* add data to a view.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* http.request('my-friends.txt').subscribe(response => this.friends = response.text());
|
||||
* ```
|
||||
*
|
||||
* The Response's interface is inspired by the Response constructor defined in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#response-class), but is considered a static value whose body
|
||||
* can be accessed many times. There are other differences in the implementation, but this is the
|
||||
* most significant.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class Response extends Body {
|
||||
/**
|
||||
* One of "basic", "cors", "default", "error", or "opaque".
|
||||
*
|
||||
* Defaults to "default".
|
||||
*/
|
||||
type: ResponseType;
|
||||
/**
|
||||
* True if the response's status is within 200-299
|
||||
*/
|
||||
ok: boolean;
|
||||
/**
|
||||
* URL of response.
|
||||
*
|
||||
* Defaults to empty string.
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* Status code returned by server.
|
||||
*
|
||||
* Defaults to 200.
|
||||
*/
|
||||
status: number;
|
||||
/**
|
||||
* Text representing the corresponding reason phrase to the `status`, as defined in [ietf rfc 2616
|
||||
* section 6.1.1](https://tools.ietf.org/html/rfc2616#section-6.1.1)
|
||||
*
|
||||
* Defaults to "OK"
|
||||
*/
|
||||
statusText: string;
|
||||
/**
|
||||
* Non-standard property
|
||||
*
|
||||
* Denotes how many of the response body's bytes have been loaded, for example if the response is
|
||||
* the result of a progress event.
|
||||
*/
|
||||
bytesLoaded: number;
|
||||
/**
|
||||
* Non-standard property
|
||||
*
|
||||
* Denotes how many bytes are expected in the final response body.
|
||||
*/
|
||||
totalBytes: number;
|
||||
/**
|
||||
* Headers object based on the `Headers` class in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#headers-class).
|
||||
*/
|
||||
headers: Headers;
|
||||
|
||||
constructor(responseOptions: ResponseOptions) {
|
||||
super();
|
||||
this._body = responseOptions.body;
|
||||
this.status = responseOptions.status;
|
||||
this.ok = (this.status >= 200 && this.status <= 299);
|
||||
this.statusText = responseOptions.statusText;
|
||||
this.headers = responseOptions.headers;
|
||||
this.type = responseOptions.type;
|
||||
this.url = responseOptions.url;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `Response with status: ${this.status} ${this.statusText} for URL: ${this.url}`;
|
||||
}
|
||||
}
|
184
packages/http/src/url_search_params.ts
Normal file
184
packages/http/src/url_search_params.ts
Normal file
@ -0,0 +1,184 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
function paramParser(rawParams: string = ''): Map<string, string[]> {
|
||||
const map = new Map<string, string[]>();
|
||||
if (rawParams.length > 0) {
|
||||
const params: string[] = rawParams.split('&');
|
||||
params.forEach((param: string) => {
|
||||
const eqIdx = param.indexOf('=');
|
||||
const [key, val]: string[] =
|
||||
eqIdx == -1 ? [param, ''] : [param.slice(0, eqIdx), param.slice(eqIdx + 1)];
|
||||
const list = map.get(key) || [];
|
||||
list.push(val);
|
||||
map.set(key, list);
|
||||
});
|
||||
}
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
* @experimental
|
||||
**/
|
||||
export class QueryEncoder {
|
||||
encodeKey(k: string): string { return standardEncoding(k); }
|
||||
|
||||
encodeValue(v: string): string { return standardEncoding(v); }
|
||||
}
|
||||
|
||||
function standardEncoding(v: string): string {
|
||||
return encodeURIComponent(v)
|
||||
.replace(/%40/gi, '@')
|
||||
.replace(/%3A/gi, ':')
|
||||
.replace(/%24/gi, '$')
|
||||
.replace(/%2C/gi, ',')
|
||||
.replace(/%3B/gi, ';')
|
||||
.replace(/%2B/gi, '+')
|
||||
.replace(/%3D/gi, '=')
|
||||
.replace(/%3F/gi, '?')
|
||||
.replace(/%2F/gi, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Map-like representation of url search parameters, based on
|
||||
* [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams) in the url living standard,
|
||||
* with several extensions for merging URLSearchParams objects:
|
||||
* - setAll()
|
||||
* - appendAll()
|
||||
* - replaceAll()
|
||||
*
|
||||
* This class accepts an optional second parameter of ${@link QueryEncoder},
|
||||
* which is used to serialize parameters before making a request. By default,
|
||||
* `QueryEncoder` encodes keys and values of parameters using `encodeURIComponent`,
|
||||
* and then un-encodes certain characters that are allowed to be part of the query
|
||||
* according to IETF RFC 3986: https://tools.ietf.org/html/rfc3986.
|
||||
*
|
||||
* These are the characters that are not encoded: `! $ \' ( ) * + , ; A 9 - . _ ~ ? /`
|
||||
*
|
||||
* If the set of allowed query characters is not acceptable for a particular backend,
|
||||
* `QueryEncoder` can be subclassed and provided as the 2nd argument to URLSearchParams.
|
||||
*
|
||||
* ```
|
||||
* import {URLSearchParams, QueryEncoder} from '@angular/http';
|
||||
* class MyQueryEncoder extends QueryEncoder {
|
||||
* encodeKey(k: string): string {
|
||||
* return myEncodingFunction(k);
|
||||
* }
|
||||
*
|
||||
* encodeValue(v: string): string {
|
||||
* return myEncodingFunction(v);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* let params = new URLSearchParams('', new MyQueryEncoder());
|
||||
* ```
|
||||
* @experimental
|
||||
*/
|
||||
export class URLSearchParams {
|
||||
paramsMap: Map<string, string[]>;
|
||||
constructor(
|
||||
public rawParams: string = '', private queryEncoder: QueryEncoder = new QueryEncoder()) {
|
||||
this.paramsMap = paramParser(rawParams);
|
||||
}
|
||||
|
||||
clone(): URLSearchParams {
|
||||
const clone = new URLSearchParams('', this.queryEncoder);
|
||||
clone.appendAll(this);
|
||||
return clone;
|
||||
}
|
||||
|
||||
has(param: string): boolean { return this.paramsMap.has(param); }
|
||||
|
||||
get(param: string): string {
|
||||
const storedParam = this.paramsMap.get(param);
|
||||
|
||||
return Array.isArray(storedParam) ? storedParam[0] : null;
|
||||
}
|
||||
|
||||
getAll(param: string): string[] { return this.paramsMap.get(param) || []; }
|
||||
|
||||
set(param: string, val: string) {
|
||||
if (val === void 0 || val === null) {
|
||||
this.delete(param);
|
||||
return;
|
||||
}
|
||||
const list = this.paramsMap.get(param) || [];
|
||||
list.length = 0;
|
||||
list.push(val);
|
||||
this.paramsMap.set(param, list);
|
||||
}
|
||||
|
||||
// A merge operation
|
||||
// For each name-values pair in `searchParams`, perform `set(name, values[0])`
|
||||
//
|
||||
// E.g: "a=[1,2,3], c=[8]" + "a=[4,5,6], b=[7]" = "a=[4], c=[8], b=[7]"
|
||||
//
|
||||
// TODO(@caitp): document this better
|
||||
setAll(searchParams: URLSearchParams) {
|
||||
searchParams.paramsMap.forEach((value, param) => {
|
||||
const list = this.paramsMap.get(param) || [];
|
||||
list.length = 0;
|
||||
list.push(value[0]);
|
||||
this.paramsMap.set(param, list);
|
||||
});
|
||||
}
|
||||
|
||||
append(param: string, val: string): void {
|
||||
if (val === void 0 || val === null) return;
|
||||
const list = this.paramsMap.get(param) || [];
|
||||
list.push(val);
|
||||
this.paramsMap.set(param, list);
|
||||
}
|
||||
|
||||
// A merge operation
|
||||
// For each name-values pair in `searchParams`, perform `append(name, value)`
|
||||
// for each value in `values`.
|
||||
//
|
||||
// E.g: "a=[1,2], c=[8]" + "a=[3,4], b=[7]" = "a=[1,2,3,4], c=[8], b=[7]"
|
||||
//
|
||||
// TODO(@caitp): document this better
|
||||
appendAll(searchParams: URLSearchParams) {
|
||||
searchParams.paramsMap.forEach((value, param) => {
|
||||
const list = this.paramsMap.get(param) || [];
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
list.push(value[i]);
|
||||
}
|
||||
this.paramsMap.set(param, list);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// A merge operation
|
||||
// For each name-values pair in `searchParams`, perform `delete(name)`,
|
||||
// followed by `set(name, values)`
|
||||
//
|
||||
// E.g: "a=[1,2,3], c=[8]" + "a=[4,5,6], b=[7]" = "a=[4,5,6], c=[8], b=[7]"
|
||||
//
|
||||
// TODO(@caitp): document this better
|
||||
replaceAll(searchParams: URLSearchParams) {
|
||||
searchParams.paramsMap.forEach((value, param) => {
|
||||
const list = this.paramsMap.get(param) || [];
|
||||
list.length = 0;
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
list.push(value[i]);
|
||||
}
|
||||
this.paramsMap.set(param, list);
|
||||
});
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
const paramsList: string[] = [];
|
||||
this.paramsMap.forEach((values, k) => {
|
||||
values.forEach(
|
||||
v => paramsList.push(
|
||||
this.queryEncoder.encodeKey(k) + '=' + this.queryEncoder.encodeValue(v)));
|
||||
});
|
||||
return paramsList.join('&');
|
||||
}
|
||||
|
||||
delete (param: string): void { this.paramsMap.delete(param); }
|
||||
}
|
19
packages/http/src/version.ts
Normal file
19
packages/http/src/version.ts
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* Entry point for all public APIs of the common package.
|
||||
*/
|
||||
|
||||
import {Version} from '@angular/core';
|
||||
/**
|
||||
* @stable
|
||||
*/
|
||||
export const VERSION = new Version('0.0.0-PLACEHOLDER');
|
170
packages/http/test/backends/jsonp_backend_spec.ts
Normal file
170
packages/http/test/backends/jsonp_backend_spec.ts
Normal file
@ -0,0 +1,170 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ReflectiveInjector} from '@angular/core';
|
||||
import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, describe, inject, it} from '@angular/core/testing/testing_internal';
|
||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
import {BrowserJsonp} from '../../src/backends/browser_jsonp';
|
||||
import {JSONPBackend, JSONPBackend_, JSONPConnection, JSONPConnection_} from '../../src/backends/jsonp_backend';
|
||||
import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options';
|
||||
import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options';
|
||||
import {ReadyState, RequestMethod, ResponseType} from '../../src/enums';
|
||||
import {Request} from '../../src/static_request';
|
||||
|
||||
let existingScripts: MockBrowserJsonp[] = [];
|
||||
|
||||
class MockBrowserJsonp extends BrowserJsonp {
|
||||
src: string;
|
||||
callbacks = new Map<string, (data: any) => any>();
|
||||
|
||||
addEventListener(type: string, cb: (data: any) => any) { this.callbacks.set(type, cb); }
|
||||
|
||||
removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); }
|
||||
|
||||
dispatchEvent(type: string, argument: any = {}) {
|
||||
const cb = this.callbacks.get(type);
|
||||
if (cb) {
|
||||
cb(argument);
|
||||
}
|
||||
}
|
||||
|
||||
build(url: string) {
|
||||
const script = new MockBrowserJsonp();
|
||||
script.src = url;
|
||||
existingScripts.push(script);
|
||||
return script;
|
||||
}
|
||||
|
||||
send(node: any) { /* noop */
|
||||
}
|
||||
cleanup(node: any) { /* noop */
|
||||
}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('JSONPBackend', () => {
|
||||
let backend: JSONPBackend_;
|
||||
let sampleRequest: Request;
|
||||
|
||||
beforeEach(() => {
|
||||
const injector = ReflectiveInjector.resolveAndCreate([
|
||||
{provide: ResponseOptions, useClass: BaseResponseOptions},
|
||||
{provide: BrowserJsonp, useClass: MockBrowserJsonp},
|
||||
{provide: JSONPBackend, useClass: JSONPBackend_}
|
||||
]);
|
||||
backend = injector.get(JSONPBackend);
|
||||
const base = new BaseRequestOptions();
|
||||
sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
|
||||
});
|
||||
|
||||
afterEach(() => { existingScripts = []; });
|
||||
|
||||
it('should create a connection', () => {
|
||||
let instance: JSONPConnection;
|
||||
expect(() => instance = backend.createConnection(sampleRequest)).not.toThrow();
|
||||
expect(instance).toBeAnInstanceOf(JSONPConnection);
|
||||
});
|
||||
|
||||
|
||||
describe('JSONPConnection', () => {
|
||||
it('should use the injected BaseResponseOptions to create the response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new JSONPConnection_(
|
||||
sampleRequest, new MockBrowserJsonp(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(res => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
async.done();
|
||||
});
|
||||
connection.finished();
|
||||
existingScripts[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should ignore load/callback when disposed',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
||||
const spy = new SpyObject();
|
||||
const loadSpy = spy.spy('load');
|
||||
const errorSpy = spy.spy('error');
|
||||
const returnSpy = spy.spy('cancelled');
|
||||
|
||||
const request = connection.response.subscribe(loadSpy, errorSpy, returnSpy);
|
||||
request.unsubscribe();
|
||||
|
||||
connection.finished('Fake data');
|
||||
existingScripts[0].dispatchEvent('load');
|
||||
|
||||
setTimeout(() => {
|
||||
expect(connection.readyState).toBe(ReadyState.Cancelled);
|
||||
expect(loadSpy).not.toHaveBeenCalled();
|
||||
expect(errorSpy).not.toHaveBeenCalled();
|
||||
expect(returnSpy).not.toHaveBeenCalled();
|
||||
async.done();
|
||||
}, 10);
|
||||
}));
|
||||
|
||||
it('should report error if loaded without invoking callback',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
||||
connection.response.subscribe(
|
||||
res => {
|
||||
expect('response listener called').toBe(false);
|
||||
async.done();
|
||||
},
|
||||
err => {
|
||||
expect(err.text()).toEqual('JSONP injected script did not invoke callback.');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingScripts[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should report error if script contains error',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
||||
|
||||
connection.response.subscribe(
|
||||
res => {
|
||||
expect('response listener called').toBe(false);
|
||||
async.done();
|
||||
},
|
||||
err => {
|
||||
expect(err.text()).toBe('Oops!');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingScripts[0].dispatchEvent('error', ({message: 'Oops!'}));
|
||||
}));
|
||||
|
||||
it('should throw if request method is not GET', () => {
|
||||
[RequestMethod.Post, RequestMethod.Put, RequestMethod.Delete, RequestMethod.Options,
|
||||
RequestMethod.Head, RequestMethod.Patch]
|
||||
.forEach(method => {
|
||||
const base = new BaseRequestOptions();
|
||||
const req = new Request(
|
||||
base.merge(new RequestOptions({url: 'https://google.com', method: method})));
|
||||
expect(() => new JSONPConnection_(req, new MockBrowserJsonp()).response.subscribe())
|
||||
.toThrowError();
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond with data passed to callback',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
||||
|
||||
connection.response.subscribe(res => {
|
||||
expect(res.json()).toEqual(({fake_payload: true, blob_id: 12345}));
|
||||
async.done();
|
||||
});
|
||||
|
||||
connection.finished(({fake_payload: true, blob_id: 12345}));
|
||||
existingScripts[0].dispatchEvent('load');
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
119
packages/http/test/backends/mock_backend_spec.ts
Normal file
119
packages/http/test/backends/mock_backend_spec.ts
Normal file
@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ReflectiveInjector} from '@angular/core';
|
||||
import {AsyncTestCompleter, beforeEach, describe, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
import {ReplaySubject} from 'rxjs/ReplaySubject';
|
||||
|
||||
import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options';
|
||||
import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options';
|
||||
import {Request} from '../../src/static_request';
|
||||
import {Response} from '../../src/static_response';
|
||||
import {MockBackend, MockConnection} from '../../testing/mock_backend';
|
||||
|
||||
export function main() {
|
||||
describe('MockBackend', () => {
|
||||
|
||||
let backend: MockBackend;
|
||||
let sampleRequest1: Request;
|
||||
let sampleResponse1: Response;
|
||||
let sampleRequest2: Request;
|
||||
let sampleResponse2: Response;
|
||||
|
||||
beforeEach(() => {
|
||||
const injector = ReflectiveInjector.resolveAndCreate(
|
||||
[{provide: ResponseOptions, useClass: BaseResponseOptions}, MockBackend]);
|
||||
backend = injector.get(MockBackend);
|
||||
const base = new BaseRequestOptions();
|
||||
sampleRequest1 = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
|
||||
sampleResponse1 = new Response(new ResponseOptions({body: 'response1'}));
|
||||
sampleRequest2 = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
|
||||
sampleResponse2 = new Response(new ResponseOptions({body: 'response2'}));
|
||||
});
|
||||
|
||||
it('should create a new MockBackend', () => { expect(backend).toBeAnInstanceOf(MockBackend); });
|
||||
|
||||
it('should create a new MockConnection', () => {
|
||||
expect(backend.createConnection(sampleRequest1)).toBeAnInstanceOf(MockConnection);
|
||||
});
|
||||
|
||||
it('should create a new connection and allow subscription', () => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => {});
|
||||
});
|
||||
|
||||
it('should allow responding after subscription',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
connection.mockRespond(sampleResponse1);
|
||||
}));
|
||||
|
||||
it('should allow subscribing after responding',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.mockRespond(sampleResponse1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
}));
|
||||
|
||||
it('should allow responding after subscription with an error',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(null, () => { async.done(); });
|
||||
connection.mockError(new Error('nope'));
|
||||
}));
|
||||
|
||||
it('should not throw when there are no unresolved requests',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
connection.mockRespond(sampleResponse1);
|
||||
backend.verifyNoPendingRequests();
|
||||
}));
|
||||
|
||||
xit('should throw when there are unresolved requests',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
backend.verifyNoPendingRequests();
|
||||
}));
|
||||
|
||||
it('should work when requests are resolved out of order',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection1: MockConnection = backend.createConnection(sampleRequest1);
|
||||
const connection2: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection1.response.subscribe(() => { async.done(); });
|
||||
connection2.response.subscribe(() => {});
|
||||
connection2.mockRespond(sampleResponse1);
|
||||
connection1.mockRespond(sampleResponse1);
|
||||
backend.verifyNoPendingRequests();
|
||||
}));
|
||||
|
||||
xit('should allow double subscribing',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const responses: Response[] = [sampleResponse1, sampleResponse2];
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(responses.shift()));
|
||||
const responseObservable: ReplaySubject<Response> =
|
||||
backend.createConnection(sampleRequest1).response;
|
||||
responseObservable.subscribe(res => expect(res.text()).toBe('response1'));
|
||||
responseObservable.subscribe(
|
||||
res => expect(res.text()).toBe('response2'), null, async.done);
|
||||
}));
|
||||
|
||||
// TODO(robwormald): readyStates are leaving?
|
||||
it('should allow resolution of requests manually', () => {
|
||||
const connection1: MockConnection = backend.createConnection(sampleRequest1);
|
||||
const connection2: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection1.response.subscribe(() => {});
|
||||
connection2.response.subscribe(() => {});
|
||||
backend.resolveAllConnections();
|
||||
backend.verifyNoPendingRequests();
|
||||
});
|
||||
});
|
||||
}
|
760
packages/http/test/backends/xhr_backend_spec.ts
Normal file
760
packages/http/test/backends/xhr_backend_spec.ts
Normal file
@ -0,0 +1,760 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, beforeEachProviders, describe, expect, inject, it} from '@angular/core/testing/testing_internal';
|
||||
import {ɵgetDOM as getDOM} from '@angular/platform-browser';
|
||||
import {BrowserXhr} from '../../src/backends/browser_xhr';
|
||||
import {CookieXSRFStrategy, XHRBackend, XHRConnection} from '../../src/backends/xhr_backend';
|
||||
import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options';
|
||||
import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options';
|
||||
import {ResponseContentType, ResponseType} from '../../src/enums';
|
||||
import {Headers} from '../../src/headers';
|
||||
import {XSRFStrategy} from '../../src/interfaces';
|
||||
import {Request} from '../../src/static_request';
|
||||
import {Response} from '../../src/static_response';
|
||||
import {URLSearchParams} from '../../src/url_search_params';
|
||||
|
||||
let abortSpy: any;
|
||||
let sendSpy: any;
|
||||
let openSpy: any;
|
||||
let setRequestHeaderSpy: any;
|
||||
let existingXHRs: MockBrowserXHR[] = [];
|
||||
|
||||
class MockBrowserXHR extends BrowserXhr {
|
||||
abort: any;
|
||||
send: any;
|
||||
open: any;
|
||||
response: any;
|
||||
responseType: string;
|
||||
responseText: string;
|
||||
setRequestHeader: any;
|
||||
callbacks = new Map<string, Function>();
|
||||
status: number;
|
||||
responseHeaders: string;
|
||||
responseURL: string;
|
||||
statusText: string;
|
||||
withCredentials: boolean;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
const spy = new SpyObject();
|
||||
this.abort = abortSpy = spy.spy('abort');
|
||||
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; }
|
||||
|
||||
setStatusText(statusText: string) { this.statusText = statusText; }
|
||||
|
||||
setResponse(value: string) { this.response = value; }
|
||||
|
||||
setResponseText(value: string) { this.responseText = value; }
|
||||
|
||||
setResponseURL(value: string) { this.responseURL = value; }
|
||||
|
||||
setResponseHeaders(value: string) { this.responseHeaders = value; }
|
||||
|
||||
getAllResponseHeaders() { return this.responseHeaders || ''; }
|
||||
|
||||
getResponseHeader(key: string) {
|
||||
return Headers.fromResponseHeaderString(this.responseHeaders).get(key);
|
||||
}
|
||||
|
||||
addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); }
|
||||
|
||||
removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); }
|
||||
|
||||
dispatchEvent(type: string) { this.callbacks.get(type)({}); }
|
||||
|
||||
build() {
|
||||
const xhr = new MockBrowserXHR();
|
||||
existingXHRs.push(xhr);
|
||||
return xhr;
|
||||
}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('XHRBackend', () => {
|
||||
let backend: XHRBackend;
|
||||
let sampleRequest: Request;
|
||||
|
||||
beforeEachProviders(
|
||||
() =>
|
||||
[{provide: ResponseOptions, useClass: BaseResponseOptions},
|
||||
{provide: BrowserXhr, useClass: MockBrowserXHR}, XHRBackend,
|
||||
{provide: XSRFStrategy, useValue: new CookieXSRFStrategy()},
|
||||
]);
|
||||
|
||||
beforeEach(inject([XHRBackend], (be: XHRBackend) => {
|
||||
backend = be;
|
||||
const base = new BaseRequestOptions();
|
||||
sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
|
||||
}));
|
||||
|
||||
afterEach(() => { existingXHRs = []; });
|
||||
|
||||
describe('creating a connection', () => {
|
||||
@Injectable()
|
||||
class NoopXsrfStrategy implements XSRFStrategy {
|
||||
configureRequest(req: Request) {}
|
||||
}
|
||||
beforeEachProviders(() => [{provide: XSRFStrategy, useClass: NoopXsrfStrategy}]);
|
||||
|
||||
it('succeeds',
|
||||
() => { expect(() => backend.createConnection(sampleRequest)).not.toThrow(); });
|
||||
});
|
||||
|
||||
if (getDOM().supportsCookies()) {
|
||||
describe('XSRF support', () => {
|
||||
it('sets an XSRF header by default', () => {
|
||||
getDOM().setCookie('XSRF-TOKEN', 'magic XSRF value');
|
||||
backend.createConnection(sampleRequest);
|
||||
expect(sampleRequest.headers.get('X-XSRF-TOKEN')).toBe('magic XSRF value');
|
||||
});
|
||||
it('should allow overwriting of existing headers', () => {
|
||||
getDOM().setCookie('XSRF-TOKEN', 'magic XSRF value');
|
||||
sampleRequest.headers.set('X-XSRF-TOKEN', 'already set');
|
||||
backend.createConnection(sampleRequest);
|
||||
expect(sampleRequest.headers.get('X-XSRF-TOKEN')).toBe('magic XSRF value');
|
||||
});
|
||||
|
||||
describe('configuration', () => {
|
||||
beforeEachProviders(() => [{
|
||||
provide: XSRFStrategy,
|
||||
useValue: new CookieXSRFStrategy('my cookie', 'X-MY-HEADER')
|
||||
}]);
|
||||
|
||||
it('uses the configured names', () => {
|
||||
getDOM().setCookie('my cookie', 'XSRF value');
|
||||
backend.createConnection(sampleRequest);
|
||||
expect(sampleRequest.headers.get('X-MY-HEADER')).toBe('XSRF value');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('XHRConnection', () => {
|
||||
it('should use the injected BaseResponseOptions to create the response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should complete a request', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(
|
||||
(res: Response) => { expect(res.type).toBe(ResponseType.Error); }, null,
|
||||
() => { async.done(); });
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should call abort when disposed', () => {
|
||||
const connection = new XHRConnection(sampleRequest, new MockBrowserXHR());
|
||||
const request = connection.response.subscribe();
|
||||
request.unsubscribe();
|
||||
expect(abortSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should create an error Response on error',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(null, (res: Response) => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[0].dispatchEvent('error');
|
||||
}));
|
||||
|
||||
it('should set the status text and status code on error',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(null, (res: Response) => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
expect(res.status).toEqual(0);
|
||||
expect(res.statusText).toEqual('');
|
||||
async.done();
|
||||
});
|
||||
const xhr = existingXHRs[0];
|
||||
// status=0 with a text='' is common for CORS errors
|
||||
xhr.setStatusCode(0);
|
||||
xhr.setStatusText('');
|
||||
xhr.dispatchEvent('error');
|
||||
}));
|
||||
|
||||
it('should call open with method and url when subscribed to', () => {
|
||||
const connection = new XHRConnection(sampleRequest, new MockBrowserXHR());
|
||||
expect(openSpy).not.toHaveBeenCalled();
|
||||
connection.response.subscribe();
|
||||
expect(openSpy).toHaveBeenCalledWith('GET', sampleRequest.url);
|
||||
});
|
||||
|
||||
it('should call send on the backend with request body when subscribed to', () => {
|
||||
const body = 'Some body to love';
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
expect(sendSpy).not.toHaveBeenCalled();
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
});
|
||||
|
||||
it('should attach headers to the request', () => {
|
||||
const headers =
|
||||
new Headers({'Content-Type': 'text/xml', 'Breaking-Bad': '<3', 'X-Multi': ['a', 'b']});
|
||||
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({headers: headers}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/xml');
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Breaking-Bad', '<3');
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('X-Multi', 'a,b');
|
||||
});
|
||||
|
||||
it('should attach default Accept header', () => {
|
||||
const headers = new Headers();
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(setRequestHeaderSpy)
|
||||
.toHaveBeenCalledWith('Accept', 'application/json, text/plain, */*');
|
||||
});
|
||||
|
||||
it('should not override user provided Accept header', () => {
|
||||
const headers = new Headers({'Accept': 'text/xml'});
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Accept', 'text/xml');
|
||||
});
|
||||
|
||||
it('should skip content type detection if custom content type header is set', () => {
|
||||
const headers = new Headers({'Content-Type': 'text/plain'});
|
||||
const body = {test: 'val'};
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body, headers: headers}))),
|
||||
new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/plain');
|
||||
expect(setRequestHeaderSpy).not.toHaveBeenCalledWith('Content-Type', 'application/json');
|
||||
expect(setRequestHeaderSpy).not.toHaveBeenCalledWith('content-type', 'application/json');
|
||||
});
|
||||
|
||||
it('should use object body and detect content type header to the request', () => {
|
||||
const body = {test: 'val'};
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(JSON.stringify(body, null, 2));
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'application/json');
|
||||
});
|
||||
|
||||
it('should use number body and detect content type header to the request', () => {
|
||||
const body = 23;
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith('23');
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/plain');
|
||||
});
|
||||
|
||||
it('should use string body and detect content type header to the request', () => {
|
||||
const body = 'some string';
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/plain');
|
||||
});
|
||||
|
||||
it('should use URLSearchParams body and detect content type header to the request', () => {
|
||||
const body = new URLSearchParams();
|
||||
body.set('test1', 'val1');
|
||||
body.set('test2', 'val2');
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith('test1=val1&test2=val2');
|
||||
expect(setRequestHeaderSpy)
|
||||
.toHaveBeenCalledWith(
|
||||
'content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
|
||||
});
|
||||
|
||||
if ((global as any /** TODO #9100 */)['Blob']) {
|
||||
// `new Blob(...)` throws an 'Illegal constructor' exception in Android browser <= 4.3,
|
||||
// but a BlobBuilder can be used instead
|
||||
const createBlob = (data: Array<string>, datatype: string) => {
|
||||
let newBlob: Blob;
|
||||
try {
|
||||
newBlob = new Blob(data || [], datatype ? {type: datatype} : {});
|
||||
} catch (e) {
|
||||
const BlobBuilder = (<any>global).BlobBuilder || (<any>global).WebKitBlobBuilder ||
|
||||
(<any>global).MozBlobBuilder || (<any>global).MSBlobBuilder;
|
||||
const builder = new BlobBuilder();
|
||||
builder.append(data);
|
||||
newBlob = builder.getBlob(datatype);
|
||||
}
|
||||
return newBlob;
|
||||
};
|
||||
|
||||
it('should use FormData body and detect content type header to the request', () => {
|
||||
const body = new FormData();
|
||||
body.append('test1', 'val1');
|
||||
body.append('test2', 123456);
|
||||
const blob = createBlob(['body { color: red; }'], 'text/css');
|
||||
body.append('userfile', blob);
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).not.toHaveBeenCalledWith();
|
||||
});
|
||||
|
||||
it('should use blob body and detect content type header to the request', () => {
|
||||
const body = createBlob(['body { color: red; }'], 'text/css');
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/css');
|
||||
});
|
||||
|
||||
it('should use blob body without type to the request', () => {
|
||||
const body = createBlob(['body { color: red; }'], null);
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).not.toHaveBeenCalledWith();
|
||||
});
|
||||
|
||||
it('should use blob body without type with custom content type header to the request',
|
||||
() => {
|
||||
const headers = new Headers({'Content-Type': 'text/css'});
|
||||
const body = createBlob(['body { color: red; }'], null);
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body, headers: headers}))),
|
||||
new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/css');
|
||||
});
|
||||
|
||||
it('should use array buffer body to the request', () => {
|
||||
const body = new ArrayBuffer(512);
|
||||
const longInt8View = new Uint8Array(body);
|
||||
for (let i = 0; i < longInt8View.length; i++) {
|
||||
longInt8View[i] = i % 255;
|
||||
}
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).not.toHaveBeenCalledWith();
|
||||
});
|
||||
|
||||
it('should use array buffer body without type with custom content type header to the request',
|
||||
() => {
|
||||
const headers = new Headers({'Content-Type': 'text/css'});
|
||||
const body = new ArrayBuffer(512);
|
||||
const longInt8View = new Uint8Array(body);
|
||||
for (let i = 0; i < longInt8View.length; i++) {
|
||||
longInt8View[i] = i % 255;
|
||||
}
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body, headers: headers}))),
|
||||
new MockBrowserXHR());
|
||||
connection.response.subscribe();
|
||||
expect(sendSpy).toHaveBeenCalledWith(body);
|
||||
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/css');
|
||||
});
|
||||
}
|
||||
|
||||
it('should return the correct status code',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 418;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(
|
||||
(res: Response) => {
|
||||
|
||||
},
|
||||
errRes => {
|
||||
expect(errRes.status).toBe(statusCode);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should call next and complete on 200 codes',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
let nextCalled = false;
|
||||
let errorCalled = false;
|
||||
const statusCode = 200;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(
|
||||
(res: Response) => {
|
||||
nextCalled = true;
|
||||
expect(res.status).toBe(statusCode);
|
||||
},
|
||||
errRes => { errorCalled = true; },
|
||||
() => {
|
||||
expect(nextCalled).toBe(true);
|
||||
expect(errorCalled).toBe(false);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should set ok to true on 200 return',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 200;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(res => {
|
||||
expect(res.ok).toBe(true);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should set ok to false on 300 return',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 300;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(
|
||||
res => { throw 'should not be called'; },
|
||||
errRes => {
|
||||
expect(errRes.ok).toBe(false);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should call error and not complete on 300+ codes',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
let nextCalled = false;
|
||||
const errorCalled = false;
|
||||
const statusCode = 301;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(
|
||||
(res: Response) => { nextCalled = true; },
|
||||
errRes => {
|
||||
expect(errRes.status).toBe(statusCode);
|
||||
expect(nextCalled).toBe(false);
|
||||
async.done();
|
||||
},
|
||||
() => { throw 'should not be called'; });
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should normalize IE\'s 1223 status code into 204',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 1223;
|
||||
const normalizedCode = 204;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.status).toBe(normalizedCode);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should ignore response body for 204 status code',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 204;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].setResponseText('Doge');
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should normalize responseText and response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const responseBody = 'Doge';
|
||||
|
||||
const connection1 =
|
||||
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||
|
||||
const connection2 =
|
||||
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||
|
||||
connection1.response.subscribe((res: Response) => {
|
||||
expect(res.text()).toBe(responseBody);
|
||||
|
||||
connection2.response.subscribe(ress => {
|
||||
expect(ress.text()).toBe(responseBody);
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[1].setStatusCode(200);
|
||||
existingXHRs[1].setResponse(responseBody);
|
||||
existingXHRs[1].dispatchEvent('load');
|
||||
});
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].setResponseText(responseBody);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should strip XSSI prefixes', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const conn =
|
||||
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||
conn.response.subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('{json: "object"}');
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].setResponseText(')]}\'\n{json: "object"}');
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should strip XSSI prefixes', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const conn =
|
||||
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||
conn.response.subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('{json: "object"}');
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].setResponseText(')]}\',\n{json: "object"}');
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should strip XSSI prefix from errors',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const conn =
|
||||
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||
conn.response.subscribe(null, (res: Response) => {
|
||||
expect(res.text()).toBe('{json: "object"}');
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[0].setStatusCode(404);
|
||||
existingXHRs[0].setResponseText(')]}\'\n{json: "object"}');
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should parse response headers and add them to the response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 200;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
const responseHeaderString = `Date: Fri, 20 Nov 2015 01:45:26 GMT
|
||||
Content-Type: application/json; charset=utf-8
|
||||
Transfer-Encoding: chunked
|
||||
Connection: keep-alive`;
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.headers.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT');
|
||||
expect(res.headers.get('Content-Type')).toEqual('application/json; charset=utf-8');
|
||||
expect(res.headers.get('Transfer-Encoding')).toEqual('chunked');
|
||||
expect(res.headers.get('Connection')).toEqual('keep-alive');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setResponseHeaders(responseHeaderString);
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should add the responseURL to the response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 200;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.url).toEqual('http://google.com');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setResponseURL('http://google.com');
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should add use the X-Request-URL in CORS situations',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 200;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
const responseHeaders = `X-Request-URL: http://somedomain.com
|
||||
Foo: Bar`;
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.url).toEqual('http://somedomain.com');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setResponseHeaders(responseHeaders);
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should return request url if it cannot be retrieved from response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 200;
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.url).toEqual('https://google.com');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should set the status text property from the XMLHttpRequest instance if present',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusText = 'test';
|
||||
const connection = new XHRConnection(sampleRequest, new MockBrowserXHR());
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.statusText).toBe(statusText);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusText(statusText);
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should set status text to "OK" if it is not present in XMLHttpRequest instance',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection = new XHRConnection(sampleRequest, new MockBrowserXHR());
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.statusText).toBe('OK');
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
|
||||
it('should set withCredentials to true when defined in request options for CORS situations',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const statusCode = 200;
|
||||
sampleRequest.withCredentials = true;
|
||||
const mockXhr = new MockBrowserXHR();
|
||||
const connection =
|
||||
new XHRConnection(sampleRequest, mockXhr, new ResponseOptions({status: statusCode}));
|
||||
const responseHeaders = `X-Request-URL: http://somedomain.com
|
||||
Foo: Bar`;
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.url).toEqual('http://somedomain.com');
|
||||
expect(existingXHRs[0].withCredentials).toBeTruthy();
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setResponseHeaders(responseHeaders);
|
||||
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) => {
|
||||
const statusCode = 200;
|
||||
const base = new BaseRequestOptions();
|
||||
const 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');
|
||||
}));
|
||||
|
||||
it('should not throw invalidStateError if response without body and responseType not equal to text',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(
|
||||
base.merge(new RequestOptions({responseType: ResponseContentType.Json}))),
|
||||
new MockBrowserXHR());
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.json()).toBe(null);
|
||||
async.done();
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(204);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
56
packages/http/test/base_request_options_spec.ts
Normal file
56
packages/http/test/base_request_options_spec.ts
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {describe, expect, it} from '@angular/core/testing/testing_internal';
|
||||
import {BaseRequestOptions, RequestOptions} from '../src/base_request_options';
|
||||
import {RequestMethod} from '../src/enums';
|
||||
import {Headers} from '../src/headers';
|
||||
|
||||
export function main() {
|
||||
describe('BaseRequestOptions', () => {
|
||||
it('should create a new object when calling merge', () => {
|
||||
const options1 = new BaseRequestOptions();
|
||||
const options2 = options1.merge(new RequestOptions({method: RequestMethod.Delete}));
|
||||
expect(options2).not.toBe(options1);
|
||||
expect(options2.method).toBe(RequestMethod.Delete);
|
||||
});
|
||||
|
||||
it('should retain previously merged values when merging again', () => {
|
||||
const options1 = new BaseRequestOptions();
|
||||
const options2 = options1.merge(new RequestOptions({method: RequestMethod.Delete}));
|
||||
expect(options2.method).toBe(RequestMethod.Delete);
|
||||
});
|
||||
|
||||
it('should accept search params as object', () => {
|
||||
const params = {a: 1, b: 'text', c: [1, 2, '3']};
|
||||
const options = new RequestOptions({params});
|
||||
|
||||
expect(options.params.paramsMap.size).toBe(3);
|
||||
expect(options.params.paramsMap.get('a')).toEqual(['1']);
|
||||
expect(options.params.paramsMap.get('b')).toEqual(['text']);
|
||||
expect(options.params.paramsMap.get('c')).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it('should merge search params as object', () => {
|
||||
const options1 = new BaseRequestOptions();
|
||||
const params = {a: 1, b: 'text', c: [1, 2, '3']};
|
||||
const options2 = options1.merge(new RequestOptions({params}));
|
||||
|
||||
expect(options2.params.paramsMap.size).toBe(3);
|
||||
expect(options2.params.paramsMap.get('a')).toEqual(['1']);
|
||||
expect(options2.params.paramsMap.get('b')).toEqual(['text']);
|
||||
expect(options2.params.paramsMap.get('c')).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it('should create a new headers object when calling merge', () => {
|
||||
const options1 = new RequestOptions({headers: new Headers()});
|
||||
const options2 = options1.merge();
|
||||
expect(options2.headers).not.toBe(options1.headers);
|
||||
});
|
||||
});
|
||||
}
|
192
packages/http/test/headers_spec.ts
Normal file
192
packages/http/test/headers_spec.ts
Normal file
@ -0,0 +1,192 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Headers} from '../src/headers';
|
||||
|
||||
export function main() {
|
||||
describe('Headers', () => {
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should conform to spec', () => {
|
||||
const httpHeaders = {
|
||||
'Content-Type': 'image/jpeg',
|
||||
'Accept-Charset': 'utf-8',
|
||||
'X-My-Custom-Header': 'Zeke are cool',
|
||||
};
|
||||
const secondHeaders = new Headers(httpHeaders);
|
||||
const secondHeadersObj = new Headers(secondHeaders);
|
||||
expect(secondHeadersObj.get('Content-Type')).toEqual('image/jpeg');
|
||||
});
|
||||
|
||||
it('should merge values in provided dictionary', () => {
|
||||
const headers = new Headers({'foo': 'bar'});
|
||||
expect(headers.get('foo')).toEqual('bar');
|
||||
expect(headers.getAll('foo')).toEqual(['bar']);
|
||||
});
|
||||
|
||||
it('should not alter the values of a provided header template', () => {
|
||||
// Spec at https://fetch.spec.whatwg.org/#concept-headers-fill
|
||||
// test for https://github.com/angular/angular/issues/6845
|
||||
const firstHeaders = new Headers();
|
||||
const secondHeaders = new Headers(firstHeaders);
|
||||
secondHeaders.append('Content-Type', 'image/jpeg');
|
||||
expect(firstHeaders.has('Content-Type')).toEqual(false);
|
||||
});
|
||||
|
||||
it('should preserve the list of values', () => {
|
||||
const src = new Headers();
|
||||
src.append('foo', 'a');
|
||||
src.append('foo', 'b');
|
||||
src.append('foo', 'c');
|
||||
const dst = new Headers(src);
|
||||
expect(dst.getAll('foo')).toEqual(src.getAll('foo'));
|
||||
});
|
||||
|
||||
it('should keep the last value when initialized from an object', () => {
|
||||
const headers = new Headers({
|
||||
'foo': 'first',
|
||||
'fOo': 'second',
|
||||
});
|
||||
|
||||
expect(headers.getAll('foo')).toEqual(['second']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.set()', () => {
|
||||
it('should clear all values and re-set for the provided key', () => {
|
||||
const headers = new Headers({'foo': 'bar'});
|
||||
expect(headers.get('foo')).toEqual('bar');
|
||||
|
||||
headers.set('foo', 'baz');
|
||||
expect(headers.get('foo')).toEqual('baz');
|
||||
|
||||
headers.set('fOO', 'bat');
|
||||
expect(headers.get('foo')).toEqual('bat');
|
||||
});
|
||||
|
||||
it('should preserve the case of the first call', () => {
|
||||
const headers = new Headers();
|
||||
headers.set('fOo', 'baz');
|
||||
headers.set('foo', 'bat');
|
||||
expect(JSON.stringify(headers)).toEqual('{"fOo":["bat"]}');
|
||||
});
|
||||
|
||||
it('should preserve cases after cloning', () => {
|
||||
const headers = new Headers();
|
||||
headers.set('fOo', 'baz');
|
||||
headers.set('foo', 'bat');
|
||||
expect(JSON.stringify(new Headers(headers))).toEqual('{"fOo":["bat"]}');
|
||||
});
|
||||
|
||||
it('should convert input array to string', () => {
|
||||
const headers = new Headers();
|
||||
headers.set('foo', ['bar', 'baz']);
|
||||
expect(headers.get('foo')).toEqual('bar,baz');
|
||||
expect(headers.getAll('foo')).toEqual(['bar,baz']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.get()', () => {
|
||||
it('should be case insensitive', () => {
|
||||
const headers = new Headers();
|
||||
headers.set('foo', 'baz');
|
||||
expect(headers.get('foo')).toEqual('baz');
|
||||
expect(headers.get('FOO')).toEqual('baz');
|
||||
});
|
||||
|
||||
it('should return null if the header is not present', () => {
|
||||
const headers = new Headers({bar: []});
|
||||
expect(headers.get('bar')).toEqual(null);
|
||||
expect(headers.get('foo')).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getAll()', () => {
|
||||
it('should be case insensitive', () => {
|
||||
const headers = new Headers({foo: ['bar', 'baz']});
|
||||
expect(headers.getAll('foo')).toEqual(['bar', 'baz']);
|
||||
expect(headers.getAll('FOO')).toEqual(['bar', 'baz']);
|
||||
});
|
||||
|
||||
it('should return null if the header is not present', () => {
|
||||
const headers = new Headers();
|
||||
expect(headers.getAll('foo')).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.delete', () => {
|
||||
it('should be case insensitive', () => {
|
||||
const headers = new Headers();
|
||||
|
||||
headers.set('foo', 'baz');
|
||||
expect(headers.has('foo')).toEqual(true);
|
||||
headers.delete('foo');
|
||||
expect(headers.has('foo')).toEqual(false);
|
||||
|
||||
headers.set('foo', 'baz');
|
||||
expect(headers.has('foo')).toEqual(true);
|
||||
headers.delete('FOO');
|
||||
expect(headers.has('foo')).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.append', () => {
|
||||
it('should append a value to the list', () => {
|
||||
const headers = new Headers();
|
||||
headers.append('foo', 'bar');
|
||||
headers.append('foo', 'baz');
|
||||
expect(headers.get('foo')).toEqual('bar');
|
||||
expect(headers.getAll('foo')).toEqual(['bar', 'baz']);
|
||||
});
|
||||
|
||||
it('should preserve the case of the first call', () => {
|
||||
const headers = new Headers();
|
||||
|
||||
headers.append('FOO', 'bar');
|
||||
headers.append('foo', 'baz');
|
||||
expect(JSON.stringify(headers)).toEqual('{"FOO":["bar","baz"]}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.toJSON()', () => {
|
||||
let headers: Headers;
|
||||
let values: string[];
|
||||
let ref: {[name: string]: string[]};
|
||||
|
||||
beforeEach(() => {
|
||||
values = ['application/jeisen', 'application/jason', 'application/patrickjs'];
|
||||
headers = new Headers();
|
||||
headers.set('Accept', values);
|
||||
ref = {'Accept': values};
|
||||
});
|
||||
|
||||
it('should be serializable with toJSON',
|
||||
() => { expect(JSON.stringify(headers)).toEqual(JSON.stringify(ref)); });
|
||||
|
||||
it('should be able to recreate serializedHeaders', () => {
|
||||
const parsedHeaders = JSON.parse(JSON.stringify(headers));
|
||||
const recreatedHeaders = new Headers(parsedHeaders);
|
||||
expect(JSON.stringify(parsedHeaders)).toEqual(JSON.stringify(recreatedHeaders));
|
||||
});
|
||||
});
|
||||
|
||||
describe('.fromResponseHeaderString()', () => {
|
||||
it('should parse a response header string', () => {
|
||||
const response = `Date: Fri, 20 Nov 2015 01:45:26 GMT\n` +
|
||||
`Content-Type: application/json; charset=utf-8\n` +
|
||||
`Transfer-Encoding: chunked\n` +
|
||||
`Connection: keep-alive`;
|
||||
const headers = Headers.fromResponseHeaderString(response);
|
||||
expect(headers.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT');
|
||||
expect(headers.get('Content-Type')).toEqual('application/json; charset=utf-8');
|
||||
expect(headers.get('Transfer-Encoding')).toEqual('chunked');
|
||||
expect(headers.get('Connection')).toEqual('keep-alive');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
540
packages/http/test/http_spec.ts
Normal file
540
packages/http/test/http_spec.ts
Normal file
@ -0,0 +1,540 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injector, ReflectiveInjector} from '@angular/core';
|
||||
import {TestBed, getTestBed} from '@angular/core/testing';
|
||||
import {AsyncTestCompleter, afterEach, beforeEach, describe, inject, it} from '@angular/core/testing/testing_internal';
|
||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {zip} from 'rxjs/observable/zip';
|
||||
|
||||
import {BaseRequestOptions, ConnectionBackend, Http, HttpModule, JSONPBackend, Jsonp, JsonpModule, Request, RequestMethod, RequestOptions, Response, ResponseContentType, ResponseOptions, URLSearchParams, XHRBackend} from '../index';
|
||||
import {stringToArrayBuffer} from '../src/http_utils';
|
||||
import {MockBackend, MockConnection} from '../testing/mock_backend';
|
||||
|
||||
export function main() {
|
||||
describe('injectables', () => {
|
||||
const url = 'http://foo.bar';
|
||||
let http: Http;
|
||||
let injector: Injector;
|
||||
let jsonpBackend: MockBackend;
|
||||
let xhrBackend: MockBackend;
|
||||
let jsonp: Jsonp;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpModule, JsonpModule],
|
||||
providers: [
|
||||
{provide: XHRBackend, useClass: MockBackend},
|
||||
{provide: JSONPBackend, useClass: MockBackend}
|
||||
]
|
||||
});
|
||||
injector = getTestBed();
|
||||
});
|
||||
|
||||
it('should allow using jsonpInjectables and httpInjectables in same injector',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
|
||||
http = injector.get(Http);
|
||||
jsonp = injector.get(Jsonp);
|
||||
jsonpBackend = injector.get(JSONPBackend) as MockBackend;
|
||||
xhrBackend = injector.get(XHRBackend) as any as MockBackend;
|
||||
|
||||
let xhrCreatedConnections = 0;
|
||||
let jsonpCreatedConnections = 0;
|
||||
|
||||
xhrBackend.connections.subscribe(() => {
|
||||
xhrCreatedConnections++;
|
||||
expect(xhrCreatedConnections).toEqual(1);
|
||||
if (jsonpCreatedConnections) {
|
||||
async.done();
|
||||
}
|
||||
});
|
||||
|
||||
http.get(url).subscribe(() => {});
|
||||
|
||||
jsonpBackend.connections.subscribe(() => {
|
||||
jsonpCreatedConnections++;
|
||||
expect(jsonpCreatedConnections).toEqual(1);
|
||||
if (xhrCreatedConnections) {
|
||||
async.done();
|
||||
}
|
||||
});
|
||||
|
||||
jsonp.request(url).subscribe(() => {});
|
||||
}));
|
||||
});
|
||||
|
||||
describe('http', () => {
|
||||
const url = 'http://foo.bar';
|
||||
let http: Http;
|
||||
let injector: Injector;
|
||||
let backend: MockBackend;
|
||||
let baseResponse: Response;
|
||||
let jsonp: Jsonp;
|
||||
|
||||
beforeEach(() => {
|
||||
injector = ReflectiveInjector.resolveAndCreate([
|
||||
BaseRequestOptions, MockBackend, {
|
||||
provide: Http,
|
||||
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
||||
return new Http(backend, defaultOptions);
|
||||
},
|
||||
deps: [MockBackend, BaseRequestOptions]
|
||||
},
|
||||
{
|
||||
provide: Jsonp,
|
||||
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
||||
return new Jsonp(backend, defaultOptions);
|
||||
},
|
||||
deps: [MockBackend, BaseRequestOptions]
|
||||
}
|
||||
]);
|
||||
http = injector.get(Http);
|
||||
jsonp = injector.get(Jsonp);
|
||||
backend = injector.get(MockBackend);
|
||||
baseResponse = new Response(new ResponseOptions({body: 'base response'}));
|
||||
spyOn(Http.prototype, 'request').and.callThrough();
|
||||
});
|
||||
|
||||
afterEach(() => backend.verifyNoPendingRequests());
|
||||
|
||||
describe('Http', () => {
|
||||
describe('.request()', () => {
|
||||
it('should return an Observable',
|
||||
() => { expect(http.request(url)).toBeAnInstanceOf(Observable); });
|
||||
|
||||
|
||||
it('should accept a fully-qualified request as its only parameter',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.url).toBe('https://google.com');
|
||||
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
|
||||
async.done();
|
||||
});
|
||||
http.request(new Request(new RequestOptions({url: 'https://google.com'})))
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
it('should accept a fully-qualified request as its only parameter',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.url).toBe('https://google.com');
|
||||
expect(c.request.method).toBe(RequestMethod.Post);
|
||||
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
|
||||
async.done();
|
||||
});
|
||||
http.request(new Request(new RequestOptions(
|
||||
{url: 'https://google.com', method: RequestMethod.Post})))
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
|
||||
it('should perform a get request for given url if only passed a string',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
|
||||
http.request('http://basic.connection').subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('base response');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should perform a post request for given url if options include a method',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toEqual(RequestMethod.Post);
|
||||
c.mockRespond(baseResponse);
|
||||
});
|
||||
const requestOptions = new RequestOptions({method: RequestMethod.Post});
|
||||
http.request('http://basic.connection', requestOptions).subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('base response');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should perform a post request for given url if options include a method',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toEqual(RequestMethod.Post);
|
||||
c.mockRespond(baseResponse);
|
||||
});
|
||||
const requestOptions = {method: RequestMethod.Post};
|
||||
http.request('http://basic.connection', requestOptions).subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('base response');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should perform a get request and complete the response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
|
||||
http.request('http://basic.connection')
|
||||
.subscribe(
|
||||
(res: Response) => { expect(res.text()).toBe('base response'); }, null,
|
||||
() => { async.done(); });
|
||||
}));
|
||||
|
||||
it('should perform multiple get requests and complete the responses',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
|
||||
|
||||
http.request('http://basic.connection').subscribe((res: Response) => {
|
||||
expect(res.text()).toBe('base response');
|
||||
});
|
||||
http.request('http://basic.connection')
|
||||
.subscribe(
|
||||
(res: Response) => { expect(res.text()).toBe('base response'); }, null,
|
||||
() => { async.done(); });
|
||||
}));
|
||||
|
||||
it('should throw if url is not a string or Request', () => {
|
||||
const req = <Request>{};
|
||||
expect(() => http.request(req))
|
||||
.toThrowError('First argument must be a url string or Request instance.');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('.get()', () => {
|
||||
it('should perform a get request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Get);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.get(url).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('.post()', () => {
|
||||
it('should perform a post request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Post);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.post(url, 'post me').subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
|
||||
it('should attach the provided body to the request',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const body = 'this is my post body';
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.text()).toBe(body);
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.post(url, body).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('.put()', () => {
|
||||
it('should perform a put request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Put);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.put(url, 'put me').subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
it('should attach the provided body to the request',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const body = 'this is my put body';
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.text()).toBe(body);
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.put(url, body).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('.delete()', () => {
|
||||
it('should perform a delete request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Delete);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.delete(url).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('.patch()', () => {
|
||||
it('should perform a patch request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Patch);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.patch(url, 'this is my patch body').subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
it('should attach the provided body to the request',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const body = 'this is my patch body';
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.text()).toBe(body);
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.patch(url, body).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('.head()', () => {
|
||||
it('should perform a head request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Head);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.head(url).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('.options()', () => {
|
||||
it('should perform an options request for given url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Options);
|
||||
expect(http.request).toHaveBeenCalled();
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
expect(http.request).not.toHaveBeenCalled();
|
||||
http.options(url).subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('searchParams', () => {
|
||||
it('should append search params to url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const params = new URLSearchParams();
|
||||
params.append('q', 'puppies');
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.url).toEqual('https://www.google.com?q=puppies');
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.get('https://www.google.com', new RequestOptions({search: params}))
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
it('should append string search params to url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.url).toEqual('https://www.google.com?q=piggies');
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.get('https://www.google.com', new RequestOptions({search: 'q=piggies'}))
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
it('should produce valid url when url already contains a query',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.url).toEqual('https://www.google.com?q=angular&as_eq=1.x');
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.get('https://www.google.com?q=angular', new RequestOptions({search: 'as_eq=1.x'}))
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
describe('params', () => {
|
||||
it('should append params to url',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.url).toEqual('https://www.google.com?q=puppies');
|
||||
backend.resolveAllConnections();
|
||||
async.done();
|
||||
});
|
||||
http.get('https://www.google.com', {params: {q: 'puppies'}})
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
});
|
||||
|
||||
describe('string method names', () => {
|
||||
it('should allow case insensitive strings for method names', () => {
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.method).toBe(RequestMethod.Post);
|
||||
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
|
||||
async.done();
|
||||
});
|
||||
http.request(
|
||||
new Request(new RequestOptions({url: 'https://google.com', method: 'PosT'})))
|
||||
.subscribe((res: Response) => {});
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw when invalid string parameter is passed for method name', () => {
|
||||
expect(() => {
|
||||
http.request(
|
||||
new Request(new RequestOptions({url: 'https://google.com', method: 'Invalid'})));
|
||||
}).toThrowError('Invalid request method. The method "Invalid" is not supported.');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Jsonp', () => {
|
||||
describe('.request()', () => {
|
||||
it('should throw if url is not a string or Request', () => {
|
||||
const req = <Request>{};
|
||||
expect(() => jsonp.request(req))
|
||||
.toThrowError('First argument must be a url string or Request instance.');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('response buffer', () => {
|
||||
|
||||
it('should attach the provided buffer to the response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
expect(c.request.responseType).toBe(ResponseContentType.ArrayBuffer);
|
||||
c.mockRespond(new Response(new ResponseOptions({body: new ArrayBuffer(32)})));
|
||||
async.done();
|
||||
});
|
||||
http.get(
|
||||
'https://www.google.com',
|
||||
new RequestOptions({responseType: ResponseContentType.ArrayBuffer}))
|
||||
.subscribe((res: Response) => {});
|
||||
}));
|
||||
|
||||
it('should be able to consume a buffer containing a String as any response type',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
|
||||
http.get('https://www.google.com').subscribe((res: Response) => {
|
||||
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
|
||||
expect(res.text()).toBe('base response');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should be able to consume a buffer containing an ArrayBuffer as any response type',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const arrayBuffer = stringToArrayBuffer('{"response": "ok"}');
|
||||
backend.connections.subscribe(
|
||||
(c: MockConnection) =>
|
||||
c.mockRespond(new Response(new ResponseOptions({body: arrayBuffer}))));
|
||||
http.get('https://www.google.com').subscribe((res: Response) => {
|
||||
expect(res.arrayBuffer()).toBe(arrayBuffer);
|
||||
expect(res.text()).toEqual('{"response": "ok"}');
|
||||
expect(res.json()).toEqual({response: 'ok'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should be able to consume a buffer containing an Object as any response type',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const simpleObject = {'content': 'ok'};
|
||||
backend.connections.subscribe(
|
||||
(c: MockConnection) =>
|
||||
c.mockRespond(new Response(new ResponseOptions({body: simpleObject}))));
|
||||
http.get('https://www.google.com').subscribe((res: Response) => {
|
||||
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
|
||||
expect(res.text()).toEqual(JSON.stringify(simpleObject, null, 2));
|
||||
expect(res.json()).toBe(simpleObject);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should preserve encoding of ArrayBuffer response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const message = 'é@θЂ';
|
||||
const arrayBuffer = stringToArrayBuffer(message);
|
||||
backend.connections.subscribe(
|
||||
(c: MockConnection) =>
|
||||
c.mockRespond(new Response(new ResponseOptions({body: arrayBuffer}))));
|
||||
http.get('https://www.google.com').subscribe((res: Response) => {
|
||||
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
|
||||
expect(res.text()).toEqual(message);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should preserve encoding of String response',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const message = 'é@θЂ';
|
||||
backend.connections.subscribe(
|
||||
(c: MockConnection) =>
|
||||
c.mockRespond(new Response(new ResponseOptions({body: message}))));
|
||||
http.get('https://www.google.com').subscribe((res: Response) => {
|
||||
expect(res.arrayBuffer()).toEqual(stringToArrayBuffer(message));
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should have an equivalent response independently of the buffer used',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const message = {'param': 'content'};
|
||||
|
||||
backend.connections.subscribe((c: MockConnection) => {
|
||||
const body = (): any => {
|
||||
switch (c.request.responseType) {
|
||||
case ResponseContentType.Text:
|
||||
return JSON.stringify(message, null, 2);
|
||||
case ResponseContentType.Json:
|
||||
return message;
|
||||
case ResponseContentType.ArrayBuffer:
|
||||
return stringToArrayBuffer(JSON.stringify(message, null, 2));
|
||||
}
|
||||
};
|
||||
c.mockRespond(new Response(new ResponseOptions({body: body()})));
|
||||
});
|
||||
|
||||
zip(http.get(
|
||||
'https://www.google.com',
|
||||
new RequestOptions({responseType: ResponseContentType.Text})),
|
||||
http.get(
|
||||
'https://www.google.com',
|
||||
new RequestOptions({responseType: ResponseContentType.Json})),
|
||||
http.get(
|
||||
'https://www.google.com',
|
||||
new RequestOptions({responseType: ResponseContentType.ArrayBuffer})))
|
||||
.subscribe((res: Array<any>) => {
|
||||
expect(res[0].text()).toEqual(res[1].text());
|
||||
expect(res[1].text()).toEqual(res[2].text());
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
112
packages/http/test/static_request_spec.ts
Normal file
112
packages/http/test/static_request_spec.ts
Normal file
@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {describe, expect, it} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {RequestOptions} from '../src/base_request_options';
|
||||
import {ContentType} from '../src/enums';
|
||||
import {Headers} from '../src/headers';
|
||||
import {ArrayBuffer, Request} from '../src/static_request';
|
||||
|
||||
export function main() {
|
||||
describe('Request', () => {
|
||||
describe('detectContentType', () => {
|
||||
it('should return ContentType.NONE', () => {
|
||||
const req = new Request(new RequestOptions({url: 'test', method: 'GET', body: null}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.NONE);
|
||||
});
|
||||
|
||||
it('should return ContentType.JSON', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: null,
|
||||
headers: new Headers({'content-type': 'application/json'})
|
||||
}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.JSON);
|
||||
});
|
||||
|
||||
it('should return ContentType.FORM', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: null,
|
||||
headers: new Headers({'content-type': 'application/x-www-form-urlencoded'})
|
||||
}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.FORM);
|
||||
});
|
||||
|
||||
it('should return ContentType.FORM_DATA', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: null,
|
||||
headers: new Headers({'content-type': 'multipart/form-data'})
|
||||
}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.FORM_DATA);
|
||||
});
|
||||
|
||||
it('should return ContentType.TEXT', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: null,
|
||||
headers: new Headers({'content-type': 'text/plain'})
|
||||
}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.TEXT);
|
||||
});
|
||||
|
||||
it('should return ContentType.BLOB', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: null,
|
||||
headers: new Headers({'content-type': 'application/octet-stream'})
|
||||
}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.BLOB);
|
||||
});
|
||||
|
||||
it('should not create a blob out of ArrayBuffer', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: new ArrayBuffer(1),
|
||||
headers: new Headers({'content-type': 'application/octet-stream'})
|
||||
}));
|
||||
|
||||
expect(req.detectContentType()).toEqual(ContentType.ARRAY_BUFFER);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return empty string if no body is present', () => {
|
||||
const req = new Request(new RequestOptions({
|
||||
url: 'test',
|
||||
method: 'GET',
|
||||
body: null,
|
||||
headers: new Headers({'content-type': 'application/json'})
|
||||
}));
|
||||
|
||||
expect(req.text()).toEqual('');
|
||||
});
|
||||
|
||||
it('should return empty string if body is undefined', () => {
|
||||
const reqOptions = new RequestOptions(
|
||||
{url: 'test', method: 'GET', headers: new Headers({'content-type': 'application/json'})});
|
||||
delete reqOptions.body;
|
||||
const req = new Request(reqOptions);
|
||||
|
||||
expect(req.text()).toEqual('');
|
||||
});
|
||||
});
|
||||
}
|
28
packages/http/test/static_response_spec.ts
Normal file
28
packages/http/test/static_response_spec.ts
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {describe, expect, it} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {ResponseOptions} from '../src/base_response_options';
|
||||
import {Response} from '../src/static_response';
|
||||
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('Response', () => {
|
||||
it('should be ok for 200 statuses', () => {
|
||||
expect(new Response(new ResponseOptions({status: 200})).ok).toEqual(true);
|
||||
expect(new Response(new ResponseOptions({status: 299})).ok).toEqual(true);
|
||||
});
|
||||
|
||||
it('should not be ok for non 200 statuses', () => {
|
||||
expect(new Response(new ResponseOptions({status: 199})).ok).toEqual(false);
|
||||
expect(new Response(new ResponseOptions({status: 300})).ok).toEqual(false);
|
||||
});
|
||||
});
|
||||
}
|
171
packages/http/test/url_search_params_spec.ts
Normal file
171
packages/http/test/url_search_params_spec.ts
Normal file
@ -0,0 +1,171 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {describe, expect, it} from '@angular/core/testing/testing_internal';
|
||||
import {URLSearchParams} from '../src/url_search_params';
|
||||
|
||||
export function main() {
|
||||
describe('URLSearchParams', () => {
|
||||
it('should conform to spec', () => {
|
||||
const paramsString = 'q=URLUtils.searchParams&topic=api';
|
||||
const searchParams = new URLSearchParams(paramsString);
|
||||
|
||||
// Tests borrowed from example at
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
|
||||
// Compliant with spec described at https://url.spec.whatwg.org/#urlsearchparams
|
||||
expect(searchParams.has('topic')).toBe(true);
|
||||
expect(searchParams.has('foo')).toBe(false);
|
||||
expect(searchParams.get('topic')).toEqual('api');
|
||||
expect(searchParams.getAll('topic')).toEqual(['api']);
|
||||
expect(searchParams.get('foo')).toBe(null);
|
||||
searchParams.append('topic', 'webdev');
|
||||
expect(searchParams.getAll('topic')).toEqual(['api', 'webdev']);
|
||||
expect(searchParams.toString()).toEqual('q=URLUtils.searchParams&topic=api&topic=webdev');
|
||||
searchParams.delete('topic');
|
||||
expect(searchParams.toString()).toEqual('q=URLUtils.searchParams');
|
||||
|
||||
// Test default constructor
|
||||
expect(new URLSearchParams().toString()).toBe('');
|
||||
});
|
||||
|
||||
|
||||
it('should optionally accept a custom parser', () => {
|
||||
const fooEveryThingParser = {
|
||||
encodeKey() { return 'I AM KEY'; },
|
||||
encodeValue() { return 'I AM VALUE'; }
|
||||
};
|
||||
const params = new URLSearchParams('', fooEveryThingParser);
|
||||
params.set('myKey', 'myValue');
|
||||
expect(params.toString()).toBe('I AM KEY=I AM VALUE');
|
||||
});
|
||||
|
||||
|
||||
it('should encode special characters in params', () => {
|
||||
const searchParams = new URLSearchParams();
|
||||
searchParams.append('a', '1+1');
|
||||
searchParams.append('b c', '2');
|
||||
searchParams.append('d%', '3$');
|
||||
expect(searchParams.toString()).toEqual('a=1+1&b%20c=2&d%25=3$');
|
||||
});
|
||||
|
||||
|
||||
it('should not encode allowed characters', () => {
|
||||
/*
|
||||
* https://tools.ietf.org/html/rfc3986#section-3.4
|
||||
* Allowed: ( pchar / "/" / "?" )
|
||||
* pchar: unreserved / pct-encoded / sub-delims / ":" / "@"
|
||||
* unreserved: ALPHA / DIGIT / "-" / "." / "_" / "~"
|
||||
* pct-encoded: "%" HEXDIG HEXDIG
|
||||
* sub-delims: "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
||||
*
|
||||
* & and = are excluded and should be encoded inside keys and values
|
||||
* because URLSearchParams is responsible for inserting this.
|
||||
**/
|
||||
|
||||
let params = new URLSearchParams();
|
||||
'! $ \' ( ) * + , ; A 9 - . _ ~ ? / ='.split(' ').forEach(
|
||||
(char, idx) => { params.set(`a${idx}`, char); });
|
||||
expect(params.toString())
|
||||
.toBe(
|
||||
`a0=!&a1=$&a2=\'&a3=(&a4=)&a5=*&a6=+&a7=,&a8=;&a9=A&a10=9&a11=-&a12=.&a13=_&a14=~&a15=?&a16=/&a17==`
|
||||
.replace(/\s/g, ''));
|
||||
|
||||
|
||||
// Original example from https://github.com/angular/angular/issues/9348 for posterity
|
||||
params = new URLSearchParams();
|
||||
params.set('q', 'repo:janbaer/howcani+type:issue');
|
||||
params.set('sort', 'created');
|
||||
params.set('order', 'desc');
|
||||
params.set('page', '1');
|
||||
expect(params.toString())
|
||||
.toBe('q=repo:janbaer/howcani+type:issue&sort=created&order=desc&page=1');
|
||||
});
|
||||
|
||||
|
||||
it('should support map-like merging operation via setAll()', () => {
|
||||
const mapA = new URLSearchParams('a=1&a=2&a=3&c=8');
|
||||
const mapB = new URLSearchParams('a=4&a=5&a=6&b=7');
|
||||
mapA.setAll(mapB);
|
||||
expect(mapA.has('a')).toBe(true);
|
||||
expect(mapA.has('b')).toBe(true);
|
||||
expect(mapA.has('c')).toBe(true);
|
||||
expect(mapA.getAll('a')).toEqual(['4']);
|
||||
expect(mapA.getAll('b')).toEqual(['7']);
|
||||
expect(mapA.getAll('c')).toEqual(['8']);
|
||||
expect(mapA.toString()).toEqual('a=4&c=8&b=7');
|
||||
});
|
||||
|
||||
|
||||
it('should support multimap-like merging operation via appendAll()', () => {
|
||||
const mapA = new URLSearchParams('a=1&a=2&a=3&c=8');
|
||||
const mapB = new URLSearchParams('a=4&a=5&a=6&b=7');
|
||||
mapA.appendAll(mapB);
|
||||
expect(mapA.has('a')).toBe(true);
|
||||
expect(mapA.has('b')).toBe(true);
|
||||
expect(mapA.has('c')).toBe(true);
|
||||
expect(mapA.getAll('a')).toEqual(['1', '2', '3', '4', '5', '6']);
|
||||
expect(mapA.getAll('b')).toEqual(['7']);
|
||||
expect(mapA.getAll('c')).toEqual(['8']);
|
||||
expect(mapA.toString()).toEqual('a=1&a=2&a=3&a=4&a=5&a=6&c=8&b=7');
|
||||
});
|
||||
|
||||
|
||||
it('should support multimap-like merging operation via replaceAll()', () => {
|
||||
const mapA = new URLSearchParams('a=1&a=2&a=3&c=8');
|
||||
const mapB = new URLSearchParams('a=4&a=5&a=6&b=7');
|
||||
mapA.replaceAll(mapB);
|
||||
expect(mapA.has('a')).toBe(true);
|
||||
expect(mapA.has('b')).toBe(true);
|
||||
expect(mapA.has('c')).toBe(true);
|
||||
expect(mapA.getAll('a')).toEqual(['4', '5', '6']);
|
||||
expect(mapA.getAll('b')).toEqual(['7']);
|
||||
expect(mapA.getAll('c')).toEqual(['8']);
|
||||
expect(mapA.toString()).toEqual('a=4&a=5&a=6&c=8&b=7');
|
||||
});
|
||||
|
||||
it('should support a clone operation via clone()', () => {
|
||||
const fooQueryEncoder = {
|
||||
encodeKey(k: string) { return encodeURIComponent(k); },
|
||||
encodeValue(v: string) { return encodeURIComponent(v); }
|
||||
};
|
||||
const paramsA = new URLSearchParams('', fooQueryEncoder);
|
||||
paramsA.set('a', '2');
|
||||
paramsA.set('q', '4+');
|
||||
paramsA.set('c', '8');
|
||||
const paramsB = new URLSearchParams();
|
||||
paramsB.set('a', '2');
|
||||
paramsB.set('q', '4+');
|
||||
paramsB.set('c', '8');
|
||||
expect(paramsB.toString()).toEqual('a=2&q=4+&c=8');
|
||||
const paramsC = paramsA.clone();
|
||||
expect(paramsC.has('a')).toBe(true);
|
||||
expect(paramsC.has('b')).toBe(false);
|
||||
expect(paramsC.has('c')).toBe(true);
|
||||
expect(paramsC.toString()).toEqual('a=2&q=4%2B&c=8');
|
||||
});
|
||||
|
||||
it('should remove the parameter when set to undefined or null', () => {
|
||||
const params = new URLSearchParams('q=Q');
|
||||
params.set('q', undefined);
|
||||
expect(params.has('q')).toBe(false);
|
||||
expect(params.toString()).toEqual('');
|
||||
params.set('q', null);
|
||||
expect(params.has('q')).toBe(false);
|
||||
expect(params.toString()).toEqual('');
|
||||
});
|
||||
|
||||
it('should ignore the value when append undefined or null', () => {
|
||||
const params = new URLSearchParams('q=Q');
|
||||
params.append('q', undefined);
|
||||
expect(params.toString()).toEqual('q=Q');
|
||||
params.append('q', null);
|
||||
expect(params.toString()).toEqual('q=Q');
|
||||
});
|
||||
|
||||
});
|
||||
}
|
14
packages/http/testing/src/index.ts
Normal file
14
packages/http/testing/src/index.ts
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* Entry point for all public APIs of the platform-server/testing package.
|
||||
*/
|
||||
export * from './mock_backend';
|
292
packages/http/testing/src/mock_backend.ts
Normal file
292
packages/http/testing/src/mock_backend.ts
Normal file
@ -0,0 +1,292 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Connection, ConnectionBackend, ReadyState, Request, Response} from '@angular/http';
|
||||
import {ReplaySubject} from 'rxjs/ReplaySubject';
|
||||
import {Subject} from 'rxjs/Subject';
|
||||
import {take} from 'rxjs/operator/take';
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Mock Connection to represent a {@link Connection} for tests.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class MockConnection implements Connection {
|
||||
// TODO Name `readyState` should change to be more generic, and states could be made to be more
|
||||
// descriptive than ResourceLoader states.
|
||||
/**
|
||||
* Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with
|
||||
* additional states. For example, state 5 indicates an aborted connection.
|
||||
*/
|
||||
readyState: ReadyState;
|
||||
|
||||
/**
|
||||
* {@link Request} instance used to create the connection.
|
||||
*/
|
||||
request: Request;
|
||||
|
||||
/**
|
||||
* {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a
|
||||
* response is available.
|
||||
*/
|
||||
response: ReplaySubject<Response>;
|
||||
|
||||
constructor(req: Request) {
|
||||
this.response = <any>take.call(new ReplaySubject(1), 1);
|
||||
this.readyState = ReadyState.Open;
|
||||
this.request = req;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a mock response to the connection. This response is the value that is emitted to the
|
||||
* {@link EventEmitter} returned by {@link Http}.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* var connection;
|
||||
* backend.connections.subscribe(c => connection = c);
|
||||
* http.request('data.json').subscribe(res => console.log(res.text()));
|
||||
* connection.mockRespond(new Response(new ResponseOptions({ body: 'fake response' }))); //logs
|
||||
* 'fake response'
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
mockRespond(res: Response) {
|
||||
if (this.readyState === ReadyState.Done || this.readyState === ReadyState.Cancelled) {
|
||||
throw new Error('Connection has already been resolved');
|
||||
}
|
||||
this.readyState = ReadyState.Done;
|
||||
this.response.next(res);
|
||||
this.response.complete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Not yet implemented!
|
||||
*
|
||||
* Sends the provided {@link Response} to the `downloadObserver` of the `Request`
|
||||
* associated with this connection.
|
||||
*/
|
||||
mockDownload(res: Response) {
|
||||
// this.request.downloadObserver.onNext(res);
|
||||
// if (res.bytesLoaded === res.totalBytes) {
|
||||
// this.request.downloadObserver.onCompleted();
|
||||
// }
|
||||
}
|
||||
|
||||
// TODO(jeffbcross): consider using Response type
|
||||
/**
|
||||
* Emits the provided error object as an error to the {@link Response} {@link EventEmitter}
|
||||
* returned
|
||||
* from {@link Http}.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* var connection;
|
||||
* backend.connections.subscribe(c => connection = c);
|
||||
* http.request('data.json').subscribe(res => res, err => console.log(err)));
|
||||
* connection.mockError(new Error('error'));
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
mockError(err?: Error) {
|
||||
// Matches ResourceLoader semantics
|
||||
this.readyState = ReadyState.Done;
|
||||
this.response.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A mock backend for testing the {@link Http} service.
|
||||
*
|
||||
* This class can be injected in tests, and should be used to override providers
|
||||
* to other backends, such as {@link XHRBackend}.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {Injectable, ReflectiveInjector} from '@angular/core';
|
||||
* import {async, fakeAsync, tick} from '@angular/core/testing';
|
||||
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';
|
||||
* import {Response, ResponseOptions} from '@angular/http';
|
||||
* import {MockBackend, MockConnection} from '@angular/http/testing';
|
||||
*
|
||||
* const HERO_ONE = 'HeroNrOne';
|
||||
* const HERO_TWO = 'WillBeAlwaysTheSecond';
|
||||
*
|
||||
* @Injectable()
|
||||
* class HeroService {
|
||||
* constructor(private http: Http) {}
|
||||
*
|
||||
* getHeroes(): Promise<String[]> {
|
||||
* return this.http.get('myservices.de/api/heroes')
|
||||
* .toPromise()
|
||||
* .then(response => response.json().data)
|
||||
* .catch(e => this.handleError(e));
|
||||
* }
|
||||
*
|
||||
* private handleError(error: any): Promise<any> {
|
||||
* console.error('An error occurred', error);
|
||||
* return Promise.reject(error.message || error);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* describe('MockBackend HeroService Example', () => {
|
||||
* beforeEach(() => {
|
||||
* this.injector = ReflectiveInjector.resolveAndCreate([
|
||||
* {provide: ConnectionBackend, useClass: MockBackend},
|
||||
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
||||
* Http,
|
||||
* HeroService,
|
||||
* ]);
|
||||
* this.heroService = this.injector.get(HeroService);
|
||||
* this.backend = this.injector.get(ConnectionBackend) as MockBackend;
|
||||
* this.backend.connections.subscribe((connection: any) => this.lastConnection = connection);
|
||||
* });
|
||||
*
|
||||
* it('getHeroes() should query current service url', () => {
|
||||
* this.heroService.getHeroes();
|
||||
* expect(this.lastConnection).toBeDefined('no http service connection at all?');
|
||||
* expect(this.lastConnection.request.url).toMatch(/api\/heroes$/, 'url invalid');
|
||||
* });
|
||||
*
|
||||
* it('getHeroes() should return some heroes', fakeAsync(() => {
|
||||
* let result: String[];
|
||||
* this.heroService.getHeroes().then((heroes: String[]) => result = heroes);
|
||||
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
||||
* body: JSON.stringify({data: [HERO_ONE, HERO_TWO]}),
|
||||
* })));
|
||||
* tick();
|
||||
* expect(result.length).toEqual(2, 'should contain given amount of heroes');
|
||||
* expect(result[0]).toEqual(HERO_ONE, ' HERO_ONE should be the first hero');
|
||||
* expect(result[1]).toEqual(HERO_TWO, ' HERO_TWO should be the second hero');
|
||||
* }));
|
||||
*
|
||||
* it('getHeroes() while server is down', fakeAsync(() => {
|
||||
* let result: String[];
|
||||
* let catchedError: any;
|
||||
* this.heroService.getHeroes()
|
||||
* .then((heroes: String[]) => result = heroes)
|
||||
* .catch((error: any) => catchedError = error);
|
||||
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
||||
* status: 404,
|
||||
* statusText: 'URL not Found',
|
||||
* })));
|
||||
* tick();
|
||||
* expect(result).toBeUndefined();
|
||||
* expect(catchedError).toBeDefined();
|
||||
* }));
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* This method only exists in the mock implementation, not in real Backends.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Injectable()
|
||||
export class MockBackend implements ConnectionBackend {
|
||||
/**
|
||||
* {@link EventEmitter}
|
||||
* of {@link MockConnection} instances that have been created by this backend. Can be subscribed
|
||||
* to in order to respond to connections.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {ReflectiveInjector} from '@angular/core';
|
||||
* import {fakeAsync, tick} from '@angular/core/testing';
|
||||
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';
|
||||
* import {Response, ResponseOptions} from '@angular/http';
|
||||
* import {MockBackend, MockConnection} from '@angular/http/testing';
|
||||
*
|
||||
* it('should get a response', fakeAsync(() => {
|
||||
* let connection:
|
||||
* MockConnection; // this will be set when a new connection is emitted from the
|
||||
* // backend.
|
||||
* let text: string; // this will be set from mock response
|
||||
* let injector = ReflectiveInjector.resolveAndCreate([
|
||||
* {provide: ConnectionBackend, useClass: MockBackend},
|
||||
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
||||
* Http,
|
||||
* ]);
|
||||
* let backend = injector.get(ConnectionBackend);
|
||||
* let http = injector.get(Http);
|
||||
* backend.connections.subscribe((c: MockConnection) => connection = c);
|
||||
* http.request('something.json').toPromise().then((res: any) => text = res.text());
|
||||
* connection.mockRespond(new Response(new ResponseOptions({body: 'Something'})));
|
||||
* tick();
|
||||
* expect(text).toBe('Something');
|
||||
* }));
|
||||
* ```
|
||||
*
|
||||
* This property only exists in the mock implementation, not in real Backends.
|
||||
*/
|
||||
connections: any; //<MockConnection>
|
||||
|
||||
/**
|
||||
* An array representation of `connections`. This array will be updated with each connection that
|
||||
* is created by this backend.
|
||||
*
|
||||
* This property only exists in the mock implementation, not in real Backends.
|
||||
*/
|
||||
connectionsArray: MockConnection[];
|
||||
/**
|
||||
* {@link EventEmitter} of {@link MockConnection} instances that haven't yet been resolved (i.e.
|
||||
* with a `readyState`
|
||||
* less than 4). Used internally to verify that no connections are pending via the
|
||||
* `verifyNoPendingRequests` method.
|
||||
*
|
||||
* This property only exists in the mock implementation, not in real Backends.
|
||||
*/
|
||||
pendingConnections: any; // Subject<MockConnection>
|
||||
constructor() {
|
||||
this.connectionsArray = [];
|
||||
this.connections = new Subject();
|
||||
this.connections.subscribe(
|
||||
(connection: MockConnection) => this.connectionsArray.push(connection));
|
||||
this.pendingConnections = new Subject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks all connections, and raises an exception if any connection has not received a response.
|
||||
*
|
||||
* This method only exists in the mock implementation, not in real Backends.
|
||||
*/
|
||||
verifyNoPendingRequests() {
|
||||
let pending = 0;
|
||||
this.pendingConnections.subscribe((c: MockConnection) => pending++);
|
||||
if (pending > 0) throw new Error(`${pending} pending connections to be resolved`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve
|
||||
* connections, if it's expected that there are connections that have not yet received a response.
|
||||
*
|
||||
* This method only exists in the mock implementation, not in real Backends.
|
||||
*/
|
||||
resolveAllConnections() { this.connections.subscribe((c: MockConnection) => c.readyState = 4); }
|
||||
|
||||
/**
|
||||
* Creates a new {@link MockConnection}. This is equivalent to calling `new
|
||||
* MockConnection()`, except that it also will emit the new `Connection` to the `connections`
|
||||
* emitter of this `MockBackend` instance. This method will usually only be used by tests
|
||||
* against the framework itself, not by end-users.
|
||||
*/
|
||||
createConnection(req: Request): MockConnection {
|
||||
if (!req || !(req instanceof Request)) {
|
||||
throw new Error(`createConnection requires an instance of Request, got ${req}`);
|
||||
}
|
||||
const connection = new MockConnection(req);
|
||||
this.connections.next(connection);
|
||||
return connection;
|
||||
}
|
||||
}
|
16
packages/http/testing/tsconfig-build.json
Normal file
16
packages/http/testing/tsconfig-build.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "./tsconfig-build",
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"@angular/core": ["../../../dist/packages-dist/core"],
|
||||
"@angular/http": ["../../../dist/packages-dist/http"],
|
||||
"rxjs/*": ["../../../node_modules/rxjs/*"]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"testing/index.ts"
|
||||
],
|
||||
"angularCompilerOptions": {
|
||||
"strictMetadataEmit": true
|
||||
}
|
||||
}
|
32
packages/http/tsconfig-build.json
Normal file
32
packages/http/tsconfig-build.json
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"declaration": true,
|
||||
"stripInternal": true,
|
||||
"experimentalDecorators": true,
|
||||
"module": "es2015",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../../../dist/packages-dist/http",
|
||||
"paths": {
|
||||
"@angular/core": ["../../../dist/packages-dist/core"],
|
||||
"@angular/common": ["../../../dist/packages-dist/common"],
|
||||
"@angular/platform-browser": ["../../../dist/packages-dist/platform-browser"]
|
||||
},
|
||||
"rootDir": ".",
|
||||
"sourceMap": true,
|
||||
"inlineSources": true,
|
||||
"target": "es2015",
|
||||
"skipLibCheck": true,
|
||||
"lib": ["es2015", "dom"]
|
||||
},
|
||||
"files": [
|
||||
"public_api.ts",
|
||||
"../../../node_modules/zone.js/dist/zone.js.d.ts"
|
||||
],
|
||||
"angularCompilerOptions": {
|
||||
"annotateForClosureCompiler": true,
|
||||
"strictMetadataEmit": true,
|
||||
"flatModuleOutFile": "index.js",
|
||||
"flatModuleId": "@angular/http"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user