diff --git a/lib/pact_broker/domain/webhook_request.rb b/lib/pact_broker/domain/webhook_request.rb index f71bd47ac..cf96b5c7f 100644 --- a/lib/pact_broker/domain/webhook_request.rb +++ b/lib/pact_broker/domain/webhook_request.rb @@ -123,7 +123,14 @@ def log_response response, execution_logger execution_logger.info "#{header.split("-").collect(&:capitalize).join('-')}: #{response[header]}" end logger.debug "body=#{response.body}" - execution_logger.info response.body + safe_body = nil + if response.body + safe_body = response.body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') + if response.body != safe_body + execution_logger.debug "Note that invalid UTF-8 byte sequences were removed from response body before saving the logs" + end + end + execution_logger.info safe_body end def log_completion_message options, execution_logger, success diff --git a/spec/lib/pact_broker/domain/webhook_request_spec.rb b/spec/lib/pact_broker/domain/webhook_request_spec.rb index ed3a4613a..448c75853 100644 --- a/spec/lib/pact_broker/domain/webhook_request_spec.rb +++ b/spec/lib/pact_broker/domain/webhook_request_spec.rb @@ -268,6 +268,25 @@ module Domain end end + context "when the response body contains a non UTF-8 character" do + let!(:http_request) do + stub_request(:post, "http://example.org/hook"). + to_return(:status => 200, :body => "This has some \xC2 invalid chars") + end + + it "removes the non UTF-8 characters before saving the logs so they don't blow up the database" do + result = subject.execute(pact, options) + expect(result.logs).to include "This has some invalid chars" + end + + it "logs that it has cleaned the string to the execution logger" do + logger = double("logger").as_null_object + allow(Logger).to receive(:new).and_return(logger) + expect(logger).to receive(:debug).with(/Note that invalid UTF-8 byte sequences were removed/) + subject.execute(pact, options) + end + end + context "when an error occurs executing the request" do class WebhookTestError < StandardError; end