Skip to content

Commit

Permalink
Fix: Response empty for HTTP gem
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeg authored and trusche committed Feb 8, 2020
1 parent 595c28b commit 065c71c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def log_benchmark(seconds)
def log_body(body, mask_body, encoding = nil, content_type = nil)
return unless config.log_response

data = parse_body(body.dup, mask_body, encoding, content_type)
data = parse_body(body, mask_body, encoding, content_type)

if config.prefix_response_lines
log('Response:')
Expand All @@ -114,12 +114,16 @@ def log_body(body, mask_body, encoding = nil, content_type = nil)
def parse_body(body, mask_body, encoding, content_type)
raise BodyParsingError, "(not showing binary data)" unless text_based?(content_type)


if body.is_a?(Net::ReadAdapter)
# open-uri wraps the response in a Net::ReadAdapter that defers reading
# the content, so the response body is not available here.
raise BodyParsingError, '(not available yet)'
end

body = body.to_s if body.is_a?(HTTP::Response::Body)
body = body.dup

if encoding =~ /gzip/ && body && !body.empty?
begin
sio = StringIO.new(body.to_s)
Expand Down Expand Up @@ -222,7 +226,7 @@ def json_payload(data = {})
data[:response_code] = transform_response_code(data[:response_code]) if data[:response_code].is_a?(Symbol)

parsed_body = begin
parse_body(data[:response_body].dup, data[:mask_body], data[:encoding], data[:content_type])
parse_body(data[:response_body], data[:mask_body], data[:encoding], data[:content_type])
rescue BodyParsingError => e
e.message
end
Expand Down
10 changes: 10 additions & 0 deletions spec/adapters/ethon_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ def send_get_request
easy = Ethon::Easy.new
easy.http_request(parse_uri(true).to_s, :get, headers: @headers)
easy.perform

easy
end

def send_head_request
easy = Ethon::Easy.new
easy.http_request(parse_uri.to_s, :head, headers: @headers)
easy.perform

easy
end

def send_post_request
easy = Ethon::Easy.new
easy.http_request(parse_uri.to_s, :post, headers: @headers, body: @data)
easy.perform

easy
end

def self.is_libcurl?
true
end

def self.response_string_for(response)
response.response_body
end
end
4 changes: 4 additions & 0 deletions spec/adapters/excon_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ def send_head_request
def send_post_request
Excon.post(parse_uri.to_s, body: @data, headers: @headers)
end

def self.response_string_for(response)
response.body
end
end
4 changes: 4 additions & 0 deletions spec/adapters/faraday_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def logs_form_data?
false
end

def self.response_string_for(response)
response.body
end

private

def connection
Expand Down
17 changes: 17 additions & 0 deletions spec/adapters/http_base_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ def expected_response_body
"\n<html>"
end

def expected_full_response_body
<<-HTML.gsub(/^ /, "").strip
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>This is the test page.</h1>
</body>
</html>
HTML
end

def self.is_libcurl?
false
end

def self.should_log_headers?
true
end

def self.response_string_for(response)
response.to_s
end
end
4 changes: 4 additions & 0 deletions spec/adapters/httpclient_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ def self.response_should_be
def logs_form_data?
false
end

def self.response_string_for(response)
response.body
end
end
4 changes: 4 additions & 0 deletions spec/adapters/net_http_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def send_post_request
def send_post_form_request
Net::HTTP.post_form(parse_uri, @params)
end

def self.response_string_for(response)
response.body
end
end
4 changes: 4 additions & 0 deletions spec/adapters/open_uri_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ def self.should_log_headers?
def logs_data?
false
end

def self.response_string_for(response)
response.string
end
end
4 changes: 4 additions & 0 deletions spec/adapters/patron_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ def send_multipart_post_request
def self.is_libcurl?
true
end

def self.response_string_for(response)
response.body
end
end
17 changes: 16 additions & 1 deletion spec/lib/http_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
let(:params) { { 'foo' => secret, 'bar' => 'foo:form-data' } }
let(:html) { File.read('./spec/support/index.html') }
let(:json) { JSON.parse(log.match(/\[httplog\]\s(.*)/).captures.first) }
let(:gray_log) { JSON.parse("{#{log.match(/{(.*)/).captures.first}") }
let(:gray_log) { JSON.parse("{#{log.match(/\{(.*)/).captures.first}") }

# Default configuration
let(:logger) { Logger.new @log }
Expand Down Expand Up @@ -101,6 +101,11 @@ def configure

it { expect(res).to be_a adapter.response if adapter.respond_to? :response }

it "does not alter adapter response" do
response_string = adapter.class.response_string_for(res)
expect(response_string).to eq(adapter.expected_full_response_body)
end

context 'with gzip encoding' do
let(:path) { '/index.html.gz' }
let(:data) { nil }
Expand Down Expand Up @@ -155,6 +160,11 @@ def configure

it { expect(res).to be_a adapter.response if adapter.respond_to? :response }

it "does not alter adapter response" do
response_string = adapter.class.response_string_for(res)
expect(response_string).to eq(adapter.expected_full_response_body)
end

context 'with non-UTF request data' do
let(:data) { "a UTF-8 striñg with an 8BIT-ASCII character: \xC3" }
it_behaves_like 'logs expected response' # == doesn't throw exception
Expand Down Expand Up @@ -379,6 +389,11 @@ def configure

it { expect(res).to be_a adapter.response if adapter.respond_to? :response }

it "does not alter adapter response" do
response_string = adapter.class.response_string_for(res)
expect(response_string).to eq(adapter.expected_full_response_body)
end

context 'with non-UTF request data' do
let(:data) { "a UTF-8 striñg with an 8BIT-ASCII character: \xC3" }
it { is_expected.to include("request_body") } # == doesn't throw exception
Expand Down

0 comments on commit 065c71c

Please sign in to comment.