Skip to content

Commit

Permalink
feat: print out notices if returned from publish pacts request
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 13, 2021
1 parent 43c79d8 commit 86caf7e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def fetch(key, fallback_key = nil)
end

def method_missing(method_name, *args, &block)
if @data.key?(method_name.to_s)
if @data.respond_to?(:key?) && @data.key?(method_name.to_s)
@data[method_name.to_s]
elsif @links.key?(method_name)
elsif @links.respond_to?(:key?) && @links.key?(method_name)
Link.new(@links[method_name], @client).run(*args)
else
nil
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/client/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def headers
__getobj__().to_hash
end

def header(name)
__getobj__()[name]
end

def raw_body
__getobj__().body
end
Expand Down
9 changes: 7 additions & 2 deletions lib/pact_broker/client/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ def expand(params)
private

def wrap_response(href, http_response)
require 'pact_broker/client/hal/entity' # avoid circular reference
require "pact_broker/client/hal/entity" # avoid circular reference
if http_response.success?
Entity.new(href, http_response.body, @http_client, http_response)
else
ErrorEntity.new(href, http_response.raw_body, @http_client, http_response)
body = begin
http_response.header("Content-Type") && http_response.header("Content-Type").include?("json") ? http_response.body : http_response.raw_body
rescue
http_response.raw_body
end
ErrorEntity.new(href, body, @http_client, http_response)
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/client/publish_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def text_message
"Successfully published pacts"
end
else
::Term::ANSIColor.red(response_entity.response.body.to_s)
if response_entity.notices
PactBroker::Client::ColorizeNotices.call(response_entity.notices.collect{ |n| OpenStruct.new(n) } )
else
::Term::ANSIColor.red(response_entity.response.raw_body)
end
end
end.join("\n")
end
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/pact_broker/client/hal/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
module PactBroker::Client
module Hal
describe Link do
before do
allow(response).to receive(:header).with("Content-Type").and_return(content_type)
end

let(:http_client) do
instance_double('PactBroker::Client::Hal::HttpClient', post: response)
end
Expand Down Expand Up @@ -33,6 +37,8 @@ module Hal
}
end

let(:content_type) { nil }

subject { Link.new(attrs, http_client) }

before do
Expand Down Expand Up @@ -67,6 +73,15 @@ module Hal
expect(PactBroker::Client::Hal::ErrorEntity).to receive(:new).with("http://foo/{bar}", response_body.to_json, http_client, response)
do_run
end

context "when a JSON error is returned" do
let(:content_type) { "application/json" }

it "parses the response body" do
expect(PactBroker::Client::Hal::ErrorEntity).to receive(:new).with("http://foo/{bar}", response_body, http_client, response)
do_run
end
end
end
end

Expand Down

0 comments on commit 86caf7e

Please sign in to comment.