From 7dfd59c29cd3eee6e42d0866fc84f10ce200ae93 Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Sat, 8 Nov 2008 08:46:14 -0600 Subject: [PATCH] the mechanize session must specially handle params {:user => {:name => "Nancy"}} now becomes {"user[name]" => "Nancy"} --- lib/webrat/mechanize/mechanize_session.rb | 11 +++++++++- .../mechanize/mechanize_session_spec.rb | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/webrat/mechanize/mechanize_session.rb b/lib/webrat/mechanize/mechanize_session.rb index cc8e241..fdd731b 100644 --- a/lib/webrat/mechanize/mechanize_session.rb +++ b/lib/webrat/mechanize/mechanize_session.rb @@ -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 diff --git a/spec/webrat/mechanize/mechanize_session_spec.rb b/spec/webrat/mechanize/mechanize_session_spec.rb index aa5077e..452d3b7 100644 --- a/spec/webrat/mechanize/mechanize_session_spec.rb +++ b/spec/webrat/mechanize/mechanize_session_spec.rb @@ -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 \ No newline at end of file