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: add http2 test for method CONNECT #15052

Closed
Closed
Changes from all commits
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
41 changes: 41 additions & 0 deletions test/parallel/test-http2-compat-method-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Flags: --expose-http2
'use strict';

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

const server = http2.createServer(common.mustNotCall());

server.listen(0, common.mustCall(() => testMethodConnect(2)));

server.once('connect', common.mustCall((req, res) => {
assert.strictEqual(req.headers[':method'], 'CONNECT');
res.statusCode = 405;
res.end();
}));

function testMethodConnect(testsToRun) {
if (!testsToRun) {
return server.close();
Copy link
Member

Choose a reason for hiding this comment

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

Just pointing out... we now have a "countdown" utility that can be used to improve this kind of logic.

For instance,

const Countdown = require('../common/countdown');
const countdown = new Countdown(2, () => console.log('done!') );
countdown.dec();
countdown.dec();

The callback based into Countdown will be called automatically and synchronously when the internal counter reaches zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @jasnell, that's super useful. Will definitely use it in the future! Did you want me to update this test as well or good as is?

Copy link
Member

Choose a reason for hiding this comment

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

It's good as is. We can convert over to the countdown util separately

}

const port = server.address().port;
const client = http2.connect(`http://localhost:${port}`);
const req = client.request({
':method': 'CONNECT',
':authority': `localhost:${port}`
});

req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 405);
}));
req.resume();
req.on('end', common.mustCall(() => {
client.destroy();
testMethodConnect(testsToRun - 1);
}));
req.end();
}