Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Support method OPTIONS for routes #140

Closed
kariem opened this issue Mar 21, 2021 · 3 comments · Fixed by #147
Closed

Support method OPTIONS for routes #140

kariem opened this issue Mar 21, 2021 · 3 comments · Fixed by #147
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@kariem
Copy link

kariem commented Mar 21, 2021

The API I would like to mock, uses OPTIONS requests. When I try to use this in a route, I get an error

{
  "id": "options-a",
  "url": "/api/v1/a",
  "method": "OPTIONS",
  "variants": [
    {
      "id": "default",
      "response": {"status": 200, "body": true}
    }
  ]
}

The error I see in the sever log

this._router[METHODS[routeVariant.method]] is not a function

However, when I set the method to GET or POST everything seems to work fine.

I have also noticed that the server tries to respond to CORS preflight requests and this seems to be the reason for this problem. When I try to extend the constant METHODS in Mock.js with a line for OPTIONS, there is no error, but the response from the server contains

access-control-allow-origin: *
access-control-allow-methods: GET,HEAD,PUT,PATCH,POST,DELETE

Although, I never included these headers in the request

If there is no easy way to implement this, is there a possible workaround?

@javierbrea
Copy link
Member

Hi @kariem,

The server automatically handle the OPTIONS method because it is usually used for front-end development, and these requests are automatically done by the browsers, so I thought it would be useful to not have to add those routes manually always. But you're right, in some scenarios it may be desirable to disable this behaviour and handle them manually.

Another problem is that there is no mention in the documentation about it, so I will try to publish a new version as soon as possible including an option to disable the built-in CORS middleware, and mention it in the docs.

For the moment, as a workaround, if you are modifying the code by yourself, you could also disable the CORS middleware removing the line this._express.use(middlewares.enableCors); in the src/server/Server.js file. But I hope to have a new release with this fix this week.

Thanks for your feedback! 🙂

@javierbrea
Copy link
Member

Hi again @kariem!

The version 2.1.0 is already released, and the docs were also updated. Now it supports OPTIONS, HEAD and TRACE methods, and it has two new options allowing to disable both CORS headers and CORS pre-flight responses separately. You can read the "how to use the OPTIONS method" section in the documentation for further info.

I hope it might help,
Thanks again for your feedback! 🙂

@kariem
Copy link
Author

kariem commented Mar 24, 2021

Hi @javierbrea,

Thank you very much for the quick update!

Just tested 2.1.0 with the adapted configuration and it works well.

Thank you!

@javierbrea javierbrea moved this to Done in Backlog May 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants