Skip to content

Commit a800ac2

Browse files
committed
feat(webhooks): support template parameters in header values, username and password
1 parent c10a6f2 commit a800ac2

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

lib/pact_broker/string_refinements.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ module PactBroker
22
module StringRefinements
33
refine String do
44
def not_blank?
5-
self && self.strip.size > 0
5+
!blank?
6+
end
7+
8+
def blank?
9+
self.strip.size == 0
610
end
711
end
812
end

lib/pact_broker/webhooks/webhook_request_template.rb

+28-13
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
require 'pact_broker/webhooks/render'
33
require 'cgi'
44
require 'pact_broker/domain/webhook_request'
5+
require 'pact_broker/string_refinements'
56

67
module PactBroker
78
module Webhooks
89
class WebhookRequestTemplate
910

1011
include PactBroker::Logging
1112
include PactBroker::Messages
13+
using PactBroker::StringRefinements
14+
1215
HEADERS_TO_REDACT = [/authorization/i, /token/i]
1316

1417
attr_accessor :method, :url, :headers, :body, :username, :password, :uuid
@@ -31,24 +34,15 @@ def build(template_params)
3134
attributes = {
3235
method: http_method,
3336
url: build_url(template_params),
34-
headers: headers,
35-
username: username,
36-
password: password,
37+
headers: build_headers(template_params),
38+
username: build_string(username, template_params),
39+
password: build_string(password, template_params),
3740
uuid: uuid,
3841
body: build_body(template_params)
3942
}
4043
PactBroker::Domain::WebhookRequest.new(attributes)
4144
end
4245

43-
def build_url(template_params)
44-
URI(PactBroker::Webhooks::Render.call(url, template_params){ | value | CGI::escape(value) if !value.nil? } ).to_s
45-
end
46-
47-
def build_body(template_params)
48-
body_string = String === body ? body : body.to_json
49-
PactBroker::Webhooks::Render.call(body_string, template_params)
50-
end
51-
5246
def description
5347
"#{http_method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}"
5448
end
@@ -68,11 +62,32 @@ def headers= headers
6862
@headers = Rack::Utils::HeaderHash.new(headers)
6963
end
7064

71-
private
7265

7366
def to_s
7467
"#{method.upcase} #{url}, username=#{username}, password=#{display_password}, headers=#{redacted_headers}, body=#{body}"
7568
end
69+
70+
private
71+
72+
def build_url(template_params)
73+
URI(PactBroker::Webhooks::Render.call(url, template_params){ | value | CGI::escape(value) if !value.nil? } ).to_s
74+
end
75+
76+
def build_body(template_params)
77+
body_string = String === body ? body : body.to_json
78+
build_string(body_string, template_params)
79+
end
80+
81+
def build_headers(template_params)
82+
headers.each_with_object(Rack::Utils::HeaderHash.new) do | (key, value), new_headers |
83+
new_headers[key] = build_string(value, template_params)
84+
end
85+
end
86+
87+
def build_string(string, template_params)
88+
return string if string.nil? || string.blank?
89+
PactBroker::Webhooks::Render.call(string, template_params)
90+
end
7691
end
7792
end
7893
end

spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb

+35-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ module Webhooks
77
{
88
method: 'POST',
99
url: url,
10-
username: "foo",
11-
password: "bar",
10+
username: "username",
11+
password: "password",
1212
uuid: "1234",
1313
body: body,
14-
headers: {'Foo' => 'bar'}
14+
headers: {'headername' => 'headervalue'}
1515
}
1616
end
1717

1818
let(:new_attributes) do
1919
{
2020
method: 'POST',
2121
url: built_url,
22-
username: "foo",
23-
password: "bar",
22+
username: "usernameBUILT",
23+
password: "passwordBUILT",
2424
uuid: "1234",
2525
body: built_body,
26-
headers: {'Foo' => 'bar'}
26+
headers: {'headername' => 'headervalueBUILT'}
2727
}
2828
end
2929

@@ -73,10 +73,39 @@ module Webhooks
7373
end
7474
end
7575

76+
it "renders each header value" do
77+
expect(PactBroker::Webhooks::Render).to receive(:call).with('headervalue', params_hash)
78+
subject
79+
end
80+
81+
it "renders the username" do
82+
expect(PactBroker::Webhooks::Render).to receive(:call).with('username', params_hash)
83+
subject
84+
end
85+
86+
it "renders the password" do
87+
expect(PactBroker::Webhooks::Render).to receive(:call).with('password', params_hash)
88+
subject
89+
end
90+
7691
it "creates a new PactBroker::Domain::WebhookRequest" do
7792
expect(PactBroker::Domain::WebhookRequest).to receive(:new).with(new_attributes)
7893
subject
7994
end
95+
96+
context "when optional attributes are missing" do
97+
let(:attributes) do
98+
{
99+
method: 'POST',
100+
url: url,
101+
uuid: "1234",
102+
}
103+
end
104+
105+
it "does not blow up" do
106+
subject
107+
end
108+
end
80109
end
81110
end
82111
end

0 commit comments

Comments
 (0)