Skip to content

Commit

Permalink
fix: correctly handle template parameters in URL when rendering webho…
Browse files Browse the repository at this point in the history
…ok resource
  • Loading branch information
bethesque committed Jun 9, 2018
1 parent 076afe6 commit a4b69db
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/pact_broker/api/contracts/webhook_contract.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'reform'
require 'reform/form'
require 'pact_broker/webhooks/check_host_whitelist'
require 'pact_broker/webhooks/render'

module PactBroker
module Api
Expand Down Expand Up @@ -106,7 +107,7 @@ def host_whitelist
end

def parse_uri(uri_string, placeholder = 'placeholder')
URI(uri_string.gsub(/\$\{pactbroker\.[^\}]+\}/, placeholder))
URI(uri_string.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, placeholder))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/webhook_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def initialize attributes = {}
end

def description
"#{method.upcase} #{URI(url).host}"
"#{method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}"
end

def display_password
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module PactBroker
module Webhooks
class Render

TEMPLATE_PARAMETER_REGEXP = /\$\{pactbroker\.[^\}]+\}/

def self.call(template, pact, verification = nil, &escaper)
base_url = PactBroker.configuration.base_url
params = {
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/pact_broker/domain/webhook_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ module Domain
it "returns a brief description of the HTTP request" do
expect(subject.description).to eq 'POST example.org'
end

context "when the URL has a template parameter in it" do
let(:url) { "http://foo/commits/${pactbroker.consumerVersionNumber}" }

it "doesn't explode" do
expect(subject.description).to eq 'POST foo'
end
end
end

describe "display_password" do
Expand Down
26 changes: 24 additions & 2 deletions spec/lib/pact_broker/webhooks/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ module Webhooks
end

let(:pact) do
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar")
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar", latest_verification: nil)
end

let(:pact_with_no_verification) { pact }

let(:pact_with_successful_verification) do
instance_double("pact",
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
latest_verification: verification)
end

let(:pact_with_failed_verification) do
instance_double("pact",
consumer_version_number: "1.2.3+foo",
consumer_name: "Foo",
provider_name: "Bar",
latest_verification: failed_verification)
end

let(:verification) do
Expand All @@ -21,6 +39,7 @@ module Webhooks
instance_double("verification", provider_version_number: "3", success: false)
end

let(:nil_pact) { nil }
let(:nil_verification) { nil }

subject { Render.call(template, pact, verification) }
Expand All @@ -34,7 +53,10 @@ module Webhooks
["${pactbroker.providerName}", "Bar", :pact, :verification],
["${pactbroker.githubVerificationStatus}", "success", :pact, :verification],
["${pactbroker.githubVerificationStatus}", "failure", :pact, :failed_verification],
["${pactbroker.githubVerificationStatus}", "", :pact, :nil_verification]
["${pactbroker.githubVerificationStatus}", "", :nil_pact, :nil_verification],
["${pactbroker.githubVerificationStatus}", "pending", :pact_with_no_verification, :nil_verification],
["${pactbroker.githubVerificationStatus}", "success", :pact_with_successful_verification, :nil_verification],
["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification]
]

TEST_CASES.each do | (template, expected_output, pact_var_name, verification_var_name) |
Expand Down

0 comments on commit a4b69db

Please sign in to comment.