"Fossies" - the Fresh Open Source Software Archive

Member "angular-cli-15.2.4/packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/allowed-hosts_spec.ts" (16 Mar 2023, 2052 Bytes) of package /linux/www/angular-cli-15.2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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.io/license
    7  */
    8 
    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 const FETCH_HEADERS = Object.freeze({ host: 'example.com' });
   19 
   20 describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
   21   describe('option: "allowedHosts"', () => {
   22     beforeEach(async () => {
   23       setupBrowserTarget(harness);
   24 
   25       // Application code is not needed for these tests
   26       await harness.writeFile('src/main.ts', '');
   27     });
   28 
   29     it('does not allow an invalid host when option is not present', async () => {
   30       harness.useTarget('serve', {
   31         ...BASE_OPTIONS,
   32       });
   33 
   34       const { result, response } = await executeOnceAndFetch(harness, '/', {
   35         request: { headers: FETCH_HEADERS },
   36       });
   37 
   38       expect(result?.success).toBeTrue();
   39       expect(await response?.text()).toBe('Invalid Host header');
   40     });
   41 
   42     it('does not allow an invalid host when option is an empty array', async () => {
   43       harness.useTarget('serve', {
   44         ...BASE_OPTIONS,
   45         allowedHosts: [],
   46       });
   47 
   48       const { result, response } = await executeOnceAndFetch(harness, '/', {
   49         request: { headers: FETCH_HEADERS },
   50       });
   51 
   52       expect(result?.success).toBeTrue();
   53       expect(await response?.text()).toBe('Invalid Host header');
   54     });
   55 
   56     it('allows a host when specified in the option', async () => {
   57       harness.useTarget('serve', {
   58         ...BASE_OPTIONS,
   59         allowedHosts: ['example.com'],
   60       });
   61 
   62       const { result, response } = await executeOnceAndFetch(harness, '/', {
   63         request: { headers: FETCH_HEADERS },
   64       });
   65 
   66       expect(result?.success).toBeTrue();
   67       expect(await response?.text()).toContain('<title>');
   68     });
   69   });
   70 });