Skip to content

Commit

Permalink
Prevent browser caching
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejewski committed May 9, 2017
1 parent 248f6ef commit 081948c
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 126 deletions.
93 changes: 50 additions & 43 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
var feathers = require('feathers');
var rest = require('feathers-rest');
var bodyParser = require('body-parser');
var nocache = require('connect-nocache');
var socketio = require('feathers-socketio');
var memory = require('feathers-memory');
var debug = require('debug')('testee:server');

function nocache(req, res, next) {
res.setHeader('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
res.setHeader('Pragma', 'No-cache');
delete req.headers['if-modified-since'];
delete req.headers['if-none-match'];
next();
}

var hosting = require('./host');
var api = exports.api = function() {
debug('initializing Feathers API');
return feathers()
.use(bodyParser.json())
.use(bodyParser.urlencoded({extended: true}))
.use(nocache())
.configure(rest())
.configure(socketio())
.use('/runs', memory())
.use('/suites', memory())
.use('/tests', memory())
.use('/coverages', memory())
.use('/logs', memory());
var api = exports.api = function () {
debug('initializing Feathers API');
return feathers()
.use(nocache)
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(rest())
.configure(socketio())
.use('/runs', memory())
.use('/suites', memory())
.use('/tests', memory())
.use('/coverages', memory())
.use('/logs', memory());
};

// Returns an Express application that hosts a Testee server with the given options:
// `root`: The root URL or path. URLs will be proxied
// `coverage`: If code coverage should be enabled and its settings
exports.create = function(options) {
// The Feathers API server
var app = api(options);
// The file hosting or proxy server
// Uses the API server as a middleware on the `api/` route
var host = hosting(options).use('/api', app);
var oldListen = host.listen;
var connectionId = 0;
var connections = {};
exports.create = function (options) {
// The Feathers API server
var app = api(options);
// The file hosting or proxy server
// Uses the API server as a middleware on the `api/` route
var host = hosting(options).use('/api', app);
var oldListen = host.listen;
var connectionId = 0;
var connections = {};

host.listen = function() {
var server = oldListen.apply(this, arguments);
server.api = app;
//Track all connections, remove them when the are closed.
server.on('connection', function(conn){
var key = connectionId++;
connections[key] = conn;
conn.on('close', function(){
delete connections[key];
host.listen = function () {
var server = oldListen.apply(this, arguments);
server.api = app;
//Track all connections, remove them when the are closed.
server.on('connection', function (conn) {
var key = connectionId++;
connections[key] = conn;
conn.on('close', function () {
delete connections[key];
});
});
});
//Any open connection will be destroyed
server.destroy = function(cb){
server.close(cb || function(){});
for(var key in connections) {
connections[key].destroy();
delete connections[key];
}
//Any open connection will be destroyed
server.destroy = function (cb) {
server.close(cb || function () { });
for (var key in connections) {
connections[key].destroy();
delete connections[key];
}
};
return server;
};
return server;
};

return host;
return host;
};
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"body-parser": "^1.15.2",
"commander": "^2.3.0",
"connect-injector": "^0.4.0",
"connect-nocache": "^1.1.0",
"debug": "^2.0.0",
"feathers": "^2.0.1",
"feathers-memory": "^1.0.1",
Expand Down Expand Up @@ -65,12 +64,11 @@
"scripts": {
"jshint": "jshint lib/*.js test/*.js --config",
"mocha": "mocha test/ --recursive --timeout 30000",
"mocha:coverage": "mocha test-coverage/coverage.test.js --timeout 30000",
"publish": "git push origin && git push origin --tags",
"release:major": "npm version major && npm publish",
"release:minor": "npm version minor && npm publish",
"release:patch": "npm version patch && npm publish",
"release:pre": "npm version prerelease && npm publish",
"test": "npm run jshint && npm run mocha && npm run mocha:coverage"
"test": "npm run jshint && npm run mocha"
}
}
}
79 changes: 0 additions & 79 deletions test-coverage/coverage.test.js

This file was deleted.

94 changes: 94 additions & 0 deletions test/coverage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
var assert = require('assert');
var path = require('path');
var del = require('del');
var fs = require('fs');
var testee = require('../lib/testee');

var fixture = path.join(__dirname, 'coverage');

describe('Coverage', function () {
var config = {
root: path.join(__dirname, '..'),
reporter: function () {
// print nothing
},
coverage: {
dir: 'test/coverage/',
reporters: ['html'],
ignore: ['node_modules']
}
};
var browser = process.env.TEST_BROWSER || 'firefox';
var browsers = [browser];

beforeEach(function () {
return del([fixture]).then(function () {
return new global.Promise(function (resolve, reject) {
fs.mkdir(fixture, function (error) {
if (error) {
return reject(error);
}
resolve();
});
});
});
});

after(function () {
return del([fixture]);
});

describe('QUnit example', function () {
it('socketio', function () {
return testee.test(['examples/qunit/index.html'], browsers, config).fail(function (error) {
assert.equal(error.message, 'There were 0 general errors and 1 total test failures.');
assert.ok(fs.existsSync('test/coverage/qunit/blogpost.js.html'), 'reports coverage of blogpost.js');
assert.ok(fs.existsSync('test/coverage/qunit/test.js.html'), 'reports coverage of qunit/test.js');
});
});

it('rest', function () {
return testee.test(['examples/qunit/rest.html'], browsers, config).fail(function (error) {
assert.equal(error.message, 'There were 0 general errors and 1 total test failures.');
assert.ok(fs.existsSync('test/coverage/qunit/blogpost.js.html'), 'reports coverage of blogpost.js');
assert.ok(fs.existsSync('test/coverage/qunit/test.js.html'), 'reports coverage of qunit/test.js');
});
});
});

describe('Jasmine example', function () {
it('socketio', function () {
return testee.test(['examples/jasmine/index.html'], browsers, config).fail(function (error) {
assert.equal(error.message, 'There were 0 general errors and 1 total test failures.');
assert.ok(fs.existsSync('test/coverage/jasmine/blogpost.js.html'), 'reports coverage of blogpost.js');
assert.ok(fs.existsSync('test/coverage/jasmine/test.js.html'), 'reports coverage of jasmine/test.js');
});
});

it('rest', function () {
return testee.test(['examples/jasmine/rest.html'], browsers, config).fail(function (error) {
assert.equal(error.message, 'There were 0 general errors and 1 total test failures.');
assert.ok(fs.existsSync('test/coverage/jasmine/blogpost.js.html'), 'reports coverage of blogpost.js');
assert.ok(fs.existsSync('test/coverage/jasmine/test.js.html'), 'reports coverage of jasmine/test.js');
});
});
});

describe('Mocha example', function () {
it('socketio', function () {
return testee.test(['examples/mocha/index.html'], browsers, config).fail(function (error) {
assert.equal(error.message, 'There were 0 general errors and 1 total test failures.');
assert.ok(fs.existsSync('test/coverage/mocha/blogpost.js.html'), 'reports coverage of blogpost.js');
assert.ok(fs.existsSync('test/coverage/mocha/test.js.html'), 'reports coverage of mocha/test.js');
});
});

it('rest', function () {
return testee.test(['examples/mocha/rest.html'], browsers, config).fail(function (error) {
assert.equal(error.message, 'There were 0 general errors and 1 total test failures.');
assert.ok(fs.existsSync('test/coverage/mocha/blogpost.js.html'), 'reports coverage of blogpost.js');
assert.ok(fs.existsSync('test/coverage/mocha/test.js.html'), 'reports coverage of mocha/test.js');
});
});
});
});

0 comments on commit 081948c

Please sign in to comment.