2015-11-05 17:07:57 -05:00
|
|
|
import {XHR} from 'angular2/src/compiler/xhr';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {isBlank, isPresent, normalizeBlank} from 'angular2/src/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
|
|
|
import {PromiseCompleter, PromiseWrapper, Promise} from 'angular2/src/facade/async';
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2015-03-31 10:55:46 -04:00
|
|
|
export class MockXHR extends XHR {
|
2015-10-06 20:11:10 -04:00
|
|
|
private _expectations: _Expectation[] = [];
|
2015-09-29 14:11:06 -04:00
|
|
|
private _definitions = new Map<string, string>();
|
2015-10-06 20:11:10 -04:00
|
|
|
private _requests: _PendingRequest[] = [];
|
2015-01-30 03:43:21 -05:00
|
|
|
|
|
|
|
get(url: string): Promise<string> {
|
|
|
|
var request = new _PendingRequest(url);
|
2015-06-17 14:17:21 -04:00
|
|
|
this._requests.push(request);
|
2015-01-30 03:43:21 -05:00
|
|
|
return request.getPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(url: string, response: string) {
|
|
|
|
var expectation = new _Expectation(url, response);
|
2015-06-17 14:17:21 -04:00
|
|
|
this._expectations.push(expectation);
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
when(url: string, response: string) { this._definitions.set(url, response); }
|
2015-01-30 03:43:21 -05:00
|
|
|
|
|
|
|
flush() {
|
|
|
|
if (this._requests.length === 0) {
|
|
|
|
throw new BaseException('No pending requests to flush');
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2015-10-07 12:09:43 -04:00
|
|
|
this._processRequest(this._requests.shift());
|
2015-01-30 03:43:21 -05:00
|
|
|
} while (this._requests.length > 0);
|
|
|
|
|
|
|
|
this.verifyNoOustandingExpectations();
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyNoOustandingExpectations() {
|
|
|
|
if (this._expectations.length === 0) return;
|
|
|
|
|
|
|
|
var urls = [];
|
|
|
|
for (var i = 0; i < this._expectations.length; i++) {
|
|
|
|
var expectation = this._expectations[i];
|
2015-06-17 14:17:21 -04:00
|
|
|
urls.push(expectation.url);
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 20:11:10 -04:00
|
|
|
throw new BaseException(`Unsatisfied requests: ${urls.join(', ')}`);
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
2015-05-20 20:19:46 -04:00
|
|
|
private _processRequest(request: _PendingRequest) {
|
2015-01-30 03:43:21 -05:00
|
|
|
var url = request.url;
|
|
|
|
|
|
|
|
if (this._expectations.length > 0) {
|
|
|
|
var expectation = this._expectations[0];
|
2015-02-24 10:05:45 -05:00
|
|
|
if (expectation.url == url) {
|
2015-01-30 03:43:21 -05:00
|
|
|
ListWrapper.remove(this._expectations, expectation);
|
|
|
|
request.complete(expectation.response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:42:56 -04:00
|
|
|
if (this._definitions.has(url)) {
|
2015-06-17 19:21:40 -04:00
|
|
|
var response = this._definitions.get(url);
|
2015-01-30 03:43:21 -05:00
|
|
|
request.complete(normalizeBlank(response));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new BaseException(`Unexpected request ${url}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PendingRequest {
|
|
|
|
url: string;
|
2015-07-24 20:24:28 -04:00
|
|
|
completer: PromiseCompleter<string>;
|
2015-01-30 03:43:21 -05:00
|
|
|
|
|
|
|
constructor(url) {
|
|
|
|
this.url = url;
|
|
|
|
this.completer = PromiseWrapper.completer();
|
|
|
|
}
|
|
|
|
|
|
|
|
complete(response: string) {
|
|
|
|
if (isBlank(response)) {
|
2015-05-19 15:48:00 -04:00
|
|
|
this.completer.reject(`Failed to load ${this.url}`, null);
|
2015-01-30 03:43:21 -05:00
|
|
|
} else {
|
2015-02-25 06:46:50 -05:00
|
|
|
this.completer.resolve(response);
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:19:46 -04:00
|
|
|
getPromise(): Promise<string> { return this.completer.promise; }
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class _Expectation {
|
|
|
|
url: string;
|
|
|
|
response: string;
|
|
|
|
constructor(url: string, response: string) {
|
|
|
|
this.url = url;
|
|
|
|
this.response = response;
|
|
|
|
}
|
|
|
|
}
|