|
| 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 { executeDevServer } from '../../index'; |
| 10 | +import { executeOnceAndGet } from '../execute-fetch'; |
| 11 | +import { describeServeBuilder } from '../jasmine-helpers'; |
| 12 | +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; |
| 13 | + |
| 14 | +const FETCH_HEADERS = Object.freeze({ Host: 'example.com' }); |
| 15 | + |
| 16 | +describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { |
| 17 | + describe('option: "allowedHosts"', () => { |
| 18 | + beforeEach(async () => { |
| 19 | + setupTarget(harness); |
| 20 | + |
| 21 | + // Application code is not needed for these tests |
| 22 | + await harness.writeFile('src/main.ts', ''); |
| 23 | + }); |
| 24 | + |
| 25 | + it('does not allow an invalid host when option is not present', async () => { |
| 26 | + harness.useTarget('serve', { |
| 27 | + ...BASE_OPTIONS, |
| 28 | + }); |
| 29 | + |
| 30 | + const { result, response } = await executeOnceAndGet(harness, '/', { |
| 31 | + request: { headers: FETCH_HEADERS }, |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result?.success).toBeTrue(); |
| 35 | + expect(response?.statusCode).toBe(403); |
| 36 | + }); |
| 37 | + |
| 38 | + it('does not allow an invalid host when option is an empty array', async () => { |
| 39 | + harness.useTarget('serve', { |
| 40 | + ...BASE_OPTIONS, |
| 41 | + allowedHosts: [], |
| 42 | + }); |
| 43 | + |
| 44 | + const { result, response } = await executeOnceAndGet(harness, '/', { |
| 45 | + request: { headers: FETCH_HEADERS }, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(result?.success).toBeTrue(); |
| 49 | + expect(response?.statusCode).toBe(403); |
| 50 | + }); |
| 51 | + |
| 52 | + it('allows a host when specified in the option', async () => { |
| 53 | + harness.useTarget('serve', { |
| 54 | + ...BASE_OPTIONS, |
| 55 | + allowedHosts: ['example.com'], |
| 56 | + }); |
| 57 | + |
| 58 | + const { result, content } = await executeOnceAndGet(harness, '/', { |
| 59 | + request: { headers: FETCH_HEADERS }, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(result?.success).toBeTrue(); |
| 63 | + expect(content).toContain('<title>'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('allows a host when option is true', async () => { |
| 67 | + harness.useTarget('serve', { |
| 68 | + ...BASE_OPTIONS, |
| 69 | + allowedHosts: true, |
| 70 | + }); |
| 71 | + |
| 72 | + const { result, content } = await executeOnceAndGet(harness, '/', { |
| 73 | + request: { headers: FETCH_HEADERS }, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(result?.success).toBeTrue(); |
| 77 | + expect(content).toContain('<title>'); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments