Ensure empty body responses are consistently returned as empty strings.

Zero-byte response bodies can be represented as either nil or '' in ruby.
Most ruby HTTP clients use '' (which, conveniently, allows clients
to assume the body is always a string), but Net::HTTP, for whatever
reason, uses nil.

This change makes it so that all faraday adapters consistently return
'' for zero-byte response bodies.
This commit is contained in:
Myron Marston 2012-06-12 22:39:43 -07:00 committed by Mislav Marohnić
parent 55b4c73313
commit f41ffaabb7
3 changed files with 10 additions and 1 deletions

View File

@ -38,7 +38,7 @@ module Faraday
raise Error::ConnectionFailed, $!
end
save_response(env, http_response.code.to_i, http_response.body) do |response_headers|
save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
http_response.each_header do |key, value|
response_headers[key] = value
end

View File

@ -159,6 +159,11 @@ module Adapters
end
end
def test_empty_body_response_represented_as_blank_string
response = get('204')
assert_equal '', response.body
end
def adapter
raise NotImplementedError.new("Need to override #adapter")
end

View File

@ -46,6 +46,10 @@ class LiveServer < Sinatra::Base
[200, {}, 'ok']
end
get '/204' do
status 204 # no content
end
error do |e|
"#{e.class}\n#{e.to_s}\n#{e.backtrace.join("\n")}"
end