Skip to content

Commit c91d04e

Browse files
committed
fix: allow pact broker template parameter in URL
1 parent 619c7e9 commit c91d04e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/pact_broker/api/contracts/webhook_contract.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def valid_method?(http_method)
7070
end
7171

7272
def valid_url?(url)
73-
uri = URI(url)
73+
uri = parse_uri(url)
7474
uri.scheme && uri.host
7575
rescue URI::InvalidURIError
7676
false
@@ -83,15 +83,15 @@ def allowed_webhook_method?(http_method)
8383
end
8484

8585
def allowed_webhook_scheme?(url)
86-
scheme = URI(url).scheme
86+
scheme = parse_uri(url).scheme
8787
PactBroker.configuration.webhook_scheme_whitelist.any? do | allowed_scheme |
8888
scheme.downcase == allowed_scheme.downcase
8989
end
9090
end
9191

9292
def allowed_webhook_host?(url)
9393
if host_whitelist.any?
94-
PactBroker::Webhooks::CheckHostWhitelist.call(URI(url).host, host_whitelist).any?
94+
PactBroker::Webhooks::CheckHostWhitelist.call(parse_uri(url).host, host_whitelist).any?
9595
else
9696
true
9797
end
@@ -100,6 +100,10 @@ def allowed_webhook_host?(url)
100100
def host_whitelist
101101
PactBroker.configuration.webhook_host_whitelist
102102
end
103+
104+
def parse_uri(uri_string)
105+
URI(uri_string.gsub(/\$\{pactbroker\.[^\}]+\}/, 'placeholder'))
106+
end
103107
end
104108

105109
required(:http_method).filled(:valid_method?, :allowed_webhook_method?)

spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,19 @@ def valid_webhook_with
179179
expect(subject.errors[:"request.url"]).to eq ["is not a valid URL eg. http://example.org"]
180180
end
181181
end
182-
end
183182

183+
context "with a URL that has templated parameters in it" do
184+
let(:json) do
185+
valid_webhook_with do |hash|
186+
hash['request']['url'] = 'https://foo/commits/${pactbroker.consumerVersionNumber}'
187+
end
188+
end
189+
190+
it "is empty" do
191+
expect(subject.errors).to be_empty
192+
end
193+
end
194+
end
184195
end
185196
end
186197
end

0 commit comments

Comments
 (0)