• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4
5// This is similar to simple/test-socket-write-after-fin, except that
6// we don't set allowHalfOpen.  Then we write after the client has sent
7// a FIN, and this is an error.  However, the standard "write after end"
8// message is too vague, and doesn't actually tell you what happens.
9
10const net = require('net');
11let serverData = '';
12let gotServerEnd = false;
13let clientData = '';
14let gotClientEnd = false;
15let gotServerError = false;
16
17const server = net.createServer(function(sock) {
18  sock.setEncoding('utf8');
19  sock.on('error', function(er) {
20    console.error(`${er.code}: ${er.message}`);
21    gotServerError = er;
22  });
23
24  sock.on('data', function(c) {
25    serverData += c;
26  });
27  sock.on('end', function() {
28    gotServerEnd = true;
29    sock.write(serverData);
30    sock.end();
31  });
32  server.close();
33});
34server.listen(0, function() {
35  const sock = net.connect(this.address().port);
36  sock.setEncoding('utf8');
37  sock.on('data', function(c) {
38    clientData += c;
39  });
40
41  sock.on('end', function() {
42    gotClientEnd = true;
43  });
44
45  process.on('exit', function() {
46    assert.strictEqual(clientData, '');
47    assert.strictEqual(serverData, 'hello1hello2hello3\nTHUNDERMUSCLE!');
48    assert(gotClientEnd);
49    assert(gotServerEnd);
50    assert(gotServerError);
51    assert.strictEqual(gotServerError.code, 'EPIPE');
52    assert.notStrictEqual(gotServerError.message, 'write after end');
53    console.log('ok');
54  });
55
56  sock.write('hello1');
57  sock.write('hello2');
58  sock.write('hello3\n');
59  sock.end('THUNDERMUSCLE!');
60});
61