blob: 9a84a90dd0d590581d645df2848673c925a466bd [file] [log] [blame]
{"version":3,"file":"testing.js","sources":["../../../../../../packages/http/testing/src/mock_backend.ts","../../../../../../packages/http/testing/src/testing.ts","../../../../../../packages/http/testing/public_api.ts","../../../../../../packages/http/testing/index.ts","../../../../../../packages/http/testing/testing.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\nimport {Connection, ConnectionBackend, ReadyState, Request, Response} from '@angular/http';\nimport {ReplaySubject, Subject} from 'rxjs';\nimport {take} from 'rxjs/operators';\n\n\n/**\n *\n * Mock Connection to represent a {@link Connection} for tests.\n *\n * @usageNotes\n * ### Example of `mockRespond()`\n *\n * ```\n * var connection;\n * backend.connections.subscribe(c => connection = c);\n * http.request('data.json').subscribe(res => console.log(res.text()));\n * connection.mockRespond(new Response(new ResponseOptions({ body: 'fake response' }))); //logs\n * 'fake response'\n * ```\n *\n * ### Example of `mockError()`\n *\n * ```\n * var connection;\n * backend.connections.subscribe(c => connection = c);\n * http.request('data.json').subscribe(res => res, err => console.log(err)));\n * connection.mockError(new Error('error'));\n * ```\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class MockConnection implements Connection {\n // TODO: Name `readyState` should change to be more generic, and states could be made to be more\n // descriptive than ResourceLoader states.\n /**\n * Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with\n * additional states. For example, state 5 indicates an aborted connection.\n */\n readyState: ReadyState;\n\n /**\n * {@link Request} instance used to create the connection.\n */\n request: Request;\n\n /**\n * {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a\n * response is available.\n */\n response: ReplaySubject<Response>;\n\n constructor(req: Request) {\n this.response = <any>new ReplaySubject(1).pipe(take(1));\n this.readyState = ReadyState.Open;\n this.request = req;\n }\n\n /**\n * Sends a mock response to the connection. This response is the value that is emitted to the\n * {@link EventEmitter} returned by {@link Http}.\n *\n */\n mockRespond(res: Response) {\n if (this.readyState === ReadyState.Done || this.readyState === ReadyState.Cancelled) {\n throw new Error('Connection has already been resolved');\n }\n this.readyState = ReadyState.Done;\n this.response.next(res);\n this.response.complete();\n }\n\n /**\n * Not yet implemented!\n *\n * Sends the provided {@link Response} to the `downloadObserver` of the `Request`\n * associated with this connection.\n */\n mockDownload(res: Response) {\n // this.request.downloadObserver.onNext(res);\n // if (res.bytesLoaded === res.totalBytes) {\n // this.request.downloadObserver.onCompleted();\n // }\n }\n\n // TODO(jeffbcross): consider using Response type\n /**\n * Emits the provided error object as an error to the {@link Response} {@link EventEmitter}\n * returned\n * from {@link Http}.\n *\n */\n mockError(err?: Error) {\n // Matches ResourceLoader semantics\n this.readyState = ReadyState.Done;\n this.response.error(err);\n }\n}\n\n/**\n * A mock backend for testing the {@link Http} service.\n *\n * This class can be injected in tests, and should be used to override providers\n * to other backends, such as {@link XHRBackend}.\n *\n * @usageNotes\n * ### Example\n *\n * ```\n * import {Injectable, Injector} from '@angular/core';\n * import {async, fakeAsync, tick} from '@angular/core/testing';\n * import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';\n * import {Response, ResponseOptions} from '@angular/http';\n * import {MockBackend, MockConnection} from '@angular/http/testing';\n *\n * const HERO_ONE = 'HeroNrOne';\n * const HERO_TWO = 'WillBeAlwaysTheSecond';\n *\n * @Injectable()\n * class HeroService {\n * constructor(private http: Http) {}\n *\n * getHeroes(): Promise<String[]> {\n * return this.http.get('myservices.de/api/heroes')\n * .toPromise()\n * .then(response => response.json().data)\n * .catch(e => this.handleError(e));\n * }\n *\n * private handleError(error: any): Promise<any> {\n * console.error('An error occurred', error);\n * return Promise.reject(error.message || error);\n * }\n * }\n *\n * describe('MockBackend HeroService Example', () => {\n * beforeEach(() => {\n * this.injector = Injector.create([\n * {provide: ConnectionBackend, useClass: MockBackend},\n * {provide: RequestOptions, useClass: BaseRequestOptions},\n * Http,\n * HeroService,\n * ]);\n * this.heroService = this.injector.get(HeroService);\n * this.backend = this.injector.get(ConnectionBackend) as MockBackend;\n * this.backend.connections.subscribe((connection: any) => this.lastConnection = connection);\n * });\n *\n * it('getHeroes() should query current service url', () => {\n * this.heroService.getHeroes();\n * expect(this.lastConnection).toBeDefined('no http service connection at all?');\n * expect(this.lastConnection.request.url).toMatch(/api\\/heroes$/, 'url invalid');\n * });\n *\n * it('getHeroes() should return some heroes', fakeAsync(() => {\n * let result: String[];\n * this.heroService.getHeroes().then((heroes: String[]) => result = heroes);\n * this.lastConnection.mockRespond(new Response(new ResponseOptions({\n * body: JSON.stringify({data: [HERO_ONE, HERO_TWO]}),\n * })));\n * tick();\n * expect(result.length).toEqual(2, 'should contain given amount of heroes');\n * expect(result[0]).toEqual(HERO_ONE, ' HERO_ONE should be the first hero');\n * expect(result[1]).toEqual(HERO_TWO, ' HERO_TWO should be the second hero');\n * }));\n *\n * it('getHeroes() while server is down', fakeAsync(() => {\n * let result: String[];\n * let catchedError: any;\n * this.heroService.getHeroes()\n * .then((heroes: String[]) => result = heroes)\n * .catch((error: any) => catchedError = error);\n * this.lastConnection.mockError(new Response(new ResponseOptions({\n * status: 404,\n * statusText: 'URL not Found',\n * })));\n * tick();\n * expect(result).toBeUndefined();\n * expect(catchedError).toBeDefined();\n * }));\n * });\n * ```\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\n@Injectable()\nexport class MockBackend implements ConnectionBackend {\n /**\n * {@link EventEmitter}\n * of {@link MockConnection} instances that have been created by this backend. Can be subscribed\n * to in order to respond to connections.\n *\n * This property only exists in the mock implementation, not in real Backends.\n */\n connections: any; //<MockConnection>\n\n /**\n * An array representation of `connections`. This array will be updated with each connection that\n * is created by this backend.\n *\n * This property only exists in the mock implementation, not in real Backends.\n */\n connectionsArray: MockConnection[];\n /**\n * {@link EventEmitter} of {@link MockConnection} instances that haven't yet been resolved (i.e.\n * with a `readyState`\n * less than 4). Used internally to verify that no connections are pending via the\n * `verifyNoPendingRequests` method.\n *\n * This property only exists in the mock implementation, not in real Backends.\n */\n pendingConnections: any; // Subject<MockConnection>\n constructor() {\n this.connectionsArray = [];\n this.connections = new Subject();\n this.connections.subscribe(\n (connection: MockConnection) => this.connectionsArray.push(connection));\n this.pendingConnections = new Subject();\n }\n\n /**\n * Checks all connections, and raises an exception if any connection has not received a response.\n *\n * This method only exists in the mock implementation, not in real Backends.\n */\n verifyNoPendingRequests() {\n let pending = 0;\n this.pendingConnections.subscribe((c: MockConnection) => pending++);\n if (pending > 0) throw new Error(`${pending} pending connections to be resolved`);\n }\n\n /**\n * Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve\n * connections, if it's expected that there are connections that have not yet received a response.\n *\n * This method only exists in the mock implementation, not in real Backends.\n */\n resolveAllConnections() { this.connections.subscribe((c: MockConnection) => c.readyState = 4); }\n\n /**\n * Creates a new {@link MockConnection}. This is equivalent to calling `new\n * MockConnection()`, except that it also will emit the new `Connection` to the `connections`\n * emitter of this `MockBackend` instance. This method will usually only be used by tests\n * against the framework itself, not by end-users.\n */\n createConnection(req: Request): MockConnection {\n if (!req || !(req instanceof Request)) {\n throw new Error(`createConnection requires an instance of Request, got ${req}`);\n }\n const connection = new MockConnection(req);\n this.connections.next(connection);\n return connection;\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n/**\n * @module\n * @description\n * Entry point for all public APIs of the platform-server/testing package.\n */\nexport * from './mock_backend';\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n/**\n * @module\n * @description\n * Entry point for all public APIs of this package.\n */\nexport * from './src/testing';\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n/**\n * @module\n * @description\n * Entry point for all public APIs of the http/testing package.\n */\n\nexport * from './public_api';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA;IAoBE,wBAAY,GAAY;QACtB,IAAI,CAAC,QAAQ,GAAQ,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACxD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,GAAG,CAAC;KACpB;;;;;;IAOD,oCAAW,GAAX,UAAY,GAAa;QACvB,IAAI,IAAI,CAAC,UAAU,KAAK,UAAU,CAAC,IAAI,IAAI,IAAI,CAAC,UAAU,KAAK,UAAU,CAAC,SAAS,EAAE;YACnF,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;SACzD;QACD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACxB,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;KAC1B;;;;;;;IAQD,qCAAY,GAAZ,UAAa,GAAa;;;;;KAKzB;;;;;;;;IASD,kCAAS,GAAT,UAAU,GAAW;;QAEnB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAC1B;IACH,qBAAC;CAAA,IAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwFA;IA0BE;QAAA,iBAMC;QALC,IAAI,CAAC,gBAAgB,GAAG,EAAE,CAAC;QAC3B,IAAI,CAAC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