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

Implemented 405 response instead of 404, resolves #25. #37

Conversation

WorstOfAny
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented Sep 4, 2017

Codecov Report

Merging #37 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #37   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          23     23           
  Lines         686    685    -1     
=====================================
- Hits          686    685    -1
Impacted Files Coverage Δ
lib/flame/dispatcher.rb 100% <100%> (ø) ⬆️
lib/flame/dispatcher/routes.rb 100% <100%> (ø) ⬆️
lib/flame/router.rb 100% <100%> (ø) ⬆️
lib/flame/router/routes.rb 100% <100%> (ø) ⬆️
lib/flame/path.rb 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 15dd686...2bea737. Read the comment docs.

return nil unless route
status 200
status(200)
Copy link
Owner

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed/

@@ -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,}], '/')
Copy link
Owner

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

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[]

Copy link
Owner

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?

Copy link
Contributor Author

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{/}

Copy link
Owner

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?

Copy link
Contributor Author

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,}}

Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

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?
Copy link
Owner

Choose a reason for hiding this comment

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

Remove external parentheses, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

@@ -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
Copy link
Owner

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.

Copy link
Contributor Author

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

@@ -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)
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as you wish!

Copy link
Owner

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.

return nil unless @endpoint
allow = @endpoint.allow
halt(405, nil, 'Allow' => allow) if allow !~ /\b#{http_method}\b/
route = @endpoint[http_method]
Copy link
Owner

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.

Copy link
Contributor Author

@WorstOfAny WorstOfAny Sep 5, 2017

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?

Copy link
Owner

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).

Copy link
Contributor Author

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

Copy link
Owner

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh. oK. understood. Changed.

@WorstOfAny WorstOfAny force-pushed the implement_405_response_instead_of_404 branch 5 times, most recently from 151af6f to faa803e Compare September 5, 2017 11:10
def available_endpoint
@available_endpoint ||=
@app_class.router.routes.endpoint(*request.path.parts)
end
Copy link
Owner

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 :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

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']
Copy link
Owner

@AlexWayfer AlexWayfer Sep 5, 2017

Choose a reason for hiding this comment

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

Why '/foo/:?'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

@WorstOfAny WorstOfAny force-pushed the implement_405_response_instead_of_404 branch from faa803e to 87f0394 Compare September 5, 2017 11:45
## Available routes endpoint
def available_endpoint
return @available_endpoint if defined? @available_endpoint
@available_endpoint ||=
Copy link
Owner

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah. changed.

@WorstOfAny WorstOfAny force-pushed the implement_405_response_instead_of_404 branch from 87f0394 to 2bea737 Compare September 5, 2017 11:57
@AlexWayfer AlexWayfer merged commit b1cf7e3 into AlexWayfer:master Sep 5, 2017
@snuggs
Copy link

snuggs commented Mar 9, 2018

AWESOME work @AlexWayfer @WorstOfAny!

@AlexWayfer
Copy link
Owner

@snuggs thank you!

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.

3 participants