Skip to content

fix(@angular-devkit/build-angular): handle regular expressions in proxy config when using Vite #26975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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