77 */
88
99import { PlatformLocation } from '@angular/common' ;
10- import { ApplicationRef , CompilerFactory , Component , NgModule , NgModuleRef , PlatformRef , destroyPlatform , getPlatform } from '@angular/core' ;
10+ import { ApplicationRef , CompilerFactory , Component , NgModule , NgModuleRef , NgZone , PlatformRef , destroyPlatform , getPlatform } from '@angular/core' ;
1111import { async , inject } from '@angular/core/testing' ;
12+ import { Http , HttpModule , Response , ResponseOptions , XHRBackend } from '@angular/http' ;
13+ import { MockBackend , MockConnection } from '@angular/http/testing' ;
1214import { DOCUMENT } from '@angular/platform-browser' ;
1315import { getDOM } from '@angular/platform-browser/src/dom/dom_adapter' ;
1416import { INITIAL_CONFIG , PlatformState , ServerModule , platformDynamicServer , renderModule , renderModuleFactory } from '@angular/platform-server' ;
17+
1518import { Subscription } from 'rxjs/Subscription' ;
1619import { filter } from 'rxjs/operator/filter' ;
1720import { first } from 'rxjs/operator/first' ;
@@ -21,7 +24,15 @@ import {toPromise} from 'rxjs/operator/toPromise';
2124class MyServerApp {
2225}
2326
24- @NgModule ( { declarations : [ MyServerApp ] , imports : [ ServerModule ] , bootstrap : [ MyServerApp ] } )
27+ @NgModule ( {
28+ bootstrap : [ MyServerApp ] ,
29+ declarations : [ MyServerApp ] ,
30+ imports : [ ServerModule ] ,
31+ providers : [
32+ MockBackend ,
33+ { provide : XHRBackend , useExisting : MockBackend } ,
34+ ]
35+ } )
2536class ExampleModule {
2637}
2738
@@ -55,6 +66,30 @@ class MyStylesApp {
5566class ExampleStylesModule {
5667}
5768
69+ @NgModule ( {
70+ bootstrap : [ MyServerApp ] ,
71+ declarations : [ MyServerApp ] ,
72+ imports : [ HttpModule , ServerModule ] ,
73+ providers : [
74+ MockBackend ,
75+ { provide : XHRBackend , useExisting : MockBackend } ,
76+ ]
77+ } )
78+ export class HttpBeforeExampleModule {
79+ }
80+
81+ @NgModule ( {
82+ bootstrap : [ MyServerApp ] ,
83+ declarations : [ MyServerApp ] ,
84+ imports : [ ServerModule , HttpModule ] ,
85+ providers : [
86+ MockBackend ,
87+ { provide : XHRBackend , useExisting : MockBackend } ,
88+ ]
89+ } )
90+ export class HttpAfterExampleModule {
91+ }
92+
5893export function main ( ) {
5994 if ( getDOM ( ) . supportsDOMEvents ( ) ) return ; // NODE only
6095
@@ -196,5 +231,86 @@ export function main() {
196231 } ) ;
197232 } ) ) ) ;
198233 } ) ;
234+
235+ describe ( 'http' , ( ) => {
236+ it ( 'can inject Http' , async ( ( ) => {
237+ const platform = platformDynamicServer (
238+ [ { provide : INITIAL_CONFIG , useValue : { document : '<app></app>' } } ] ) ;
239+ platform . bootstrapModule ( ExampleModule ) . then ( ref => {
240+ expect ( ref . injector . get ( Http ) instanceof Http ) . toBeTruthy ( ) ;
241+ } ) ;
242+ } ) ) ;
243+ it ( 'can make Http requests' , async ( ( ) => {
244+ const platform = platformDynamicServer (
245+ [ { provide : INITIAL_CONFIG , useValue : { document : '<app></app>' } } ] ) ;
246+ platform . bootstrapModule ( ExampleModule ) . then ( ref => {
247+ const mock = ref . injector . get ( MockBackend ) ;
248+ const http = ref . injector . get ( Http ) ;
249+ ref . injector . get ( NgZone ) . run ( ( ) => {
250+ NgZone . assertInAngularZone ( ) ;
251+ mock . connections . subscribe ( ( mc : MockConnection ) => {
252+ NgZone . assertInAngularZone ( ) ;
253+ expect ( mc . request . url ) . toBe ( '/testing' ) ;
254+ mc . mockRespond ( new Response ( new ResponseOptions ( { body : 'success!' , status : 200 } ) ) ) ;
255+ } ) ;
256+ http . get ( '/testing' ) . subscribe ( resp => {
257+ NgZone . assertInAngularZone ( ) ;
258+ expect ( resp . text ( ) ) . toBe ( 'success!' ) ;
259+ } ) ;
260+ } ) ;
261+ } ) ;
262+ } ) ) ;
263+ it ( 'requests are macrotasks' , async ( ( ) => {
264+ const platform = platformDynamicServer (
265+ [ { provide : INITIAL_CONFIG , useValue : { document : '<app></app>' } } ] ) ;
266+ platform . bootstrapModule ( ExampleModule ) . then ( ref => {
267+ const mock = ref . injector . get ( MockBackend ) ;
268+ const http = ref . injector . get ( Http ) ;
269+ expect ( ref . injector . get ( NgZone ) . hasPendingMacrotasks ) . toBeFalsy ( ) ;
270+ ref . injector . get ( NgZone ) . run ( ( ) => {
271+ NgZone . assertInAngularZone ( ) ;
272+ mock . connections . subscribe ( ( mc : MockConnection ) => {
273+ expect ( ref . injector . get ( NgZone ) . hasPendingMacrotasks ) . toBeTruthy ( ) ;
274+ mc . mockRespond ( new Response ( new ResponseOptions ( { body : 'success!' , status : 200 } ) ) ) ;
275+ } ) ;
276+ http . get ( '/testing' ) . subscribe ( resp => { expect ( resp . text ( ) ) . toBe ( 'success!' ) ; } ) ;
277+ } ) ;
278+ } ) ;
279+ } ) ) ;
280+ it ( 'works when HttpModule is included before ServerModule' , async ( ( ) => {
281+ const platform = platformDynamicServer (
282+ [ { provide : INITIAL_CONFIG , useValue : { document : '<app></app>' } } ] ) ;
283+ platform . bootstrapModule ( HttpBeforeExampleModule ) . then ( ref => {
284+ const mock = ref . injector . get ( MockBackend ) ;
285+ const http = ref . injector . get ( Http ) ;
286+ expect ( ref . injector . get ( NgZone ) . hasPendingMacrotasks ) . toBeFalsy ( ) ;
287+ ref . injector . get ( NgZone ) . run ( ( ) => {
288+ NgZone . assertInAngularZone ( ) ;
289+ mock . connections . subscribe ( ( mc : MockConnection ) => {
290+ expect ( ref . injector . get ( NgZone ) . hasPendingMacrotasks ) . toBeTruthy ( ) ;
291+ mc . mockRespond ( new Response ( new ResponseOptions ( { body : 'success!' , status : 200 } ) ) ) ;
292+ } ) ;
293+ http . get ( '/testing' ) . subscribe ( resp => { expect ( resp . text ( ) ) . toBe ( 'success!' ) ; } ) ;
294+ } ) ;
295+ } ) ;
296+ } ) ) ;
297+ it ( 'works when HttpModule is included after ServerModule' , async ( ( ) => {
298+ const platform = platformDynamicServer (
299+ [ { provide : INITIAL_CONFIG , useValue : { document : '<app></app>' } } ] ) ;
300+ platform . bootstrapModule ( HttpAfterExampleModule ) . then ( ref => {
301+ const mock = ref . injector . get ( MockBackend ) ;
302+ const http = ref . injector . get ( Http ) ;
303+ expect ( ref . injector . get ( NgZone ) . hasPendingMacrotasks ) . toBeFalsy ( ) ;
304+ ref . injector . get ( NgZone ) . run ( ( ) => {
305+ NgZone . assertInAngularZone ( ) ;
306+ mock . connections . subscribe ( ( mc : MockConnection ) => {
307+ expect ( ref . injector . get ( NgZone ) . hasPendingMacrotasks ) . toBeTruthy ( ) ;
308+ mc . mockRespond ( new Response ( new ResponseOptions ( { body : 'success!' , status : 200 } ) ) ) ;
309+ } ) ;
310+ http . get ( '/testing' ) . subscribe ( resp => { expect ( resp . text ( ) ) . toBe ( 'success!' ) ; } ) ;
311+ } ) ;
312+ } ) ;
313+ } ) ) ;
314+ } ) ;
199315 } ) ;
200316}
0 commit comments