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'); 24const assert = require('assert'); 25 26// Verify that ECONNRESET is raised when writing to a http request 27// where the server has ended the socket. 28 29const http = require('http'); 30const server = http.createServer(function(req, res) { 31 setImmediate(function() { 32 res.destroy(); 33 }); 34}); 35 36server.listen(0, function() { 37 const req = http.request({ 38 port: this.address().port, 39 path: '/', 40 method: 'POST' 41 }); 42 43 function write() { 44 req.write('hello', function() { 45 setImmediate(write); 46 }); 47 } 48 49 req.on('error', common.mustCall(function(er) { 50 assert.strictEqual(req.res, null); 51 switch (er.code) { 52 // This is the expected case 53 case 'ECONNRESET': 54 break; 55 56 // On Windows, this sometimes manifests as ECONNABORTED 57 case 'ECONNABORTED': 58 break; 59 60 // This test is timing sensitive so an EPIPE is not out of the question. 61 // It should be infrequent, given the 50 ms timeout, but not impossible. 62 case 'EPIPE': 63 break; 64 65 default: 66 // Write to a torn down client should RESET or ABORT 67 assert.fail(`Unexpected error code ${er.code}`); 68 } 69 70 71 assert.strictEqual(req.outputData.length, 0); 72 server.close(); 73 })); 74 75 req.on('response', common.mustNotCall()); 76 77 write(); 78}); 79