diff --git a/lib/grape.rb b/lib/grape.rb index 680bea819f..f4ec5701fa 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -42,6 +42,10 @@ def self.deprecator @deprecator ||= ActiveSupport::Deprecation.new('2.0', 'Grape') end + def self.rack3? + Gem::Version.new(::Rack.release) >= Gem::Version.new('3') + end + eager_autoload do autoload :API autoload :Endpoint diff --git a/lib/grape/http/headers.rb b/lib/grape/http/headers.rb index 3014ccff0d..e8a368b82c 100644 --- a/lib/grape/http/headers.rb +++ b/lib/grape/http/headers.rb @@ -11,15 +11,7 @@ module Headers REQUEST_METHOD = 'REQUEST_METHOD' QUERY_STRING = 'QUERY_STRING' - if Gem::Version.new(Rack.release) < Gem::Version.new('3') - ALLOW = 'Allow' - CACHE_CONTROL = 'Cache-Control' - CONTENT_LENGTH = 'Content-Length' - CONTENT_TYPE = 'Content-Type' - LOCATION = 'Location' - TRANSFER_ENCODING = 'Transfer-Encoding' - X_CASCADE = 'X-Cascade' - else + if Grape.rack3? ALLOW = 'allow' CACHE_CONTROL = 'cache-control' CONTENT_LENGTH = 'content-length' @@ -27,6 +19,14 @@ module Headers LOCATION = 'location' TRANSFER_ENCODING = 'transfer-encoding' X_CASCADE = 'x-cascade' + else + ALLOW = 'Allow' + CACHE_CONTROL = 'Cache-Control' + CONTENT_LENGTH = 'Content-Length' + CONTENT_TYPE = 'Content-Type' + LOCATION = 'Location' + TRANSFER_ENCODING = 'Transfer-Encoding' + X_CASCADE = 'X-Cascade' end GET = 'GET' diff --git a/lib/grape/request.rb b/lib/grape/request.rb index 669f04f413..f522ea923b 100644 --- a/lib/grape/request.rb +++ b/lib/grape/request.rb @@ -46,12 +46,14 @@ def build_headers end end - def transform_header(header) - if Gem::Version.new(Rack.release) < Gem::Version.new('3') - -header[5..].split('_').map(&:capitalize).join('-') - else + if Grape.rack3? + def transform_header(header) -header[5..].tr('_', '-').downcase end + else + def transform_header(header) + -header[5..].split('_').map(&:capitalize).join('-') + end end end end diff --git a/spec/support/headers_helpers.rb b/spec/support/headers_helpers.rb index 05a8e133ac..c1389cf706 100644 --- a/spec/support/headers_helpers.rb +++ b/spec/support/headers_helpers.rb @@ -4,21 +4,7 @@ module Spec module Support module Helpers def rack_versioned_headers - if Gem::Version.new(Rack.release) < Gem::Version.new('3') - { - cache_control: 'Cache-Control', - content_length: 'Content-Length', - content_type: 'Content-Type', - grape_likes_symbolic: 'Grape-Likes-Symbolic', - location: 'Location', - symbol_header: 'Symbol-Header', - transfer_encoding: 'Transfer-Encoding', - x_access_token: 'X-Access-Token', - x_cascade: 'X-Cascade', - x_grape_client: 'X-Grape-Client', - x_grape_is_cool: 'X-Grape-Is-Cool' - } - else + if Grape.rack3? { cache_control: 'cache-control', content_length: 'content-length', @@ -32,6 +18,21 @@ def rack_versioned_headers x_grape_client: 'x-grape-client', x_grape_is_cool: 'x-grape-is-cool' } + else + { + cache_control: 'Cache-Control', + content_length: 'Content-Length', + content_type: 'Content-Type', + grape_likes_symbolic: 'Grape-Likes-Symbolic', + location: 'Location', + symbol_header: 'Symbol-Header', + transfer_encoding: 'Transfer-Encoding', + x_access_token: 'X-Access-Token', + x_cascade: 'X-Cascade', + x_grape_client: 'X-Grape-Client', + x_grape_is_cool: 'X-Grape-Is-Cool' + } + end end end