From 580a301984aef358bfaf22977c6ac4d25cea60aa Mon Sep 17 00:00:00 2001 From: Gwyn Morfey Date: Wed, 23 Apr 2008 10:41:34 +0100 Subject: [PATCH] Make sure that PUTS and DELETES are handled correctly in merb --- lib/boot_merb.rb | 7 ++++++- test/clicks_button_test.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/boot_merb.rb b/lib/boot_merb.rb index 59fe2b24..e6f6e64e 100644 --- a/lib/boot_merb.rb +++ b/lib/boot_merb.rb @@ -35,8 +35,13 @@ def save_and_open_page #which is where we get our status and response from. # #We have to preserve cookies like this, or the session is lost. + # + #While (in a web application) a PUT is modelled as a POST with a parameter _method, + #this close to the metal we need to make sure that we actually hit the underlying 'put' method, + #so we rewrite 'method'. def request_via_redirect(method,path,parameters={},headers={}) - mycookies = @controller.cookies rescue nil #will be nil if no requests yet + method = parameters["_method"] if !parameters["_method"].blank? + mycookies = defined?(@controller) ? @controller.cookies : nil #will be nil if no requests yet @controller=self.send(method, path, parameters, headers) do |new_controller| new_controller.cookies = mycookies end diff --git a/test/clicks_button_test.rb b/test/clicks_button_test.rb index 4239b2e2..23a297ee 100644 --- a/test/clicks_button_test.rb +++ b/test/clicks_button_test.rb @@ -9,6 +9,8 @@ def setup @session.stubs(:current_page).returns(@page) @response = mock @session.stubs(:response).returns(@response) + @controller = mock + @controller.stubs(:status).returns(200) end def test_should_fail_if_no_buttons @@ -349,4 +351,29 @@ def test_should_send_default_empty_text_field_values @session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.clicks_button end + + def test_should_use_put_method_when_needed + @response.stubs(:body).returns(<<-EOS) +
+ + +
+ EOS + @session.stubs(:redirect?).returns(false) + @session.expects(:put).returns(@controller) + @session.clicks_button + end + + def test_should_use_delete_method_when_needed + @response.stubs(:body).returns(<<-EOS) +
+ + +
+ EOS + @session.stubs(:redirect?).returns(false) + @session.expects(:delete).returns(@controller) + @session.clicks_button + end + end