diff --git a/CHANGELOG.md b/CHANGELOG.md index 68bd6ea4..104e8189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,9 @@ the bug and providing a great example test case. * Allow requests to be stubbed by external libraries (e.g. WebMock, FakeWeb or Typhoeus) without needing to turn VCR off. +* Fix bug in handling of Faraday requests with multipart uploads. + Thanks to [Tyler Hunt](https://github.com/tylerhunt) for reporting + and fixing the bug. ## 2.1.1 (April 24, 2012) diff --git a/README.md b/README.md index 1b76f164..5460c3a9 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ Thanks also to the following people who have contributed patches or helpful sugg * [playupchris](https://github.com/playupchris) * [Ryan Bates](https://github.com/ryanb) * [Sathya Sekaran](https://github.com/sfsekaran) +* [Tyler Hunt](https://github.com/tylerhunt) * [Wesley Beary](https://github.com/geemus) ## Ports in other languages diff --git a/lib/vcr/middleware/faraday.rb b/lib/vcr/middleware/faraday.rb index 55b581e9..08f312b2 100644 --- a/lib/vcr/middleware/faraday.rb +++ b/lib/vcr/middleware/faraday.rb @@ -54,20 +54,21 @@ def delay_finishing? end def vcr_request - if env[:body].respond_to?(:read) - body = env[:body].read - env[:body].rewind if env[:body].respond_to?(:rewind) - else - body = env[:body] - end - @vcr_request ||= VCR::Request.new \ env[:method], env[:url].to_s, - body, + raw_body_from(env[:body]), env[:request_headers] end + def raw_body_from(body) + return body unless body.respond_to?(:read) + + body.read.tap do |b| + body.rewind if body.respond_to?(:rewind) + end + end + def response_for(env) response = env[:response] return nil unless response @@ -75,7 +76,7 @@ def response_for(env) VCR::Response.new( VCR::ResponseStatus.new(response.status, nil), response.headers, - response.body, + raw_body_from(response.body), nil ) end diff --git a/spec/vcr/middleware/faraday_spec.rb b/spec/vcr/middleware/faraday_spec.rb index 98e3c48b..fe2f8baa 100644 --- a/spec/vcr/middleware/faraday_spec.rb +++ b/spec/vcr/middleware/faraday_spec.rb @@ -9,6 +9,38 @@ :not_disableable end + context 'when performing a multipart upload' do + let(:connection) do + ::Faraday.new("http://localhost:#{VCR::SinatraApp.port}/") do |b| + b.request :multipart + end + end + + def self.test_recording + it 'records the request body correctly' do + payload = { :file => Faraday::UploadIO.new(__FILE__, 'text/plain') } + + VCR.should_receive(:record_http_interaction) do |i| + i.request.headers['Content-Type'].first.should include("multipart") + i.request.body.should include(File.read(__FILE__)) + end + + VCR.use_cassette("upload") do + connection.post '/files', payload + end + end + end + + context 'when the net_http adapter is used' do + before { connection.builder.adapter :net_http } + test_recording + end + + context 'when no adapter is used' do + test_recording + end + end + context 'when making parallel requests' do include VCRStubHelpers let(:connection) { ::Faraday.new { |b| b.adapter :typhoeus } }