Skip to content

Commit

Permalink
fix(platform-server): provide XhrFactory for HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhub committed Jul 18, 2017
1 parent 17b7bc3 commit 4ce29f3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/platform-server/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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]]
Expand Down
50 changes: 50 additions & 0 deletions packages/platform-server/test/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
}
Expand Down Expand Up @@ -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();
});
});
}));
});
});
}

0 comments on commit 4ce29f3

Please sign in to comment.