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

Additional documentation for func Methods #699

Closed
wants to merge 1 commit into from

Conversation

henrikac
Copy link

@henrikac henrikac commented Oct 9, 2022

Reading through #694 it sounds like that the behaviour of chaining multiple func Methods(methods ...string)is not clear. This PR is not a fix to this issue but it attempts to help make this behaviour more clear to other/new users.

Summary of Changes

  1. A small note to func Methods(methods ...string) that explains the behavior of chaining multiple Methods.
  2. Updated the methods section in the README

Copy link
Contributor

@amustaque97 amustaque97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @henrikac I left a few comments. Would you mind taking look at them? Looking forward to hearing from you.

@@ -322,6 +322,10 @@ func (m methodMatcher) Match(r *http.Request, match *RouteMatch) bool {
// Methods adds a matcher for HTTP methods.
// It accepts a sequence of one or more methods to be matched, e.g.:
// "GET", "POST", "PUT".
//
// NOTE: Chaining multiple Methods will result in the last added
// Methods will override the allowed methods specified by the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is not the actual behaviour, have you tried this? when I write something like the below:

r := mux.NewRouter()
r.HandleFunc("/health", healthHandle).Methods("POST").Methods("GET")

When I curl /health endpoint, it returns HTTP ERROR 405

Copy link
Author

@henrikac henrikac Nov 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amustaque97 this is weird but I am getting 405 for both the POST and GET request 🤔 but

// ...
r.HandleFunc("/health", healthHandler).Methods("GET", "POST").Methods("GET")
// ...

will return GET 200 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henrikac this is expected behaviour. Consider it as series of filter. When you filter the requests based on POST and then try to do it with GET, the result will be 0 and hence 405 error

Copy link
Author

@henrikac henrikac Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henrikac this is expected behaviour. Consider it as series of filter. When you filter the requests based on POST and then try to do it with GET, the result will be 0 and hence 405 error

Yeah, I figured. In my comment below for the README I mentioned that I think it looks like it behaves like it performs an intersection operation :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henrikac so is this PR required? Could you modify or decline PR based on changes/feedback

@@ -107,6 +107,8 @@ r.PathPrefix("/products/")

```go
r.Methods("GET", "POST")
// This is equivalent to .Methods("GET")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks incorrect to me :(

Because when we write multiple Methods calls like: Methods("GET", "POST").Methods("PATCH")

Apparently, In one of the iterations here matchErr is set ErrMethodMismatch and it will return false. I hope you understand what I'm trying to say here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, .Methods(methods ...string) seems to act like a set and by chaining them makes them do some kind of intersection operation.

.Methods("GET", "POST").Methods("PATCH") // will return 405 for all requests

.Methods("GET", "POST").Methods("GET", "PATCH") // will return 405 for all requests but GET requests

@henrikac henrikac closed this Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants