Skip to content

Commit

Permalink
Merge pull request #168 from netmask/master
Browse files Browse the repository at this point in the history
Goliath sends symbols in the header hash
  • Loading branch information
dblock committed Apr 25, 2012
2 parents e39a583 + 2fe0688 commit bb4c77f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def default_options
:parsers => {}
}
end

def headers
env.dup.inject({}){|h,(k,v)| h[k.downcase[5..-1]] = v if k.downcase.start_with?('http_'); h}
env.dup.inject({}){|h,(k,v)| h[k.to_s.downcase[5..-1]] = v if k.downcase.to_s.start_with?('http_'); h}
end

def before
fmt = format_from_extension || options[:format] || format_from_header || options[:default_format]
if content_types.key?(fmt)
Expand All @@ -39,7 +39,7 @@ def before
throw :error, :status => 406, :message => 'The requested format is not supported.'
end
end

def format_from_extension
parts = request.path.split('.')
extension = parts.last.to_sym
Expand All @@ -49,7 +49,7 @@ def format_from_extension
end
nil
end

def format_from_header
mime_array.each do |t|
if mime_types.key?(t)
Expand All @@ -58,15 +58,15 @@ def format_from_header
end
nil
end

def mime_array
accept = headers['accept'] or return []

accept.gsub(/\b/,'').scan(%r((\w+/[\w+.-]+)(?:(?:;[^,]*?)?;\s*q=([\d.]+))?)).sort_by { |_, q| -q.to_f }.map {|mime, _|
mime.sub(%r(vnd\.[^+]+\+), '')
}
end

def after
status, headers, bodies = *@app_response
formatter = formatter_for env['api.format']
Expand Down
5 changes: 5 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def to_xml
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml'})
subject.env['api.format'].should == :xml
end

it 'should properly parse headers with symbols as hash keys' do
subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml', :system_time => '091293'})
subject.env[:system_time].should == '091293'
end
end

context 'Content-type' do
Expand Down

0 comments on commit bb4c77f

Please sign in to comment.