Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goliath sends symbols in the header hash #168

Merged
merged 2 commits into from
Apr 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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