Closed kaikuchn closed 7 years ago
Here's the monkey patch that fixes this in the mean time, for anyone who absolutely needs this to work..
class Hanami::Router
def redirect(path, options = {}, &endpoint)
get(path).redirect(@router.find(options), options[:code] || 301).tap do |route|
route.dest = Hanami::Routing::Endpoint.new(route.dest)
end
end
end
It is save to just use the Endpoint
class since redirect will always result in a callable for the route's destination.
@kaikuchn Hi and sorry for the late reply. Can you please review this fix? #149
@jodosha No worries! I'll be able to take a look at it over the weekend.
Hey folks,
@padget stumbled on a bug in hanami-router. After some digging around I found out what's broken. So here's the issue report.
Suppose you have the following routes definition:
When you visit
/redirect-me
in the browser you will get redirected. And everything seems to work. However, when you want to write a test case like this:You will receive this error:
The reason is that the destination of the recognized route is not wrapped in a
Hanami::Routing::Endpoint
for theHanami::Router::redirect
call. It seems that the redirect keyword was not considered when theHanami::Routing::Endpoint
classes where introduced. I was unable to find a good way to fix this with minimal changes.For all the other methods (get, post, root, etc.) the flow is that the corresponding http verb method is called on
Hanami::HttpRouter
which returns aHanami::Routing::Route
by going throughHanami::Routing::Route#generate
which gets aHanami::Routing::EndpointResolver
passed whose job it is - among other things - to wrap theHanami::Routing::Route
@dest
in aHanami::Routing::Endpoint
depending on the endpoint type.Redirect, however, calls
::HttpRouter::Route#redirect
on the result ofHanami::HttpRouter#get
and that method overrides the@dest
instance variable of theHanami::Routing::Route
. TheHanami::Routing::EndpointResolver
is not visible in eitherHanami::Routing::Route
orHanami::Router
, it is only known to theHanami::Routing::HttpRouter
who is no longer involved.I hope I could explain this in a manner that is easy to follow. It was quite difficult gathering all the moving pieces to locate the issue.