Allow a Pathname to be passed as a Response body #331

Merged
merged 3 commits into from Nov 13, 2013
View
@@ -100,10 +100,10 @@ def ==(other)
private
def stringify_body!
- if @body.is_a?(IO)
+ if @body.is_a?(IO) || @body.is_a?(Pathname)
io = @body
@body = io.read
- io.close
+ io.close if io.respond_to?(:close)
end
end
@@ -48,7 +48,7 @@
end
let(:file) { File.new(__FILE__) }
- let(:file_contents) { File.new(__FILE__).read }
+ let(:file_contents) { File.read(__FILE__) }
it 'handles file uploads correctly' do
stub_request(:put, "http://example.com/upload").with(:body => file_contents)
@@ -92,13 +92,13 @@ class MyException < StandardError; end;
describe "when response body was declared as IO" do
it "should return response body" do
stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
- http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
+ http_request(:get, "http://www.example.com/").body.should == File.read(__FILE__)
end
it "should return response body if requested many times" do
stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
2.times do
- http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
+ http_request(:get, "http://www.example.com/").body.should == File.read(__FILE__)
end
end
@@ -199,7 +199,7 @@ def call(request)
describe "when response was declared as a string with a raw response" do
before(:each) do
- @input = File.new(CURL_EXAMPLE_OUTPUT_PATH).read
+ @input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
stub_request(:get, "www.example.com").to_return(@input)
@response = http_request(:get, "http://www.example.com/")
end
View
@@ -17,7 +17,7 @@
require 'support/webmock_server'
require 'support/my_rack_app'
-CURL_EXAMPLE_OUTPUT_PATH = File.expand_path(File.dirname(__FILE__)) + "/support/example_curl_output.txt" unless defined? CURL_EXAMPLE_OUTPUT_PATH
+CURL_EXAMPLE_OUTPUT_PATH = File.expand_path('../support/example_curl_output.txt', __FILE__)
RSpec.configure do |config|
unless NetworkConnection.is_network_available?
View
@@ -111,15 +111,19 @@
it "should report content of a IO object if provided" do
@response = WebMock::Response.new(:body => File.new(__FILE__))
- @response.body.should == File.new(__FILE__).read
+ @response.body.should == File.read(__FILE__)
end
it "should report many times content of a IO object if provided" do
@response = WebMock::Response.new(:body => File.new(__FILE__))
- @response.body.should == File.new(__FILE__).read
- @response.body.should == File.new(__FILE__).read
+ @response.body.should == File.read(__FILE__)
+ @response.body.should == File.read(__FILE__)
end
+ it "should work with Pathnames" do
+ @response = WebMock::Response.new(:body => Pathname.new(__FILE__))
+ @response.body.should == File.read(__FILE__)
+ end
end
describe "from raw response" do
@@ -157,7 +161,7 @@
describe "when input is String" do
before(:each) do
- @input = File.new(CURL_EXAMPLE_OUTPUT_PATH).read
+ @input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
@response = WebMock::Response.new(@input)
end