diff --git a/lib/ezclient/request.rb b/lib/ezclient/request.rb index 35ab5e4..0a0fdce 100644 --- a/lib/ezclient/request.rb +++ b/lib/ezclient/request.rb @@ -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 diff --git a/lib/ezclient/response.rb b/lib/ezclient/response.rb index 5f77257..6f0f641 100644 --- a/lib/ezclient/response.rb +++ b/lib/ezclient/response.rb @@ -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 @@ -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 diff --git a/spec/ezclient_spec.rb b/spec/ezclient_spec.rb index 10c08dc..daa6ac5 100644 --- a/spec/ezclient_spec.rb +++ b/spec/ezclient_spec.rb @@ -374,6 +374,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 " 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=>\"#\", " \ + ":hdrs=>#\"ezclient/#{EzClient::VERSION}\", " \ + "\"Connection\"=>\"close\", " \ + "\"Host\"=>\"example.com\"}>}, " \ + ":resp=>{" \ + ":raw=>\"#\", " \ + ":hdrs=>#, " \ + ":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) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a072d73..c7ba115 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ require "simplecov" require "simplecov-lcov" +require "pry" SimpleCov::Formatter::LcovFormatter.config do |config| config.report_with_single_file = true