|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import * as http from 'http';
|
| 10 | +import { JasmineBuilderHarness } from '../../../../testing/jasmine-helpers'; |
10 | 11 | import { executeDevServer } from '../../index';
|
11 | 12 | import { executeOnceAndFetch } from '../execute-fetch';
|
12 | 13 | import { describeServeBuilder } from '../jasmine-helpers';
|
13 | 14 | import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
|
14 | 15 |
|
15 |
| -describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { |
| 16 | +describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget, isVite) => { |
16 | 17 | describe('option: "proxyConfig"', () => {
|
17 | 18 | beforeEach(async () => {
|
18 | 19 | setupTarget(harness);
|
@@ -232,9 +233,75 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
|
232 | 233 | }),
|
233 | 234 | );
|
234 | 235 | });
|
| 236 | + |
| 237 | + /** |
| 238 | + * **************************************************************************************************** |
| 239 | + * ********************************** Below only Vite specific tests ********************************** |
| 240 | + * **************************************************************************************************** |
| 241 | + */ |
| 242 | + if (isVite) { |
| 243 | + viteOnlyTests(harness); |
| 244 | + } |
235 | 245 | });
|
236 | 246 | });
|
237 | 247 |
|
| 248 | +/** |
| 249 | + * Vite specific tests |
| 250 | + */ |
| 251 | +function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void { |
| 252 | + it('proxies support regexp as context', async () => { |
| 253 | + harness.useTarget('serve', { |
| 254 | + ...BASE_OPTIONS, |
| 255 | + proxyConfig: 'proxy.config.json', |
| 256 | + }); |
| 257 | + |
| 258 | + const proxyServer = createProxyServer(); |
| 259 | + try { |
| 260 | + await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve)); |
| 261 | + const proxyAddress = proxyServer.address() as import('net').AddressInfo; |
| 262 | + |
| 263 | + await harness.writeFiles({ |
| 264 | + 'proxy.config.json': ` |
| 265 | + { "^/api/.*": { "target": "http://127.0.0.1:${proxyAddress.port}" } } |
| 266 | + `, |
| 267 | + }); |
| 268 | + |
| 269 | + const { result, response } = await executeOnceAndFetch(harness, '/api/test'); |
| 270 | + |
| 271 | + expect(result?.success).toBeTrue(); |
| 272 | + expect(await response?.text()).toContain('TEST_API_RETURN'); |
| 273 | + } finally { |
| 274 | + await new Promise<void>((resolve) => proxyServer.close(() => resolve())); |
| 275 | + } |
| 276 | + }); |
| 277 | + |
| 278 | + it('proxies support negated regexp as context', async () => { |
| 279 | + harness.useTarget('serve', { |
| 280 | + ...BASE_OPTIONS, |
| 281 | + proxyConfig: 'proxy.config.json', |
| 282 | + }); |
| 283 | + |
| 284 | + const proxyServer = createProxyServer(); |
| 285 | + try { |
| 286 | + await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve)); |
| 287 | + const proxyAddress = proxyServer.address() as import('net').AddressInfo; |
| 288 | + |
| 289 | + await harness.writeFiles({ |
| 290 | + 'proxy.config.json': ` |
| 291 | + { "^\\/(?!something).*": { "target": "http://127.0.0.1:${proxyAddress.port}" } } |
| 292 | + `, |
| 293 | + }); |
| 294 | + |
| 295 | + const { result, response } = await executeOnceAndFetch(harness, '/api/test'); |
| 296 | + |
| 297 | + expect(result?.success).toBeTrue(); |
| 298 | + expect(await response?.text()).toContain('TEST_API_RETURN'); |
| 299 | + } finally { |
| 300 | + await new Promise<void>((resolve) => proxyServer.close(() => resolve())); |
| 301 | + } |
| 302 | + }); |
| 303 | +} |
| 304 | + |
238 | 305 | /**
|
239 | 306 | * Creates an HTTP Server used for proxy testing that provides a `/test` endpoint
|
240 | 307 | * that returns a 200 response with a body of `TEST_API_RETURN`. All other requests
|
|
0 commit comments