source: trip-planner-front/node_modules/handle-thing/test/api-test.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/* eslint-env mocha */
2
3var assert = require('assert')
4var net = require('net')
5var streamPair = require('stream-pair')
6
7var thing = require('../')
8
9describe('Handle Thing', function () {
10 var handle
11 var pair
12 var socket;
13
14 [ 'normal', 'lazy' ].forEach(function (mode) {
15 describe(mode, function () {
16 beforeEach(function () {
17 pair = streamPair.create()
18 handle = thing.create(mode === 'normal' ? pair.other : null)
19 socket = new net.Socket({
20 handle: handle,
21 readable: true,
22 writable: true
23 })
24
25 if (mode === 'lazy') {
26 setTimeout(function () {
27 handle.setStream(pair.other)
28 }, 50)
29 }
30 })
31
32 afterEach(function () {
33 assert(handle._stream)
34 })
35
36 it('should write data to Socket', function (done) {
37 pair.write('hello')
38 pair.write(' world')
39 pair.end('... ok')
40
41 var chunks = ''
42 socket.on('data', function (chunk) {
43 chunks += chunk
44 })
45 socket.on('end', function () {
46 assert.strictEqual(chunks, 'hello world... ok')
47
48 // allowHalfOpen is `false`, so the `end` should be followed by `close`
49 socket.once('close', function () {
50 done()
51 })
52 })
53 })
54
55 it('should read data from Socket', function (done) {
56 socket.write('hello')
57 socket.write(' world')
58 socket.end('... ok')
59
60 var chunks = ''
61 pair.on('data', function (chunk) {
62 chunks += chunk
63 })
64 pair.on('end', function () {
65 assert.strictEqual(chunks, 'hello world... ok')
66
67 done()
68 })
69 })
70
71 it('should invoke `close` callback', function (done) {
72 handle._options.close = function (callback) {
73 done()
74 process.nextTick(callback)
75 }
76
77 pair.end('hello')
78 socket.resume()
79 })
80
81 it('should kill pending requests', function (done) {
82 handle._options.close = function () {
83 setTimeout(done, 75)
84 }
85
86 socket.write('hello')
87 socket.destroy()
88 })
89
90 if (mode === 'normal') {
91 it('should invoke `getPeerName` callback', function () {
92 handle._options.getPeerName = function () {
93 return { address: 'ohai' }
94 }
95
96 assert.strictEqual(socket.remoteAddress, 'ohai')
97 })
98
99 it('should emit ECONNRESET at `close` event', function (done) {
100 pair.other.emit('close')
101
102 socket.on('error', function (err) {
103 assert(/ECONNRESET/.test(err.message))
104 done()
105 })
106 })
107 }
108 })
109 })
110})
Note: See TracBrowser for help on using the repository browser.