From c91d04e717d478da2f042f4959cec56c19c83da7 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 9 Jun 2018 22:11:46 +1000 Subject: [PATCH] fix: allow pact broker template parameter in URL --- lib/pact_broker/api/contracts/webhook_contract.rb | 10 +++++++--- .../api/contracts/webhook_contract_spec.rb | 13 ++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/api/contracts/webhook_contract.rb b/lib/pact_broker/api/contracts/webhook_contract.rb index aa07ffb64..ca8dda6b2 100644 --- a/lib/pact_broker/api/contracts/webhook_contract.rb +++ b/lib/pact_broker/api/contracts/webhook_contract.rb @@ -70,7 +70,7 @@ def valid_method?(http_method) end def valid_url?(url) - uri = URI(url) + uri = parse_uri(url) uri.scheme && uri.host rescue URI::InvalidURIError false @@ -83,7 +83,7 @@ def allowed_webhook_method?(http_method) end def allowed_webhook_scheme?(url) - scheme = URI(url).scheme + scheme = parse_uri(url).scheme PactBroker.configuration.webhook_scheme_whitelist.any? do | allowed_scheme | scheme.downcase == allowed_scheme.downcase end @@ -91,7 +91,7 @@ def allowed_webhook_scheme?(url) def allowed_webhook_host?(url) if host_whitelist.any? - PactBroker::Webhooks::CheckHostWhitelist.call(URI(url).host, host_whitelist).any? + PactBroker::Webhooks::CheckHostWhitelist.call(parse_uri(url).host, host_whitelist).any? else true end @@ -100,6 +100,10 @@ def allowed_webhook_host?(url) def host_whitelist PactBroker.configuration.webhook_host_whitelist end + + def parse_uri(uri_string) + URI(uri_string.gsub(/\$\{pactbroker\.[^\}]+\}/, 'placeholder')) + end end required(:http_method).filled(:valid_method?, :allowed_webhook_method?) diff --git a/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb b/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb index 20c02aa82..e7ef2f68e 100644 --- a/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb +++ b/spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb @@ -179,8 +179,19 @@ def valid_webhook_with expect(subject.errors[:"request.url"]).to eq ["is not a valid URL eg. http://example.org"] end end - end + context "with a URL that has templated parameters in it" do + let(:json) do + valid_webhook_with do |hash| + hash['request']['url'] = 'https://foo/commits/${pactbroker.consumerVersionNumber}' + end + end + + it "is empty" do + expect(subject.errors).to be_empty + end + end + end end end end