-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: support generic
Duplex
streams
Support generic `Duplex` streams through more duck typing on the server and client sides. Since HTTP is, as a protocol, independent of its underlying transport layer, Node.js should not enforce any restrictions on what streams its HTTP parser may use. Ref: #16256 PR-URL: #16267 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
- Loading branch information
Showing
4 changed files
with
88 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const MakeDuplexPair = require('../common/duplexpair'); | ||
|
||
// Test 1: Simple HTTP test, no keep-alive. | ||
{ | ||
const testData = 'Hello, World!\n'; | ||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/plain'); | ||
res.end(testData); | ||
})); | ||
|
||
const { clientSide, serverSide } = MakeDuplexPair(); | ||
server.emit('connection', serverSide); | ||
|
||
const req = http.request({ | ||
createConnection: common.mustCall(() => clientSide) | ||
}, common.mustCall((res) => { | ||
res.setEncoding('utf8'); | ||
res.on('data', common.mustCall((data) => { | ||
assert.strictEqual(data, testData); | ||
})); | ||
})); | ||
req.end(); | ||
} | ||
|
||
// Test 2: Keep-alive for 2 requests. | ||
{ | ||
const testData = 'Hello, World!\n'; | ||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/plain'); | ||
res.end(testData); | ||
}, 2)); | ||
|
||
const { clientSide, serverSide } = MakeDuplexPair(); | ||
server.emit('connection', serverSide); | ||
|
||
function doRequest(cb) { | ||
const req = http.request({ | ||
createConnection: common.mustCall(() => clientSide), | ||
headers: { Connection: 'keep-alive' } | ||
}, common.mustCall((res) => { | ||
res.setEncoding('utf8'); | ||
res.on('data', common.mustCall((data) => { | ||
assert.strictEqual(data, testData); | ||
})); | ||
res.on('end', common.mustCall(cb)); | ||
})); | ||
req.shouldKeepAlive = true; | ||
req.end(); | ||
} | ||
|
||
doRequest(() => { | ||
doRequest(); | ||
}); | ||
} |