Skip to content

Commit

Permalink
Make sure that PUTS and DELETES are handled correctly in merb
Browse files Browse the repository at this point in the history
  • Loading branch information
Gwyn Morfey committed Apr 23, 2008
1 parent 94f0086 commit 580a301
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/boot_merb.rb
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/clicks_button_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
<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

0 comments on commit 580a301

Please sign in to comment.