fixed-server icon indicating copy to clipboard operation
fixed-server copied to clipboard

Add initialize function for server setup

Open mlmorg opened this issue 11 years ago • 4 comments

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!

mlmorg avatar Mar 26 '14 07:03 mlmorg

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).

twolfson avatar Mar 26 '14 08:03 twolfson

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!).

twolfson avatar Mar 26 '14 08:03 twolfson

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.

mlmorg avatar Mar 26 '14 15:03 mlmorg

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.

twolfson avatar Mar 26 '14 19:03 twolfson