-
Notifications
You must be signed in to change notification settings - Fork 23
/
http_example.js
35 lines (31 loc) · 996 Bytes
/
http_example.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
'use strict';
const http = require('http');
module.exports = function example() {
const inputData = 'How much wood would a woodchuck chuck, if a woodchuck could chuck wood?';
const requestBody = Buffer.from(inputData, 'utf8');
return new Promise((resolve, reject) => {
http.request({
method: 'POST',
hostname: 'posttestserver.com',
path: '/post.php',
headers: {
'Content-Type': 'text/plain',
'Content-Length': requestBody.length,
},
}, (res) => {
let responseBody = null;
res.on('data', (chunk) => {
if (!responseBody) {
responseBody = chunk;
} else {
responseBody = Buffer.concat([responseBody, chunk]);
}
});
res.on('end', () => {
const outputData = responseBody.toString('utf8');
const expected = `Post body was ${requestBody.length} chars long`;
resolve(outputData.indexOf(expected) !== -1);
});
}).end(requestBody);
});
};