source: trip-planner-front/node_modules/select-hose/test/api-test.js@ 6c1585f

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1var assert = require('assert');
2var net = require('net');
3var streamPair = require('stream-pair');
4var thing = require('handle-thing');
5var Buffer = require('buffer').Buffer;
6
7var fixtures = require('./fixtures');
8
9var hose = require('../');
10
11describe('Select Hose', function() {
12 var pair;
13 var socket;
14 beforeEach(function() {
15 pair = streamPair.create();
16
17 var handle = thing.create(pair.other);
18 socket = new net.Socket({ handle: handle });
19
20 // For v0.8
21 socket.readable = true;
22 socket.writable = true;
23 });
24
25 it('should select handler using first byte', function(done) {
26 var filter = hose.create(socket, function filter(data, callback) {
27 if (data[0] === 0x80)
28 return callback(null, 'spdy');
29 else
30 return callback(null, 'http');
31 });
32
33 filter.on('select', function(protocol, socket) {
34 assert.equal(protocol, 'spdy');
35 socket.on('data', function(chunk) {
36 assert.equal(chunk.toString('hex'), '80030001');
37 done();
38 });
39 });
40
41 pair.write(new Buffer('80030001', 'hex'));
42 });
43
44 it('should select handler using two packets', function(done) {
45 var filter = hose.create(socket, function filter(data, callback) {
46 if (data.length < 2)
47 return;
48
49 if (data[0] === 0x80 && data[1] === 0x03)
50 return callback(null, 'spdy');
51 else
52 return callback(null, 'http');
53 });
54
55 filter.on('select', function(protocol, socket) {
56 assert.equal(protocol, 'spdy');
57 socket.on('data', function(chunk) {
58 assert.equal(chunk.toString('hex'), '80030001');
59 done();
60 });
61 });
62
63 pair.write(new Buffer('80', 'hex'));
64 setTimeout(function() {
65 pair.write(new Buffer('030001', 'hex'));
66 }, 20);
67 });
68
69 it('should read excessive packets', function(done) {
70 var filter = hose.create(socket, function filter(data, callback) {
71 if (data.length < 2)
72 return;
73
74 if (data[0] === 0x61 && data[1] === 0x62)
75 return callback(null, 'spdy');
76 else
77 return callback(null, 'http');
78 });
79
80 filter.on('select', function(protocol, socket) {
81 assert.equal(protocol, 'spdy');
82 fixtures.expectData(socket, 'abcd', done);
83 });
84
85 pair.write('a');
86 setTimeout(function() {
87 pair.write('b');
88 setTimeout(function() {
89 pair.end('cd');
90 }, 20);
91 }, 20);
92 });
93
94 it('should re-emit errors', function(done) {
95 var filter = hose.create(socket, function filter(data, callback) {
96 if (data.length < 2)
97 return;
98
99 if (data[0] === 0x61 && data[1] === 0x62)
100 return callback(null, 'spdy');
101 else
102 return callback(null, 'http');
103 });
104
105 filter.on('error', function(err) {
106 done();
107 });
108
109 socket.emit('error', new Error(123));
110 });
111});
Note: See TracBrowser for help on using the repository browser.