Skip to content

Commit 366e51b

Browse files
mrxzKeen Yee Liau
authored and
Keen Yee Liau
committed
feat(@angular-devkit/build-angular): expose webpack-dev-server's allowedHosts option
Closes #13656
1 parent eb38a84 commit 366e51b

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

packages/angular/cli/lib/config/schema.json

+8
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,14 @@
11191119
"type": "string",
11201120
"description": "The URL that the browser client (or live-reload client, if enabled) should use to connect to the development server. Use for a complex dev server setup, such as one with reverse proxies."
11211121
},
1122+
"allowedHosts": {
1123+
"type": "array",
1124+
"description": "Whitelist of hosts that are allowed to access the dev server.",
1125+
"default": [],
1126+
"items": {
1127+
"type": "string"
1128+
}
1129+
},
11221130
"servePath": {
11231131
"type": "string",
11241132
"description": "The pathname where the app will be served."

packages/angular_devkit/build_angular/src/dev-server/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ export function buildServerConfig(
342342
// inline is always false, because we add live reloading scripts in _addLiveReload when needed
343343
inline: false,
344344
public: serverOptions.publicHost,
345+
allowedHosts: serverOptions.allowedHosts,
345346
disableHostCheck: serverOptions.disableHostCheck,
346347
publicPath: servePath,
347348
hot: serverOptions.hmr,

packages/angular_devkit/build_angular/src/dev-server/schema.json

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
"type": "string",
5656
"description": "The URL that the browser client (or live-reload client, if enabled) should use to connect to the development server. Use for a complex dev server setup, such as one with reverse proxies."
5757
},
58+
"allowedHosts": {
59+
"type": "array",
60+
"description": "Whitelist of hosts that are allowed to access the dev server.",
61+
"default": [],
62+
"items": {
63+
"type": "string"
64+
}
65+
},
5866
"servePath": {
5967
"type": "string",
6068
"description": "The pathname where the app will be served."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { Architect, BuilderRun } from '@angular-devkit/architect';
9+
import fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
10+
import { DevServerBuilderOutput } from '../../src/dev-server';
11+
import { createArchitect, host } from '../utils';
12+
13+
14+
describe('Dev Server Builder allowed host', () => {
15+
// We have to spoof the host to a non-numeric one because Webpack Dev Server does not
16+
// check the hosts anymore when requests come from numeric IP addresses.
17+
const headers = { host: 'spoofy.mcspoofface' };
18+
19+
const target = { project: 'app', target: 'serve' };
20+
let architect: Architect;
21+
// We use runs like this to ensure it WILL stop the servers at the end of each tests.
22+
let runs: BuilderRun[];
23+
24+
beforeEach(async () => {
25+
await host.initialize().toPromise();
26+
architect = (await createArchitect(host.root())).architect;
27+
runs = [];
28+
});
29+
afterEach(async () => {
30+
await host.restore().toPromise();
31+
await Promise.all(runs.map(r => r.stop()));
32+
});
33+
34+
it('works', async () => {
35+
const run = await architect.scheduleTarget(target, { allowedHosts: ['spoofy.mcspoofface'] });
36+
runs.push(run);
37+
const output = await run.result as DevServerBuilderOutput;
38+
expect(output.success).toBe(true);
39+
expect(output.baseUrl).toBe('http://localhost:4200/');
40+
41+
const response = await fetch(`${output.baseUrl}`, { headers });
42+
expect(await response.text()).toContain('<title>HelloWorldApp</title>');
43+
}, 30000);
44+
});

packages/angular_devkit/build_angular/test/dev-server/public-host_spec_large.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createArchitect, host } from '../utils';
1414
describe('Dev Server Builder public host', () => {
1515
// We have to spoof the host to a non-numeric one because Webpack Dev Server does not
1616
// check the hosts anymore when requests come from numeric IP addresses.
17-
const headers = { host: 'http://spoofy.mcspoofface' };
17+
const headers = { host: 'spoofy.mcspoofface' };
1818

1919
const target = { project: 'app', target: 'serve' };
2020
let architect: Architect;

0 commit comments

Comments
 (0)