Skip to content

Commit bb4c77f

Browse files
committed
Merge pull request ruby-grape#168 from netmask/master
Goliath sends symbols in the header hash
2 parents e39a583 + 2fe0688 commit bb4c77f

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/grape/middleware/formatter.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ def default_options
1313
:parsers => {}
1414
}
1515
end
16-
16+
1717
def headers
18-
env.dup.inject({}){|h,(k,v)| h[k.downcase[5..-1]] = v if k.downcase.start_with?('http_'); h}
18+
env.dup.inject({}){|h,(k,v)| h[k.to_s.downcase[5..-1]] = v if k.downcase.to_s.start_with?('http_'); h}
1919
end
20-
20+
2121
def before
2222
fmt = format_from_extension || options[:format] || format_from_header || options[:default_format]
2323
if content_types.key?(fmt)
@@ -39,7 +39,7 @@ def before
3939
throw :error, :status => 406, :message => 'The requested format is not supported.'
4040
end
4141
end
42-
42+
4343
def format_from_extension
4444
parts = request.path.split('.')
4545
extension = parts.last.to_sym
@@ -49,7 +49,7 @@ def format_from_extension
4949
end
5050
nil
5151
end
52-
52+
5353
def format_from_header
5454
mime_array.each do |t|
5555
if mime_types.key?(t)
@@ -58,15 +58,15 @@ def format_from_header
5858
end
5959
nil
6060
end
61-
61+
6262
def mime_array
6363
accept = headers['accept'] or return []
6464

6565
accept.gsub(/\b/,'').scan(%r((\w+/[\w+.-]+)(?:(?:;[^,]*?)?;\s*q=([\d.]+))?)).sort_by { |_, q| -q.to_f }.map {|mime, _|
6666
mime.sub(%r(vnd\.[^+]+\+), '')
6767
}
6868
end
69-
69+
7070
def after
7171
status, headers, bodies = *@app_response
7272
formatter = formatter_for env['api.format']

spec/grape/middleware/formatter_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def to_xml
116116
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml'})
117117
subject.env['api.format'].should == :xml
118118
end
119+
120+
it 'should properly parse headers with symbols as hash keys' do
121+
subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml', :system_time => '091293'})
122+
subject.env[:system_time].should == '091293'
123+
end
119124
end
120125

121126
context 'Content-type' do

0 commit comments

Comments
 (0)