Skip to content

Commit d76911a

Browse files
clydindgp1130
authored andcommitted
test: use node-fetch package in E2E tests instead of custom request helper utility
The http E2E helper utility module is now unused within the E2E tests and can be removed. This also removes usage of the `request` package which is not an actual dependency of the project. (cherry picked from commit c99f43c)
1 parent e8f504b commit d76911a

File tree

9 files changed

+94
-113
lines changed

9 files changed

+94
-113
lines changed

tests/legacy-cli/e2e/tests/basic/rebuild.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../../utils/process';
77
import { writeFile, writeMultipleFiles } from '../../utils/fs';
88
import { wait } from '../../utils/utils';
9-
import { request } from '../../utils/http';
9+
import fetch from 'node-fetch';
1010

1111
const validBundleRegEx = / Compiled successfully./;
1212

@@ -136,7 +136,8 @@ export default function () {
136136
]),
137137
)
138138
.then(() => wait(2000))
139-
.then(() => request('http://localhost:4200/main.js'))
139+
.then(() => fetch('http://localhost:4200/main.js'))
140+
.then((response) => response.text())
140141
.then((body) => {
141142
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_1/)) {
142143
throw new Error('Expected golden value 1.');
@@ -157,7 +158,8 @@ export default function () {
157158
]),
158159
)
159160
.then(() => wait(2000))
160-
.then(() => request('http://localhost:4200/main.js'))
161+
.then(() => fetch('http://localhost:4200/main.js'))
162+
.then((response) => response.text())
161163
.then((body) => {
162164
if (!body.match(/testingTESTING123/)) {
163165
throw new Error('Expected component HTML to update.');
@@ -172,7 +174,8 @@ export default function () {
172174
]),
173175
)
174176
.then(() => wait(2000))
175-
.then(() => request('http://localhost:4200/main.js'))
177+
.then(() => fetch('http://localhost:4200/main.js'))
178+
.then((response) => response.text())
176179
.then((body) => {
177180
if (!body.match(/color:\s?blue/)) {
178181
throw new Error('Expected component CSS to update.');

tests/legacy-cli/e2e/tests/basic/serve.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { request } from '../../utils/http';
1+
import fetch from 'node-fetch';
22
import { killAllProcesses } from '../../utils/process';
33
import { ngServe } from '../../utils/project';
44

@@ -18,9 +18,9 @@ export default async function () {
1818
}
1919

2020
async function verifyResponse(): Promise<void> {
21-
const response = await request('http://localhost:4200/');
21+
const response = await fetch('http://localhost:4200/');
2222

23-
if (!/<app-root><\/app-root>/.test(response)) {
23+
if (!/<app-root><\/app-root>/.test(await response.text())) {
2424
throw new Error('Response does not match expected value.');
2525
}
2626
}

tests/legacy-cli/e2e/tests/commands/serve/serve-path.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { request } from '../../../utils/http';
1+
import * as assert from 'assert';
2+
import fetch from 'node-fetch';
23
import { killAllProcesses } from '../../../utils/process';
34
import { ngServe } from '../../../utils/project';
45

@@ -7,17 +8,15 @@ export default function () {
78

89
return Promise.resolve()
910
.then(() => ngServe('--serve-path', 'test/'))
10-
.then(() => request('http://localhost:4200/test'))
11-
.then((body) => {
12-
if (!body.match(/<app-root><\/app-root>/)) {
13-
throw new Error('Response does not match expected value.');
14-
}
11+
.then(() => fetch('http://localhost:4200/test', { headers: { 'Accept': 'text/html' } }))
12+
.then(async (response) => {
13+
assert.strictEqual(response.status, 200);
14+
assert.match(await response.text(), /<app-root><\/app-root>/);
1515
})
16-
.then(() => request('http://localhost:4200/test/abc'))
17-
.then((body) => {
18-
if (!body.match(/<app-root><\/app-root>/)) {
19-
throw new Error('Response does not match expected value.');
20-
}
16+
.then(() => fetch('http://localhost:4200/test/abc', { headers: { 'Accept': 'text/html' } }))
17+
.then(async (response) => {
18+
assert.strictEqual(response.status, 200);
19+
assert.match(await response.text(), /<app-root><\/app-root>/);
2120
})
2221
.then(
2322
() => killAllProcesses(),
@@ -27,7 +26,8 @@ export default function () {
2726
},
2827
);
2928
// .then(() => ngServe('--base-href', 'test/'))
30-
// .then(() => request('http://localhost:4200/test'))
29+
// .then((response) => response.text())
30+
// .then(() => fetch('http://localhost:4200/test', { headers: { 'Accept': 'text/html' } }))
3131
// .then(body => {
3232
// if (!body.match(/<app-root><\/app-root>/)) {
3333
// throw new Error('Response does not match expected value.');

tests/legacy-cli/e2e/tests/misc/fallback.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { request } from '../../utils/http';
1+
import * as assert from 'assert';
2+
import fetch from 'node-fetch';
23
import { killAllProcesses } from '../../utils/process';
34
import { ngServe } from '../../utils/project';
45
import { updateJsonFile } from '../../utils/project';
@@ -11,11 +12,10 @@ export default function () {
1112
return (
1213
Promise.resolve()
1314
.then(() => ngServe())
14-
.then(() => request('http://localhost:4200/'))
15-
.then((body) => {
16-
if (!body.match(/<app-root><\/app-root>/)) {
17-
throw new Error('Response does not match expected value.');
18-
}
15+
.then(() => fetch('http://localhost:4200/', { headers: { 'Accept': 'text/html' } }))
16+
.then(async (response) => {
17+
assert.strictEqual(response.status, 200);
18+
assert.match(await response.text(), /<app-root><\/app-root>/);
1919
})
2020
.then(
2121
() => killAllProcesses(),
@@ -33,11 +33,10 @@ export default function () {
3333
}),
3434
)
3535
.then(() => ngServe())
36-
.then(() => request('http://localhost:4200/'))
37-
.then((body) => {
38-
if (!body.match(/<app-root><\/app-root>/)) {
39-
throw new Error('Response does not match expected value.');
40-
}
36+
.then(() => fetch('http://localhost:4200/', { headers: { 'Accept': 'text/html' } }))
37+
.then(async (response) => {
38+
assert.strictEqual(response.status, 200);
39+
assert.match(await response.text(), /<app-root><\/app-root>/);
4140
})
4241
.then(
4342
() => killAllProcesses(),

tests/legacy-cli/e2e/tests/misc/proxy-config.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import express from 'express';
22
import * as http from 'http';
33

44
import { writeFile } from '../../utils/fs';
5-
import { request } from '../../utils/http';
5+
import fetch from 'node-fetch';
66
import { killAllProcesses, ng } from '../../utils/process';
77
import { ngServe } from '../../utils/project';
88
import { updateJsonFile } from '../../utils/project';
99
import { expectToFail } from '../../utils/utils';
10+
import { AddressInfo } from 'net';
11+
import * as assert from 'assert';
1012

1113
export default function () {
1214
// TODO(architect): Delete this test. It is now in devkit/build-angular.
@@ -16,13 +18,13 @@ export default function () {
1618
const server = http.createServer(app);
1719
server.listen(0);
1820

19-
app.set('port', server.address().port);
21+
app.set('port', (server.address() as AddressInfo).port);
2022
app.get('/api/test', function (req, res) {
2123
res.send('TEST_API_RETURN');
2224
});
2325

2426
const backendHost = 'localhost';
25-
const backendPort = server.address().port;
27+
const backendPort = (server.address() as AddressInfo).port;
2628
const proxyServerUrl = `http://${backendHost}:${backendPort}`;
2729
const proxyConfigFile = 'proxy.config.json';
2830
const proxyConfig = {
@@ -35,11 +37,10 @@ export default function () {
3537
Promise.resolve()
3638
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
3739
.then(() => ngServe('--proxy-config', proxyConfigFile))
38-
.then(() => request('http://localhost:4200/api/test'))
39-
.then((body) => {
40-
if (!body.match(/TEST_API_RETURN/)) {
41-
throw new Error('Response does not match expected value.');
42-
}
40+
.then(() => fetch('http://localhost:4200/api/test'))
41+
.then(async (response) => {
42+
assert.strictEqual(response.status, 200);
43+
assert.match(await response.text(), /TEST_API_RETURN/);
4344
})
4445
.then(
4546
() => killAllProcesses(),
@@ -56,11 +57,10 @@ export default function () {
5657
// };
5758
// }))
5859
// .then(() => ngServe())
59-
// .then(() => request('http://localhost:4200/api/test'))
60-
// .then(body => {
61-
// if (!body.match(/TEST_API_RETURN/)) {
62-
// throw new Error('Response does not match expected value.');
63-
// }
60+
// .then(() => fetch('http://localhost:4200/api/test'))
61+
// .then(async (response) => {
62+
// assert.strictEqual(response.status, 200);
63+
// assert.match(await response.text(), /TEST_API_RETURN/)
6464
// })
6565
// .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
6666

tests/legacy-cli/e2e/tests/misc/public-host.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as os from 'os';
2-
3-
import { request } from '../../utils/http';
2+
import fetch from 'node-fetch';
43
import { killAllProcesses } from '../../utils/process';
54
import { ngServe } from '../../utils/project';
65

@@ -20,15 +19,17 @@ export default function () {
2019
// Disabling this test. Webpack Dev Server does not check the hots anymore when binding to
2120
// numeric IP addresses.
2221
// .then(() => ngServe('--host=0.0.0.0'))
23-
// .then(() => request(localAddress))
22+
// .then(() => fetch(localAddress))
23+
// .then((response) => response.text())
2424
// .then(body => {
2525
// if (!body.match(/Invalid Host header/)) {
2626
// throw new Error('Response does not match expected value.');
2727
// }
2828
// })
2929
// .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
3030
.then(() => ngServe('--host=0.0.0.0', `--public-host=${publicHost}`))
31-
.then(() => request(localAddress))
31+
.then(() => fetch(localAddress))
32+
.then((response) => response.text())
3233
.then((body) => {
3334
if (!body.match(/<app-root><\/app-root>/)) {
3435
throw new Error('Response does not match expected value.');
@@ -42,7 +43,8 @@ export default function () {
4243
},
4344
)
4445
.then(() => ngServe('--host=0.0.0.0', `--disable-host-check`))
45-
.then(() => request(localAddress))
46+
.then(() => fetch(localAddress))
47+
.then((response) => response.text())
4648
.then((body) => {
4749
if (!body.match(/<app-root><\/app-root>/)) {
4850
throw new Error('Response does not match expected value.');
@@ -56,7 +58,8 @@ export default function () {
5658
},
5759
)
5860
.then(() => ngServe('--host=0.0.0.0', `--public-host=${localAddress}`))
59-
.then(() => request(localAddress))
61+
.then(() => fetch(localAddress))
62+
.then((response) => response.text())
6063
.then((body) => {
6164
if (!body.match(/<app-root><\/app-root>/)) {
6265
throw new Error('Response does not match expected value.');
@@ -70,7 +73,8 @@ export default function () {
7073
},
7174
)
7275
.then(() => ngServe('--host=0.0.0.0', `--public-host=${firstLocalIp}`))
73-
.then(() => request(localAddress))
76+
.then(() => fetch(localAddress))
77+
.then((response) => response.text())
7478
.then((body) => {
7579
if (!body.match(/<app-root><\/app-root>/)) {
7680
throw new Error('Response does not match expected value.');
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import { request } from '../../utils/http';
1+
import * as assert from 'assert';
2+
import { Agent } from 'https';
3+
import fetch from 'node-fetch';
24
import { killAllProcesses } from '../../utils/process';
35
import { ngServe } from '../../utils/project';
46

5-
export default function () {
7+
export default async function () {
68
// TODO(architect): Delete this test. It is now in devkit/build-angular.
79

8-
return Promise.resolve()
9-
.then(() => ngServe('--ssl', 'true'))
10-
.then(() => request('https://localhost:4200/'))
11-
.then((body) => {
12-
if (!body.match(/<app-root><\/app-root>/)) {
13-
throw new Error('Response does not match expected value.');
14-
}
15-
})
16-
.then(
17-
() => killAllProcesses(),
18-
(err) => {
19-
killAllProcesses();
20-
throw err;
21-
},
22-
);
10+
try {
11+
await ngServe('--ssl', 'true');
12+
13+
const response = await fetch('https://localhost:4200/', {
14+
agent: new Agent({ rejectUnauthorized: false }),
15+
});
16+
17+
assert.strictEqual(response.status, 200);
18+
assert.match(await response.text(), /<app-root><\/app-root>/);
19+
} finally {
20+
killAllProcesses();
21+
}
2322
}
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
import { request } from '../../utils/http';
1+
import * as assert from 'assert';
2+
import { Agent } from 'https';
3+
import fetch from 'node-fetch';
24
import { assetDir } from '../../utils/assets';
35
import { killAllProcesses } from '../../utils/process';
46
import { ngServe } from '../../utils/project';
57

6-
export default function () {
8+
export default async function () {
79
// TODO(architect): Delete this test. It is now in devkit/build-angular.
810

9-
return Promise.resolve()
10-
.then(() =>
11-
ngServe(
12-
'--ssl',
13-
'true',
14-
'--ssl-key',
15-
assetDir('ssl/server.key'),
16-
'--ssl-cert',
17-
assetDir('ssl/server.crt'),
18-
),
19-
)
20-
.then(() => request('https://localhost:4200/'))
21-
.then((body) => {
22-
if (!body.match(/<app-root><\/app-root>/)) {
23-
throw new Error('Response does not match expected value.');
24-
}
25-
})
26-
.then(
27-
() => killAllProcesses(),
28-
(err) => {
29-
killAllProcesses();
30-
throw err;
31-
},
11+
try {
12+
await ngServe(
13+
'--ssl',
14+
'true',
15+
'--ssl-key',
16+
assetDir('ssl/server.key'),
17+
'--ssl-cert',
18+
assetDir('ssl/server.crt'),
3219
);
20+
21+
const response = await fetch('https://localhost:4200/', {
22+
agent: new Agent({ rejectUnauthorized: false }),
23+
});
24+
25+
assert.strictEqual(response.status, 200);
26+
assert.match(await response.text(), /<app-root><\/app-root>/);
27+
} finally {
28+
killAllProcesses();
29+
}
3330
}

tests/legacy-cli/e2e/utils/http.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)