Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): handle regular expressions in pro…
Browse files Browse the repository at this point in the history
…xy config when using Vite

This commit enables proxies to have a RegExp as context when using Vite. See: https://vitejs.dev/config/server-options#server-proxy

Closes #26970

(cherry picked from commit 822e7a4)
  • Loading branch information
alan-agius4 committed Jan 26, 2024
1 parent a0e3060 commit 235c840
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
*/

import { createServer } from 'node:http';
import { JasmineBuilderHarness } from '../../../../testing/jasmine-helpers';
import { executeDevServer } from '../../index';
import { executeOnceAndFetch } from '../execute-fetch';
import { describeServeBuilder } from '../jasmine-helpers';
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';

describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget, isVite) => {
describe('option: "proxyConfig"', () => {
beforeEach(async () => {
setupTarget(harness);
Expand Down Expand Up @@ -235,6 +236,15 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
await proxyServer.close();
}
});

/**
* ****************************************************************************************************
* ********************************** Below only Vite specific tests **********************************
* ****************************************************************************************************
*/
if (isVite) {
viteOnlyTests(harness);
}
});
});

Expand All @@ -261,3 +271,54 @@ async function createProxyServer() {
close: () => new Promise<void>((resolve) => proxyServer.close(() => resolve())),
};
}

/**
* Vite specific tests
*/
function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
it('proxies support regexp as context', async () => {
harness.useTarget('serve', {
...BASE_OPTIONS,
proxyConfig: 'proxy.config.json',
});

const proxyServer = await createProxyServer();
try {
await harness.writeFiles({
'proxy.config.json': `
{ "^/api/.*": { "target": "http://127.0.0.1:${proxyServer.address.port}" } }
`,
});

const { result, response } = await executeOnceAndFetch(harness, '/api/test');

expect(result?.success).toBeTrue();
expect(await response?.text()).toContain('TEST_API_RETURN');
} finally {
await proxyServer.close();
}
});

it('proxies support negated regexp as context', async () => {
harness.useTarget('serve', {
...BASE_OPTIONS,
proxyConfig: 'proxy.config.json',
});

const proxyServer = await createProxyServer();
try {
await harness.writeFiles({
'proxy.config.json': `
{ "^\\/(?!something).*": { "target": "http://127.0.0.1:${proxyServer.address.port}" } }
`,
});

const { result, response } = await executeOnceAndFetch(harness, '/api/test');

expect(result?.success).toBeTrue();
expect(await response?.text()).toContain('TEST_API_RETURN');
} finally {
await proxyServer.close();
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function normalizeProxyConfiguration(

// TODO: Consider upstreaming glob support
for (const key of Object.keys(normalizedProxy)) {
if (isDynamicPattern(key)) {
if (key[0] !== '^' && isDynamicPattern(key)) {
const pattern = makeRegExpFromGlob(key).source;
normalizedProxy[pattern] = normalizedProxy[key];
delete normalizedProxy[key];
Expand Down

0 comments on commit 235c840

Please sign in to comment.