Skip to content

Commit

Permalink
fix: allow pact broker template parameter in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 9, 2018
1 parent 619c7e9 commit c91d04e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/pact_broker/api/contracts/webhook_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -83,15 +83,15 @@ 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
end

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
Expand All @@ -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?)
Expand Down
13 changes: 12 additions & 1 deletion spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c91d04e

Please sign in to comment.