Skip to content

Commit 06a354f

Browse files
clydinfilipesilva
authored andcommitted
test(@angular-devkit/build-angular): add dev-server builder allowedHosts option tests
This change adds expanded unit tests for the dev-server builder's `allowedHosts` option using the builder test harness.
1 parent 037a1b8 commit 06a354f

File tree

2 files changed

+98
-44
lines changed

2 files changed

+98
-44
lines changed

packages/angular_devkit/build_angular/src/dev-server/allowed-hosts_spec.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
9+
import { mergeMap, take, timeout } from 'rxjs/operators';
10+
import { serveWebpackBrowser } from '../../index';
11+
import {
12+
BASE_OPTIONS,
13+
BUILD_TIMEOUT,
14+
DEV_SERVER_BUILDER_INFO,
15+
describeBuilder,
16+
setupBrowserTarget,
17+
} from '../setup';
18+
19+
const FETCH_HEADERS = Object.freeze({ host: 'example.com' });
20+
21+
describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
22+
describe('option: "allowedHosts"', () => {
23+
beforeEach(async () => {
24+
setupBrowserTarget(harness);
25+
26+
// Application code is not needed for these tests
27+
await harness.writeFile('src/main.ts', '');
28+
});
29+
30+
it('does not allow an invalid host when option is not present', async () => {
31+
harness.useTarget('serve', {
32+
...BASE_OPTIONS,
33+
});
34+
35+
await harness
36+
.execute()
37+
.pipe(
38+
timeout(BUILD_TIMEOUT),
39+
mergeMap(async ({ result }) => {
40+
expect(result?.success).toBeTrue();
41+
42+
if (result?.success) {
43+
const response = await fetch(`${result.baseUrl}`, { headers: FETCH_HEADERS });
44+
expect(await response.text()).toBe('Invalid Host header');
45+
}
46+
}),
47+
take(1),
48+
)
49+
.toPromise();
50+
});
51+
52+
it('does not allow an invalid host when option is an empty array', async () => {
53+
harness.useTarget('serve', {
54+
...BASE_OPTIONS,
55+
allowedHosts: [],
56+
});
57+
58+
await harness
59+
.execute()
60+
.pipe(
61+
timeout(BUILD_TIMEOUT),
62+
mergeMap(async ({ result }) => {
63+
expect(result?.success).toBeTrue();
64+
65+
if (result?.success) {
66+
const response = await fetch(`${result.baseUrl}`, { headers: FETCH_HEADERS });
67+
expect(await response.text()).toBe('Invalid Host header');
68+
}
69+
}),
70+
take(1),
71+
)
72+
.toPromise();
73+
});
74+
75+
it('allows a host when specified in the option', async () => {
76+
harness.useTarget('serve', {
77+
...BASE_OPTIONS,
78+
allowedHosts: ['example.com'],
79+
});
80+
81+
await harness
82+
.execute()
83+
.pipe(
84+
timeout(BUILD_TIMEOUT),
85+
mergeMap(async ({ result }) => {
86+
expect(result?.success).toBeTrue();
87+
88+
if (result?.success) {
89+
const response = await fetch(`${result.baseUrl}`, { headers: FETCH_HEADERS });
90+
expect(await response.text()).toContain('<title>');
91+
}
92+
}),
93+
take(1),
94+
)
95+
.toPromise();
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)