Skip to content

Commit

Permalink
Some improvements to @tylerhunt's faraday multipart upload fix.
Browse files Browse the repository at this point in the history
- Add tests.
- Move the body reading out of #vcr_request and into a helper method--
  we don't want it to happen multiple times and #vcr_request is memoized
  for a reason.
- Refactor the body reading a bit.
- Use it for the response body, too. I don't really understand it, but
  for some reason the multipart request body becomes the response body
  when no HTTP adapter is set, and due to the change I made in
  d924f66, an error was raised.

Closes vcr#172.
  • Loading branch information
myronmarston committed May 30, 2012
1 parent cec9fe3 commit 4752496
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -41,6 +41,9 @@
the bug and providing a great example test case. the bug and providing a great example test case.
* Allow requests to be stubbed by external libraries (e.g. WebMock, * Allow requests to be stubbed by external libraries (e.g. WebMock,
FakeWeb or Typhoeus) without needing to turn VCR off. 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) ## 2.1.1 (April 24, 2012)


Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -160,6 +160,7 @@ Thanks also to the following people who have contributed patches or helpful sugg
* [playupchris](https://github.com/playupchris) * [playupchris](https://github.com/playupchris)
* [Ryan Bates](https://github.com/ryanb) * [Ryan Bates](https://github.com/ryanb)
* [Sathya Sekaran](https://github.com/sfsekaran) * [Sathya Sekaran](https://github.com/sfsekaran)
* [Tyler Hunt](https://github.com/tylerhunt)
* [Wesley Beary](https://github.com/geemus) * [Wesley Beary](https://github.com/geemus)


## Ports in other languages ## Ports in other languages
Expand Down
19 changes: 10 additions & 9 deletions lib/vcr/middleware/faraday.rb
Expand Up @@ -54,28 +54,29 @@ def delay_finishing?
end end


def vcr_request 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 \ @vcr_request ||= VCR::Request.new \
env[:method], env[:method],
env[:url].to_s, env[:url].to_s,
body, raw_body_from(env[:body]),
env[:request_headers] env[:request_headers]
end 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) def response_for(env)
response = env[:response] response = env[:response]
return nil unless response return nil unless response


VCR::Response.new( VCR::Response.new(
VCR::ResponseStatus.new(response.status, nil), VCR::ResponseStatus.new(response.status, nil),
response.headers, response.headers,
response.body, raw_body_from(response.body),
nil nil
) )
end end
Expand Down
32 changes: 32 additions & 0 deletions spec/vcr/middleware/faraday_spec.rb
Expand Up @@ -9,6 +9,38 @@
:not_disableable :not_disableable
end 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 context 'when making parallel requests' do
include VCRStubHelpers include VCRStubHelpers
let(:connection) { ::Faraday.new { |b| b.adapter :typhoeus } } let(:connection) { ::Faraday.new { |b| b.adapter :typhoeus } }
Expand Down

0 comments on commit 4752496

Please sign in to comment.