diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-10-17 22:19:27 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-10-23 17:11:57 +0200 |
commit | 3e25e4d00f382641af1c78a28b02296bef686c1e (patch) | |
tree | 8db28c0265694d200e1564b215d05a9126e20060 /test/parallel/test-http-generic-streams.js | |
parent | ab16eec4369f60f14014933886f84a926488f368 (diff) | |
download | android-node-v8-3e25e4d00f382641af1c78a28b02296bef686c1e.tar.gz android-node-v8-3e25e4d00f382641af1c78a28b02296bef686c1e.tar.bz2 android-node-v8-3e25e4d00f382641af1c78a28b02296bef686c1e.zip |
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: https://github.com/nodejs/node/issues/16256
PR-URL: https://github.com/nodejs/node/pull/16267
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Diffstat (limited to 'test/parallel/test-http-generic-streams.js')
-rw-r--r-- | test/parallel/test-http-generic-streams.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/parallel/test-http-generic-streams.js b/test/parallel/test-http-generic-streams.js new file mode 100644 index 0000000000..bc28d1fcd4 --- /dev/null +++ b/test/parallel/test-http-generic-streams.js @@ -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(); + }); +} |