|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { createServer } from 'node:http'; |
| 10 | +import { executeDevServer } from '../../index'; |
| 11 | +import { executeOnceAndFetch } from '../execute-fetch'; |
| 12 | +import { describeServeBuilder } from '../jasmine-helpers'; |
| 13 | +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; |
| 14 | + |
| 15 | +describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { |
| 16 | + describe('option: "middlewareConfig"', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + setupTarget(harness); |
| 19 | + |
| 20 | + // Application code is not needed for these tests |
| 21 | + await harness.writeFile('src/main.ts', ''); |
| 22 | + }); |
| 23 | + |
| 24 | + it('middleware configuration export single function (CommonJS)', async () => { |
| 25 | + harness.useTarget('serve', { |
| 26 | + ...BASE_OPTIONS, |
| 27 | + middlewareConfig: 'middleware.config.js', |
| 28 | + }); |
| 29 | + const proxyServer = await createProxyServer(); |
| 30 | + try { |
| 31 | + await harness.writeFiles({ |
| 32 | + 'middleware.config.js': `module.exports = (req, res, next) => { res.end('TEST_MIDDLEWARE'); next();}`, |
| 33 | + }); |
| 34 | + |
| 35 | + const { result, response } = await executeOnceAndFetch(harness, '/test'); |
| 36 | + |
| 37 | + expect(result?.success).toBeTrue(); |
| 38 | + expect(await response?.text()).toContain('TEST_MIDDLEWARE'); |
| 39 | + } finally { |
| 40 | + await proxyServer.close(); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + it('middleware configuration export an array of multiple functions (CommonJS)', async () => { |
| 45 | + harness.useTarget('serve', { |
| 46 | + ...BASE_OPTIONS, |
| 47 | + middlewareConfig: 'middleware.config.js', |
| 48 | + }); |
| 49 | + const proxyServer = await createProxyServer(); |
| 50 | + try { |
| 51 | + await harness.writeFiles({ |
| 52 | + 'middleware.config.js': `module.exports = [(req, res, next) => { next();}, (req, res, next) => { res.end('TEST_MIDDLEWARE'); next();}]`, |
| 53 | + }); |
| 54 | + |
| 55 | + const { result, response } = await executeOnceAndFetch(harness, '/test'); |
| 56 | + |
| 57 | + expect(result?.success).toBeTrue(); |
| 58 | + expect(await response?.text()).toContain('TEST_MIDDLEWARE'); |
| 59 | + } finally { |
| 60 | + await proxyServer.close(); |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +/** |
| 67 | + * Creates an HTTP Server used for proxy testing that provides a `/test` endpoint |
| 68 | + * that returns a 200 response with a body of `TEST_API_RETURN`. All other requests |
| 69 | + * will return a 404 response. |
| 70 | + */ |
| 71 | +async function createProxyServer() { |
| 72 | + const proxyServer = createServer((request, response) => { |
| 73 | + if (request.url?.endsWith('/test')) { |
| 74 | + response.writeHead(200); |
| 75 | + response.end('TEST_API_RETURN'); |
| 76 | + } else { |
| 77 | + response.writeHead(404); |
| 78 | + response.end(); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve)); |
| 83 | + |
| 84 | + return { |
| 85 | + address: proxyServer.address() as import('net').AddressInfo, |
| 86 | + close: () => new Promise<void>((resolve) => proxyServer.close(() => resolve())), |
| 87 | + }; |
| 88 | +} |
0 commit comments