api-test.js 2.7 KB

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