Skip to content

Commit ac87f34

Browse files
committed
Add specs to ensure rendering does not conflict with Rails send_data calls
1 parent 538dd34 commit ac87f34

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

spec/system/dummy/controllers/api/v1/health_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class HealthController < Api::ApplicationController
66
def index
77
render(json: { ok: true })
88
end
9+
10+
def download
11+
send_data("binary data content", filename: "report.txt", type: "text/plain")
12+
end
913
end
1014
end
1115
end
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
# frozen_string_literal: true
22

33
class BaseController
4-
def render(json:)
4+
def render(json: nil, body: nil, **options)
5+
return body if body
6+
57
JSON.parse(JSON.generate(json))
68
end
9+
10+
# Simulates Rails' send_data method which internally calls render
11+
# See: https://api.rubyonrails.org/classes/ActionController/DataStreaming.html
12+
#
13+
# Note: Rails calls render with a Hash (not keyword arguments):
14+
# render options.slice(:status, :content_type).merge(body: data)
15+
#
16+
# We simulate this exactly to catch any issues with Hash vs kwargs handling
17+
def send_data(data, options = {})
18+
render(**options.slice(:status, :content_type).merge(body: data))
19+
end
720
end

spec/system/rendering_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,21 @@
116116
expect(controller.index).to eq(expected_json)
117117
end
118118
end
119+
120+
describe "#download" do
121+
it "returns binary data without serialization" do
122+
expect(controller.download).to eq("binary data content")
123+
end
124+
125+
it "does not call serialize" do
126+
expect(controller).not_to receive(:serialize)
127+
controller.download
128+
end
129+
130+
it "does not call lookup_serializer" do
131+
expect(controller).not_to receive(:lookup_serializer)
132+
controller.download
133+
end
134+
end
119135
end
120136
end

0 commit comments

Comments
 (0)