Skip to content

Commit ce8f99a

Browse files
authored
Detailed response inspectation (#20)
1 parent e6806b9 commit ce8f99a

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

lib/ezclient/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(verb, url, options)
2323
def perform
2424
http_response = perform_request
2525

26-
EzClient::Response.new(http_response).tap do |response|
26+
EzClient::Response.new(http_response, http_request).tap do |response|
2727
on_complete.call(self, response, options[:metadata])
2828
end
2929
rescue => error

lib/ezclient/response.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# frozen_string_literal: true
22

33
class EzClient::Response
4-
attr_accessor :http_response, :body
4+
attr_accessor :http_response, :body, :http_request
55

6-
def initialize(http_response)
6+
def initialize(http_response, http_request)
77
self.http_response = http_response
8+
self.http_request = http_request
89
self.body = http_response.body.to_s # Make sure we read the body for persistent connection
910
end
1011

@@ -40,4 +41,22 @@ def server_error?
4041
def error?
4142
client_error? || server_error?
4243
end
44+
45+
def inspect
46+
{
47+
req: {
48+
raw: http_request.inspect,
49+
hdrs: http_request.headers,
50+
},
51+
resp: {
52+
raw: http_response.inspect,
53+
hdrs: headers,
54+
body: body,
55+
},
56+
}.to_s
57+
end
58+
59+
def to_s
60+
inspect
61+
end
4362
end

spec/ezclient_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,51 @@ def self.sign!(*); end
394394
let(:response) { request.perform }
395395
let(:webmock_response) { { status: 201 } }
396396

397+
context "object inspectation" do
398+
specify "#inspect" do
399+
expect(response.inspect).to match({
400+
req: {
401+
raw: response.http_request.inspect,
402+
hdrs: response.http_request.headers,
403+
},
404+
resp: {
405+
raw: response.http_response.inspect,
406+
hdrs: response.http_response.headers,
407+
body: response.body,
408+
},
409+
}.to_s)
410+
end
411+
412+
specify "#to_s" do
413+
aggregate_failures "<inspect> representation" do
414+
expect(response.inspect).to eq(response.to_s)
415+
416+
# NOTE:
417+
# - for better visual representability;
418+
# - example.com is fetched from `request` object:
419+
# - see request (@http_request.host)
420+
# - see request.verb
421+
# - see request.url
422+
# rubocop:disable Layout/LineEndStringConcatenationIndentation
423+
# rubocop:disable Style/TrailingCommaInArguments
424+
expect(response.inspect).to eq(
425+
"{:req=>{" \
426+
":raw=>\"#<HTTP::Request/1.1 POST http://example.com/>\", " \
427+
":hdrs=>#<HTTP::Headers " \
428+
"{\"User-Agent\"=>\"ezclient/#{EzClient::VERSION}\", " \
429+
"\"Connection\"=>\"close\", " \
430+
"\"Host\"=>\"example.com\"}>}, " \
431+
":resp=>{" \
432+
":raw=>\"#<HTTP::Response/1.1 #{webmock_response[:status]} Created {}>\", " \
433+
":hdrs=>#<HTTP::Headers {}>, " \
434+
":body=>\"\"}}"
435+
)
436+
# rubocop:enable Layout/LineEndStringConcatenationIndentation
437+
# rubocop:enable Style/TrailingCommaInArguments
438+
end
439+
end
440+
end
441+
397442
context "201 response code" do
398443
specify do
399444
expect(response.code).to eq(201)

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "simplecov"
44
require "simplecov-lcov"
5+
require "pry"
56

67
SimpleCov::Formatter::LcovFormatter.config do |config|
78
config.report_with_single_file = true

0 commit comments

Comments
 (0)