diff --git a/CHANGELOG.md b/CHANGELOG.md index 01e159abfc..8081249bd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [#2408](https://github.com/ruby-grape/grape/pull/2408): Fix params method redefined warnings - [@ericproulx](https://github.com/ericproulx). * [#2410](https://github.com/ruby-grape/grape/pull/2410): Gem deprecations will raise a DeprecationWarning in specs - [@ericproulx](https://github.com/ericproulx). * [#2389](https://github.com/ruby-grape/grape/pull/2389): Remove rack-accept dependency - [@ericproulx](https://github.com/ericproulx). +* [#2425](https://github.com/ruby-grape/grape/pull/2425): Replace `{}` with `Rack::Header` (for rack 3.0 and greater) or Rack::Utils::HeaderHash` (for rack less than 3.0) in Grape::DSL::Headers - [@dhruvCW](https://github.com/dhruvCW). * Your contribution here. #### Fixes diff --git a/lib/grape/dsl/headers.rb b/lib/grape/dsl/headers.rb index b84c4efe4d..962624774e 100644 --- a/lib/grape/dsl/headers.rb +++ b/lib/grape/dsl/headers.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'grape/util/header' module Grape module DSL module Headers @@ -12,7 +13,7 @@ def header(key = nil, val = nil) if key val ? header[key.to_s] = val : header.delete(key.to_s) else - @header ||= {} + @header ||= Util::Header.new end end alias headers header diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 121b1c4d68..389bf4b63b 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'grape/util/header' + module Grape # An Endpoint is the proxy scope in which all routing # blocks are executed. In other words, any methods @@ -238,7 +240,7 @@ def equals?(e) def run ActiveSupport::Notifications.instrument('endpoint_run.grape', endpoint: self, env: env) do - @header = {} + @header = Grape::Util::Header.new @request = Grape::Request.new(env, build_params_with: namespace_inheritable(:build_params_with)) @params = @request.params @headers = @request.headers diff --git a/lib/grape/request.rb b/lib/grape/request.rb index c907f0b4ab..da020b6ba4 100644 --- a/lib/grape/request.rb +++ b/lib/grape/request.rb @@ -46,14 +46,8 @@ def build_headers end end - if Grape.lowercase_headers? - def transform_header(header) - -header[5..].tr('_', '-').downcase - end - else - def transform_header(header) - -header[5..].split('_').map(&:capitalize).join('-') - end + def transform_header(header) + -header[5..].tr('_', '-').downcase end end end diff --git a/lib/grape/util/header.rb b/lib/grape/util/header.rb new file mode 100644 index 0000000000..b3aee99adb --- /dev/null +++ b/lib/grape/util/header.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Grape + module Util + begin + # For `Rack::Headers` (Rack 3+): + require 'rack/headers' + Header = Rack::Headers + rescue LoadError + # For `Rack::Utils::HeaderHash`: + require 'rack/utils' + Header = Rack::Utils::HeaderHash + end + end +end diff --git a/spec/grape/api/custom_validations_spec.rb b/spec/grape/api/custom_validations_spec.rb index a6b7a629c1..3b01e95baa 100644 --- a/spec/grape/api/custom_validations_spec.rb +++ b/spec/grape/api/custom_validations_spec.rb @@ -169,12 +169,12 @@ def validate(request) end def access_header - Grape.lowercase_headers? ? 'x-access-token' : 'X-Access-Token' + 'x-access-token' end end end let(:app) { Rack::Builder.new(subject) } - let(:x_access_token_header) { Grape.lowercase_headers? ? 'x-access-token' : 'X-Access-Token' } + let(:x_access_token_header) { 'x-access-token' } before do described_class.register_validator('admin', admin_validator) diff --git a/spec/grape/dsl/headers_spec.rb b/spec/grape/dsl/headers_spec.rb index 9ced6304ff..154fbf82b2 100644 --- a/spec/grape/dsl/headers_spec.rb +++ b/spec/grape/dsl/headers_spec.rb @@ -11,8 +11,8 @@ class Dummy subject { HeadersSpec::Dummy.new } let(:header_data) do - { 'First Key' => 'First Value', - 'Second Key' => 'Second Value' } + { 'first key' => 'First Value', + 'second key' => 'Second Value' } end context 'when headers are set' do @@ -23,8 +23,8 @@ class Dummy describe 'get' do it 'returns a specifc value' do - expect(subject.header['First Key']).to eq 'First Value' - expect(subject.header['Second Key']).to eq 'Second Value' + expect(subject.header['first key']).to eq 'First Value' + expect(subject.header['second key']).to eq 'Second Value' end it 'returns all set headers' do @@ -35,15 +35,15 @@ class Dummy describe 'set' do it 'returns value' do - expect(subject.header('Third Key', 'Third Value')) - expect(subject.header['Third Key']).to eq 'Third Value' + expect(subject.header('third key', 'Third Value')) + expect(subject.header['third key']).to eq 'Third Value' end end describe 'delete' do it 'deletes a header key-value pair' do - expect(subject.header('First Key')).to eq header_data['First Key'] - expect(subject.header).not_to have_key('First Key') + expect(subject.header('first key')).to eq header_data['first key'] + expect(subject.header).not_to have_key('first key') end end end @@ -52,8 +52,8 @@ class Dummy context 'when no headers are set' do describe '#header' do it 'returns nil' do - expect(subject.header['First Key']).to be_nil - expect(subject.header('First Key')).to be_nil + expect(subject.header['first key']).to be_nil + expect(subject.header('first key')).to be_nil end end end diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index d8e88aeb10..a1a10d2441 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -146,7 +146,7 @@ def app it 'includes additional request headers' do get '/headers', nil, 'HTTP_X_GRAPE_CLIENT' => '1' - x_grape_client_header = Grape.lowercase_headers? ? 'x-grape-client' : 'X-Grape-Client' + x_grape_client_header = 'x-grape-client' expect(JSON.parse(last_response.body)[x_grape_client_header]).to eq('1') end @@ -154,7 +154,7 @@ def app env = Rack::MockRequest.env_for('/headers') env[:HTTP_SYMBOL_HEADER] = 'Goliath passes symbols' body = read_chunks(subject.call(env)[2]).join - symbol_header = Grape.lowercase_headers? ? 'symbol-header' : 'Symbol-Header' + symbol_header = 'symbol-header' expect(JSON.parse(body)[symbol_header]).to eq('Goliath passes symbols') end end diff --git a/spec/grape/request_spec.rb b/spec/grape/request_spec.rb index b84b6dffdc..eaf9a6ac97 100644 --- a/spec/grape/request_spec.rb +++ b/spec/grape/request_spec.rb @@ -90,7 +90,7 @@ module Grape } end let(:x_grape_is_cool_header) do - Grape.lowercase_headers? ? 'x-grape-is-cool' : 'X-Grape-Is-Cool' + 'x-grape-is-cool' end it 'cuts HTTP_ prefix and capitalizes header name words' do @@ -120,7 +120,7 @@ module Grape default_env.merge(request_headers) end let(:grape_likes_symbolic_header) do - Grape.lowercase_headers? ? 'grape-likes-symbolic' : 'Grape-Likes-Symbolic' + 'grape-likes-symbolic' end it 'converts them to string' do