|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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.io/license |
| 7 | + */ |
| 8 | +import { URL } from 'url'; |
| 9 | +import { serveWebpackBrowser } from '../../index'; |
| 10 | +import { executeOnceAndFetch } from '../execute-fetch'; |
| 11 | +import { |
| 12 | + BASE_OPTIONS, |
| 13 | + DEV_SERVER_BUILDER_INFO, |
| 14 | + describeBuilder, |
| 15 | + setupBrowserTarget, |
| 16 | +} from '../setup'; |
| 17 | + |
| 18 | +function getResultPort(result: Record<string, unknown> | undefined): string | undefined { |
| 19 | + if (typeof result?.baseUrl !== 'string') { |
| 20 | + fail(`Expected builder result with a string 'baseUrl' property. Received: ${result?.baseUrl}`); |
| 21 | + |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + return new URL(result.baseUrl).port; |
| 27 | + } catch { |
| 28 | + fail(`Expected a valid URL in builder result 'baseUrl' property. Received: ${result.baseUrl}`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => { |
| 33 | + describe('option: "port"', () => { |
| 34 | + beforeEach(async () => { |
| 35 | + setupBrowserTarget(harness); |
| 36 | + |
| 37 | + // Application code is not needed for these tests |
| 38 | + await harness.writeFile('src/main.ts', ''); |
| 39 | + }); |
| 40 | + |
| 41 | + it('uses default port (4200) when not present', async () => { |
| 42 | + harness.useTarget('serve', { |
| 43 | + ...BASE_OPTIONS, |
| 44 | + // Base options set port to zero |
| 45 | + port: undefined, |
| 46 | + }); |
| 47 | + |
| 48 | + const { result, response, logs } = await executeOnceAndFetch(harness, '/'); |
| 49 | + |
| 50 | + expect(result?.success).toBeTrue(); |
| 51 | + expect(getResultPort(result)).toBe('4200'); |
| 52 | + expect(await response?.text()).toContain('<title>'); |
| 53 | + |
| 54 | + expect(logs).toContain( |
| 55 | + jasmine.objectContaining({ |
| 56 | + message: jasmine.stringMatching(/:4200/), |
| 57 | + }), |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('uses a random free port when set to 0 (zero)', async () => { |
| 62 | + harness.useTarget('serve', { |
| 63 | + ...BASE_OPTIONS, |
| 64 | + port: 0, |
| 65 | + }); |
| 66 | + |
| 67 | + const { result, response, logs } = await executeOnceAndFetch(harness, '/'); |
| 68 | + |
| 69 | + expect(result?.success).toBeTrue(); |
| 70 | + const port = getResultPort(result); |
| 71 | + expect(port).not.toBe('4200'); |
| 72 | + expect(port).toMatch(/\d{4,6}/); |
| 73 | + expect(await response?.text()).toContain('<title>'); |
| 74 | + |
| 75 | + expect(logs).toContain( |
| 76 | + jasmine.objectContaining({ |
| 77 | + message: jasmine.stringMatching(':' + port), |
| 78 | + }), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('uses specific port when a non-zero number is specified', async () => { |
| 83 | + harness.useTarget('serve', { |
| 84 | + ...BASE_OPTIONS, |
| 85 | + port: 8000, |
| 86 | + }); |
| 87 | + |
| 88 | + const { result, response, logs } = await executeOnceAndFetch(harness, '/'); |
| 89 | + |
| 90 | + expect(result?.success).toBeTrue(); |
| 91 | + expect(getResultPort(result)).toBe('8000'); |
| 92 | + expect(await response?.text()).toContain('<title>'); |
| 93 | + |
| 94 | + expect(logs).toContain( |
| 95 | + jasmine.objectContaining({ |
| 96 | + message: jasmine.stringMatching(':8000'), |
| 97 | + }), |
| 98 | + ); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments