Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9ed0989

Browse files
committedJun 17, 2022
http: test using public API only
1 parent 09ab6d2 commit 9ed0989

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed
 

‎test/parallel/test-http-parser-multiple-execute.js

+28-17
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,36 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5-
const { HTTPParser } = process.binding('http_parser');
5+
const { request } = require('http');
6+
const { Duplex } = require('stream');
67

7-
let second = false;
8-
const parser = new HTTPParser(HTTPParser.RESPONSE, false);
8+
let socket;
99

10-
parser.initialize(HTTPParser.RESPONSE, {}, 0, 0);
10+
function createConnection(...args) {
11+
socket = new Duplex({
12+
read() {},
13+
write(chunk, encoding, callback) {
14+
if (chunk.toString().includes('\r\n\r\n')) {
15+
this.push('HTTP/1.1 100 Continue\r\n\r\n');
16+
}
1117

12-
parser[HTTPParser.kOnHeadersComplete] = common.mustCall(
13-
function(_versionMajor, _versionMinor, _headers, _method, _url, statusCode) {
14-
if (!second) {
15-
second = true;
16-
17-
assert.strictEqual(statusCode, 100);
18-
parser.execute(Buffer.from('HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'));
19-
} else {
20-
assert.strictEqual(statusCode, 200);
18+
callback();
2119
}
22-
},
23-
2
24-
);
20+
});
21+
22+
return socket;
23+
}
24+
25+
const req = request('http://localhost:8080', { createConnection });
26+
27+
req.on('information', common.mustCall(({ statusCode }) => {
28+
assert.strictEqual(statusCode, 100);
29+
socket.push('HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n');
30+
socket.push(null);
31+
}));
32+
33+
req.on('response', common.mustCall(({ statusCode }) => {
34+
assert.strictEqual(statusCode, 200);
35+
}));
2536

26-
parser.execute(Buffer.from('HTTP/1.1 100 Continue\r\n\r\n'));
37+
req.end();

0 commit comments

Comments
 (0)
Please sign in to comment.