Skip to content

Commit

Permalink
Handle improper Accept headers (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschmeck authored and dblock committed Oct 2, 2018
1 parent 35b432c commit ed0e221
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#1795](/~https://github.com/ruby-grape/grape/pull/1795): Fix vendor/subtype parsing of an invalid Accept header - [@bschmeck](/~https://github.com/bschmeck).
* [#1791](/~https://github.com/ruby-grape/grape/pull/1791): Support `summary`, `hidden`, `deprecated`, `is_array`, `nickname`, `produces`, `consumes`, `tags` options in `desc` block - [@darren987469](/~https://github.com/darren987469).

#### Fixes
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/middleware/versioner/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def error_headers
# @return [Boolean] whether the content type sets a vendor
def vendor?(media_type)
_, subtype = Rack::Accept::Header.parse_media_type(media_type)
subtype[HAS_VENDOR_REGEX]
subtype.present? && subtype[HAS_VENDOR_REGEX]
end

def request_vendor(media_type)
Expand All @@ -190,7 +190,7 @@ def request_version(media_type)
# @return [Boolean] whether the content type sets an API version
def version?(media_type)
_, subtype = Rack::Accept::Header.parse_media_type(media_type)
subtype[HAS_VERSION_REGEX]
subtype.present? && subtype[HAS_VERSION_REGEX]
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/grape/middleware/versioner/header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@
expect(subject.call({}).first).to eq(200)
end

it 'succeeds if :strict is set to false and given an invalid header' do
@options[:version_options][:strict] = false
expect(subject.call('HTTP_ACCEPT' => 'yaml').first).to eq(200)
expect(subject.call({}).first).to eq(200)
end

context 'when :strict is set' do
before do
@options[:versions] = ['v1']
Expand Down

0 comments on commit ed0e221

Please sign in to comment.