Skip to content

Commit

Permalink
[FIX #331] Add simultaneous support for both Rack 2 and 3
Browse files Browse the repository at this point in the history
[#318] introduced a [regression](#331) for apps running Rack 2.
Rack 3 follows the http 2 spec and requires lowercase headers.
By using the Rack header constants we get the correct capitalization
regardless of which rack version is loaded.
  • Loading branch information
JoeDupuis committed Aug 22, 2023
1 parent 4f7e392 commit 33f6d58
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/web_console/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def initialize(body, headers)
def inject(content)
# Set content-length header to the size of the current body
# + the extra content. Otherwise the response will be truncated.
if @headers["content-length"]
@headers["content-length"] = (@body.bytesize + content.bytesize).to_s
if @headers[Rack::CONTENT_LENGTH]
@headers[Rack::CONTENT_LENGTH] = (@body.bytesize + content.bytesize).to_s
end

[
Expand Down
4 changes: 2 additions & 2 deletions lib/web_console/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def call(env)
private

def acceptable_content_type?(headers)
headers["content-type"].to_s.include?("html")
headers[Rack::CONTENT_TYPE].to_s.include?("html")
end

def json_response(opts = {})
status = opts.fetch(:status, 200)
headers = { "content-type" => "application/json; charset = utf-8" }
headers = { Rack::CONTENT_TYPE => "application/json; charset = utf-8" }
body = yield.to_json

[ status, headers, [ body ] ]
Expand Down
4 changes: 2 additions & 2 deletions lib/web_console/testing/fake_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Testing
class FakeMiddleware
I18n.load_path.concat(Dir[Helper.gem_root.join("lib/web_console/locales/*.yml")])

DEFAULT_HEADERS = { "content-type" => "application/javascript" }
DEFAULT_HEADERS = { Rack::CONTENT_TYPE => "application/javascript" }

def initialize(opts)
@headers = opts.fetch(:headers, DEFAULT_HEADERS)
Expand All @@ -20,7 +20,7 @@ def initialize(opts)

def call(env)
body = render(req_path(env))
@headers["content-length"] = body.bytesize.to_s
@headers[Rack::CONTENT_LENGTH] = body.bytesize.to_s

[ 200, @headers, [ body ] ]
end
Expand Down
8 changes: 4 additions & 4 deletions test/templates/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
map "/html" do
run WebConsole::Testing::FakeMiddleware.new(
req_path_regex: %r{^/html/(.*)},
headers: {"content-type" => "text/html"},
headers: {Rack::CONTENT_TYPE => "text/html"},
view_path: TEST_ROOT.join("html"),
)
end
Expand All @@ -35,19 +35,19 @@ map "/templates" do
end

map "/mock/repl_sessions/result" do
headers = { 'content-type' => 'application/json' }
headers = { Rack::CONTENT_TYPE => 'application/json' }
body = [ { output: '=> "fake-result"\n', context: [ [ :something, :somewhat, :somewhere ] ] }.to_json ]
run lambda { |env| [ 200, headers, body ] }
end

map "/mock/repl_sessions/error" do
headers = { 'content-type' => 'application/json' }
headers = { Rack::CONTENT_TYPE => 'application/json' }
body = [ { output: 'fake-error-message' }.to_json ]
run lambda { |env| [ 400, headers, body ] }
end

map "/mock/repl_sessions/error.txt" do
headers = { 'content-type' => 'plain/text' }
headers = { Rack::CONTENT_TYPE => 'plain/text' }
body = [ 'error message' ]
run lambda { |env| [ 400, headers, body ] }
end
2 changes: 1 addition & 1 deletion test/web_console/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def status
end

def headers
{ "content-type" => "text/html; charset=utf-8" }
{ Rack::CONTENT_TYPE => "text/html; charset=utf-8" }
end

def body
Expand Down
4 changes: 2 additions & 2 deletions test/web_console/injector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class InjectorTest < ActiveSupport::TestCase

test "updates the content-length header" do
body = [ "foo" ]
headers = { "content-length" => 3 }
headers = { Rack::CONTENT_LENGTH => 3 }

assert_equal [ [ "foobar" ], { "content-length" => "6" } ], Injector.new(body, headers).inject("bar")
assert_equal [ [ "foobar" ], { Rack::CONTENT_LENGTH => "6" } ], Injector.new(body, headers).inject("bar")
end
end
end
6 changes: 3 additions & 3 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def status

def headers
Hash.new.tap do |header_hash|
header_hash["content-type"] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil?
header_hash["content-length"] = @response_content_length unless @response_content_length.nil?
header_hash[Rack::CONTENT_TYPE] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil?
header_hash[Rack::CONTENT_LENGTH] = @response_content_length unless @response_content_length.nil?
end
end
end
Expand Down Expand Up @@ -90,7 +90,7 @@ def headers

get "/", params: nil

assert_equal(response.body.size, response.headers["content-length"].to_i)
assert_equal(response.body.size, response.headers[Rack::CONTENT_LENGTH].to_i)
end

test "it closes original body if rendering console" do
Expand Down

0 comments on commit 33f6d58

Please sign in to comment.