This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.integration.utils.js
69 lines (62 loc) · 1.69 KB
/
http.integration.utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module.exports.get = (http) => {
return new Promise((resolve) => {
const url = 'http://jsonplaceholder.typicode.com/users/1?q=123';
http.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on('error', (err) => {
// We resolve with error.
resolve(err);
});
});
};
module.exports.getError = (http) => {
return new Promise((resolve) => {
const url = 'http://httpstat.us/404';
http.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on('error', (err) => {
// We resolve with error.
resolve(err);
});
});
};
module.exports.post = (https) => {
return new Promise((resolve) => {
const data = JSON.stringify({
todo: 'Buy the milk'
});
const options = {
hostname: 'flaviocopes.com',
port: 443,
path: '/todos',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
return resolve();
});
req.on('end', () => {
return resolve();
});
req.on('error', (error) => {
return resolve(error);
});
req.write(data);
req.end();
});
};