fix(platform-server): provide XhrFactory for HttpClient

This commit is contained in:
Alex Rickabaugh 2017-07-18 12:45:47 -07:00
parent 5c62e300e1
commit 807648251f
2 changed files with 55 additions and 5 deletions

View File

@ -11,7 +11,7 @@ const xhr2: any = require('xhr2');
import {Injectable, Optional, Provider} from '@angular/core';
import {BrowserXhr, Connection, ConnectionBackend, Http, ReadyState, Request, RequestOptions, Response, XHRBackend, XSRFStrategy} from '@angular/http';
import {HttpClient, HttpRequest, HttpHandler, HttpInterceptor, HttpResponse, HTTP_INTERCEPTORS, HttpBackend, XhrFactory, ɵinterceptingHandler as interceptingHandler} from '@angular/common/http';
import {HttpClient, HttpEvent, HttpRequest, HttpHandler, HttpInterceptor, HttpResponse, HTTP_INTERCEPTORS, HttpBackend, XhrFactory, ɵinterceptingHandler as interceptingHandler} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
@ -143,12 +143,12 @@ export class ZoneMacroTaskBackend implements ConnectionBackend {
}
export class ZoneClientBackend extends
ZoneMacroTaskWrapper<HttpRequest<any>, HttpResponse<any>> implements HttpBackend {
ZoneMacroTaskWrapper<HttpRequest<any>, HttpEvent<any>> implements HttpBackend {
constructor(private backend: HttpBackend) { super(); }
handle(request: HttpRequest<any>): Observable<HttpResponse<any>> { return this.wrap(request); }
handle(request: HttpRequest<any>): Observable<HttpEvent<any>> { return this.wrap(request); }
protected delegate(request: HttpRequest<any>): Observable<HttpResponse<any>> {
protected delegate(request: HttpRequest<any>): Observable<HttpEvent<any>> {
return this.backend.handle(request);
}
}
@ -167,7 +167,7 @@ export function zoneWrappedInterceptingHandler(
export const SERVER_HTTP_PROVIDERS: Provider[] = [
{provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions]},
{provide: BrowserXhr, useClass: ServerXhr}, {provide: XSRFStrategy, useClass: ServerXsrfStrategy},
{
{provide: XhrFactory, useClass: ServerXhr}, {
provide: HttpHandler,
useFactory: zoneWrappedInterceptingHandler,
deps: [HttpBackend, [new Optional(), HTTP_INTERCEPTORS]]

View File

@ -8,6 +8,8 @@
import {animate, style, transition, trigger} from '@angular/animations';
import {APP_BASE_HREF, PlatformLocation, isPlatformServer} from '@angular/common';
import {HttpClient, HttpClientModule} from '@angular/common/http';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {ApplicationRef, CompilerFactory, Component, HostListener, Input, NgModule, NgModuleRef, NgZone, PLATFORM_ID, PlatformRef, ViewEncapsulation, destroyPlatform, getPlatform} from '@angular/core';
import {TestBed, async, inject} from '@angular/core/testing';
import {Http, HttpModule, Response, ResponseOptions, XHRBackend} from '@angular/http';
@ -145,6 +147,14 @@ export class HttpBeforeExampleModule {
export class HttpAfterExampleModule {
}
@NgModule({
bootstrap: [MyServerApp],
declarations: [MyServerApp],
imports: [ServerModule, HttpClientModule, HttpClientTestingModule],
})
export class HttpClientExmapleModule {
}
@Component({selector: 'app', template: `<img [src]="'link'">`})
class ImageApp {
}
@ -534,5 +544,45 @@ export function main() {
});
}));
});
describe('HttpClient', () => {
it('can inject HttpClient', async(() => {
const platform = platformDynamicServer(
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
platform.bootstrapModule(HttpClientExmapleModule).then(ref => {
expect(ref.injector.get(HttpClient) instanceof HttpClient).toBeTruthy();
});
}));
it('can make HttpClient requests', async(() => {
const platform = platformDynamicServer(
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
platform.bootstrapModule(HttpClientExmapleModule).then(ref => {
const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
const http = ref.injector.get(HttpClient);
ref.injector.get(NgZone).run(() => {
http.get('http://localhost/testing').subscribe(body => {
NgZone.assertInAngularZone();
expect(body).toEqual('success!');
});
mock.expectOne('http://localhost/testing').flush('success!');
});
});
}));
it('requests are macrotasks', async(() => {
const platform = platformDynamicServer(
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
platform.bootstrapModule(HttpClientExmapleModule).then(ref => {
const mock = ref.injector.get(HttpTestingController) as HttpTestingController;
const http = ref.injector.get(HttpClient);
ref.injector.get(NgZone).run(() => {
http.get('http://localhost/testing').subscribe(body => {
expect(body).toEqual('success!');
});
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeTruthy();
mock.expectOne('http://localhost/testing').flush('success!');
expect(ref.injector.get(NgZone).hasPendingMacrotasks).toBeFalsy();
});
});
}));
});
});
}