From f43537ac70d29b7ef6572c9c16dc717aa3eaa319 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 5 Feb 2020 17:18:33 +1300 Subject: [PATCH] Use `Utils::HeaderHash` to add to existing headers. Fixes #1222. --- lib/rack/runtime.rb | 4 +++- test/spec_runtime.rb | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rack/runtime.rb b/lib/rack/runtime.rb index a0f8ac7fa..d9b2d8ed1 100644 --- a/lib/rack/runtime.rb +++ b/lib/rack/runtime.rb @@ -20,9 +20,11 @@ def initialize(app, name = nil) def call(env) start_time = Utils.clock_time status, headers, body = @app.call(env) + headers = Utils::HeaderHash[headers] + request_time = Utils.clock_time - start_time - unless headers.has_key?(@header_name) + unless headers.key?(@header_name) headers[@header_name] = FORMAT_STRING % request_time end diff --git a/test/spec_runtime.rb b/test/spec_runtime.rb index 10c0c382e..e4fc3f95a 100644 --- a/test/spec_runtime.rb +++ b/test/spec_runtime.rb @@ -11,6 +11,12 @@ def request Rack::MockRequest.env_for end + it "works even if headers is an array" do + app = lambda { |env| [200, [['Content-Type', 'text/plain']], "Hello, World!"] } + response = runtime_app(app).call(request) + response[1]['X-Runtime'].must_match(/[\d\.]+/) + end + it "sets X-Runtime is none is set" do app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, "Hello, World!"] } response = runtime_app(app).call(request)