2015-09-04 01:01:36 -04:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-07-28 16:10:25 -04:00
|
|
|
import {Request} from '../static_request';
|
|
|
|
import {Response} from '../static_response';
|
|
|
|
import {ReadyStates} from '../enums';
|
|
|
|
import {Connection, ConnectionBackend} from '../interfaces';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {ObservableWrapper, EventEmitter} from 'angular2/src/core/facade/async';
|
|
|
|
import {isPresent} from 'angular2/src/core/facade/lang';
|
2015-09-10 18:25:36 -04:00
|
|
|
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
|
|
|
/**
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
2015-06-24 03:27:07 -04:00
|
|
|
* Mock Connection to represent a {@link Connection} for tests.
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
2015-04-29 02:07:55 -04:00
|
|
|
**/
|
2015-08-26 14:41:41 -04:00
|
|
|
export class MockConnection implements Connection {
|
2015-06-09 18:18:57 -04:00
|
|
|
// TODO Name `readyState` should change to be more generic, and states could be made to be more
|
|
|
|
// descriptive than XHR states.
|
2015-04-29 02:07:55 -04:00
|
|
|
/**
|
2015-06-09 18:18:57 -04:00
|
|
|
* Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with
|
|
|
|
* additional states. For example, state 5 indicates an aborted connection.
|
|
|
|
*/
|
|
|
|
readyState: ReadyStates;
|
2015-04-29 02:07:55 -04:00
|
|
|
|
|
|
|
/**
|
2015-06-09 18:18:57 -04:00
|
|
|
* {@link Request} instance used to create the connection.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
request: Request;
|
2015-06-09 18:18:57 -04:00
|
|
|
|
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a
|
|
|
|
* response is available.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
response: EventEmitter;
|
2015-04-29 02:07:55 -04:00
|
|
|
|
|
|
|
constructor(req: Request) {
|
2015-06-19 15:14:12 -04:00
|
|
|
this.response = new EventEmitter();
|
2015-08-26 16:40:12 -04:00
|
|
|
this.readyState = ReadyStates.Open;
|
2015-04-29 02:07:55 -04:00
|
|
|
this.request = req;
|
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Changes the `readyState` of the connection to a custom state of 5 (cancelled).
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
dispose() {
|
2015-08-26 16:40:12 -04:00
|
|
|
if (this.readyState !== ReadyStates.Done) {
|
|
|
|
this.readyState = ReadyStates.Cancelled;
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-09 18:18:57 -04:00
|
|
|
* Sends a mock response to the connection. This response is the value that is emitted to the
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link EventEmitter} returned by {@link Http}.
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* var connection;
|
|
|
|
* backend.connections.subscribe(c => connection = c);
|
|
|
|
* http.request('data.json').subscribe(res => console.log(res.text()));
|
|
|
|
* connection.mockRespond(new Response('fake response')); //logs 'fake response'
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
mockRespond(res: Response) {
|
2015-08-26 16:40:12 -04:00
|
|
|
if (this.readyState === ReadyStates.Done || this.readyState === ReadyStates.Cancelled) {
|
2015-06-19 15:14:12 -04:00
|
|
|
throw new BaseException('Connection has already been resolved');
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
2015-08-26 16:40:12 -04:00
|
|
|
this.readyState = ReadyStates.Done;
|
2015-06-19 15:14:12 -04:00
|
|
|
ObservableWrapper.callNext(this.response, res);
|
|
|
|
ObservableWrapper.callReturn(this.response);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Not yet implemented!
|
|
|
|
*
|
|
|
|
* Sends the provided {@link Response} to the `downloadObserver` of the `Request`
|
|
|
|
* associated with this connection.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
mockDownload(res: Response) {
|
2015-06-09 18:18:57 -04:00
|
|
|
// this.request.downloadObserver.onNext(res);
|
|
|
|
// if (res.bytesLoaded === res.totalBytes) {
|
|
|
|
// this.request.downloadObserver.onCompleted();
|
|
|
|
// }
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
// TODO(jeffbcross): consider using Response type
|
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* Emits the provided error object as an error to the {@link Response} {@link EventEmitter}
|
|
|
|
* returned
|
2015-06-09 18:18:57 -04:00
|
|
|
* from {@link Http}.
|
|
|
|
*/
|
2015-07-07 23:03:00 -04:00
|
|
|
mockError(err?: Error) {
|
2015-04-29 02:07:55 -04:00
|
|
|
// Matches XHR semantics
|
2015-08-26 16:40:12 -04:00
|
|
|
this.readyState = ReadyStates.Done;
|
2015-06-19 15:14:12 -04:00
|
|
|
ObservableWrapper.callThrow(this.response, err);
|
|
|
|
ObservableWrapper.callReturn(this.response);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* A mock backend for testing the {@link Http} service.
|
|
|
|
*
|
|
|
|
* This class can be injected in tests, and should be used to override bindings
|
|
|
|
* to other backends, such as {@link XHRBackend}.
|
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
2015-08-20 17:28:25 -04:00
|
|
|
* import {MockBackend, DefaultOptions, Http} from 'angular2/http';
|
2015-06-09 18:18:57 -04:00
|
|
|
* it('should get some data', inject([AsyncTestCompleter], (async) => {
|
|
|
|
* var connection;
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* MockBackend,
|
|
|
|
* bind(Http).toFactory((backend, defaultOptions) => {
|
|
|
|
* return new Http(backend, defaultOptions)
|
|
|
|
* }, [MockBackend, DefaultOptions])]);
|
|
|
|
* var http = injector.get(Http);
|
|
|
|
* var backend = injector.get(MockBackend);
|
|
|
|
* //Assign any newly-created connection to local variable
|
|
|
|
* backend.connections.subscribe(c => connection = c);
|
|
|
|
* http.request('data.json').subscribe((res) => {
|
|
|
|
* expect(res.text()).toBe('awesome');
|
|
|
|
* async.done();
|
|
|
|
* });
|
|
|
|
* connection.mockRespond(new Response('awesome'));
|
|
|
|
* }));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This method only exists in the mock implementation, not in real Backends.
|
|
|
|
**/
|
2015-04-29 02:07:55 -04:00
|
|
|
@Injectable()
|
2015-08-26 14:41:41 -04:00
|
|
|
export class MockBackend implements ConnectionBackend {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link EventEmitter}
|
2015-06-09 18:18:57 -04:00
|
|
|
* of {@link MockConnection} instances that have been created by this backend. Can be subscribed
|
|
|
|
* to in order to respond to connections.
|
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
2015-08-20 17:28:25 -04:00
|
|
|
* import {MockBackend, Http, BaseRequestOptions} from 'angular2/http';
|
2015-09-04 01:01:36 -04:00
|
|
|
* import {Injector} from 'angular2/core';
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* it('should get a response', () => {
|
|
|
|
* var connection; //this will be set when a new connection is emitted from the backend.
|
|
|
|
* var text; //this will be set from mock response
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* MockBackend,
|
|
|
|
* bind(Http).toFactory(backend, options) {
|
|
|
|
* return new Http(backend, options);
|
|
|
|
* }, [MockBackend, BaseRequestOptions]]);
|
|
|
|
* var backend = injector.get(MockBackend);
|
|
|
|
* var http = injector.get(Http);
|
|
|
|
* backend.connections.subscribe(c => connection = c);
|
|
|
|
* http.request('something.json').subscribe(res => {
|
|
|
|
* text = res.text();
|
|
|
|
* });
|
2015-06-24 03:27:07 -04:00
|
|
|
* connection.mockRespond(new Response({body: 'Something'}));
|
2015-06-09 18:18:57 -04:00
|
|
|
* expect(text).toBe('Something');
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This property only exists in the mock implementation, not in real Backends.
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
connections: EventEmitter; //<MockConnection>
|
2015-06-09 18:18:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-08-28 14:29:19 -04:00
|
|
|
connectionsArray: MockConnection[];
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link EventEmitter} of {@link MockConnection} instances that haven't yet been resolved (i.e.
|
|
|
|
* with a `readyState`
|
2015-06-09 18:18:57 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
pendingConnections: EventEmitter; //<MockConnection>
|
2015-04-29 02:07:55 -04:00
|
|
|
constructor() {
|
|
|
|
this.connectionsArray = [];
|
2015-06-19 15:14:12 -04:00
|
|
|
this.connections = new EventEmitter();
|
2015-07-07 23:03:00 -04:00
|
|
|
ObservableWrapper.subscribe<MockConnection>(
|
|
|
|
this.connections, connection => this.connectionsArray.push(connection));
|
2015-06-19 15:14:12 -04:00
|
|
|
this.pendingConnections = new EventEmitter();
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
verifyNoPendingRequests() {
|
|
|
|
let pending = 0;
|
2015-06-19 15:14:12 -04:00
|
|
|
ObservableWrapper.subscribe(this.pendingConnections, c => pending++);
|
|
|
|
if (pending > 0) throw new BaseException(`${pending} pending connections to be resolved`);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-07-07 23:03:00 -04:00
|
|
|
resolveAllConnections() {
|
|
|
|
ObservableWrapper.subscribe<MockConnection>(this.connections, c => c.readyState = 4);
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Creates a new {@link MockConnection}. This is equivalent to calling `new
|
|
|
|
* MockConnection()`, except that it also will emit the new `Connection` to the `connections`
|
2015-06-24 03:27:07 -04:00
|
|
|
* emitter of this `MockBackend` instance. This method will usually only be used by tests
|
2015-06-09 18:18:57 -04:00
|
|
|
* against the framework itself, not by end-users.
|
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
createConnection(req: Request): Connection {
|
2015-06-19 15:14:12 -04:00
|
|
|
if (!isPresent(req) || !(req instanceof Request)) {
|
|
|
|
throw new BaseException(`createConnection requires an instance of Request, got ${req}`);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
2015-06-09 18:18:57 -04:00
|
|
|
let connection = new MockConnection(req);
|
2015-06-19 15:14:12 -04:00
|
|
|
ObservableWrapper.callNext(this.connections, connection);
|
2015-04-29 02:07:55 -04:00
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
}
|