Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detailed response inspectation #20

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions spec/ezclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ 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,
tycooon marked this conversation as resolved.
Show resolved Hide resolved
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
expect(response.inspect).to eq(response.to_s)
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
Loading