From ed0e2210dc3179545b4412632061248f7b1ba083 Mon Sep 17 00:00:00 2001 From: Ben Schmeckpeper Date: Tue, 2 Oct 2018 08:54:35 -0500 Subject: [PATCH] Handle improper Accept headers (#1795) --- CHANGELOG.md | 1 + lib/grape/middleware/versioner/header.rb | 4 ++-- spec/grape/middleware/versioner/header_spec.rb | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef2b8ee1e0..9312324e60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/grape/middleware/versioner/header.rb b/lib/grape/middleware/versioner/header.rb index e07cacb2a1..b2cd806df8 100644 --- a/lib/grape/middleware/versioner/header.rb +++ b/lib/grape/middleware/versioner/header.rb @@ -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) @@ -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 diff --git a/spec/grape/middleware/versioner/header_spec.rb b/spec/grape/middleware/versioner/header_spec.rb index 5befcc6a7f..aaedc757d4 100644 --- a/spec/grape/middleware/versioner/header_spec.rb +++ b/spec/grape/middleware/versioner/header_spec.rb @@ -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']