Skip to content

Commit

Permalink
Merge pull request #508 from dm1try/content_type_with_params
Browse files Browse the repository at this point in the history
Allow parameters, such as content encoding, in content_type.
  • Loading branch information
dblock committed Nov 12, 2013
2 parents fd18d1c + 05565cd commit 7beb328
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Next Release

#### Fixes

* [#508](https://github.com/intridea/grape/pull/508): Allow parameters, such as content encoding, in `content_type`. - [@dm1try](https://github.com/dm1try).
* [#492](https://github.com/intridea/grape/pull/492): Don't allow to have nil value when a param is required and has a list of allowed values. - [@Antti](https://github.com/Antti)
* [#495](https://github.com/intridea/grape/pull/495): Fix `ParamsScope#params` for parameters nested inside arrays - [@asross](https://github.com/asross).
* [#498](https://github.com/intridea/grape/pull/498): Dry up options and headers logic, allow headers to be passed to OPTIONS requests - [@karlfreeman](https://github.com/karlfreeman).
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def content_type
end

def mime_types
content_types.invert
content_types.each_with_object({}) { |(k, v), types_without_params|
types_without_params[k] = v.split(';').first
}.invert
end

end
Expand Down
21 changes: 21 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,27 @@ def app
last_response.body.should == '{"error":"The requested content-type \'application/xml\' is not supported."}'
end

context 'content type with params' do
before do
subject.format :json
subject.content_type :json, 'application/json; charset=utf-8'

subject.post do
params[:data]
end
post '/', MultiJson.dump(data: { some: 'payload' }), { 'CONTENT_TYPE' => 'application/json' }
end

it "should not response with 406 for same type without params" do
last_response.status.should_not be 406
end

it "should response with given content type in headers" do
last_response.headers['Content-Type'].should eq 'application/json; charset=utf-8'
end

end

context 'precedence' do

before do
Expand Down

0 comments on commit 7beb328

Please sign in to comment.