the mechanize session must specially handle params

{:user => {:name => "Nancy"}}
now becomes
{"user[name]" => "Nancy"}
This commit is contained in:
Jeremy Burks 2008-11-08 08:46:14 -06:00
parent d039c9590f
commit 7dfd59c29c
2 changed files with 31 additions and 1 deletions

View File

@ -15,7 +15,16 @@ module Webrat
end
def post(url, data, headers_argument_not_used = nil)
@mechanize_page = @mechanize.post(url, data)
post_data = data.inject({}) do |memo, param|
case param.last
when Hash
param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
else
memo[param.first] = param.last
end
memo
end
@mechanize_page = @mechanize.post(url, post_data)
end
def response_body

View File

@ -12,4 +12,25 @@ describe Webrat::MechanizeSession do
@mech.headers.should == {}
end
end
describe "post" do
def url
'http://test.host/users'
end
def data
{:user => {:first_name => 'Nancy', :last_name => 'Callahan'}}
end
def flattened_data
{'user[first_name]' => 'Nancy', 'user[last_name]' => 'Callahan'}
end
it "should flatten model post data" do
mechanize = mock :mechanize
mechanize.should_receive(:post).with(url, flattened_data)
Webrat::MechanizeSession.new(mechanize).post(url, data)
end
end
end