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

Handle improper Accept headers #1795

Merged
merged 5 commits into from
Oct 2, 2018
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
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