diff --git a/lib/pact/hal/entity.rb b/lib/pact/hal/entity.rb index 8fe2f5c4..4a09852a 100644 --- a/lib/pact/hal/entity.rb +++ b/lib/pact/hal/entity.rb @@ -68,6 +68,14 @@ def respond_to_missing?(method_name, include_private = false) end class ErrorEntity < Entity + + def initialize(data, http_client, response = nil) + @data = data + @links = {} + @client = http_client + @response = response + end + def success? false end diff --git a/lib/pact/hal/http_client.rb b/lib/pact/hal/http_client.rb index 130e362c..d83b202a 100644 --- a/lib/pact/hal/http_client.rb +++ b/lib/pact/hal/http_client.rb @@ -3,11 +3,12 @@ module Pact module Hal class HttpClient - attr_accessor :username, :password + attr_accessor :username, :password, :verbose def initialize options @username = options[:username] @password = options[:password] + @verbose = options[:verbose] end def get href, params = {}, headers = {} @@ -41,9 +42,11 @@ def create_request uri, http_method, body = nil, headers = {} end def perform_request request, uri - options = {:use_ssl => uri.scheme == 'https'} response = Retry.until_true do - Net::HTTP.start(uri.host, uri.port, :ENV, options) do |http| + http = Net::HTTP.new(uri.host, uri.port, :ENV) + http.set_debug_output(Pact.configuration.output_stream) if verbose + http.use_ssl = (uri.scheme == 'https') + http.start do |http| http.request request end end @@ -52,7 +55,7 @@ def perform_request request, uri class Response < SimpleDelegator def body - bod = __getobj__().body + bod = raw_body if bod && bod != '' JSON.parse(bod) else @@ -60,6 +63,10 @@ def body end end + def raw_body + __getobj__().body + end + def success? __getobj__().code.start_with?("2") end diff --git a/lib/pact/hal/link.rb b/lib/pact/hal/link.rb index 9414e8ee..4804b9ad 100644 --- a/lib/pact/hal/link.rb +++ b/lib/pact/hal/link.rb @@ -49,7 +49,7 @@ def wrap_response(http_response) if http_response.success? Entity.new(http_response.body, @http_client, http_response) else - ErrorEntity.new(http_response.body, @http_client, http_response) + ErrorEntity.new(http_response.raw_body, @http_client, http_response) end end