summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-compat-serverresponse-statuscode.js
blob: dd92db8d96597a482f75f3e6d5a71538c4c39e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';

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

// Http2ServerResponse should have a statusCode property

const server = h2.createServer();
server.listen(0, common.mustCall(function() {
  const port = server.address().port;
  server.once('request', common.mustCall(function(request, response) {
    const expectedDefaultStatusCode = 200;
    const realStatusCodes = {
      continue: 100,
      ok: 200,
      multipleChoices: 300,
      badRequest: 400,
      internalServerError: 500
    };
    const fakeStatusCodes = {
      tooLow: 99,
      tooHigh: 600
    };

    assert.strictEqual(response.statusCode, expectedDefaultStatusCode);

    // Setting the response.statusCode should not throw.
    response.statusCode = realStatusCodes.ok;
    response.statusCode = realStatusCodes.multipleChoices;
    response.statusCode = realStatusCodes.badRequest;
    response.statusCode = realStatusCodes.internalServerError;

    common.expectsError(function() {
      response.statusCode = realStatusCodes.continue;
    }, {
      code: 'ERR_HTTP2_INFO_STATUS_NOT_ALLOWED',
      type: RangeError
    });
    common.expectsError(function() {
      response.statusCode = fakeStatusCodes.tooLow;
    }, {
      code: 'ERR_HTTP2_STATUS_INVALID',
      type: RangeError
    });
    common.expectsError(function() {
      response.statusCode = fakeStatusCodes.tooHigh;
    }, {
      code: 'ERR_HTTP2_STATUS_INVALID',
      type: RangeError
    });

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

  const url = `http://localhost:${port}`;
  const client = h2.connect(url, common.mustCall(function() {
    const headers = {
      ':path': '/',
      ':method': 'GET',
      ':scheme': 'http',
      ':authority': `localhost:${port}`
    };
    const request = client.request(headers);
    request.on('end', common.mustCall(function() {
      client.close();
    }));
    request.end();
    request.resume();
  }));
}));