Skip to content
This repository
Browse code

Fix excon adapter to handle :body => some_file_object.

  • Loading branch information...
commit f1a3293b5a5cd8e32c4eaf6dd4f8fbbaebc755e8 1 parent 86a0158
Myron Marston authored September 28, 2012
11  lib/webmock/http_lib_adapters/excon_adapter.rb
@@ -43,7 +43,16 @@ def self.build_request(params)
43 43
           method  = (params.delete(:method) || :get).to_s.downcase.to_sym
44 44
           params[:query] = to_query(params[:query]) if params[:query].is_a?(Hash)
45 45
           uri = Addressable::URI.new(params).to_s
46  
-          WebMock::RequestSignature.new method, uri, :body => params[:body], :headers => params[:headers]
  46
+          WebMock::RequestSignature.new method, uri, :body => body_from(params), :headers => params[:headers]
  47
+        end
  48
+
  49
+        def self.body_from(params)
  50
+          body = params[:body]
  51
+          return body unless body.respond_to?(:read)
  52
+
  53
+          contents = body.read
  54
+          body.rewind if body.respond_to?(:rewind)
  55
+          contents
47 56
         end
48 57
 
49 58
         def self.real_response(mock)
15  spec/acceptance/excon/excon_spec.rb
@@ -11,5 +11,20 @@
11 11
     Excon.get('http://example.com', :path => "resource/", :query => {:a => 1, :b => 2}).body.should == "abc"
12 12
   end
13 13
 
  14
+  let(:file) { File.new(__FILE__) }
  15
+  let(:file_contents) { File.new(__FILE__).read }
  16
+
  17
+  it 'handles file uploads correctly' do
  18
+    stub_request(:put, "http://example.com/upload").with(:body => file_contents)
  19
+
  20
+    yielded_request_body = nil
  21
+    WebMock.after_request do |req, res|
  22
+      yielded_request_body = req.body
  23
+    end
  24
+
  25
+    Excon.put("http://example.com", :path => "upload", :body => file)
  26
+
  27
+    yielded_request_body.should eq(file_contents)
  28
+  end
14 29
 end
15 30
 

0 notes on commit f1a3293

Please sign in to comment.
Something went wrong with that request. Please try again.