Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Net::HTTP adapter so that it returns nil for an empty body response. #190

Merged
merged 1 commit into from
Jul 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ def start_with_conditional_connect(&block)

def build_net_http_response(webmock_response, &block)
response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
response.instance_variable_set(:@body, webmock_response.body)
body = webmock_response.body
body = nil if body.to_s == ''

response.instance_variable_set(:@body, body)
webmock_response.headers.to_a.each do |name, values|
values = [values] unless values.is_a?(Array)
values.each do |value|
Expand Down
13 changes: 13 additions & 0 deletions spec/acceptance/shared/complex_cross_concern_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@
played_back_response.headers.keys.should include('Set-Cookie')
played_back_response.should == real_response
end

let(:no_content_url) { 'http://httpstat.us/204' }
[nil, ''].each do |stub_val|
it "returns the same value (nil or "") for a request stubbed as #{stub_val.inspect} that a real empty response has" do
WebMock.allow_net_connect!

real_response = http_request(:get, no_content_url)
stub_request(:get, no_content_url).to_return(:status => 204, :body => stub_val)
stubbed_response = http_request(:get, no_content_url)

stubbed_response.body.should eq(real_response.body)
end
end
end