Skip to content

Commit

Permalink
Merge pull request #1185 from tylerdooling/use-custom-formatters
Browse files Browse the repository at this point in the history
Use formatters for custom vendored content types.
  • Loading branch information
dblock committed Oct 16, 2015
2 parents 9d25e5d + 724cba7 commit 9c8713d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@

#### Fixes

* [#1185](https://github.com/ruby-grape/grape/pull/1185): Use formatters for custom vendored content types - [@tylerdooling](https://github.com/tylerdooling).
* [#1156](https://github.com/ruby-grape/grape/pull/1156): Fixed `no implicit conversion of Symbol into Integer` with nested `values` validation - [@quickpay](https://github.com/quickpay).
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).
* [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest).
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/formatter.rb
Expand Up @@ -167,7 +167,7 @@ def mime_array

accept.scan(accept_into_mime_and_quality)
.sort_by { |_, quality_preference| -quality_preference.to_f }
.map { |mime, _| mime.sub(vendor_prefix_pattern, '') }
.flat_map { |mime, _| [mime, mime.sub(vendor_prefix_pattern, '')] }
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Expand Up @@ -132,6 +132,18 @@ def to_xml
expect(subject.env['api.format']).to eq(:xml)
end

context 'with custom vendored content types' do
before do
subject.options[:content_types] = {}
subject.options[:content_types][:custom] = 'application/vnd.test+json'
end

it 'it uses the custom type' do
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
expect(subject.env['api.format']).to eq(:custom)
end
end

it 'parses headers with symbols as hash keys' do
subject.call('PATH_INFO' => '/info', 'http_accept' => 'application/xml', system_time: '091293')
expect(subject.env[:system_time]).to eq('091293')
Expand All @@ -157,6 +169,16 @@ def to_xml
_, headers, = subject.call('PATH_INFO' => '/info.custom')
expect(headers['Content-type']).to eq('application/x-custom')
end
it 'is set for vendored with registered type' do
subject.options[:content_types] = {}
subject.options[:content_types][:custom] = 'application/vnd.test+json'
_, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
expect(headers['Content-type']).to eq('application/vnd.test+json')
end
it 'is set to closest generic for custom vendored/versioned without registered type' do
_, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
expect(headers['Content-type']).to eq('application/json')
end
end

context 'format' do
Expand Down

0 comments on commit 9c8713d

Please sign in to comment.