2017-09-30 10:06:21 -04:00
|
|
|
'use strict';
|
2017-12-11 03:56:41 -02:00
|
|
|
// Flags: --expose-internals
|
2017-09-30 10:06:21 -04:00
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
2019-12-25 18:02:16 +01:00
|
|
|
const assert = require('assert');
|
2017-09-30 10:06:21 -04:00
|
|
|
const http2 = require('http2');
|
2018-08-14 16:01:54 -07:00
|
|
|
const { internalBinding } = require('internal/test/binding');
|
|
|
|
const { Http2Stream } = internalBinding('http2');
|
2018-02-18 19:30:51 -08:00
|
|
|
|
2017-09-30 10:06:21 -04:00
|
|
|
const server = http2.createServer();
|
|
|
|
|
2018-02-18 19:30:51 -08:00
|
|
|
Http2Stream.prototype.respond = () => 1;
|
|
|
|
server.on('stream', common.mustCall((stream) => {
|
2017-09-30 10:06:21 -04:00
|
|
|
|
2018-02-18 19:30:51 -08:00
|
|
|
// Send headers
|
2018-04-11 16:11:35 -07:00
|
|
|
stream.respond({ 'content-type': 'text/plain' });
|
2018-02-18 19:30:51 -08:00
|
|
|
|
|
|
|
// Should throw if headers already sent
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-02-18 19:30:51 -08:00
|
|
|
() => stream.respond(),
|
|
|
|
{
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'Error',
|
2018-02-18 19:30:51 -08:00
|
|
|
code: 'ERR_HTTP2_HEADERS_SENT',
|
|
|
|
message: 'Response has already been initiated.'
|
|
|
|
}
|
|
|
|
);
|
2017-09-30 10:06:21 -04:00
|
|
|
|
2018-02-18 19:30:51 -08:00
|
|
|
// Should throw if stream already destroyed
|
|
|
|
stream.destroy();
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-02-18 19:30:51 -08:00
|
|
|
() => stream.respond(),
|
|
|
|
{
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'Error',
|
2018-02-18 19:30:51 -08:00
|
|
|
code: 'ERR_HTTP2_INVALID_STREAM',
|
|
|
|
message: 'The stream has been destroyed'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}));
|
2017-09-30 10:06:21 -04:00
|
|
|
|
2018-02-18 19:30:51 -08:00
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const client = http2.connect(`http://localhost:${server.address().port}`);
|
|
|
|
const req = client.request();
|
2017-09-30 10:06:21 -04:00
|
|
|
|
|
|
|
req.on('end', common.mustCall(() => {
|
2017-12-12 11:34:17 -08:00
|
|
|
client.close();
|
2018-02-18 19:30:51 -08:00
|
|
|
server.close();
|
2017-09-30 10:06:21 -04:00
|
|
|
}));
|
2018-02-18 19:30:51 -08:00
|
|
|
req.resume();
|
|
|
|
req.end();
|
|
|
|
}));
|