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

Middleware for any http method as per documentation #140

Closed
phil-armstrong-itv opened this issue Mar 17, 2021 · 4 comments · Fixed by #142
Closed

Middleware for any http method as per documentation #140

phil-armstrong-itv opened this issue Mar 17, 2021 · 4 comments · Fixed by #142
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation
Milestone

Comments

@phil-armstrong-itv
Copy link

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):**

  • OS: macOS Catalina 10.15.7
  • Node.js: v10.21.0
  • npm: 6.14.4
@phil-armstrong-itv
Copy link
Author

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
Copy link
Member

javierbrea commented Mar 17, 2021

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 javierbrea added bug Something isn't working documentation Improvements or additions to documentation labels Mar 17, 2021
@javierbrea javierbrea added this to the v2.1.0 milestone Mar 17, 2021
@javierbrea javierbrea self-assigned this Mar 17, 2021
@javierbrea
Copy link
Member

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
Copy link
Author

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

javierbrea added a commit that referenced this issue Mar 23, 2022
…tier-7.x

chore(deps): update dependency eslint-config-prettier to v7
javierbrea pushed a commit that referenced this issue Mar 24, 2022
chore(deps): update dependency fs-extra to v10.0.1
javierbrea pushed a commit that referenced this issue Mar 24, 2022
…-resolve-13.x

chore(deps): update dependency @rollup/plugin-node-resolve to v13.0.4
javierbrea pushed a commit that referenced this issue Mar 25, 2022
chore(deps): update dependency rollup to v2.52.3
javierbrea pushed a commit that referenced this issue Mar 25, 2022
chore(deps): update dependency rollup to v2.52.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants