fixed-server
fixed-server copied to clipboard
Add initialize function for server setup
I often find myself wanting to interact with the express server and set it up a certain way before making requests to it. For example, if you're testing a global middleware and want to use some jade templating:
var fixedServer = FixedServer.fromFile('./fixtures/fixed-server', {
port: 1337,
initialize: function (app) {
app.engine('jade', jade.__express);
app.set('view engine', 'jade');
app.use(myGlobalMiddleware());
}
});
And if you'd like to set your server up on a per-test basis, you could do something like (with the addition of #5):
fixedServer.createServer(['GET 200 /']).setup(mySetupFunc).run();
This might not be the best approach, but something along these lines would be very useful!
Can you provide more examples of when this comes into play?
This is starting to highlight lines where we could have abstracted things better (e.g. make fixed-server a router middleware factory and make the server factory its own library).
For external reference, I belive the current workaround is:
response: [
myGlobalMiddleware(),
function (req, res) {}
]
which runs the response handlers in order via a middleware chain (thanks express!).
You're right, that is the current workaround. For some reason I thought you couldn't run server setup methods, like app.set() within a route middleware chain, but it seems you can. This PR seems unnecessary, knowing that.
I could still see this being practical if someone doesn't want express.urlencoded() for any POST-based mocks. Reopening the issue to prevent burial.