Skip to content

Commit

Permalink
Tests added
Browse files Browse the repository at this point in the history
Test infrasctructure copied from https://github.com/senchalabs/connect
  • Loading branch information
pirxpilot committed Sep 17, 2013
1 parent 44e7254 commit b9e9621
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 5 deletions.
8 changes: 5 additions & 3 deletions .jshintrc
@@ -1,7 +1,9 @@
{ {
"undef": "true", "undef": true,
"unused": "true", "unused": true,
"laxbreak": "true", "laxbreak": true,
"laxcomma": true,
"proto": true,
"globals": { "globals": {
"console": false, "console": false,
"exports": false, "exports": false,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -27,7 +27,8 @@
"connect": ">=2" "connect": ">=2"
}, },
"devDependencies": { "devDependencies": {
"jshint": "~2.1.10", "jshint": "~2",
"mocha": "~1.13.0" "mocha": "~1",
"should": "~1"
} }
} }
1 change: 1 addition & 0 deletions test/fixtures/print.css
@@ -0,0 +1 @@
body{color:"green";}
1 change: 1 addition & 0 deletions test/fixtures/print.css.gz
@@ -0,0 +1 @@
compressed
1 change: 1 addition & 0 deletions test/fixtures/style.css
@@ -0,0 +1 @@
body{color:"red";}
77 changes: 77 additions & 0 deletions test/gzip-static.js
@@ -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);
});

});
2 changes: 2 additions & 0 deletions test/mocha.opts
@@ -0,0 +1,2 @@
--require should
--require test/support/http
116 changes: 116 additions & 0 deletions test/support/http.js
@@ -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.