Skip to content
Browse files

test: more coverage

* zlib-pool: improve APIs
  • Loading branch information...
1 parent 76dfea3 commit eab837c32712cdf100224be3b061ced4dc7a53f0 @indutny committed Jul 19, 2012
Showing with 111 additions and 104 deletions.
  1. +12 −5 lib/spdy/utils.js
  2. +4 −5 lib/spdy/zlib-pool.js
  3. +2 −6 test/unit/framer-test.js
  4. +92 −87 test/unit/parser-test.js
  5. +1 −1 test/unit/server-test.js
View
17 lib/spdy/utils.js
@@ -1,4 +1,5 @@
-var utils = exports;
+var spdy = require('../spdy'),
+ utils = exports;
var zlib = require('zlib'),
Buffer = require('buffer').Buffer;
@@ -7,8 +8,11 @@ var zlib = require('zlib'),
// ### function createDeflate ()
// Creates deflate stream with SPDY dictionary
//
-utils.createDeflate = function createDeflate(dictionary) {
- var deflate = zlib.createDeflate({ dictionary: dictionary, windowBits: 11 });
+utils.createDeflate = function createDeflate(version) {
+ var deflate = zlib.createDeflate({
+ dictionary: spdy.protocol[version].dictionary,
+ windowBits: 11
+ });
// Define lock information early
deflate.locked = false;
@@ -21,8 +25,11 @@ utils.createDeflate = function createDeflate(dictionary) {
// ### function createInflate ()
// Creates inflate stream with SPDY dictionary
//
-utils.createInflate = function createInflate(dictionary) {
- var inflate = zlib.createInflate({ dictionary: dictionary, windowBits: 15 });
+utils.createInflate = function createInflate(version) {
+ var inflate = zlib.createInflate({
+ dictionary: spdy.protocol[version].dictionary,
+ windowBits: 15
+ });
// Define lock information early
inflate.locked = false;
View
9 lib/spdy/zlib-pool.js
@@ -26,15 +26,14 @@ var x = 0;
// Returns pair from pool or a new one
//
Pool.prototype.get = function get(version, callback) {
- var version_number = version.split(/\//)[1],
- dictionary = spdy.protocol[version_number].dictionary;
-
if (this.pool[version].length > 0) {
return this.pool[version].pop();
} else {
+ var id = version.split('/', 2)[1];
+
return {
- deflate: spdy.utils.createDeflate(dictionary),
- inflate: spdy.utils.createInflate(dictionary)
+ deflate: spdy.utils.createDeflate(id),
+ inflate: spdy.utils.createInflate(id)
};
}
};
View
8 test/unit/framer-test.js
@@ -9,12 +9,8 @@ suite('A Framer of SPDY module', function() {
framer;
setup(function() {
- inflate = spdy.utils.zwrap(spdy.utils.createInflate(
- spdy.protocol[2].dictionary
- ));
- deflate = spdy.utils.zwrap(spdy.utils.createDeflate(
- spdy.protocol[2].dictionary
- ));
+ inflate = spdy.utils.zwrap(spdy.utils.createInflate(2));
+ deflate = spdy.utils.zwrap(spdy.utils.createDeflate(2));
framer = new spdy.protocol[2].Framer(deflate, inflate);
});
View
179 test/unit/parser-test.js
@@ -5,93 +5,98 @@ var assert = require('assert'),
suite('A Parser of SPDY module', function() {
var parser;
- setup(function() {
- var deflate = spdy.utils.createDeflate(),
- inflate = spdy.utils.createInflate();
-
- parser = new spdy.parser.create({
- socket: {
- setNoDelay: function() {}
- }
- }, deflate, inflate);
-
- parser.createFramer(2);
- });
-
- test('should wait for headers initially', function() {
- assert.equal(parser.waiting, 8);
- });
-
- test('should update buffered property once given < 8 bytes', function() {
- parser.write(new Buffer(5));
- assert.equal(parser.buffered, 5);
- });
-
- test('given SYN_STREAM header should start waiting for body', function() {
- parser.write(new Buffer([
- 0x80, 0x02, 0x00, 0x01, // Control frame, version, type (SYN_STREAM)
- 0x00, 0x00, 0x12, 0x34
- ]));
-
- assert.equal(parser.waiting, 0x1234);
- assert.equal(parser.state.type, 'frame-body');
- assert.ok(parser.state.header.control);
- assert.equal(parser.state.header.flags, 0);
- assert.equal(parser.state.header.length, 0x1234);
- });
-
- test('given DATA header should start waiting for body', function() {
- parser.write(new Buffer([
- 0x00, 0x00, 0x00, 0x01, // Data frame, stream ID
- 0x00, 0x00, 0x12, 0x34
- ]));
-
- assert.equal(parser.waiting, 0x1234);
- assert.equal(parser.state.type, 'frame-body');
- assert.ok(!parser.state.header.control);
- assert.equal(parser.state.header.id, 1);
- assert.equal(parser.state.header.flags, 0);
- assert.equal(parser.state.header.length, 0x1234);
- });
-
- test('given chunked header should not fail', function() {
- parser.write(new Buffer([
- 0x80, 0x02, 0x00, 0x01 // Control frame, version, type (SYN_STREAM)
- ]));
- assert.equal(parser.buffered, 4);
-
- parser.write(new Buffer([
- 0x00, 0x00, 0x12, 0x34
- ]));
- assert.equal(parser.buffered, 0);
-
- assert.equal(parser.waiting, 0x1234);
- assert.equal(parser.state.type, 'frame-body');
- assert.ok(parser.state.header.control);
- assert.equal(parser.state.header.flags, 0);
- assert.equal(parser.state.header.length, 0x1234);
- });
-
- test('given header and body should emit `frame`', function(done) {
- parser.on('frame', function(frame) {
- assert.ok(frame.type === 'DATA');
- assert.equal(frame.id, 1);
- assert.equal(frame.data.length, 4);
- assert.equal(frame.data[0], 0x01);
- assert.equal(frame.data[1], 0x02);
- assert.equal(frame.data[2], 0x03);
- assert.equal(frame.data[3], 0x04);
- done();
+ [2,3].forEach(function(version) {
+ suite('version ' + version, function() {
+ setup(function() {
+ var deflate = spdy.utils.createDeflate(version),
+ inflate = spdy.utils.createInflate(version);
+
+ parser = new spdy.parser.create({
+ socket: {
+ setNoDelay: function() {}
+ },
+ write: function() {}
+ }, deflate, inflate);
+
+ parser.createFramer(version);
+ });
+
+ test('should wait for headers initially', function() {
+ assert.equal(parser.waiting, 8);
+ });
+
+ test('should update buffered property once given < 8 bytes', function() {
+ parser.write(new Buffer(5));
+ assert.equal(parser.buffered, 5);
+ });
+
+ test('given SYN_STREAM header should start waiting for body', function() {
+ parser.write(new Buffer([
+ 0x80, 0x02, 0x00, 0x01, // Control frame, version, type (SYN_STREAM)
+ 0x00, 0x00, 0x12, 0x34
+ ]));
+
+ assert.equal(parser.waiting, 0x1234);
+ assert.equal(parser.state.type, 'frame-body');
+ assert.ok(parser.state.header.control);
+ assert.equal(parser.state.header.flags, 0);
+ assert.equal(parser.state.header.length, 0x1234);
+ });
+
+ test('given DATA header should start waiting for body', function() {
+ parser.write(new Buffer([
+ 0x00, 0x00, 0x00, 0x01, // Data frame, stream ID
+ 0x00, 0x00, 0x12, 0x34
+ ]));
+
+ assert.equal(parser.waiting, 0x1234);
+ assert.equal(parser.state.type, 'frame-body');
+ assert.ok(!parser.state.header.control);
+ assert.equal(parser.state.header.id, 1);
+ assert.equal(parser.state.header.flags, 0);
+ assert.equal(parser.state.header.length, 0x1234);
+ });
+
+ test('given chunked header should not fail', function() {
+ parser.write(new Buffer([
+ 0x80, 0x02, 0x00, 0x01 // Control frame, version, type (SYN_STREAM)
+ ]));
+ assert.equal(parser.buffered, 4);
+
+ parser.write(new Buffer([
+ 0x00, 0x00, 0x12, 0x34
+ ]));
+ assert.equal(parser.buffered, 0);
+
+ assert.equal(parser.waiting, 0x1234);
+ assert.equal(parser.state.type, 'frame-body');
+ assert.ok(parser.state.header.control);
+ assert.equal(parser.state.header.flags, 0);
+ assert.equal(parser.state.header.length, 0x1234);
+ });
+
+ test('given header and body should emit `frame`', function(done) {
+ parser.on('frame', function(frame) {
+ assert.ok(frame.type === 'DATA');
+ assert.equal(frame.id, 1);
+ assert.equal(frame.data.length, 4);
+ assert.equal(frame.data[0], 0x01);
+ assert.equal(frame.data[1], 0x02);
+ assert.equal(frame.data[2], 0x03);
+ assert.equal(frame.data[3], 0x04);
+ done();
+ });
+
+ parser.write(new Buffer([
+ 0x00, 0x00, 0x00, 0x01, // Data frame, stream ID
+ 0x00, 0x00, 0x00, 0x04,
+ 0x01, 0x02, 0x03, 0x04 // Body
+ ]));
+
+ // Waits for next frame
+ assert.equal(parser.waiting, 8);
+ assert.equal(parser.state.type, 'frame-head');
+ });
});
-
- parser.write(new Buffer([
- 0x00, 0x00, 0x00, 0x01, // Data frame, stream ID
- 0x00, 0x00, 0x00, 0x04,
- 0x01, 0x02, 0x03, 0x04 // Body
- ]));
-
- // Waits for next frame
- assert.equal(parser.waiting, 8);
- assert.equal(parser.state.type, 'frame-head');
});
});
View
2 test/unit/server-test.js
@@ -39,7 +39,7 @@ suite('A SPDY Server', function() {
'localhost',
{ NPNProtocols: ['spdy/2'] },
function() {
- var deflate = spdy.utils.createDeflate(),
+ var deflate = spdy.utils.createDeflate(2),
chunks = [],
length = 0;

0 comments on commit eab837c

Please sign in to comment.
Something went wrong with that request. Please try again.