Skip to content

Commit 836fb51

Browse files
committed
tests: add test for http2 stream api
1 parent 191e4b6 commit 836fb51

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

test/http2.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var describeHttp2 = !http2
88
: describe
99

1010
describeHttp2('using http2 streams', function () {
11-
it('should read body streams', function (done) {
11+
it('should read from compatibility api', function (done) {
1212
var server = http2.createServer(function onRequest (req, res) {
1313
getRawBody(req, { length: req.headers['content-length'] }, function (err, body) {
1414
if (err) {
@@ -32,6 +32,43 @@ describeHttp2('using http2 streams', function () {
3232
getRawBody(request, { encoding: true }, function (err, str) {
3333
http2close(server, session, function onClose () {
3434
assert.ifError(err)
35+
assert.strictEqual(headers[':status'], 200)
36+
assert.strictEqual(str, 'hello, world!')
37+
done()
38+
})
39+
})
40+
})
41+
})
42+
})
43+
44+
it('should read body streams', function (done) {
45+
var server = http2.createServer()
46+
47+
server.on('stream', function onStream (stream, headers) {
48+
getRawBody(stream, { length: headers['content-length'] }, function (err, body) {
49+
if (err) {
50+
stream.resume()
51+
stream.respond({ ':status': 500 })
52+
stream.end(err.message)
53+
return
54+
}
55+
56+
stream.end(body)
57+
})
58+
})
59+
60+
server.listen(function onListen () {
61+
var addr = server.address()
62+
var session = http2.connect('http://localhost:' + addr.port)
63+
var request = session.request({ ':method': 'POST', ':path': '/' })
64+
65+
request.end('hello, world!')
66+
67+
request.on('response', function onResponse (headers) {
68+
getRawBody(request, { encoding: true }, function (err, str) {
69+
http2close(server, session, function onClose () {
70+
assert.ifError(err)
71+
assert.strictEqual(headers[':status'], 200)
3572
assert.strictEqual(str, 'hello, world!')
3673
done()
3774
})
@@ -61,10 +98,11 @@ describeHttp2('using http2 streams', function () {
6198

6299
request.end('hello, world!')
63100

64-
request.on('response', function onResponse (res) {
101+
request.on('response', function onResponse (headers) {
65102
getRawBody(request, { encoding: true }, function (err, str) {
66103
http2close(server, session, function onClose () {
67104
assert.ifError(err)
105+
assert.strictEqual(headers[':status'], 500)
68106
assert.strictEqual(str, 'stream encoding should not be set')
69107
done()
70108
})

0 commit comments

Comments
 (0)