From 5710aa724f2de6f07219030cfff384360cb77eff Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Wed, 17 Sep 2025 13:16:00 +0200 Subject: [PATCH] Send HTML to campfire instead of JSON --- app/models/webhook/delivery.rb | 10 +++++++++- test/models/webhook/delivery_test.rb | 12 ++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/models/webhook/delivery.rb b/app/models/webhook/delivery.rb index 0108a44a0..fbd3398b0 100644 --- a/app/models/webhook/delivery.rb +++ b/app/models/webhook/delivery.rb @@ -103,7 +103,7 @@ class Webhook::Delivery < ApplicationRecord def headers { "User-Agent" => USER_AGENT, - "Content-Type" => "application/json", + "Content-Type" => content_type, "X-Webhook-Signature" => signature, "X-Webhook-Timestamp" => event.created_at.utc.iso8601 } @@ -113,6 +113,14 @@ class Webhook::Delivery < ApplicationRecord OpenSSL::HMAC.hexdigest("SHA256", webhook.signing_secret, payload) end + def content_type + if webhook.for_campfire? + "text/html" + else + "application/json" + end + end + def payload @payload ||= if webhook.for_basecamp? { line: { content: render_payload(formats: :html) } }.to_json diff --git a/test/models/webhook/delivery_test.rb b/test/models/webhook/delivery_test.rb index 53828c206..80c7af8af 100644 --- a/test/models/webhook/delivery_test.rb +++ b/test/models/webhook/delivery_test.rb @@ -127,7 +127,8 @@ class Webhook::DeliveryTest < ActiveSupport::TestCase request_stub = stub_request(:post, webhook.url) .with do |request| body = JSON.parse(request.body) - body.key?("line") && body["line"].key?("content") && body["line"]["content"].present? + body.key?("line") && body["line"].key?("content") && body["line"]["content"].present? && + request.headers["Content-Type"] == "application/json" end .to_return(status: 200) @@ -148,7 +149,8 @@ class Webhook::DeliveryTest < ActiveSupport::TestCase request_stub = stub_request(:post, webhook.url) .with do |request| - request.body.is_a?(String) && !request.body.start_with?("{") && request.body.present? + request.body.is_a?(String) && !request.body.start_with?("{") && request.body.present? && + request.headers["Content-Type"] == "text/html" end .to_return(status: 200) @@ -170,7 +172,8 @@ class Webhook::DeliveryTest < ActiveSupport::TestCase request_stub = stub_request(:post, webhook.url) .with do |request| body = JSON.parse(request.body) - body.key?("text") && body["text"].present? + body.key?("text") && body["text"].present? && + request.headers["Content-Type"] == "application/json" end .to_return(status: 200) @@ -192,7 +195,8 @@ class Webhook::DeliveryTest < ActiveSupport::TestCase request_stub = stub_request(:post, webhook.url) .with do |request| body = JSON.parse(request.body) - body.present? && !body.key?("line") && !body.key?("text") + body.present? && !body.key?("line") && !body.key?("text") && + request.headers["Content-Type"] == "application/json" end .to_return(status: 200)