mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
293 stars 17 forks source link

Middleware for any http method as per documentation #140

Closed phil-armstrong-itv closed 3 years ago

phil-armstrong-itv commented 3 years ago

Describe the bug When trying the middleware example presented in the docs there is an error when starting up the server:

TypeError: this._router[METHODS[routeVariant.method]] is not a function at _routesVariants.forEach (.../node_modules/@mocks-server/core/src/mocks/Mock.js:34:49) at Array.forEach (<anonymous>) at Mock._initRouter (.../node_modules/@mocks-server/core/src/mocks/Mock.js:33:26) at new Mock (.../node_modules/@mocks-server/core/src/mocks/Mock.js:28:10) at _mocks._mocksDefinitions.map (.../node_modules/@mocks-server/core/src/mocks/Mocks.js:83:18) at Array.map (<anonymous>) at Mocks._processMocks (.../node_modules/@mocks-server/core/src/mocks/Mocks.js:81:8) at Mocks.load (.../node_modules/@mocks-server/core/src/mocks/Mocks.js:147:10) at Orchestrator._onLoadMocks (.../node_modules/@mocks-server/core/src/Orchestrator.js:54:19) at EventEmitter.emit (events.js:198:13)

To Reproduce Use the content of the middlewares.js file in the documentation and add either of the routes to the 'base' mock setup. When you start the server the above error will occur. If you add method: "GET" to the route, it works, however it looks like a wildcard doesn't work for that field (*)

Expected behavior The server loads and the middleware route fires on all requests

Operating system, Node.js an npm versions, or browser version (please complete the following information):

phil-armstrong-itv commented 3 years ago

To clarify, this mock definition won't work:

module.exports = [ { id: "add-headers", url: "/*", variants: [ { id: "enabled", response: { status: 200, body: [] } } ] } ];

This does work but wouldn't work across all methods:

module.exports = [ { id: "add-headers", url: "/*", method: "GET", variants: [ { id: "enabled", response: { status: 200, body: [] } } ] } ];

javierbrea commented 3 years ago

Hi @phil-armstrong-itv,

Thank you, the example is wrong. As you have noticed, the route variants has to have defined a method property to work, but for the moment it does not support wildcards, so you have to define a different route variant for each method.

Supporting wildcards, or at least an array of methods in the method property is a very good idea, I will create an issue for implementing it.

For the moment, as a workaround you could define a base route variant in a variable and use the spread operator to reuse it in many routes, as in:

const baseMiddleware =  {
    url: "*",
    variants: [
      {
        id: "enabled",
        response: (req, res, next) => {
          res.set('Content-Type', 'application/json; charset=utf-8');
          next();
        }
      },
      {
        id: "disabled",
        response: (req, res, next) => next()
      }
    ]
 } ;

module.exports = [
  {
    ...baseMiddleware,
    method: "GET",
    id: "add-headers-get",
  } ,
  {
    ...baseMiddleware,
    method: "POST",
    id: "add-headers-post",
  },
  {
    ...baseMiddleware,
    method: "DELETE",
    id: "add-headers-delete",
  }  
];

I know that this is an ugly workaround, so I will try to release a new version supporting arrays in the method property as soon as possible.

javierbrea commented 3 years ago

Hi again @phil-armstrong-itv , the version v2.1.0, which adds support for multiple methods in routes is already released and the docs are also updated: https://www.mocks-server.org/docs/guides-using-middlewares#adding-common-headers-to-all-requests

I hope it might help you.

Thanks again for your feedback! 🙂

phil-armstrong-itv commented 3 years ago

Hey awesome news thank you! Did a little demo of the library to my team this week, I really like it