mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 15:25:35 +08:00
DEV: Add modifier for webhook event header generation (#27054)
This commit is contained in:
parent
a388f41dd4
commit
9264479c27
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user