Skip to content

Commit

Permalink
Merge pull request #213 from bblimke/fix_excon_file_uploads
Browse files Browse the repository at this point in the history
Fix excon adapter to handle :body => some_file_object.
  • Loading branch information
bblimke committed Sep 29, 2012
2 parents 86a0158 + f1a3293 commit 9915698
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/webmock/http_lib_adapters/excon_adapter.rb
Expand Up @@ -43,7 +43,16 @@ def self.build_request(params)
method = (params.delete(:method) || :get).to_s.downcase.to_sym
params[:query] = to_query(params[:query]) if params[:query].is_a?(Hash)
uri = Addressable::URI.new(params).to_s
WebMock::RequestSignature.new method, uri, :body => params[:body], :headers => params[:headers]
WebMock::RequestSignature.new method, uri, :body => body_from(params), :headers => params[:headers]
end

def self.body_from(params)
body = params[:body]
return body unless body.respond_to?(:read)

contents = body.read
body.rewind if body.respond_to?(:rewind)
contents
end

def self.real_response(mock)
Expand Down
15 changes: 15 additions & 0 deletions spec/acceptance/excon/excon_spec.rb
Expand Up @@ -11,5 +11,20 @@
Excon.get('http://example.com', :path => "resource/", :query => {:a => 1, :b => 2}).body.should == "abc"
end

let(:file) { File.new(__FILE__) }
let(:file_contents) { File.new(__FILE__).read }

it 'handles file uploads correctly' do
stub_request(:put, "http://example.com/upload").with(:body => file_contents)

yielded_request_body = nil
WebMock.after_request do |req, res|
yielded_request_body = req.body
end

Excon.put("http://example.com", :path => "upload", :body => file)

yielded_request_body.should eq(file_contents)
end
end

0 comments on commit 9915698

Please sign in to comment.