Skip to content

Commit

Permalink
Lowercase response headers
Browse files Browse the repository at this point in the history
The latest rack 3.0 (unreleased) does not accept uppercase headers.
See rack/rack#1592 for more details.
refs:
- rack/rack#1812
- rails/sprockets#744
  • Loading branch information
patrickm68 committed Jun 9, 2022
1 parent 609eb7f commit 6d5d1a2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions extensions/chrome/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ function initHttpListener() {
function onResponse(details) {
var headers = getHeaders(details);
var sessionId;
if (sessionId = headers['X-Web-Console-Session-Id']) {
if (sessionId = headers['x-web-console-session-id']) {
sessions[details.tabId] = {
sessionId: sessionId,
mountPoint: headers['X-Web-Console-Mount-Point'],
mountPoint: headers['x-web-console-mount-point'],
remoteHost: details.url.match(/([^:]+:\/\/[^\/]+)\/?/)[1]
};
}
Expand Down
6 changes: 3 additions & 3 deletions lib/web_console/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def initialize(body, headers)
end

def inject(content)
# Set Content-Length header to the size of the current body
# 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["content-length"]
@headers["content-length"] = (@body.bytesize + content.bytesize).to_s
end

[
Expand Down
8 changes: 4 additions & 4 deletions lib/web_console/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def call(env)
status, headers, body = call_app(env)

if (session = Session.from(Thread.current)) && acceptable_content_type?(headers)
headers["X-Web-Console-Session-Id"] = session.id
headers["X-Web-Console-Mount-Point"] = mount_point
headers["x-web-console-session-id"] = session.id
headers["x-web-console-mount-point"] = mount_point

template = Template.new(env, session)
body, headers = Injector.new(body, headers).inject(template.render("index"))
Expand All @@ -52,12 +52,12 @@ def call(env)
private

def acceptable_content_type?(headers)
headers["Content-Type"].to_s.include?("html")
headers["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 = { "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/templates/console.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ REPLConsole.request = function request(method, url, params, callback) {
var xhr = new REPLConsole.XMLHttpRequest();

xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("x-requested-with", "XMLHttpRequest");
xhr.send(params);

xhr.onreadystatechange = function() {
Expand Down
2 changes: 1 addition & 1 deletion 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 = { "content-type" => "application/javascript" }

def initialize(opts)
@headers = opts.fetch(:headers, DEFAULT_HEADERS)
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: {"content-type" => "text/html"},
view_path: TEST_ROOT.join("html"),
)
end
Expand All @@ -42,19 +42,19 @@ map "/templates" do
end

map "/mock/repl_sessions/result" do
headers = { 'Content-Type' => 'application/json' }
headers = { '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 = { '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 = { '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" }
{ "content-type" => "text/html; charset=utf-8" }
end

def body
Expand Down
6 changes: 3 additions & 3 deletions test/web_console/injector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class InjectorTest < ActiveSupport::TestCase
assert_equal [ [ "foobar" ], {} ], Injector.new(body, {}).inject("bar")
end

test "updates the Content-Length header" do
test "updates the content-length header" do
body = [ "foo" ]
headers = { "Content-Length" => 3 }
headers = { "content-length" => 3 }

assert_equal [ [ "foobar" ], { "Content-Length" => "6" } ], Injector.new(body, headers).inject("bar")
assert_equal [ [ "foobar" ], { "content-length" => "6" } ], Injector.new(body, headers).inject("bar")
end
end
end
12 changes: 6 additions & 6 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["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?
end
end
end
Expand Down Expand Up @@ -84,13 +84,13 @@ def headers
assert_select "#console"
end

test "sets correct Content-Length header" do
test "sets correct content-length header" do
Thread.current[:__web_console_binding] = binding
@app = Middleware.new(Application.new(response_content_length: 7))

get "/", params: nil

assert_equal(response.body.size, response.headers["Content-Length"].to_i)
assert_equal(response.body.size, response.headers["content-length"].to_i)
end

test "it closes original body if rendering console" do
Expand Down Expand Up @@ -121,12 +121,12 @@ def headers
assert_select "#console", 0
end

test "returns X-Web-Console-Session-Id as response header" do
test "returns x-web-console-session-id as response header" do
Thread.current[:__web_console_binding] = binding

get "/", params: nil

session_id = response.headers["X-Web-Console-Session-Id"]
session_id = response.headers["x-web-console-session-id"]

assert_not Session.find(session_id).nil?
end
Expand Down

0 comments on commit 6d5d1a2

Please sign in to comment.