Permalink
Browse files

Provides access to the response even when rails does a redirect from …

…within middleware
  • Loading branch information...
gus committed Jun 15, 2010
1 parent ce3c8af commit caffd8a12ae323211ff232a04bf2b1e1d475ef4b
View
@@ -1,5 +1,9 @@
*0.2.0.pre.1*
+* When rails does redirects, it does not set action_controller.instance as you might expect; users should still have access to the response, though. This fixes that [jaknowlden]
+
+*0.2.0.pre.1*
+
* Actually relying on riot-0.11.1. Whoops. [jaknowlden]
*0.2.0.pre*
View
@@ -1 +1 @@
-0.2.0.pre.1
+0.2.0.pre.2
@@ -24,17 +24,12 @@ def setup_context_macros(context)
def setup_situation(context)
context.helper(:app) { @app }
context.helper(:env) { @env }
- context.helper(:controller) { env["action_controller.instance"] }
-
- context.helper(:request) do
- return controller.request if controller
- raise Exception, "No request made yet"
+ context.helper(:controller) do
+ env["action_controller.instance"] || raise(Exception, "Instance of controller not found")
end
- context.helper(:response) do
- return controller.response if controller
- raise Exception, "No response since no request made yet"
- end
+ context.helper(:request) { controller.request }
+ context.helper(:response) { @response || raise(Exception, "No response found") }
context.setup(true) do
self.class_eval { include RiotRails::ActionController::HttpMethods }
@@ -8,11 +8,10 @@ def put(uri, params={}); perform_request("PUT", uri, params); end
def delete(uri, params={}); perform_request("DELETE", uri, params); end
private
def perform_request(request_method, uri, params)
- http_reset
params = params.inject({}) { |acc,(key,val)| acc[key] = val.to_s; acc }
@env = ::Rack::MockRequest.env_for(uri, {:params => params, :method => request_method}).merge(@env)
@env['action_dispatch.show_exceptions'] = false
- @app.call(@env)
+ @app.call(@env).tap { |state| @response = state.last }
end
end # HttpMethods
end # ActionController
@@ -20,15 +20,12 @@
asserts("@env") { assigned(:@env) }.equals({})
asserts("helper env") { topic.env }.equals { assigned(:@env) }
- asserts("helper controller") { topic.controller }.equals { assigned(:@controller) }
+ asserts("helper controller") do
+ topic.controller
+ end.raises(Exception, "Instance of controller not found")
- asserts("request before any request made") do
- topic.request
- end.raises(Exception, "No request made yet")
-
- asserts("response before any request made") do
- topic.response
- end.raises(Exception, "No response since no request made yet")
+ asserts(:request).raises(Exception, "Instance of controller not found")
+ asserts(:response).raises(Exception, "No response found")
asserts_topic.responds_to(:get)
asserts_topic.responds_to(:post)
@@ -59,8 +56,8 @@
topic.env["action_controller.instance"]
end.kind_of(RoomsController)
- asserts("helper request") { topic.request }.kind_of(Rack::Request)
- asserts("helper response") { topic.response }.kind_of(Rack::Response)
+ asserts(:request).kind_of(Rack::Request)
+ asserts(:response).kind_of(Rack::Response)
end # post-request state
end # ActionController middleware

0 comments on commit caffd8a

Please sign in to comment.