@@ -11,6 +11,9 @@ var treeKill = require('tree-kill');
11
11
var child_process = require ( 'child_process' ) ;
12
12
var ng = require ( '../helpers/ng' ) ;
13
13
var root = path . join ( process . cwd ( ) , 'tmp' ) ;
14
+ var express = require ( 'express' ) ;
15
+ var http = require ( 'http' ) ;
16
+ var request = require ( 'request' ) ;
14
17
15
18
function existsSync ( path ) {
16
19
try {
@@ -564,6 +567,81 @@ describe('Basic end-to-end Workflow', function () {
564
567
throw new Error ( msg ) ;
565
568
} ) ;
566
569
} ) ;
570
+
571
+ it ( 'Serve with proxy config' , function ( ) {
572
+ this . timeout ( 240000 ) ;
573
+ var ngServePid ;
574
+ var server ;
575
+
576
+ function executor ( resolve , reject ) {
577
+ var startedProtractor = false ;
578
+ var app = express ( ) ;
579
+ server = http . createServer ( app ) ;
580
+ server . listen ( ) ;
581
+ app . set ( 'port' , server . address ( ) . port ) ;
582
+
583
+ app . get ( '/api/test' , function ( req , res ) {
584
+ res . send ( 'TEST_API_RETURN' ) ;
585
+ } ) ;
586
+ var backendHost = 'localhost' ;
587
+ var backendPort = server . address ( ) . port
588
+
589
+ var proxyServerUrl = `http://${ backendHost } :${ backendPort } ` ;
590
+ const proxyConfigFile = path . join ( process . cwd ( ) , 'proxy.config.json' ) ;
591
+ const proxyConfig = {
592
+ '/api/*' : {
593
+ target : proxyServerUrl
594
+ }
595
+ } ;
596
+ fs . writeFileSync ( proxyConfigFile , JSON . stringify ( proxyConfig , null , 2 ) , 'utf8' ) ;
597
+ var serveProcess = child_process . exec ( `${ ngBin } serve --proxy-config proxy.config.json` , { maxBuffer : 500 * 1024 } ) ;
598
+ ngServePid = serveProcess . pid ;
599
+
600
+ serveProcess . stdout . on ( 'data' , ( data ) => {
601
+ if ( / w e b p a c k : b u n d l e i s n o w V A L I D / . test ( data . toString ( 'utf-8' ) ) && ! startedProtractor ) {
602
+
603
+ // How to get the url with out hardcoding here?
604
+ request ( 'http://localhost:4200/api/test' , function ( err , response , body ) {
605
+ expect ( response . statusCode ) . to . be . equal ( 200 ) ;
606
+ expect ( body ) . to . be . equal ( 'TEST_API_RETURN' ) ;
607
+ resolve ( ) ;
608
+ } ) ;
609
+ }
610
+ } ) ;
611
+
612
+ serveProcess . stderr . on ( 'data' , ( data ) => {
613
+ reject ( data ) ;
614
+ } ) ;
615
+ serveProcess . on ( 'close' , ( code ) => {
616
+ code === 0 ? resolve ( ) : reject ( 'ng serve command closed with error' )
617
+ } ) ;
618
+ }
619
+
620
+ // Need a way to close the express server
621
+ return new Promise ( executor )
622
+ . then ( ( ) => {
623
+ if ( ngServePid ) treeKill ( ngServePid ) ;
624
+ if ( server ) {
625
+ server . close ( ) ;
626
+ }
627
+ } )
628
+ . catch ( ( msg ) => {
629
+ if ( ngServePid ) treeKill ( ngServePid ) ;
630
+ if ( server ) {
631
+ server . close ( ) ;
632
+ }
633
+ throw new Error ( msg ) ;
634
+ } ) ;
635
+ } ) ;
636
+
637
+ it ( 'Serve fails on invalid proxy config file' , function ( done ) {
638
+ this . timeout ( 420000 ) ;
639
+ sh . exec ( `${ ngBin } serve --proxy-config proxy.config.does_not_exist.json` , ( code ) => {
640
+ expect ( code ) . to . not . equal ( 0 ) ;
641
+ done ( ) ;
642
+ } ) ;
643
+ } ) ;
644
+
567
645
} ) ;
568
646
569
647
function isMobileTest ( ) {
0 commit comments