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
9 changes: 4 additions & 5 deletions lib/apia/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ def body
# @return [Array]
def rack_triplet
# Errors will always be sent as a hash intended for JSON encoding,
# even if the endpoint specifies a plain text response, so only
# send a pain response if the type is plaintext _and_ the body is
# a string
if @type == PLAIN && body.is_a?(String)
Rack.plain_triplet(body, headers: headers, status: status)
# even if the endpoint specifies a non-JSON response, so only send a
# plain response if the type is not JSON _and_ the body is a string.
if @type != JSON && body.is_a?(String)
Rack.response_triplet(body, content_type: @type, headers: headers, status: status)
else
Rack.json_triplet(body, headers: headers, status: status)
end
Expand Down
20 changes: 20 additions & 0 deletions spec/specs/apia/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@
end
end

context 'with an arbitrary response type' do
let(:endpoint) do
Apia::Endpoint.create('ExampleEndpoint') { response_type 'text/vnd.turbo-stream.html' }
end

it 'should provide the configured content-type and return the body as-is' do
response = Apia::Response.new(request, endpoint)
response.body = '<turbo-stream></turbo-stream>'
expect(response.rack_triplet[1]['content-type']).to eq 'text/vnd.turbo-stream.html'
expect(response.rack_triplet[2][0]).to eq '<turbo-stream></turbo-stream>'
end

it 'should fall back to JSON if the body is not a string' do
response = Apia::Response.new(request, endpoint)
response.body = { hello: 'world' }
expect(response.rack_triplet[1]['content-type']).to eq 'application/json'
expect(response.rack_triplet[2][0]).to eq '{"hello":"world"}'
end
end

context 'with a legacy plain text response' do
it 'should return 200 by default' do
endpoint = Apia::Endpoint.create('ExampleEndpoint')
Expand Down