@@ -25,28 +25,67 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
25
25
await harness . writeFile ( 'src/main.ts' , '' ) ;
26
26
} ) ;
27
27
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 ( ) => {
29
29
harness . useTarget ( 'serve' , {
30
30
...BASE_OPTIONS ,
31
31
proxyConfig : 'proxy.config.json' ,
32
32
} ) ;
33
33
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' ,
42
56
} ) ;
43
57
58
+ const proxyServer = createProxyServer ( ) ;
44
59
try {
45
60
await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
46
61
const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
47
62
48
63
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 } " } }` ,
50
89
} ) ;
51
90
52
91
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
@@ -75,3 +114,22 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
75
114
} ) ;
76
115
} ) ;
77
116
} ) ;
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