diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index 316cd1076..b5462b571 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -1,11 +1,13 @@ require 'erb' require 'pact_broker/pacts/metadata' +require 'pact_broker/logging' module PactBroker module Api module PactBrokerUrls include PactBroker::Pacts::Metadata + include PactBroker::Logging # TODO make base_url the last and optional argument for all methods, defaulting to '' extend self @@ -70,8 +72,13 @@ def encode_webhook_metadata(metadata) def decode_webhook_metadata(metadata) if metadata - Rack::Utils.parse_nested_query(Base64.strict_decode64(metadata)).each_with_object({}) do | (k, v), new_hash | - new_hash[k.to_sym] = v + begin + Rack::Utils.parse_nested_query(Base64.strict_decode64(metadata)).each_with_object({}) do | (k, v), new_hash | + new_hash[k.to_sym] = v + end + rescue StandardError => e + logger.warn("Exception parsing webhook metadata: #{metadata}", e) + {} end else {} diff --git a/spec/lib/pact_broker/api/pact_broker_urls_spec.rb b/spec/lib/pact_broker/api/pact_broker_urls_spec.rb index 45edc52f1..cb51ed0ae 100644 --- a/spec/lib/pact_broker/api/pact_broker_urls_spec.rb +++ b/spec/lib/pact_broker/api/pact_broker_urls_spec.rb @@ -3,6 +3,10 @@ module PactBroker module Api describe PactBrokerUrls do + before do + allow(PactBrokerUrls).to receive(:logger).and_return(logger) + end + let(:logger) { double('logger').as_null_object } # Regex find all the URL parameter names # \/\{[^\}\s\[\(\.]+\} @@ -117,6 +121,17 @@ module Api expect(PactBrokerUrls.decode_webhook_metadata(nil)).to eq({}) end end + + context "when the metadata is not valid base64" do + it "returns an empty hash" do + expect(PactBrokerUrls.decode_webhook_metadata("foo==,")).to eq({}) + end + + it "logs a warning" do + expect(logger).to receive(:warn).with("Exception parsing webhook metadata: foo==,", ArgumentError) + PactBrokerUrls.decode_webhook_metadata("foo==,") + end + end end describe "latest_verification_for_pact_url" do