Skip to content

Commit 2052ccb

Browse files
committed
Added content_type :binary, "application/octet-stream"
Updated request_response_spec test for new content_type Updated CHANGELOG Added tests for application/octet-stream and other untested content-types Updated README for undocumented content-types. Flipped :binary and :txt order in content_types.rb
1 parent 21a5a33 commit 2052ccb

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
============
33

44
* Your contribution here.
5+
* [#745](https://github.com/intridea/grape/pull/745): Added `:binary, application/octet-stream` content-type - [@contributor](https://github.com/akabraham).
56

67
0.9.0 (8/27/2014)
78
=================

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ end
11781178

11791179
## API Formats
11801180

1181-
By default, Grape supports _XML_, _JSON_, and _TXT_ content-types. The default format is `:txt`.
1181+
By default, Grape supports _XML_, _JSON_, _ATOM_, _RSS_, _BINARY_, and _TXT_ content-types. The default format is `:txt`.
11821182

11831183
Serialization takes place automatically. For example, you do not have to call `to_json` in each JSON API implementation.
11841184

@@ -1246,6 +1246,9 @@ Built-in formats are the following.
12461246
* `:xml`: use object's `to_xml` when available, usually via `MultiXml`, otherwise call `to_s`
12471247
* `:txt`: use object's `to_txt` when available, otherwise `to_s`
12481248
* `:serializable_hash`: use object's `serializable_hash` when available, otherwise fallback to `:json`
1249+
* `:atom`
1250+
* `:rss`
1251+
* `:binary`
12491252

12501253
Use `default_format` to set the fallback format when the format could not be determined from the `Accept` header.
12511254
See below for the order for choosing the API format.

lib/grape/util/content_types.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module ContentTypes
88
:jsonapi, 'application/vnd.api+json',
99
:atom, 'application/atom+xml',
1010
:rss, 'application/rss+xml',
11-
:txt, 'text/plain',
11+
:binary, 'application/octet-stream',
12+
:txt, 'text/plain'
1213
]
1314

1415
def self.content_types_for(from_settings)

spec/grape/api_spec.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,25 +745,55 @@ def subject.enable_root_route!
745745
expect(last_response.headers['Content-Type']).to eql 'text/plain'
746746
end
747747

748+
it 'sets content type for xml' do
749+
get '/foo.xml'
750+
expect(last_response.headers['Content-Type']).to eql 'application/xml'
751+
end
752+
748753
it 'sets content type for json' do
749754
get '/foo.json'
750755
expect(last_response.headers['Content-Type']).to eql 'application/json'
751756
end
752757

758+
it 'sets content type for jsonapi format' do
759+
get '/foo.jsonapi'
760+
expect(last_response.headers['Content-Type']).to eql 'application/vnd.api+json'
761+
end
762+
763+
it 'sets content type for serializable hash format' do
764+
get '/foo.serializable_hash'
765+
expect(last_response.headers['Content-Type']).to eql 'application/json'
766+
end
767+
768+
it 'sets content type for atom format' do
769+
get '/foo.atom'
770+
expect(last_response.headers['Content-Type']).to eql 'application/atom+xml'
771+
end
772+
773+
it 'sets content type for rss format' do
774+
get '/foo.rss'
775+
expect(last_response.headers['Content-Type']).to eql 'application/rss+xml'
776+
end
777+
778+
it 'sets content type for binary format' do
779+
get '/foo.binary'
780+
expect(last_response.headers['Content-Type']).to eql 'application/octet-stream'
781+
end
782+
753783
it 'sets content type for error' do
754784
subject.get('/error') { error!('error in plain text', 500) }
755785
get '/error'
756786
expect(last_response.headers['Content-Type']).to eql 'text/plain'
757787
end
758788

759-
it 'sets content type for error' do
789+
it 'sets content type for json error' do
760790
subject.format :json
761791
subject.get('/error') { error!('error in json', 500) }
762792
get '/error.json'
763793
expect(last_response.headers['Content-Type']).to eql 'application/json'
764794
end
765795

766-
it 'sets content type for xml' do
796+
it 'sets content type for xml error' do
767797
subject.format :xml
768798
subject.get('/error') { error!('error in xml', 500) }
769799
get '/error.xml'

spec/grape/dsl/request_response_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def self.imbue(key, value)
9696
jsonapi: "application/vnd.api+json",
9797
atom: "application/atom+xml",
9898
rss: "application/rss+xml",
99-
txt: "text/plain")
99+
txt: "text/plain",
100+
binary: "application/octet-stream")
100101
end
101102
end
102103

0 commit comments

Comments
 (0)