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

Search action using method argument #3

Merged
merged 6 commits into from Mar 8, 2023
Merged

Conversation

genya0407
Copy link
Contributor

@genya0407 genya0407 commented Dec 31, 2021

If we define two actions with the same path but a different method, requests pointing to the path result in unintuitive behavior:

  • What we expect: action with the same path and the same method is executed
  • What happens: action with the same path but different method is executed
    • Sometimes, the desired action might be executed

This is because, in dispatching requests, Shelf's current implementation does not pass method to R3::Tree#match.

In order to fix this issue, I modified Dispatcher to use method in addition to path to match @tree.

@@ -43,8 +43,9 @@ def initialize(map = {})
def call(env)
path = env[PATH_INFO]
method = R3.method_code(env[REQUEST_METHOD])
raise ArgumentError, "Invalid HTTP method: #{env[REQUEST_METHOD].inspect}." unless method
Copy link
Contributor Author

@genya0407 genya0407 Jan 2, 2022

Choose a reason for hiding this comment

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

Without this, if we get an invalid HTTP request method, Shelf raises confusing an error like TypeError: can't convert nil to Integer.

Copy link
Owner

Choose a reason for hiding this comment

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

that should also result in an HTTP 405 response instead of an server exception

test/builder.rb Outdated
@@ -139,7 +139,17 @@ def self.env_for(uri = '', opts = {})
assert_equal 200, app.call(env_for('/any', method: 'PUT'))[0]
assert_equal 200, app.call(env_for('/any', method: 'GET'))[0]
assert_equal 200, app.call(env_for('/put', method: 'PUT'))[0]
assert_equal 405, app.call(env_for('/put', method: 'GET'))[0]
assert_equal 404, app.call(env_for('/put', method: 'GET'))[0]
Copy link
Owner

Choose a reason for hiding this comment

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

I think HTTP 405 method not allowed should be returned in that case

@genya0407
Copy link
Contributor Author

@katzer
Thank you for reviewing my PR, and sorry for my late reply!
I fixed the pointed problems. Could you check it?

@katzer katzer merged commit c3d389c into katzer:master Mar 8, 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

2 participants