From 5725f717774dbd6b53de3d62fe4032d43b6fb29e Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Thu, 30 Jul 2015 16:29:16 +0200 Subject: [PATCH] fix(http): allow using JSONP_INJECTABLES and HTTP_INJECTABLES in same injector Fixes #3365 Closes #3390 --- modules/http/http.ts | 19 +++++++---- modules/http/test/http_spec.ts | 62 ++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/modules/http/http.ts b/modules/http/http.ts index 6c77fbbe15..21eebeeca8 100644 --- a/modules/http/http.ts +++ b/modules/http/http.ts @@ -62,19 +62,26 @@ export {URLSearchParams} from './src/url_search_params'; * */ export const HTTP_BINDINGS: List = [ - bind(ConnectionBackend) - .toClass(XHRBackend), + // TODO(pascal): use factory type annotations once supported in DI + // issue: https://github.com/angular/angular/issues/3183 + bind(Http) + .toFactory((xhrBackend, requestOptions) => { return new Http(xhrBackend, requestOptions);}, + [XHRBackend, RequestOptions]), BrowserXhr, bind(RequestOptions).toClass(BaseRequestOptions), bind(ResponseOptions).toClass(BaseResponseOptions), - Http + XHRBackend ]; export const JSONP_BINDINGS: List = [ - bind(ConnectionBackend) - .toClass(JSONPBackend), + // TODO(pascal): use factory type annotations once supported in DI + // issue: https://github.com/angular/angular/issues/3183 + bind(Jsonp) + .toFactory( + (jsonpBackend, requestOptions) => { return new Jsonp(jsonpBackend, requestOptions);}, + [JSONPBackend, RequestOptions]), BrowserJsonp, bind(RequestOptions).toClass(BaseRequestOptions), bind(ResponseOptions).toClass(BaseResponseOptions), - Jsonp + JSONPBackend ]; diff --git a/modules/http/test/http_spec.ts b/modules/http/test/http_spec.ts index c9fb435f87..0b8ffc7961 100644 --- a/modules/http/test/http_spec.ts +++ b/modules/http/test/http_spec.ts @@ -17,13 +17,18 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; import { BaseRequestOptions, ConnectionBackend, - Http, Request, RequestMethods, RequestOptions, Response, ResponseOptions, - URLSearchParams + URLSearchParams, + JSONP_BINDINGS, + HTTP_BINDINGS, + XHRBackend, + JSONPBackend, + Http, + Jsonp } from 'http/http'; class SpyObserver extends SpyObject { @@ -39,6 +44,59 @@ class SpyObserver extends SpyObject { } export function main() { + describe('injectables', () => { + var url = 'http://foo.bar'; + var http: Http; + var parentInjector: Injector; + var childInjector: Injector; + var jsonpBackend: MockBackend; + var xhrBackend: MockBackend; + var jsonp: Jsonp; + var http: Http; + + it('should allow using jsonpInjectables and httpInjectables in same injector', + inject([AsyncTestCompleter], (async) => { + parentInjector = Injector.resolveAndCreate( + [bind(XHRBackend).toClass(MockBackend), bind(JSONPBackend).toClass(MockBackend)]); + + childInjector = parentInjector.resolveAndCreateChild([ + HTTP_BINDINGS, + JSONP_BINDINGS, + bind(XHRBackend).toClass(MockBackend), + bind(JSONPBackend).toClass(MockBackend) + ]); + + http = childInjector.get(Http); + jsonp = childInjector.get(Jsonp); + jsonpBackend = childInjector.get(JSONPBackend); + xhrBackend = childInjector.get(XHRBackend); + + var xhrCreatedConnections = 0; + var jsonpCreatedConnections = 0; + + + ObservableWrapper.subscribe(xhrBackend.connections, () => { + xhrCreatedConnections++; + expect(xhrCreatedConnections).toEqual(1); + if (jsonpCreatedConnections) { + async.done(); + } + }); + + ObservableWrapper.subscribe(http.get(url), () => {}); + + ObservableWrapper.subscribe(jsonpBackend.connections, () => { + jsonpCreatedConnections++; + expect(jsonpCreatedConnections).toEqual(1); + if (xhrCreatedConnections) { + async.done(); + } + }); + + ObservableWrapper.subscribe(jsonp.request(url), () => {}); + })); + }); + describe('http', () => { var url = 'http://foo.bar'; var http: Http;