test(@angular-devkit/build-angular): add dev-server builder proxyConfig option tests

This change adds expanded unit tests for the dev-server builder's `proxyConfig` option using the builder test harness.
This commit is contained in:
Charles Lyding 2021-03-26 19:43:01 -04:00 committed by Filipe Silva
parent 66f1b13eab
commit db4c6baf5a
2 changed files with 76 additions and 75 deletions

View File

@ -1,75 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Architect, BuilderRun } from '@angular-devkit/architect';
import { DevServerBuilderOutput } from '@angular-devkit/build-angular';
import * as express from 'express'; // tslint:disable-line:no-implicit-dependencies
import * as http from 'http';
import { AddressInfo } from 'net';
import fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
import { createArchitect, host } from '../test-utils';
describe('Dev Server Builder proxy', () => {
const target = { project: 'app', target: 'serve' };
let architect: Architect;
// We use runs like this to ensure it WILL stop the servers at the end of each tests.
let runs: BuilderRun[];
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
runs = [];
});
afterEach(async () => {
await host.restore().toPromise();
await Promise.all(runs.map(r => r.stop()));
});
it('works', async () => {
// Create an express app that serves as a proxy.
const app = express();
const server = http.createServer(app);
server.listen(0);
app.set('port', (server.address() as AddressInfo).port);
app.get('/api/test', function (_req, res) {
res.send('TEST_API_RETURN');
});
const backendHost = 'localhost';
// cast is safe, the HTTP server is not using a pipe or UNIX domain socket
const backendPort = (server.address() as AddressInfo).port;
const proxyServerUrl = `http://${backendHost}:${backendPort}`;
host.writeMultipleFiles({
'proxy.config.json': `{ "/api/*": { "target": "${proxyServerUrl}" } }`,
});
const run = await architect.scheduleTarget(target, { proxyConfig: 'proxy.config.json' });
runs.push(run);
const output = await run.result as DevServerBuilderOutput;
expect(output.success).toBe(true);
expect(output.baseUrl).toBe('http://localhost:4200/');
const response = await fetch('http://localhost:4200/api/test');
expect(await response.text()).toContain('TEST_API_RETURN');
server.close();
}, 30000);
it('errors out with a missing proxy file', async () => {
const run = await architect.scheduleTarget(target, { proxyConfig: 'INVALID.json' });
runs.push(run);
try {
await run.result;
expect('THE ABOVE LINE SHOULD THROW').toBe('true');
} catch {
// Success!
}
}, 30000);
});

View File

@ -0,0 +1,76 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as http from 'http';
import { serveWebpackBrowser } from '../../index';
import { executeOnceAndFetch } from '../execute-fetch';
import {
BASE_OPTIONS,
DEV_SERVER_BUILDER_INFO,
describeBuilder,
setupBrowserTarget,
} from '../setup';
describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
describe('option: "proxyConfig"', () => {
beforeEach(async () => {
setupBrowserTarget(harness);
// Application code is not needed for these tests
await harness.writeFile('src/main.ts', '');
});
it('proxies requests based on the proxy configuration file provided in the option', async () => {
harness.useTarget('serve', {
...BASE_OPTIONS,
proxyConfig: 'proxy.config.json',
});
const proxyServer = http.createServer((request, response) => {
if (request.url?.endsWith('/test')) {
response.writeHead(200);
response.end('TEST_API_RETURN');
} else {
response.writeHead(404);
response.end();
}
});
try {
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
await harness.writeFiles({
'proxy.config.json': `{ "/api/*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }`,
});
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
expect(result?.success).toBeTrue();
expect(await response?.text()).toContain('TEST_API_RETURN');
} finally {
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
}
});
it('throws an error when proxy configuration file cannot be found', async () => {
harness.useTarget('serve', {
...BASE_OPTIONS,
proxyConfig: 'INVALID.json',
});
const { result, error } = await harness.executeOnce({ outputLogsOnException: false });
expect(result).toBeUndefined();
expect(error).toEqual(
jasmine.objectContaining({
message: jasmine.stringMatching('INVALID\\.json does not exist'),
}),
);
});
});
});