Skip to content

Commit

Permalink
Replace Hash with Rack::Header/Rack::Utils::HeaderHash for Grape Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvCW committed Apr 6, 2024
1 parent 5121279 commit 2523a5c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/dsl/headers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'grape/util/header'
module Grape
module DSL
module Headers
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions lib/grape/util/header.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/grape/api/custom_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions spec/grape/dsl/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ 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

it 'includes headers passed as symbols' do
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
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2523a5c

Please sign in to comment.