Skip to content
This repository

Fix excon adapter to handle :body => some_file_object. #213

Merged
merged 1 commit into from over 1 year ago

2 participants

Myron Marston Bartosz Blimke
Myron Marston
Collaborator

I investigated an issue a VCR user was having:

myronmarston/vcr#198

Turns out Excon supports :body => File.new("path/to/some_file") but WebMock wasn't handling this properly. This PR fixes the issue.

Bartosz Blimke bblimke merged commit 9915698 into from September 29, 2012
Bartosz Blimke bblimke closed this September 29, 2012
Bartosz Blimke
Owner

Thanks Myron. It's released as 1.8.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 28, 2012
Myron Marston Fix excon adapter to handle :body => some_file_object. f1a3293
This page is out of date. Refresh to see the latest.
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
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.