diff --git a/lib/pact_broker/client/hal/entity.rb b/lib/pact_broker/client/hal/entity.rb index ee26ece3..27a65d24 100644 --- a/lib/pact_broker/client/hal/entity.rb +++ b/lib/pact_broker/client/hal/entity.rb @@ -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 diff --git a/lib/pact_broker/client/hal/http_client.rb b/lib/pact_broker/client/hal/http_client.rb index ad27a5f3..a20160c0 100644 --- a/lib/pact_broker/client/hal/http_client.rb +++ b/lib/pact_broker/client/hal/http_client.rb @@ -144,6 +144,10 @@ def headers __getobj__().to_hash end + def header(name) + __getobj__()[name] + end + def raw_body __getobj__().body end diff --git a/lib/pact_broker/client/hal/link.rb b/lib/pact_broker/client/hal/link.rb index f5508b2c..7398938e 100644 --- a/lib/pact_broker/client/hal/link.rb +++ b/lib/pact_broker/client/hal/link.rb @@ -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 diff --git a/lib/pact_broker/client/publish_pacts.rb b/lib/pact_broker/client/publish_pacts.rb index 8849d9b0..665b33b9 100644 --- a/lib/pact_broker/client/publish_pacts.rb +++ b/lib/pact_broker/client/publish_pacts.rb @@ -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 diff --git a/spec/lib/pact_broker/client/hal/link_spec.rb b/spec/lib/pact_broker/client/hal/link_spec.rb index efe14591..a6410b8a 100644 --- a/spec/lib/pact_broker/client/hal/link_spec.rb +++ b/spec/lib/pact_broker/client/hal/link_spec.rb @@ -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 @@ -33,6 +37,8 @@ module Hal } end + let(:content_type) { nil } + subject { Link.new(attrs, http_client) } before do @@ -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