Skip to content

Commit ed0e221

Browse files
bschmeckdblock
authored andcommitted
Handle improper Accept headers (#1795)
1 parent 35b432c commit ed0e221

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#1795](https://github.com/ruby-grape/grape/pull/1795): Fix vendor/subtype parsing of an invalid Accept header - [@bschmeck](https://github.com/bschmeck).
67
* [#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).
78

89
#### Fixes

lib/grape/middleware/versioner/header.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def error_headers
173173
# @return [Boolean] whether the content type sets a vendor
174174
def vendor?(media_type)
175175
_, subtype = Rack::Accept::Header.parse_media_type(media_type)
176-
subtype[HAS_VENDOR_REGEX]
176+
subtype.present? && subtype[HAS_VENDOR_REGEX]
177177
end
178178

179179
def request_vendor(media_type)
@@ -190,7 +190,7 @@ def request_version(media_type)
190190
# @return [Boolean] whether the content type sets an API version
191191
def version?(media_type)
192192
_, subtype = Rack::Accept::Header.parse_media_type(media_type)
193-
subtype[HAS_VERSION_REGEX]
193+
subtype.present? && subtype[HAS_VERSION_REGEX]
194194
end
195195
end
196196
end

spec/grape/middleware/versioner/header_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@
160160
expect(subject.call({}).first).to eq(200)
161161
end
162162

163+
it 'succeeds if :strict is set to false and given an invalid header' do
164+
@options[:version_options][:strict] = false
165+
expect(subject.call('HTTP_ACCEPT' => 'yaml').first).to eq(200)
166+
expect(subject.call({}).first).to eq(200)
167+
end
168+
163169
context 'when :strict is set' do
164170
before do
165171
@options[:versions] = ['v1']

0 commit comments

Comments
 (0)