diff --git a/app/jobs/regular/emit_web_hook_event.rb b/app/jobs/regular/emit_web_hook_event.rb index ab648e6572c..95522950c09 100644 --- a/app/jobs/regular/emit_web_hook_event.rb +++ b/app/jobs/regular/emit_web_hook_event.rb @@ -45,7 +45,15 @@ module Jobs web_hook_body = build_webhook_body web_hook_event = create_webhook_event(web_hook_body) uri = URI(@web_hook.payload_url.strip) + web_hook_headers = build_webhook_headers(uri, web_hook_body, web_hook_event) + web_hook_headers = + DiscoursePluginRegistry.apply_modifier( + :web_hook_event_headers, + web_hook_headers, + web_hook_body, + web_hook_event, + ) emitter = WebHookEmitter.new(@web_hook, web_hook_event) web_hook_response = emitter.emit!(headers: web_hook_headers, body: web_hook_body) diff --git a/spec/jobs/emit_web_hook_event_spec.rb b/spec/jobs/emit_web_hook_event_spec.rb index 50a5113e415..5bf4b34eff4 100644 --- a/spec/jobs/emit_web_hook_event_spec.rb +++ b/spec/jobs/emit_web_hook_event_spec.rb @@ -345,5 +345,37 @@ RSpec.describe Jobs::EmitWebHookEvent do ) expect(event.payload).to eq(MultiJson.dump(ping: "OK")) end + + context "with `webhook_event_headers` modifier" do + let(:modifier_block) do + Proc.new do |headers, _, _| + headers["D-Test-Woo"] = "xyz" + headers + end + end + it "Allows for header modifications" do + plugin_instance = Plugin::Instance.new + plugin_instance.register_modifier(:web_hook_event_headers, &modifier_block) + + stub_request(:post, post_hook.payload_url).to_return(body: "OK", status: 200) + + topic_event_type = WebHookEventType.all.first + web_hook_id = Fabricate("#{topic_event_type.name.gsub("_created", "")}_web_hook").id + + job.execute( + web_hook_id: web_hook_id, + event_type: topic_event_type.name, + payload: { test: "some payload" }.to_json, + ) + webhook_event = WebHookEvent.last + expect(JSON.parse(webhook_event.headers)).to include("D-Test-Woo" => "xyz") + ensure + DiscoursePluginRegistry.unregister_modifier( + plugin_instance, + :web_hook_event_headers, + &modifier_block + ) + end + end end end