test: http2 compat response.write() error checks

PR-URL: https://github.com/nodejs/node/pull/18859
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Trivikram 2018-02-18 13:39:06 -08:00 committed by Ruben Bridgewater
parent 5782c51dfb
commit a926c1a18f
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 52 additions and 95 deletions

View File

@ -1,95 +0,0 @@
'use strict';
const { mustCall,
mustNotCall,
expectsError,
hasCrypto, skip } = require('../common');
if (!hasCrypto)
skip('missing crypto');
const { createServer, connect } = require('http2');
// Http2ServerResponse.write does not imply there is a callback
{
const server = createServer();
server.listen(0, mustCall(() => {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = connect(url, mustCall(() => {
const request = client.request();
request.resume();
request.on('end', mustCall());
request.on('close', mustCall(() => {
client.close();
}));
}));
server.once('request', mustCall((request, response) => {
client.destroy();
response.stream.session.on('close', mustCall(() => {
response.on('error', mustNotCall());
expectsError(
() => { response.write('muahaha'); },
{
code: 'ERR_HTTP2_INVALID_STREAM'
}
);
server.close();
}));
}));
}));
}
{
const server = createServer();
server.listen(0, mustCall(() => {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = connect(url, mustCall(() => {
const request = client.request();
request.resume();
request.on('end', mustCall());
request.on('close', mustCall(() => client.close()));
}));
server.once('request', mustCall((request, response) => {
client.destroy();
response.stream.session.on('close', mustCall(() => {
expectsError(
() => response.write('muahaha'),
{
code: 'ERR_HTTP2_INVALID_STREAM'
}
);
server.close();
}));
}));
}));
}
{
const server = createServer();
server.listen(0, mustCall(() => {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = connect(url, mustCall(() => {
const request = client.request();
request.resume();
request.on('end', mustCall());
request.on('close', mustCall(() => client.close()));
}));
server.once('request', mustCall((request, response) => {
response.stream.session.on('close', mustCall(() => {
expectsError(
() => response.write('muahaha', 'utf8'),
{
code: 'ERR_HTTP2_INVALID_STREAM'
}
);
server.close();
}));
client.destroy();
}));
}));
}

View File

@ -0,0 +1,52 @@
'use strict';
const {
mustCall,
mustNotCall,
expectsError,
hasCrypto,
skip
} = require('../common');
if (!hasCrypto)
skip('missing crypto');
const { createServer, connect } = require('http2');
const assert = require('assert');
const server = createServer();
server.listen(0, mustCall(() => {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = connect(url, mustCall(() => {
const request = client.request();
request.resume();
request.on('end', mustCall());
request.on('close', mustCall(() => {
client.close();
}));
}));
server.once('request', mustCall((request, response) => {
// response.write() returns true
assert(response.write('muahaha', 'utf8', mustCall()));
response.stream.close(0, mustCall(() => {
response.on('error', mustNotCall());
// response.write() without cb returns error
expectsError(
() => { response.write('muahaha'); },
{
type: Error,
code: 'ERR_HTTP2_INVALID_STREAM',
message: 'The stream has been destroyed'
}
);
// response.write() with cb returns falsy value
assert(!response.write('muahaha', mustCall()));
client.destroy();
server.close();
}));
}));
}));