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

added case insensitive matcher for request methods #763

Closed

Conversation

cornejong
Copy link

@cornejong cornejong commented Jun 20, 2024

Ignore this PR.. is replaced by #764

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Go Version Update
  • Dependency Update

Description

This PR adds the ability to define case insensitive method matching for routes. as suggested in issue #762.
To achieve this this pr adds the following:

type methodCaseInsensitiveMatcher []string

func (r *Route) MethodsCaseInsensitive(methods ...string) *Route
func (r *Route) MethodsCaseSensitive(methods ...string) *Route
func (r *Route) MatchMethodCaseInsensitive(value bool) *Route

func (r *Router) MatchMethodCaseInsensitive(value bool) *Router

type routeConf struct {
    ...
    // If true, methods will be matched case insensitive.
    // The methodCaseInsensitiveMatcher will be used instead of methodMatcher
    matchMethodCaseInsensitive bool
}

// as well a the helper
func sliceToUpper(slice []string) []string 

Usage

Setting matchMethodCaseInsensitive to true all routes defined after will end up getting the methodCaseInsensitiveMatcher instead of the default methodMatcher.

Enabling insesitivity for a single route can be done in one of the following ways:

r.HandleFunc("/", func).MatchMethodCaseInsensitive(true).Methods("GET")
// Not the prefferd method, order of opperations is critcal here. 
// Since the Methods(...) func adds the matcher. 
// Invoking the MatchMethodCaseInsensitive(true) method after it will have no effect.

r.HandleFunc("/", func).MethodsCaseInsensitive("GET")
// Explicitly match the methods for this route without case sensitivity

If methodCaseInsensitiveMatcher on the router is set to true, disabling it for a specific route can be done like:

r.HandleFunc("/", func).MethodsCaseSensitive("GET")
// Explicitly match the methods for this route with case sensitivity

In all other cases the Methods(...) func will add the appropriate matcher based on the value of the routeConf option methodCaseInsensitiveMatcher

Related Tickets & Documents

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

Run verifications and test

  • make verify is passing
  • make test is passing

Signed-off-by: Corné de Jong <me@corne.co>
@cornejong cornejong marked this pull request as draft June 20, 2024 12:05
@cornejong cornejong closed this Jun 20, 2024
@cornejong cornejong deleted the feature_ignore_method_casing branch June 21, 2024 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Ignore case sensitivity when matching defined methods
1 participant