-
Notifications
You must be signed in to change notification settings - Fork 7
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
Implemented 405 response instead of 404, resolves #25. #37
Implemented 405 response instead of 404, resolves #25. #37
Conversation
Codecov Report
@@ Coverage Diff @@
## master #37 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 23 23
Lines 686 685 -1
=====================================
- Hits 686 685 -1
Continue to review full report at Codecov.
|
lib/flame/dispatcher/routes.rb
Outdated
return nil unless route | ||
status 200 | ||
status(200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed/
lib/flame/path.rb
Outdated
@@ -11,7 +11,7 @@ class Path | |||
## @param parts [Array<String, Flame::Path>] parts of expected path | |||
## @return [Flame::Path] path from parts | |||
def self.merge(*parts) | |||
parts.join('/').gsub(%r{\/{2,}}, '/') | |||
parts.join('/').gsub(%r[/{2,}], '/') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because shielding not needed if you use %r[]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pry: main > '/foo//bar///'.gsub(%r[/{2,}], '/')
=> "/foo/bar/"
pry: main > '/foo//bar///'.gsub(%r{/{2,}}, '/')
=> "/foo/bar/"
???
Can you give proof, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i meant that %r{\/}
same that %r{/}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. So, why did you change the brackets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because r%[/{2,}]
looks more understandable than r%{/{2,}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ruby Style Guide recommends %r|...|
for cases with {}
inside Regexp
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Changed.
.should.equal nil | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests for Routes#endpoint
, please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
lib/flame/router.rb
Outdated
break if route || path_parts.empty? | ||
path_parts.pop | ||
route = routes.endpoint(*path_parts)&.values&.first | ||
break if (route&.is_a?(Flame::Router::Route)) || path_parts.pop.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove external parentheses, please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
lib/flame/dispatcher.rb
Outdated
@@ -155,7 +156,7 @@ def cached_tilts | |||
## Return response if HTTP-method is OPTIONS | |||
def try_options | |||
return unless request.http_method == :OPTIONS | |||
allow = @app_class.router.routes.dig(request.path)&.allow | |||
allow = @endpoint&.allow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write tests for case with OPTIONS
on route with optional parameters and without their values in path.
Before that PR it would not work, as I understand it, but now it will be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
works as you expected. Done
lib/flame/dispatcher.rb
Outdated
@@ -29,6 +29,7 @@ def initialize(app_class, env) | |||
@env = env | |||
@request = Flame::Dispatcher::Request.new(env) | |||
@response = Flame::Dispatcher::Response.new | |||
@endpoint = @app_class.router.routes.endpoint(*request.path.parts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe lazy-loading via getter-method? And more obvious name, like available_endpoint
, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as you wish!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for you understanding: there is no point in wasting resources to find a route if static requested.
lib/flame/dispatcher/routes.rb
Outdated
return nil unless @endpoint | ||
allow = @endpoint.allow | ||
halt(405, nil, 'Allow' => allow) if allow !~ /\b#{http_method}\b/ | ||
route = @endpoint[http_method] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe halt 405
will be better after trying to find a route by specific HTTP-method? If route not found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@endpoint
existence meaning that there is a nested routes or route here. so if we are not allowed for a HTTP-method, why should we to try find route with this method? allow
is returning HTTP-methods, that exist, so, if there is not allowed HTTP-method , why should we try find route by him?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow
is the server response header about what HTTP-methods are allowed, in case the requested HTTP-method is not found. Routes#allow
returns a string composed of the available keys, and then you search for the item in this string — this is a strange "back and forth" action.
I see a logic like this: there is a request, there is the Dispatcher#try_route
method, if the route not found with requested HTTP-method, but there are others with it — error (halt
) with Allow
header.
You first build a halt
, and then check how much it's appropriate. It seems to me strange and not very obvious (perhaps not very simple).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the route not found with requested HTTP-method, but there are others
i trying to say that available_endpoint
existence means that there is a routes or route. So we know, that there is a route or routes and have to see whether the HTTP-method is allowed. If HTTP-method not allowed then 405
, else - execute route
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i trying to say that available_endpoint existence means that there is a routes or route.
Route
is not possible there. Routes
or nil
.
I'm trying to say that code without Regexp
may be simpler :)
allow = available_endpoint.allow
halt(405, nil, 'Allow' => allow) if allow !~ /\b#{http_method}\b/
status 200
execute_route available_endpoint[http_method]
vs.
route = available_endpoint[http_method]
halt(405, nil, 'Allow' => available_endpoint.allow) unless route
status 200
execute_route(route)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh. oK. understood. Changed.
151af6f
to
faa803e
Compare
lib/flame/dispatcher.rb
Outdated
def available_endpoint | ||
@available_endpoint ||= | ||
@app_class.router.routes.endpoint(*request.path.parts) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will try to find endpoint in each method calling if it not found :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
spec/unit/router/routes_spec.rb
Outdated
should 'return nested routes from path' do | ||
routes = @init.call(['/foo', '/:?var', '/bar']) | ||
routes.endpoint('/foo').should.equal routes['foo'][':?var'] | ||
routes.endpoint('/foo/:?').should.equal routes['foo'][':?var'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why '/foo/:?'
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
faa803e
to
87f0394
Compare
lib/flame/dispatcher.rb
Outdated
## Available routes endpoint | ||
def available_endpoint | ||
return @available_endpoint if defined? @available_endpoint | ||
@available_endpoint ||= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to use ||
if there is defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah. changed.
87f0394
to
2bea737
Compare
AWESOME work @AlexWayfer @WorstOfAny! |
@snuggs thank you! |
No description provided.