• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4
5const http = require('http');
6const OutgoingMessage = http.OutgoingMessage;
7const ClientRequest = http.ClientRequest;
8const ServerResponse = http.ServerResponse;
9
10assert.strictEqual(
11  typeof ClientRequest.prototype._implicitHeader, 'function');
12assert.strictEqual(
13  typeof ServerResponse.prototype._implicitHeader, 'function');
14
15// validateHeader
16assert.throws(() => {
17  const outgoingMessage = new OutgoingMessage();
18  outgoingMessage.setHeader();
19}, {
20  code: 'ERR_INVALID_HTTP_TOKEN',
21  name: 'TypeError',
22  message: 'Header name must be a valid HTTP token ["undefined"]'
23});
24
25assert.throws(() => {
26  const outgoingMessage = new OutgoingMessage();
27  outgoingMessage.setHeader('test');
28}, {
29  code: 'ERR_HTTP_INVALID_HEADER_VALUE',
30  name: 'TypeError',
31  message: 'Invalid value "undefined" for header "test"'
32});
33
34assert.throws(() => {
35  const outgoingMessage = new OutgoingMessage();
36  outgoingMessage.setHeader(404);
37}, {
38  code: 'ERR_INVALID_HTTP_TOKEN',
39  name: 'TypeError',
40  message: 'Header name must be a valid HTTP token ["404"]'
41});
42
43assert.throws(() => {
44  const outgoingMessage = new OutgoingMessage();
45  outgoingMessage.setHeader.call({ _header: 'test' }, 'test', 'value');
46}, {
47  code: 'ERR_HTTP_HEADERS_SENT',
48  name: 'Error',
49  message: 'Cannot set headers after they are sent to the client'
50});
51
52assert.throws(() => {
53  const outgoingMessage = new OutgoingMessage();
54  outgoingMessage.setHeader('200', 'あ');
55}, {
56  code: 'ERR_INVALID_CHAR',
57  name: 'TypeError',
58  message: 'Invalid character in header content ["200"]'
59});
60
61// write
62{
63  const outgoingMessage = new OutgoingMessage();
64
65  assert.throws(
66    () => {
67      outgoingMessage.write('');
68    },
69    {
70      code: 'ERR_METHOD_NOT_IMPLEMENTED',
71      name: 'Error',
72      message: 'The _implicitHeader() method is not implemented'
73    }
74  );
75}
76
77assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));
78
79assert.throws(() => {
80  const outgoingMessage = new OutgoingMessage();
81  outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' });
82}, {
83  code: 'ERR_INVALID_ARG_TYPE',
84  name: 'TypeError',
85  message: 'The first argument must be of type string or an instance of ' +
86           'Buffer or Uint8Array. Received undefined'
87});
88
89assert.throws(() => {
90  const outgoingMessage = new OutgoingMessage();
91  outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' }, 1);
92}, {
93  code: 'ERR_INVALID_ARG_TYPE',
94  name: 'TypeError',
95  message: 'The first argument must be of type string or an instance of ' +
96           'Buffer or Uint8Array. Received type number (1)'
97});
98
99// addTrailers()
100// The `Error` comes from the JavaScript engine so confirm that it is a
101// `TypeError` but do not check the message. It will be different in different
102// JavaScript engines.
103assert.throws(() => {
104  const outgoingMessage = new OutgoingMessage();
105  outgoingMessage.addTrailers();
106}, TypeError);
107
108assert.throws(() => {
109  const outgoingMessage = new OutgoingMessage();
110  outgoingMessage.addTrailers({ 'あ': 'value' });
111}, {
112  code: 'ERR_INVALID_HTTP_TOKEN',
113  name: 'TypeError',
114  message: 'Trailer name must be a valid HTTP token ["あ"]'
115});
116
117assert.throws(() => {
118  const outgoingMessage = new OutgoingMessage();
119  outgoingMessage.addTrailers({ 404: 'あ' });
120}, {
121  code: 'ERR_INVALID_CHAR',
122  name: 'TypeError',
123  message: 'Invalid character in trailer content ["404"]'
124});
125
126{
127  const outgoingMessage = new OutgoingMessage();
128  assert.strictEqual(outgoingMessage.destroyed, false);
129  outgoingMessage.destroy();
130  assert.strictEqual(outgoingMessage.destroyed, true);
131}
132