Make sure that PUTS and DELETES are handled correctly in merb

This commit is contained in:
Gwyn Morfey 2008-04-23 10:41:34 +01:00
parent 94f0086a55
commit 580a301984
2 changed files with 33 additions and 1 deletions

View File

@ -35,8 +35,13 @@ class Merb::Test::RspecStory
#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

View File

@ -9,6 +9,8 @@ class ClicksButtonTest < Test::Unit::TestCase
@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 @@ class ClicksButtonTest < Test::Unit::TestCase
@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)
<form method="put" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
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)
<form method="delete" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
EOS
@session.stubs(:redirect?).returns(false)
@session.expects(:delete).returns(@controller)
@session.clicks_button
end
end