diff --git a/.gitignore b/.gitignore index 18c8458..de16ecc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ doc ri email.txt .svn - +log diff --git a/lib/webrat/mechanize.rb b/lib/webrat/mechanize.rb index 5ca0da5..77d0f77 100644 --- a/lib/webrat/mechanize.rb +++ b/lib/webrat/mechanize.rb @@ -1,27 +1,39 @@ require "mechanize" module Webrat - class MechanizeSession < Session #:nodoc: + class MechanizeSession < Session + + attr_accessor :response + alias :page :response def initialize(mechanize = WWW::Mechanize.new) super() @mechanize = mechanize end - def get(url, data) - @mechanize_page = @mechanize.get(url, data) + def get(url, data, headers_argument_not_used = nil) + @response = @mechanize.get(url, data) end - def post(url, data) - @mechanize_page = @mechanize.post(url, data) + def post(url, data, headers_argument_not_used = nil) + 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 + @response = @mechanize.post(url, post_data) end def response_body - @mechanize_page.content + @response.content end def response_code - @mechanize_page.code.to_i + @response.code.to_i end end 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