• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24
25// Verify that ECONNRESET is raised when writing to a http request
26// where the server has ended the socket.
27
28const assert = require('assert');
29const http = require('http');
30
31const kResponseDestroyed = Symbol('kResponseDestroyed');
32
33const server = http.createServer(function(req, res) {
34  req.on('data', common.mustCall(function() {
35    res.destroy();
36    server.emit(kResponseDestroyed);
37  }));
38});
39
40server.listen(0, function() {
41  const req = http.request({
42    port: this.address().port,
43    path: '/',
44    method: 'POST'
45  });
46
47  server.once(kResponseDestroyed, common.mustCall(function() {
48    req.write('hello');
49  }));
50
51  req.on('error', common.mustCall(function(er) {
52    assert.strictEqual(req.res, null);
53    switch (er.code) {
54      // This is the expected case
55      case 'ECONNRESET':
56        break;
57
58      // On Windows, this sometimes manifests as ECONNABORTED
59      case 'ECONNABORTED':
60        break;
61
62      // This test is timing sensitive so an EPIPE is not out of the question.
63      // It should be infrequent, given the 50 ms timeout, but not impossible.
64      case 'EPIPE':
65        break;
66
67      default:
68        // Write to a torn down client should RESET or ABORT
69        assert.fail(`Unexpected error code ${er.code}`);
70    }
71
72
73    assert.strictEqual(req.outputData.length, 0);
74    server.close();
75  }));
76
77  req.on('response', common.mustNotCall());
78  req.write('hello', common.mustSucceed());
79});
80