@@ -25,28 +25,67 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
2525 await harness . writeFile ( 'src/main.ts' , '' ) ;
2626 } ) ;
2727
28- it ( 'proxies requests based on the proxy configuration file provided in the option' , async ( ) => {
28+ it ( 'proxies requests based on the JSON proxy configuration file provided in the option' , async ( ) => {
2929 harness . useTarget ( 'serve' , {
3030 ...BASE_OPTIONS ,
3131 proxyConfig : 'proxy.config.json' ,
3232 } ) ;
3333
34- const proxyServer = http . createServer ( ( request , response ) => {
35- if ( request . url ?. endsWith ( '/test' ) ) {
36- response . writeHead ( 200 ) ;
37- response . end ( 'TEST_API_RETURN' ) ;
38- } else {
39- response . writeHead ( 404 ) ;
40- response . end ( ) ;
41- }
34+ const proxyServer = createProxyServer ( ) ;
35+ try {
36+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
37+ const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
38+
39+ await harness . writeFiles ( {
40+ 'proxy.config.json' : `{ "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
41+ } ) ;
42+
43+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
44+
45+ expect ( result ?. success ) . toBeTrue ( ) ;
46+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
47+ } finally {
48+ await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
49+ }
50+ } ) ;
51+
52+ it ( 'proxies requests based on the JS proxy configuration file provided in the option' , async ( ) => {
53+ harness . useTarget ( 'serve' , {
54+ ...BASE_OPTIONS ,
55+ proxyConfig : 'proxy.config.js' ,
4256 } ) ;
4357
58+ const proxyServer = createProxyServer ( ) ;
4459 try {
4560 await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
4661 const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
4762
4863 await harness . writeFiles ( {
49- 'proxy.config.json' : `{ "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
64+ 'proxy.config.js' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
65+ } ) ;
66+
67+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
68+
69+ expect ( result ?. success ) . toBeTrue ( ) ;
70+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
71+ } finally {
72+ await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
73+ }
74+ } ) ;
75+
76+ it ( 'proxies requests based on the MJS proxy configuration file provided in the option' , async ( ) => {
77+ harness . useTarget ( 'serve' , {
78+ ...BASE_OPTIONS ,
79+ proxyConfig : 'proxy.config.mjs' ,
80+ } ) ;
81+
82+ const proxyServer = createProxyServer ( ) ;
83+ try {
84+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
85+ const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
86+
87+ await harness . writeFiles ( {
88+ 'proxy.config.mjs' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
5089 } ) ;
5190
5291 const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
@@ -75,3 +114,22 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
75114 } ) ;
76115 } ) ;
77116} ) ;
117+
118+ /**
119+ * Creates an HTTP Server used for proxy testing that provides a `/test` endpoint
120+ * that returns a 200 response with a body of `TEST_API_RETURN`. All other requests
121+ * will return a 404 response.
122+ *
123+ * @returns An HTTP Server instance.
124+ */
125+ function createProxyServer ( ) {
126+ return http . createServer ( ( request , response ) => {
127+ if ( request . url ?. endsWith ( '/test' ) ) {
128+ response . writeHead ( 200 ) ;
129+ response . end ( 'TEST_API_RETURN' ) ;
130+ } else {
131+ response . writeHead ( 404 ) ;
132+ response . end ( ) ;
133+ }
134+ } ) ;
135+ }
0 commit comments