From 0caa335ef05b16af821012fa3b0bd4f5a5ac42d3 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 28 Nov 2017 10:59:53 +1100 Subject: [PATCH] FIX: Handle more cases where HTTP status is not correct HTTP status was not correct with send_file which uses streaming --- lib/hijack.rb | 3 ++- spec/components/hijack_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/hijack.rb b/lib/hijack.rb index 78943119e19..3ee50f42305 100644 --- a/lib/hijack.rb +++ b/lib/hijack.rb @@ -39,6 +39,7 @@ module Hijack instance = controller_class.new response = ActionDispatch::Response.new instance.response = response + instance.request = request_copy instance.params = params_copy @@ -48,7 +49,7 @@ module Hijack Rails.logger.warn("Failed to process hijacked response correctly #{e}") end - unless instance.response_body + unless instance.response_body || response.committed? instance.status = 500 end diff --git a/spec/components/hijack_spec.rb b/spec/components/hijack_spec.rb index 7d25f67ea62..feb84ae0c5f 100644 --- a/spec/components/hijack_spec.rb +++ b/spec/components/hijack_spec.rb @@ -59,6 +59,32 @@ describe Hijack do expect(tester.io.string).to include("world") end + it "handles send_file correctly" do + tester.hijack_test do + send_file __FILE__, disposition: nil + end + + expect(tester.io.string).to start_with("HTTP/1.1 200") + end + + it "renders a redirect correctly" do + tester.hijack_test do + redirect_to 'http://awesome.com' + end + + result = "HTTP/1.1 302 Found\r\nLocation: http://awesome.com\r\nContent-Type: text/html\r\nContent-Length: 84\r\nConnection: close\r\n\r\nYou are being redirected." + expect(tester.io.string).to eq(result) + end + + it "renders stuff correctly if is empty" do + tester.hijack_test do + render body: nil + end + + result = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" + expect(tester.io.string).to eq(result) + end + it "renders stuff correctly if it works" do tester.hijack_test do render plain: "hello world"