Permalink
Browse files

Some improvements to @tylerhunt's faraday multipart upload fix.

- 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 #172.
  • Loading branch information...
1 parent cec9fe3 commit 4752496fd57c8a881daa72919c705a8115628339 @myronmarston committed May 30, 2012
Showing with 46 additions and 9 deletions.
  1. +3 −0 CHANGELOG.md
  2. +1 −0 README.md
  3. +10 −9 lib/vcr/middleware/faraday.rb
  4. +32 −0 spec/vcr/middleware/faraday_spec.rb
View
@@ -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)
View
@@ -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
@@ -54,28 +54,29 @@ 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
VCR::Response.new(
VCR::ResponseStatus.new(response.status, nil),
response.headers,
- response.body,
+ raw_body_from(response.body),
nil
)
end
@@ -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 } }

0 comments on commit 4752496

Please sign in to comment.