Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase Http2ServerResponse test coverage #15074

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Flags: --expose-http2
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');

// Check that destroying the Http2ServerResponse stream produces
// the expected result, including the ability to throw an error
// which is emitted on server.streamError

const errors = [
'test-error',
Error('test')
];
let nextError;

const server = http2.createServer(common.mustCall((req, res) => {
req.once('error', common.mustNotCall());
req.once('error', common.mustNotCall());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this are both req. I think res should receive the error, considering the semantics of .destroy(err) throughout the codebase. Check with H1, but I think we have a bug in the compat layer because of this.


res.on('finish', common.mustCall(() => {
assert.doesNotThrow(() => res.destroy(nextError));
assert.strictEqual(res.closed, true);
}));

if (req.path !== '/') {
nextError = errors.shift();
}
res.destroy(nextError);
}, 3));

server.on(
'streamError',
common.mustCall((err) => assert.strictEqual(err, nextError), 2)
);

server.listen(0, common.mustCall(() => {
const port = server.address().port;
const client = http2.connect(`http://localhost:${port}`);
const req = client.request({
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
});

req.on('response', common.mustNotCall());
req.on('error', common.mustNotCall());
req.on('end', common.mustCall());

req.resume();
req.end();

const req2 = client.request({
':path': '/error',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
});

req2.on('response', common.mustNotCall());
req2.on('error', common.mustNotCall());
req2.on('end', common.mustCall());

req2.resume();
req2.end();

const req3 = client.request({
':path': '/error',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
});

req3.on('response', common.mustNotCall());
req3.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 2'
}));
req3.on('end', common.mustCall(() => {
server.close();
client.destroy();
}));

req3.resume();
req3.end();
}));
2 changes: 2 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const {
// Http2ServerResponse.end callback is called only the first time,
// but may be invoked repeatedly without throwing errors.
const server = createServer(mustCall((request, response) => {
strictEqual(response.closed, false);
response.end(mustCall(() => {
server.close();
}));
response.end(mustNotCall());
strictEqual(response.closed, true);
}));
server.listen(0, mustCall(() => {
const { port } = server.address();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ const server = h2.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
server.once('request', common.mustCall(function(request, response) {
assert.strictEqual(response.headersSent, false);
response.flushHeaders();
assert.strictEqual(response.headersSent, true);
response.flushHeaders(); // Idempotent

common.expectsError(() => {
response.writeHead(400, { 'foo-bar': 'abc123' });
}, {
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ server.listen(0, common.mustCall(function() {
response.getHeaders()[fake] = fake;
assert.strictEqual(response.hasHeader(fake), false);

assert.strictEqual(response.sendDate, true);
response.sendDate = false;
assert.strictEqual(response.sendDate, false);

response.on('finish', common.mustCall(function() {
server.close();
}));
Expand Down