diff --git a/lib/pact_broker/string_refinements.rb b/lib/pact_broker/string_refinements.rb index 677a6ba71..a49e01fb3 100644 --- a/lib/pact_broker/string_refinements.rb +++ b/lib/pact_broker/string_refinements.rb @@ -2,7 +2,11 @@ module PactBroker module StringRefinements refine String do def not_blank? - self && self.strip.size > 0 + !blank? + end + + def blank? + self.strip.size == 0 end end end diff --git a/lib/pact_broker/webhooks/webhook_request_template.rb b/lib/pact_broker/webhooks/webhook_request_template.rb index b9bff503b..68d930bac 100644 --- a/lib/pact_broker/webhooks/webhook_request_template.rb +++ b/lib/pact_broker/webhooks/webhook_request_template.rb @@ -2,6 +2,7 @@ require 'pact_broker/webhooks/render' require 'cgi' require 'pact_broker/domain/webhook_request' +require 'pact_broker/string_refinements' module PactBroker module Webhooks @@ -9,6 +10,8 @@ class WebhookRequestTemplate include PactBroker::Logging include PactBroker::Messages + using PactBroker::StringRefinements + HEADERS_TO_REDACT = [/authorization/i, /token/i] attr_accessor :method, :url, :headers, :body, :username, :password, :uuid @@ -31,24 +34,15 @@ def build(template_params) attributes = { method: http_method, url: build_url(template_params), - headers: headers, - username: username, - password: password, + headers: build_headers(template_params), + username: build_string(username, template_params), + password: build_string(password, template_params), uuid: uuid, body: build_body(template_params) } PactBroker::Domain::WebhookRequest.new(attributes) end - def build_url(template_params) - URI(PactBroker::Webhooks::Render.call(url, template_params){ | value | CGI::escape(value) if !value.nil? } ).to_s - end - - def build_body(template_params) - body_string = String === body ? body : body.to_json - PactBroker::Webhooks::Render.call(body_string, template_params) - end - def description "#{http_method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}" end @@ -68,11 +62,32 @@ def headers= headers @headers = Rack::Utils::HeaderHash.new(headers) end - private def to_s "#{method.upcase} #{url}, username=#{username}, password=#{display_password}, headers=#{redacted_headers}, body=#{body}" end + + private + + def build_url(template_params) + URI(PactBroker::Webhooks::Render.call(url, template_params){ | value | CGI::escape(value) if !value.nil? } ).to_s + end + + def build_body(template_params) + body_string = String === body ? body : body.to_json + build_string(body_string, template_params) + end + + def build_headers(template_params) + headers.each_with_object(Rack::Utils::HeaderHash.new) do | (key, value), new_headers | + new_headers[key] = build_string(value, template_params) + end + end + + def build_string(string, template_params) + return string if string.nil? || string.blank? + PactBroker::Webhooks::Render.call(string, template_params) + end end end end diff --git a/spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb b/spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb index ad62592da..c78fb8f19 100644 --- a/spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb +++ b/spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb @@ -7,11 +7,11 @@ module Webhooks { method: 'POST', url: url, - username: "foo", - password: "bar", + username: "username", + password: "password", uuid: "1234", body: body, - headers: {'Foo' => 'bar'} + headers: {'headername' => 'headervalue'} } end @@ -19,11 +19,11 @@ module Webhooks { method: 'POST', url: built_url, - username: "foo", - password: "bar", + username: "usernameBUILT", + password: "passwordBUILT", uuid: "1234", body: built_body, - headers: {'Foo' => 'bar'} + headers: {'headername' => 'headervalueBUILT'} } end @@ -73,10 +73,39 @@ module Webhooks end end + it "renders each header value" do + expect(PactBroker::Webhooks::Render).to receive(:call).with('headervalue', params_hash) + subject + end + + it "renders the username" do + expect(PactBroker::Webhooks::Render).to receive(:call).with('username', params_hash) + subject + end + + it "renders the password" do + expect(PactBroker::Webhooks::Render).to receive(:call).with('password', params_hash) + subject + end + it "creates a new PactBroker::Domain::WebhookRequest" do expect(PactBroker::Domain::WebhookRequest).to receive(:new).with(new_attributes) subject end + + context "when optional attributes are missing" do + let(:attributes) do + { + method: 'POST', + url: url, + uuid: "1234", + } + end + + it "does not blow up" do + subject + end + end end end end