Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/logtail-rack/http_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "json"

require "logtail-rack/util/encoding"

module Logtail
Expand Down Expand Up @@ -26,7 +28,7 @@ def initialize(attributes)
@service_name = attributes[:service_name]

if @headers
@headers_json = Util::Encoding.force_utf8_encoding(@headers).to_json
@headers_json = ::JSON.generate(Util::Encoding.force_utf8_encoding(@headers))
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/logtail-rack/http_response.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "json"

require "logtail-rack/util/encoding"

module Logtail
Expand All @@ -21,7 +23,7 @@ def initialize(attributes)
@duration_ms = attributes[:duration_ms]

if @headers
@headers_json = Util::Encoding.force_utf8_encoding(@headers).to_json
@headers_json = ::JSON.generate(Util::Encoding.force_utf8_encoding(@headers))
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/logtail-rack/http_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@
require "spec_helper"

RSpec.describe Logtail::Integrations::Rack::HTTPRequest do
it "build headers_json when to_json raises, as it does with json 3 and ActiveSupport 8.0 or older" do
# ActiveSupport up to 8.0 encodes a direct #to_json call itself and passes quirks_mode: to JSON.generate,
# which json 3 rejects. JSON.generate calls #to_json with a JSON::State, which ActiveSupport hands to json.
allow_any_instance_of(Hash).to receive(:to_json).and_wrap_original do |to_json, *args|
raise ArgumentError, "unknown keyword: :quirks_mode" unless args.first.is_a?(JSON::State)
to_json.call(*args)
end

http_request = described_class.new(headers: { "Content_Type" => "text/plain", "Accept" => "*/*" })

expect(http_request.headers_json).to eq('{"Content_Type":"text/plain","Accept":"*/*"}')
end
end
12 changes: 12 additions & 0 deletions spec/logtail-rack/http_response_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
require "spec_helper"

RSpec.describe Logtail::Integrations::Rack::HTTPResponse do
it "build headers_json when to_json raises, as it does with json 3 and ActiveSupport 8.0 or older" do
# ActiveSupport up to 8.0 encodes a direct #to_json call itself and passes quirks_mode: to JSON.generate,
# which json 3 rejects. JSON.generate calls #to_json with a JSON::State, which ActiveSupport hands to json.
allow_any_instance_of(Hash).to receive(:to_json).and_wrap_original do |to_json, *args|
raise ArgumentError, "unknown keyword: :quirks_mode" unless args.first.is_a?(JSON::State)
to_json.call(*args)
end

http_response = described_class.new(headers: { "content-type" => "text/plain", "content-length" => "5" }, status: 200)

expect(http_response.headers_json).to eq('{"content-type":"text/plain","content-length":"5"}')
end
end
Loading