Skip to content

Commit 33f6d58

Browse files
committed
[FIX #331] Add simultaneous support for both Rack 2 and 3
[#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.
1 parent 4f7e392 commit 33f6d58

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

lib/web_console/injector.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def initialize(body, headers)
1515
def inject(content)
1616
# Set content-length header to the size of the current body
1717
# + the extra content. Otherwise the response will be truncated.
18-
if @headers["content-length"]
19-
@headers["content-length"] = (@body.bytesize + content.bytesize).to_s
18+
if @headers[Rack::CONTENT_LENGTH]
19+
@headers[Rack::CONTENT_LENGTH] = (@body.bytesize + content.bytesize).to_s
2020
end
2121

2222
[

lib/web_console/middleware.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ def call(env)
5252
private
5353

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

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

6363
[ status, headers, [ body ] ]

lib/web_console/testing/fake_middleware.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Testing
1010
class FakeMiddleware
1111
I18n.load_path.concat(Dir[Helper.gem_root.join("lib/web_console/locales/*.yml")])
1212

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

1515
def initialize(opts)
1616
@headers = opts.fetch(:headers, DEFAULT_HEADERS)
@@ -20,7 +20,7 @@ def initialize(opts)
2020

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

2525
[ 200, @headers, [ body ] ]
2626
end

test/templates/config.ru

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
map "/html" do
1616
run WebConsole::Testing::FakeMiddleware.new(
1717
req_path_regex: %r{^/html/(.*)},
18-
headers: {"content-type" => "text/html"},
18+
headers: {Rack::CONTENT_TYPE => "text/html"},
1919
view_path: TEST_ROOT.join("html"),
2020
)
2121
end
@@ -35,19 +35,19 @@ map "/templates" do
3535
end
3636

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

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

4949
map "/mock/repl_sessions/error.txt" do
50-
headers = { 'content-type' => 'plain/text' }
50+
headers = { Rack::CONTENT_TYPE => 'plain/text' }
5151
body = [ 'error message' ]
5252
run lambda { |env| [ 400, headers, body ] }
5353
end

test/web_console/helper_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def status
2020
end
2121

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

2626
def body

test/web_console/injector_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class InjectorTest < ActiveSupport::TestCase
3030

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

35-
assert_equal [ [ "foobar" ], { "content-length" => "6" } ], Injector.new(body, headers).inject("bar")
35+
assert_equal [ [ "foobar" ], { Rack::CONTENT_LENGTH => "6" } ], Injector.new(body, headers).inject("bar")
3636
end
3737
end
3838
end

test/web_console/middleware_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def status
3535

3636
def headers
3737
Hash.new.tap do |header_hash|
38-
header_hash["content-type"] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil?
39-
header_hash["content-length"] = @response_content_length unless @response_content_length.nil?
38+
header_hash[Rack::CONTENT_TYPE] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil?
39+
header_hash[Rack::CONTENT_LENGTH] = @response_content_length unless @response_content_length.nil?
4040
end
4141
end
4242
end
@@ -90,7 +90,7 @@ def headers
9090

9191
get "/", params: nil
9292

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

9696
test "it closes original body if rendering console" do

0 commit comments

Comments
 (0)