Skip to content

Commit

Permalink
Mocha tests and use 'send'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgco committed Jul 18, 2012
1 parent 95b44db commit eaf85cb
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 125 deletions.
35 changes: 30 additions & 5 deletions lib/staticGzip.js
Expand Up @@ -10,7 +10,8 @@

// Commented out as I think that connect is avalible from within express...
// try {
var staticMiddleware = require('connect').static;
// var staticMiddleware = require('connect').static;

// } catch (e) {
// staticMiddleware = require('express').static;
// }
Expand All @@ -23,8 +24,9 @@ var fs = require('fs'),
StoreStream = require('./storeStream'),
FileAsset = require('./fileAsset');

var mime = staticMiddleware.mime,
staticSend = staticMiddleware.send;
var mime = require('mime'),
send = require('send')
;

/**
* Strip `Content-*` headers from `res`.
Expand Down Expand Up @@ -59,6 +61,23 @@ exports.filter = function(req, res){
return type.match(/json|text|javascript/);
};

/**
* Parse the `req` url with memoization.
*
* @param {ServerRequest} req
* @return {Object}
* @api private
*/

var parseUrl = function(req){
var parsed = req._parsedUrl;
if (parsed && parsed.href == req.url) {
return parsed;
} else {
return req._parsedUrl = parse(req.url);
}
};

/**
* By default gzip's static's that match the given regular expression /text|javascript|json/
* and then serves them with Connects static provider, denoted by the given `dirPath`.
Expand Down Expand Up @@ -92,7 +111,7 @@ exports = module.exports = function staticGzip(dirPath, options){

var maxAge = options.maxAge || 86400000,
contentTypeMatch = options.contentTypeMatch || /text|javascript|json/,
clientMaxAge = options.clientMaxAge || 0,
clientMaxAge = options.clientMaxAge || 604800000,
prefix = options.prefix || '',
names = Object.keys(methods),
compressionOptions = options.compression || {},
Expand All @@ -114,7 +133,13 @@ exports = module.exports = function staticGzip(dirPath, options){
var o = Object.create(options);
o.path = name;
o.maxAge = clientMaxAge;
staticSend(req, res, next, o);
var urlReq = parseUrl(req).pathname;
console.log(dirPath + urlReq);
send(req, urlReq)
.maxage(clientMaxAge || 0)
.root(dirPath)
.pipe(res)
;
}

function setHeaders(stat, asset) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -14,7 +14,8 @@
, "scripts": { "test": "mocha"
}
, "main" : "./index.js"
, "dependencies" : {
, "dependencies" : { "send": "*"
, "mime": "*"
}
, "devDependencies": { "should": "*"
, "connect": "*"
Expand Down
67 changes: 67 additions & 0 deletions test/prefix.test.js
@@ -0,0 +1,67 @@
var assert = require('assert')
, http = require('http')
, fs = require('fs')
, connect = require('connect')
, join = require('path').join
, gzippo = require('../')
;
var fixtures = join(__dirname, 'fixtures')
, port = 32124
, app
, request
;

// read a fixture file synchronously
function file(name) {
return fs.readFileSync(join(fixtures, name));
}

describe('gzippo.statisGzip (with prefix)', function() {

it('should successfully serve a .json file with a path prefix', function(done) {
var app = connect.createServer();
app.use(gzippo.staticGzip(fixtures, { prefix: '/foo' }));
request = require('./request')({ port: port + 5 });

app.listen(port + 5, function() {
request('/foo/user.json', { 'Accept-Encoding': 'gzip' },
function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'application/json; charset=UTF-8');
assert.equal(data.length, '69');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('user.gzip'));

done();
}
);
});
});

it('should serve files as expected with a / prefix', function(done) {
var app = connect.createServer();
app.use(gzippo.staticGzip(fixtures, { prefix: '/' }));
request = require('./request')({ port: port + 6});

app.listen(port + 6, function() {
request('/user.json', { 'Accept-Encoding': 'gzip' },
function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'application/json; charset=UTF-8');
assert.equal(data.length, '69');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('user.gzip'));

done();
}
);
});
});

});
51 changes: 51 additions & 0 deletions test/request.js
@@ -0,0 +1,51 @@
var http = require('http')
;

var port;
// basic request mocking function
module.exports = function (options) {
port = options.port || 32123;
return request;
};

var request = function(path, headers, callback) {
var options = {
host: '127.0.0.1',
port: port,
path: path,
headers: headers || {},
method: 'GET'
};

var req = http.request(options, function(res) {
var buffers = []
, total = 0;

res.on('data', function(buf) {
buffers.push(buf);
total += buf.length;
});

res.on('end', function() {
var data = new Buffer(total)
, offset = 0;

for (var i = 0; i < buffers.length; i++) {
buffers[i].copy(data, offset);
offset += buffers[i].length;
}

callback(null, res, data);
});

res.on('error', function(err) {
callback(err);
});
});

req.on('error', function(err) {
callback(err);
});

req.end();
}
140 changes: 21 additions & 119 deletions test/test-static.js
@@ -1,59 +1,14 @@
var assert = require('assert')
, http = require('http')
, fs = require('fs')
, connect = require('connect')
, join = require('path').join
, gzippo = require('../');


var fixtures = join(__dirname, 'fixtures')
, gzippo = require('../')
;
var fixtures = join(__dirname, 'fixtures')
, port = 32123
, app;


// basic request mocking function
function request(path, headers, callback) {
var options = {
host: '127.0.0.1',
port: port,
path: path,
headers: headers || {},
method: 'GET'
};

var req = http.request(options, function(res) {
var buffers = []
, total = 0;

res.on('data', function(buf) {
buffers.push(buf);
total += buf.length;
});

res.on('end', function() {
var data = new Buffer(total)
, offset = 0;

for (var i = 0; i < buffers.length; i++) {
buffers[i].copy(data, offset);
offset += buffers[i].length;
}

callback(null, res, data);
});

res.on('error', function() {
callback(err);
});
});

req.on('error', function(err) {
callback(err);
});

req.end();
}

, app
, request = require('./request')({ port: port })
;

// builds a `request` callback which asserts that the response's statusCode is
// what we expect
Expand All @@ -75,26 +30,26 @@ function file(name) {

describe('gzippo.staticGzip', function() {

app = connect.createServer();
app.use(gzippo.staticGzip(fixtures));
app.listen(port);

// set up a new server for each test
beforeEach(function(done) {
app = connect.createServer();
app.use(gzippo.staticGzip(fixtures));
app.listen(port, done);
});
// beforeEach(function(done) {
// });

afterEach(function() {
app.close();
});
// afterEach(function() {
// app.exit();
// });


it('should gzip static .json file', function(done) {
request('/user.json', { 'Accept-Encoding': 'gzip' },
function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'application/json; charset=UTF-8');
assert.equal(res.headers['content-length'], '69');
assert.equal(data.length, '69');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('user.gzip'));
Expand All @@ -112,7 +67,7 @@ describe('gzippo.staticGzip', function() {
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8');
assert.equal(res.headers['content-length'], '35');
assert.equal(data.length, '35');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('test.js.gzip'));
Expand All @@ -127,8 +82,7 @@ describe('gzippo.staticGzip', function() {
request('/test.js', {}, function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-length'], '15');
assert.equal(data.length, '15');
assert.deepEqual(data, file('test.js'));

done();
Expand All @@ -143,7 +97,7 @@ describe('gzippo.staticGzip', function() {
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'text/plain; charset=UTF-8');
assert.equal(res.headers['content-length'], '2031');
assert.equal(data.length, '2031');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('utf8.txt.gz'));
Expand Down Expand Up @@ -217,63 +171,11 @@ describe('gzippo.staticGzip', function() {
function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);
assert.equal(res.headers['content-length'], '366');
assert.equal(res.headers['content-length'], '616');

done();
}
);
});

});


describe('gzippo.statisGzip (with prefix)', function() {

it('should successfully serve a .json file with a path prefix', function(done) {
var app = connect.createServer();
app.use(gzippo.staticGzip(fixtures, { prefix: '/foo' }));

app.listen(port, function() {
request('/foo/user.json', { 'Accept-Encoding': 'gzip' },
function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'application/json; charset=UTF-8');
assert.equal(res.headers['content-length'], '69');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('user.gzip'));

app.close();
done();
}
);
});
});


it('should serve files as expected with a / prefix', function(done) {
var app = connect.createServer();
app.use(gzippo.staticGzip(fixtures, { prefix: '/' }));

app.listen(port, function() {
request('/user.json', { 'Accept-Encoding': 'gzip' },
function(err, res, data) {
if (err) throw err;
assert.equal(res.statusCode, 200);

assert.equal(res.headers['content-type'], 'application/json; charset=UTF-8');
assert.equal(res.headers['content-length'], '69');
assert.equal(res.headers['content-encoding'], 'gzip');

assert.deepEqual(data, file('user.gzip'));

app.close();
done();
}
);
});
});

});
});

0 comments on commit eaf85cb

Please sign in to comment.