Skip to content

Commit

Permalink
Merge commit 'jrun/master'
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
  • Loading branch information
brynary committed Nov 14, 2008
2 parents 5cbd5c4 + a570d40 commit e8c9f04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,4 +5,4 @@ doc
ri
email.txt
.svn

log
26 changes: 19 additions & 7 deletions 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
Expand Down
21 changes: 21 additions & 0 deletions spec/webrat/mechanize/mechanize_session_spec.rb
Expand Up @@ -12,4 +12,25 @@
@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

0 comments on commit e8c9f04

Please sign in to comment.