Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewrote test suite to use mocha instead of the now deprecated expresso

  • Loading branch information...
commit 6dc3bb87a4c3bb37796f20bf465d755e6a6b7514 1 parent aa0b6cd
@evlun evlun authored
View
4 package.json
@@ -17,14 +17,14 @@
"node": ">= 0.5 < 0.9"
},
"scripts": {
- "test": "expresso"
+ "test": "mocha"
},
"main": "./index.js",
"dependencies": {
"mime": ">= 1.2"
},
"devDependencies": {
- "expresso": ">= 0.9",
+ "mocha": ">= 1.2.0",
"should": ">= 0.3",
"connect": "~1"
},
View
88 test/prefexTest.js
@@ -1,88 +0,0 @@
-
-/**
- * Module dependencies.
- */
-
-var staticProvider,
- assert = require('assert'),
- should = require('should'),
- http = require('http'),
- gzippo = require('../');
-
-try {
- staticProvider = require('connect');
-} catch (e) {
- staticProvider = require('express');
-}
-
-/**
- * Path to ./test/fixtures/
- */
-
-var fixturesPath = __dirname + '/fixtures';
-
-module.exports = {
- 'requesting without a prefix succeeds': function() {
- var app = staticProvider.createServer(
- gzippo.staticGzip(fixturesPath)
- );
-
- assert.response(app,
- {
- url: '/user.json',
- headers: {
- 'Accept-Encoding':"gzip"
- }
- },
- function(res){
- var gzippedData = res.body;
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-type', 'application/json; charset=UTF-8');
- res.headers.should.have.property('content-length', '69');
- res.headers.should.have.property('content-encoding', 'gzip');
- }
- );
- },
- 'requesting with a prefix succeeds': function() {
- var app = staticProvider.createServer(
- gzippo.staticGzip(fixturesPath, { prefix: '/resource' })
- );
-
- assert.response(app,
- {
- url: '/resource/user.json',
- headers: {
- 'Accept-Encoding':"gzip"
- }
- },
- function(res){
- var gzippedData = res.body;
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-type', 'application/json; charset=UTF-8');
- res.headers.should.have.property('content-length', '69');
- res.headers.should.have.property('content-encoding', 'gzip');
- }
- );
- },
- 'requesting with a / prefix succeeds': function() {
- var app = staticProvider.createServer(
- gzippo.staticGzip(fixturesPath, { prefix: '/'})
- );
-
- assert.response(app,
- {
- url: '/user.json',
- headers: {
- 'Accept-Encoding':"gzip"
- }
- },
- function(res){
- var gzippedData = res.body;
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-type', 'application/json; charset=UTF-8');
- res.headers.should.have.property('content-length', '69');
- res.headers.should.have.property('content-encoding', 'gzip');
- }
- );
- }
-};
View
234 test/staticGzipTest.js
@@ -1,234 +0,0 @@
-
-/**
- * Module dependencies.
- */
-
-var staticProvider,
- assert = require('assert'),
- should = require('should'),
- http = require('http'),
- gzippo = require('../'),
- crypto = require('crypto'),
- fs = require('fs'),
- shasum = crypto.createHash('sha1');
-
-try {
- staticProvider = require('connect');
-} catch (e) {
- staticProvider = require('express');
-}
-
-/**
- * Path to ./test/fixtures/
- */
-
-var fixturesPath = __dirname + '/fixtures';
-
-function getApp() {
- return staticProvider.createServer(gzippo.staticGzip(fixturesPath));
-}
-
-module.exports = {
- 'requesting gzipped json file succeeds': function() {
- assert.response(getApp(),
- {
- url: '/user.json',
- headers: {
- 'Accept-Encoding':"gzip"
- }
- },
- function(res){
- var gzippedData = res.body;
- assert.response(getApp(), { url: '/user.gzip' }, function(res) {
- assert.equal(gzippedData, res.body, "Data is not gzipped");
- });
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-type', 'application/json; charset=UTF-8');
- res.headers.should.have.property('content-length', '69');
- res.headers.should.have.property('content-encoding', 'gzip');
- }
- );
- },
- 'requesting gzipped js file succeeds': function() {
- assert.response(getApp(),
- {
- url: '/test.js',
- headers: {
- 'Accept-Encoding':"gzip"
- }
- },
- function(res){
- var gzippedData = res.body;
- assert.response(getApp(), { url: '/test.js.gzip' }, function(res) {
- assert.equal(gzippedData, res.body, "Data is not gzipped");
- });
-
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-type', 'application/javascript; charset=UTF-8');
- res.headers.should.have.property('content-length', '35');
- res.headers.should.have.property('content-encoding', 'gzip');
- }
- );
- },
- 'requesting js file without gzip succeeds': function() {
- assert.response(getApp(),
- {
- url: '/test.js'
- },
- function(res){
- var gzippedData = res.body;
-
- fs.readFile(fixturesPath + '/test.js', function (err, data) {
- if (err) throw err;
- assert.equal(gzippedData, data, "Data returned does not match file data on filesystem");
- });
-
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-length', '15');
- }
- );
- },
- 'requesting gzipped utf-8 file succeeds': function() {
- assert.response(getApp(),
- {
- url: '/utf8.txt',
- headers: {
- 'Accept-Encoding':"gzip"
- }
- },
- function(res){
- var gzippedData = res.body;
- assert.response(getApp(), { url: '/utf8.txt.gz' }, function(res) {
- assert.equal(gzippedData, res.body, "Data is not gzipped");
- });
-
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-type', 'text/plain; charset=UTF-8');
- res.headers.should.have.property('content-length', '2031');
- res.headers.should.have.property('content-encoding', 'gzip');
- }
- );
- },
- 'requesting gzipped utf-8 file returns 304': function() {
- assert.response(getApp(),
- {
- url: '/utf8.txt',
- headers: {
- 'Accept-Encoding': "gzip"
- }
- },
- function(res) {
- res.statusCode.should.equal(200);
- assert.response(getApp(),
- {
- url: '/utf8.txt',
- headers: {
- 'Accept-Encoding': "gzip",
- 'If-Modified-Since': res.headers['last-modified']
- }
- },
- function(res2) {
- res2.statusCode.should.equal(304);
- }
- );
- }
- );
- },
- 'requesting gzipped utf-8 file returns 200': function() {
- assert.response(getApp(),
- {
- url: '/utf8.txt',
- headers: {
- 'Accept-Encoding': "gzip"
- }
- },
- function(res) {
- res.statusCode.should.equal(200);
- }
- );
- },
- 'requesting directory returns index.html if found': function() {
- assert.response(getApp(),
- {
- url: '/index_test/',
- headers: {
- 'Accept-Encoding': "gzip"
- }
- },
- function(res) {
- res.statusCode.should.equal(200);
- res.headers.should.have.property('content-length', '366');
- }
- );
- },
- 'ensuring max age is set on resources which are passed to the default static content provider': function() {
- assert.response(getApp(),
- {
- url: '/tomg.co.png'
- },
- function(res) {
- assert.includes(res.headers['cache-control'], 'max-age=60480');
- }
- );
- },
- 'Normal traversal should work': function() {
- assert.response(getApp(),
- {
- url: '/nom/../tomg.co.png'
- },
- function(res) {
- res.statusCode.should.equal(200);
- }
- );
- },
- 'Ensuring that when viewing a directory a redirect works correctly': function() {
- assert.response(getApp(),
- {
- url: '/js'
- },
- function(res) {
- res.statusCode.should.not.equal(301);
- }
- );
- },
- 'ensuring that gzippo works with a space in a static content path': function() {
- assert.response(getApp(),
- {
- url: '/space%20the%20final%20frontier/tomg.co.png'
- },
- function(res) {
- res.statusCode.should.not.equal(404);
- }
- );
- },
- 'Ensuring req.url isnt passed to staticSend on error': function() {
- assert.response(getApp(),
- {
- url: '/etc/passwd'
- },
- function(res) {
- res.statusCode.should.equal(404);
- }
- );
- },
- 'Ensuring you cannot traverse up the directory tree': function() {
- assert.response(getApp(),
- {
- url: '/../prefexTest.js'
- },
- function(res) {
- res.statusCode.should.equal(403);
- }
- );
- },
- 'Ensuring you cannot traverse up the directory tree (urlencoded)': function() {
- assert.response(getApp(),
- {
- url: '/%2e%2e/prefexTest.js'
- },
- function(res) {
- res.statusCode.should.equal(403);
- }
- );
- }
-};
View
279 test/test-static.js
@@ -0,0 +1,279 @@
+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 = 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();
+}
+
+
+// builds a `request` callback which asserts that the response's statusCode is
+// what we expect
+function statusCode(expected, callback) {
+ return function(err, res, data) {
+ if (err) throw err;
+ assert.equal(res.statusCode, expected);
+
+ callback();
+ };
+}
+
+
+// read a fixture file synchronously
+function file(name) {
+ return fs.readFileSync(join(fixtures, name));
+}
+
+
+describe('gzippo.staticGzip', function() {
+
+ // set up a new server for each test
+ beforeEach(function(done) {
+ app = connect.createServer();
+ app.use(gzippo.staticGzip(fixtures));
+ app.listen(port, done);
+ });
+
+ afterEach(function() {
+ app.close();
+ });
+
+
+ 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(res.headers['content-encoding'], 'gzip');
+
+ assert.deepEqual(data, file('user.gzip'));
+
+ done();
+ }
+ );
+ });
+
+
+ it('should gzip static .js file', function(done) {
+ request('/test.js', { 'Accept-Encoding': 'gzip' },
+ function(err, res, data) {
+ if (err) throw err;
+ 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(res.headers['content-encoding'], 'gzip');
+
+ assert.deepEqual(data, file('test.js.gzip'));
+
+ done();
+ }
+ );
+ });
+
+
+ it('should serve a .js file uncompressed when the accept-encoding header has not been set', function(done) {
+ request('/test.js', {}, function(err, res, data) {
+ if (err) throw err;
+ assert.equal(res.statusCode, 200);
+
+ assert.equal(res.headers['content-length'], '15');
+ assert.deepEqual(data, file('test.js'));
+
+ done();
+ });
+ });
+
+
+ it('should successfully gzip a utf-8 file', function(done) {
+ request('/utf8.txt', { 'Accept-Encoding': 'gzip' },
+ function(err, res, data) {
+ if (err) throw err;
+ 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(res.headers['content-encoding'], 'gzip');
+
+ assert.deepEqual(data, file('utf8.txt.gz'));
+
+ done();
+ }
+ );
+ });
+
+
+ it('should cache a previously gzipped utf-8 file (and respond with a 304 Not Modified)', function(done) {
+ request('/utf8.txt', { 'Accept-Encoding': 'gzip' },
+ function(err, res, data) {
+ if (err) throw err;
+ assert.equal(res.statusCode, 200);
+
+ var headers = {
+ 'Accept-Encoding': 'gzip',
+ 'If-Modified-Since': res.headers['last-modified']
+ };
+
+ request('/utf8.txt', headers, statusCode(304, done));
+ }
+ );
+ });
+
+
+ it('should set max age resources which are passed to the default static content provider', function(done) {
+ request('/tomg.co.png', {},
+ function(err, res, data) {
+ if (err) throw err;
+ assert.equal(res.statusCode, 200);
+ assert.notEqual(res.headers['cache-control'].indexOf('max-age=604800'), -1);
+
+ done();
+ }
+ );
+ });
+
+
+ it('should allow normal traversal', function(done) {
+ request('/nom/../tomg.co.png', {},
+ function(err, res, data) {
+ if (err) throw err;
+ assert.equal(res.statusCode, 200);
+ assert.deepEqual(data, file('tomg.co.png'));
+
+ done();
+ }
+ );
+ });
+
+
+ it('should work for paths containing URI-encoded spaces', function(done) {
+ request('/space%20the%20final%20frontier/tomg.co.png', {}, statusCode(200, done));
+ });
+
+
+ it('should not let the user access files outside of the static directory (urlencoded)', function(done) {
+ request('/../test-static.js', {}, statusCode(403, done));
+ });
+
+
+ it('should not let the user access files outside of the static directory', function(done) {
+ request('/%2e%2e/test-static.js', {}, statusCode(403, done));
+ });
+
+
+ it('should serve index.html for directories (if found)', function(done) {
+ request('/index_test/', { 'Accept-Encoding': 'gzip' },
+ function(err, res, data) {
+ if (err) throw err;
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-length'], '366');
+
+ 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 6dc3bb8

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