Skip to content

Commit

Permalink
Detailed response inspectation (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp authored Jul 31, 2024
1 parent e6806b9 commit ce8f99a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/ezclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(verb, url, options)
def perform
http_response = perform_request

EzClient::Response.new(http_response).tap do |response|
EzClient::Response.new(http_response, http_request).tap do |response|
on_complete.call(self, response, options[:metadata])
end
rescue => error
Expand Down
23 changes: 21 additions & 2 deletions lib/ezclient/response.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

class EzClient::Response
attr_accessor :http_response, :body
attr_accessor :http_response, :body, :http_request

def initialize(http_response)
def initialize(http_response, http_request)
self.http_response = http_response
self.http_request = http_request
self.body = http_response.body.to_s # Make sure we read the body for persistent connection
end

Expand Down Expand Up @@ -40,4 +41,22 @@ def server_error?
def error?
client_error? || server_error?
end

def inspect
{
req: {
raw: http_request.inspect,
hdrs: http_request.headers,
},
resp: {
raw: http_response.inspect,
hdrs: headers,
body: body,
},
}.to_s
end

def to_s
inspect
end
end
45 changes: 45 additions & 0 deletions spec/ezclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,51 @@ def self.sign!(*); end
let(:response) { request.perform }
let(:webmock_response) { { status: 201 } }

context "object inspectation" do
specify "#inspect" do
expect(response.inspect).to match({
req: {
raw: response.http_request.inspect,
hdrs: response.http_request.headers,
},
resp: {
raw: response.http_response.inspect,
hdrs: response.http_response.headers,
body: response.body,
},
}.to_s)
end

specify "#to_s" do
aggregate_failures "<inspect> representation" do
expect(response.inspect).to eq(response.to_s)

# NOTE:
# - for better visual representability;
# - example.com is fetched from `request` object:
# - see request (@http_request.host)
# - see request.verb
# - see request.url
# rubocop:disable Layout/LineEndStringConcatenationIndentation
# rubocop:disable Style/TrailingCommaInArguments
expect(response.inspect).to eq(
"{:req=>{" \
":raw=>\"#<HTTP::Request/1.1 POST http://example.com/>\", " \
":hdrs=>#<HTTP::Headers " \
"{\"User-Agent\"=>\"ezclient/#{EzClient::VERSION}\", " \
"\"Connection\"=>\"close\", " \
"\"Host\"=>\"example.com\"}>}, " \
":resp=>{" \
":raw=>\"#<HTTP::Response/1.1 #{webmock_response[:status]} Created {}>\", " \
":hdrs=>#<HTTP::Headers {}>, " \
":body=>\"\"}}"
)
# rubocop:enable Layout/LineEndStringConcatenationIndentation
# rubocop:enable Style/TrailingCommaInArguments
end
end
end

context "201 response code" do
specify do
expect(response.code).to eq(201)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "simplecov"
require "simplecov-lcov"
require "pry"

SimpleCov::Formatter::LcovFormatter.config do |config|
config.report_with_single_file = true
Expand Down

0 comments on commit ce8f99a

Please sign in to comment.