Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending middleware functionality, and documenting in config_file #1053

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions docs/config_file.md
Expand Up @@ -77,6 +77,7 @@ Common Configuration Options
launchers: [Object] a specification for all custom launchers
launch_in_dev: [Array] list of launchers to use for dev runs
launch_in_ci: [Array] list of launchers to use for CI runs
middleware: [Array] list of functions to use as express middleware (each receives app and server as arguments)
phantomjs_debug_port: [Number] port used to attach phantomjs debugger
phantomjs_args: [Array] custom arguments for the phantomjs launcher
phantomjs_launch_script: [String] path of custom phantomjs launch script
Expand Down
6 changes: 3 additions & 3 deletions lib/server/index.js
Expand Up @@ -103,7 +103,7 @@ Server.prototype = {

this.configureExpress(app);

this.injectMiddleware(app);
this.injectMiddleware(app, this.server);

this.configureProxy(app);

Expand Down Expand Up @@ -152,11 +152,11 @@ Server.prototype = {
});
app.use(express.static(__dirname + '/../../public'));
},
injectMiddleware: function(app) {
injectMiddleware: function(app, server) {
var middlewares = this.config.get('middleware');
if (middlewares) {
middlewares.forEach(function(middleware) {
middleware(app);
middleware(app, server);
});
}
},
Expand Down
28 changes: 27 additions & 1 deletion tests/server_tests.js
Expand Up @@ -9,6 +9,8 @@ var fs = require('fs');
var expect = require('chai').expect;
var http = require('http');
var https = require('https');
var BaseServer = require('net').Server;
var express = require('express');

describe('Server', function() {
this.timeout(10000);
Expand Down Expand Up @@ -81,7 +83,9 @@ describe('Server', function() {
it('gets scripts for the home page', function(done) {
request(baseUrl, function(err, req, text) {
var $ = cheerio.load(text);
var srcs = $('script').map(function() { return $(this).attr('src'); }).get();
var srcs = $('script').map(function() {
return $(this).attr('src');
}).get();
expect(srcs).to.deep.equal([
'//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js',
'/testem.js',
Expand Down Expand Up @@ -400,6 +404,28 @@ describe('Server', function() {
expect(config.get('port')).to.eq(server.server.address().port);
});
});

describe('injectMiddleware', function() {
it('passes app and server', function(done) {
config = new Config('dev', {
port: 0,
cwd: 'tests',
middleware: [
function(app, server) {
expect(app).to.be.instanceof(express);
expect(server).to.be.instanceof(BaseServer);
done();
}
]
});
server = new Server(config);
server.start();
server.stop(function() {
done();
});
});
});

});

function assertUrlReturnsFileContents(url, file, done) {
Expand Down