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

Fixes path order dependent response when a nil handler is defined for a route #517

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rkilingr
Copy link

@rkilingr rkilingr commented Sep 2, 2019

Fixes #515

Summary of Changes

  1. Checks route handler != nil condition only for setting the matched handler, not for clearing Method mismatch error
  2. Adds test-cases to check order dependent response if request handler for the path is nil

mux_test.go Outdated
t.Fatalf("Expected status code 200 (got %d)", w.Code)
}

reversedPathRouter := NewRouter()
Copy link
Contributor

Choose a reason for hiding this comment

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

Break these into subtests - e.g.

t.Run("Test handler ordering", func(t *testing.T) { 
   // test 
})

t.Run("Test reversed handler ordering", func(t *testing.T) { 
   // test 
})

I would also add additional test cases:

  1. Test ordering where handlers have the same method & path, but a different handler (expected: the first handler added is the one executed)
  2. Same path, different methods (no path variables)
  3. Same as Host() with a port fails to match when the host is supplied in the HTTP Host: header #2, but with middleware

Copy link
Author

Choose a reason for hiding this comment

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

Have made the listed changes

mux_test.go Outdated
reversedPathRouter := NewRouter()
reversedPathRouter.Path("/a/{a}").Handler(http.HandlerFunc(handler)).Methods(http.MethodGet)
reversedPathRouter.Path("/a/b").Handler(nil).Methods(http.MethodOptions)
reversedPathRouter.Use(MiddlewareFunc(testOptionsMiddleWare))
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be before the paths are added.

mux_test.go Outdated
reversedPathRouter.Path("/a/b").Handler(nil).Methods(http.MethodOptions)
reversedPathRouter.Use(MiddlewareFunc(testOptionsMiddleWare))

w = NewRecorder()
Copy link
Contributor

Choose a reason for hiding this comment

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

Make it a subtest (as above) and call newRequest again to separate it.

route.go Show resolved Hide resolved
@elithrar
Copy link
Contributor

@rkilingr - did you still want to complete this?

@gorilla gorilla deleted a comment from pierreneter Oct 13, 2019
@elithrar
Copy link
Contributor

@rkilingr - just following up again if you're still keen on completing this?

@rkilingr
Copy link
Author

@rkilingr - just following up again if you're still keen on completing this?

Apologies for the huge delay. Was not active last year, will be updating the PR

route.go Show resolved Hide resolved
@rkilingr rkilingr closed this Feb 16, 2021
@rkilingr rkilingr reopened this Feb 16, 2021
@amustaque97
Copy link
Contributor

Hi @rkilingr - quick follow-up - are you interested in getting this merged? Will do a review again if existing comments are addressed.

@codecov
Copy link

codecov bot commented Aug 17, 2023

Codecov Report

Merging #517 (9576551) into main (85123bf) will increase coverage by 0.17%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##             main     #517      +/-   ##
==========================================
+ Coverage   78.01%   78.19%   +0.17%     
==========================================
  Files           5        5              
  Lines         887      885       -2     
==========================================
  Hits          692      692              
+ Misses        140      138       -2     
  Partials       55       55              
Files Changed Coverage Δ
route.go 68.85% <0.00%> (+0.39%) ⬆️

@coreydaley coreydaley enabled auto-merge (squash) August 18, 2023 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

[bug] Giving methodNotAllowed even when route is defined
5 participants