Permalink
Browse files

Tests added

Test infrasctructure copied from https://github.com/senchalabs/connect
  • Loading branch information...
pirxpilot committed Sep 17, 2013
1 parent 44e7254 commit b9e96215198d50cc7ace8b2390e6a2988eb9b10d
Showing with 206 additions and 5 deletions.
  1. +5 −3 .jshintrc
  2. +3 −2 package.json
  3. +1 −0 test/fixtures/print.css
  4. +1 −0 test/fixtures/print.css.gz
  5. +1 −0 test/fixtures/style.css
  6. +77 −0 test/gzip-static.js
  7. +2 −0 test/mocha.opts
  8. +116 −0 test/support/http.js
View
@@ -1,7 +1,9 @@
{
"undef": "true",
"unused": "true",
"laxbreak": "true",
"undef": true,
"unused": true,
"laxbreak": true,
"laxcomma": true,
"proto": true,
"globals": {
"console": false,
"exports": false,
View
@@ -27,7 +27,8 @@
"connect": ">=2"
},
"devDependencies": {
"jshint": "~2.1.10",
"mocha": "~1.13.0"
"jshint": "~2",
"mocha": "~1",
"should": "~1"
}
}
View
@@ -0,0 +1 @@
body{color:"green";}
@@ -0,0 +1 @@
compressed
View
@@ -0,0 +1 @@
body{color:"red";}
View
@@ -0,0 +1,77 @@
var gzipStatic = require('..');
var connect = require('connect');
var fixtures = __dirname + '/fixtures';
var app = connect();
/* global describe, it */
app.use(gzipStatic(fixtures));
app.use(function(req, res){
res.statusCode = 404;
res.end('sorry!');
});
describe('gzipStatic', function(){
it('should serve static files', function(done){
app.request()
.get('/style.css')
.expect('body{color:"red";}', done);
});
it('should set Content-Type', function(done){
app.request()
.get('/style.css')
.expect('Content-Type', 'text/css; charset=UTF-8', done);
});
it('should default max-age=0', function(done){
app.request()
.get('/style.css')
.expect('Cache-Control', 'public, max-age=0', done);
});
it('should serve uncompressed files unless requested', function(done){
app.request()
.get('/print.css')
.expect('body{color:"green";}', done);
});
it('should serve compressed files when requested', function(done){
app.request()
.set('Accept-Encoding', 'gzip')
.get('/print.css')
.expect('compressed', done);
});
it('should set Content-Type for compressed files', function(done){
app.request()
.set('Accept-Encoding', 'gzip')
.get('/print.css')
.expect('Content-Type', 'text/css; charset=UTF-8', done);
});
it('should set Vary for compressed files', function(done){
app.request()
.set('Accept-Encoding', 'gzip')
.get('/print.css')
.expect('Vary', 'Accept-Encoding', done);
});
it('should set Content-Encoding for compressed files', function(done){
app.request()
.set('Accept-Encoding', 'gzip')
.get('/print.css')
.expect('Content-Encoding', 'gzip', done);
});
it('should ignore POST requests', function(done){
app.request()
.set('Accept-Encoding', 'gzip')
.post('/print.css')
.expect(404, done);
});
});
View
@@ -0,0 +1,2 @@
--require should
--require test/support/http
View
@@ -0,0 +1,116 @@
/**
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter
, methods = ['get', 'post', 'put', 'delete', 'head']
, connect = require('connect')
, http = require('http');
module.exports = request;
connect.proto.request = function(){
return request(this);
};
function request(app) {
return new Request(app);
}
function Request(app) {
var self = this;
this.data = [];
this.header = {};
this.app = app;
if (!this.server) {
this.server = http.Server(app);
this.server.listen(0, function(){
self.addr = self.server.address();
self.listening = true;
});
}
}
/**
* Inherit from `EventEmitter.prototype`.
*/
Request.prototype.__proto__ = EventEmitter.prototype;
methods.forEach(function(method){
Request.prototype[method] = function(path){
return this.request(method, path);
};
});
Request.prototype.set = function(field, val){
this.header[field] = val;
return this;
};
Request.prototype.write = function(data){
this.data.push(data);
return this;
};
Request.prototype.request = function(method, path){
this.method = method;
this.path = path;
return this;
};
Request.prototype.expect = function(body, fn){
var args = arguments;
this.end(function(res){
switch (args.length) {
case 3:
res.headers.should.have.property(body.toLowerCase(), args[1]);
args[2]();
break;
default:
if ('number' == typeof body) {
res.statusCode.should.equal(body);
} else {
res.body.should.equal(body);
}
fn();
}
});
};
Request.prototype.end = function(fn){
var self = this;
if (this.listening) {
var req = http.request({
method: this.method
, port: this.addr.port
, host: this.addr.address
, path: this.path
, headers: this.header
});
this.data.forEach(function(chunk){
req.write(chunk);
});
req.on('response', function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk; });
res.on('end', function(){
res.body = buf;
fn(res);
});
});
req.end();
} else {
this.server.on('listening', function(){
self.end(fn);
});
}
return this;
};

0 comments on commit b9e9621

Please sign in to comment.