11import { createProxyMiddleware , createApp , createAppWithPath } from './_utils' ;
22import * as request from 'supertest' ;
3- import { getLocal , Mockttp } from 'mockttp' ;
3+ import { getLocal , generateCACertificate , Mockttp } from 'mockttp' ;
4+
5+ const untrustedCACert = generateCACertificate ( { bits : 1024 } ) ;
6+
7+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
48
59describe ( 'E2E router' , ( ) => {
610 let targetServerA : Mockttp ;
711 let targetServerB : Mockttp ;
812 let targetServerC : Mockttp ;
913
1014 beforeEach ( async ( ) => {
11- targetServerA = getLocal ( ) ;
12- targetServerB = getLocal ( ) ;
13- targetServerC = getLocal ( ) ;
15+ targetServerA = getLocal ( { https : await untrustedCACert } ) ;
16+ targetServerB = getLocal ( { https : await untrustedCACert } ) ;
17+ targetServerC = getLocal ( { https : await untrustedCACert } ) ;
18+
19+ await targetServerA
20+ . anyRequest ( )
21+ . thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
22+ await targetServerB
23+ . anyRequest ( )
24+ . thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
25+ await targetServerC
26+ . anyRequest ( )
27+ . thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
28+
29+ await targetServerA
30+ . anyRequest ( )
31+ . thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'A' : 'NOT HTTPS A' } ) ) ;
32+ await targetServerB
33+ . anyRequest ( )
34+ . thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'B' : 'NOT HTTPS B' } ) ) ;
35+ await targetServerC
36+ . anyRequest ( )
37+ . thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'C' : 'NOT HTTPS C' } ) ) ;
1438
1539 await targetServerA . start ( 6001 ) ;
1640 await targetServerB . start ( 6002 ) ;
1741 await targetServerC . start ( 6003 ) ;
18-
19- targetServerA . get ( ) . thenReply ( 200 , 'A' ) ;
20- targetServerB . get ( ) . thenReply ( 200 , 'B' ) ;
21- targetServerC . get ( ) . thenReply ( 200 , 'C' ) ;
2242 } ) ;
2343
2444 afterEach ( async ( ) => {
@@ -27,13 +47,50 @@ describe('E2E router', () => {
2747 await targetServerC . stop ( ) ;
2848 } ) ;
2949
30- describe ( 'router with proxyTable' , ( ) => {
31- it ( 'should proxy to: "localhost:6003/api"' , async ( ) => {
50+ describe ( 'router with req' , ( ) => {
51+ it ( 'should work with a string' , async ( ) => {
52+ const app = createApp (
53+ createProxyMiddleware ( {
54+ target : 'https://localhost:6001' ,
55+ secure : false ,
56+ changeOrigin : true ,
57+ router ( req ) {
58+ return 'https://localhost:6003' ;
59+ }
60+ } )
61+ ) ;
62+
63+ const agent = request ( app ) ;
64+ const response = await agent . get ( '/api' ) . expect ( 200 ) ;
65+ expect ( response . text ) . toBe ( 'C' ) ;
66+ } ) ;
67+
68+ it ( 'should work with an object' , async ( ) => {
3269 const app = createApp (
3370 createProxyMiddleware ( {
34- target : `http://localhost:6001` ,
71+ target : 'https://localhost:6001' ,
72+ secure : false ,
73+ changeOrigin : true ,
3574 router ( req ) {
36- return 'http://localhost:6003' ;
75+ return { host : 'localhost' , port : 6003 , protocol : 'https:' } ;
76+ }
77+ } )
78+ ) ;
79+ const agent = request ( app ) ;
80+ const response = await agent . get ( '/api' ) . expect ( 200 ) ;
81+ expect ( response . text ) . toBe ( 'C' ) ;
82+ } ) ;
83+
84+ it ( 'should work with an async callback' , async ( ) => {
85+ const app = createApp (
86+ createProxyMiddleware ( {
87+ target : 'https://localhost:6001' ,
88+ secure : false ,
89+ changeOrigin : true ,
90+ router : async req => {
91+ return new Promise ( resolve =>
92+ resolve ( { host : 'localhost' , port : 6003 , protocol : 'https:' } )
93+ ) ;
3794 }
3895 } )
3996 ) ;
@@ -42,6 +99,25 @@ describe('E2E router', () => {
4299 const response = await agent . get ( '/api' ) . expect ( 200 ) ;
43100 expect ( response . text ) . toBe ( 'C' ) ;
44101 } ) ;
102+
103+ it ( 'missing a : will cause it to use http' , async ( ) => {
104+ const app = createApp (
105+ createProxyMiddleware ( {
106+ target : 'https://localhost:6001' ,
107+ secure : false ,
108+ changeOrigin : true ,
109+ router : async req => {
110+ return new Promise ( resolve =>
111+ resolve ( { host : 'localhost' , port : 6003 , protocol : 'https' } )
112+ ) ;
113+ }
114+ } )
115+ ) ;
116+
117+ const agent = request ( app ) ;
118+ const response = await agent . get ( '/api' ) . expect ( 200 ) ;
119+ expect ( response . text ) . toBe ( 'NOT HTTPS C' ) ;
120+ } ) ;
45121 } ) ;
46122
47123 describe ( 'router with proxyTable' , ( ) => {
@@ -51,11 +127,13 @@ describe('E2E router', () => {
51127 const app = createAppWithPath (
52128 '/' ,
53129 createProxyMiddleware ( {
54- target : 'http://localhost:6001' ,
130+ target : 'https://localhost:6001' ,
131+ secure : false ,
132+ changeOrigin : true ,
55133 router : {
56- 'alpha.localhost:6000' : 'http ://localhost:6001' ,
57- 'beta.localhost:6000' : 'http ://localhost:6002' ,
58- 'localhost:6000/api' : 'http ://localhost:6003'
134+ 'alpha.localhost:6000' : 'https ://localhost:6001' ,
135+ 'beta.localhost:6000' : 'https ://localhost:6002' ,
136+ 'localhost:6000/api' : 'https ://localhost:6003'
59137 }
60138 } )
61139 ) ;
0 commit comments