• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const assert = require('assert');
7const h2 = require('http2');
8
9// Http2ServerResponse.writeHead should override previous headers
10
11const server = h2.createServer();
12server.listen(0, common.mustCall(function() {
13  const port = server.address().port;
14  server.once('request', common.mustCall(function(request, response) {
15    response.setHeader('foo-bar', 'def456');
16
17    // Override
18    const returnVal = response.writeHead(418, { 'foo-bar': 'abc123' });
19
20    assert.strictEqual(returnVal, response);
21
22    assert.throws(() => { response.writeHead(300); }, {
23      code: 'ERR_HTTP2_HEADERS_SENT'
24    });
25
26    response.on('finish', common.mustCall(function() {
27      server.close();
28      process.nextTick(common.mustCall(() => {
29        // The stream is invalid at this point,
30        // and this line verifies this does not throw.
31        response.writeHead(300);
32      }));
33    }));
34    response.end();
35  }));
36
37  const url = `http://localhost:${port}`;
38  const client = h2.connect(url, common.mustCall(function() {
39    const headers = {
40      ':path': '/',
41      ':method': 'GET',
42      ':scheme': 'http',
43      ':authority': `localhost:${port}`
44    };
45    const request = client.request(headers);
46    request.on('response', common.mustCall(function(headers) {
47      assert.strictEqual(headers['foo-bar'], 'abc123');
48      assert.strictEqual(headers[':status'], 418);
49    }, 1));
50    request.on('end', common.mustCall(function() {
51      client.close();
52    }));
53    request.end();
54    request.resume();
55  }));
56}));
57