Skip to content

Commit dc71118

Browse files
author
Marcin Olichwirowicz
committed
changing error response to 406, according to RFC
1 parent 900c22b commit dc71118

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/grape/middleware/versioner/header.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ module Versioner
1818
# env['api.version] => 'v1'
1919
# env['api.format] => 'format'
2020
#
21-
# If version does not match this route, then a 404 is throw with
21+
# If version does not match this route, then a 406 is throw with
2222
# X-Cascade header to alert Rack::Mount to attempt the next matched
2323
# route.
2424
class Header < Base
2525
def before
2626
accept = env['HTTP_ACCEPT'] || ""
2727

2828
if options[:version_options] && options[:version_options].keys.include?(:strict) && options[:version_options][:strict]
29-
if (incorrect_header?(accept)) && options[:version_options][:using] == :header
30-
throw :error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found"
29+
if (is_accept_header_valid?(accept)) && options[:version_options][:using] == :header
30+
throw :error, :status => 406, :headers => {'X-Cascade' => 'pass'}, :message => "406 API Version Not Found"
3131
end
3232
end
3333
accept.strip.scan(/^(.+?)\/(.+?)$/) do |type, subtype|
@@ -39,7 +39,7 @@ def before
3939
is_vendored_match = is_vendored ? options[:version_options][:vendor] == vendor : true
4040

4141
if (options[:versions] && !options[:versions].include?(version)) || !is_vendored_match
42-
throw :error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found"
42+
throw :error, :status => 406, :headers => {'X-Cascade' => 'pass'}, :message => "406 API Version Not Found"
4343
end
4444

4545
env['api.version'] = version
@@ -50,7 +50,7 @@ def before
5050
end
5151

5252
protected
53-
def incorrect_header?(header)
53+
def is_accept_header_valid?(header)
5454
(header.strip =~ /application\/vnd\.(.+?)-(.+?)\+(.+?)/).nil?
5555
end
5656
end

spec/grape/middleware/versioner/header_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
end
7171

7272
context 'when strict header versioning is used' do
73-
it 'should return a 404 when no header' do
73+
it 'should return a 406 when no header' do
7474
@options = {
7575
:versions => ['v1'],
7676
:version_options => {:using => :header, :strict => true}
@@ -79,13 +79,13 @@
7979
env = subject.call('HTTP_ACCEPT' => '').last
8080
}.to throw_symbol(
8181
:error,
82-
:status => 404,
82+
:status => 406,
8383
:headers => {'X-Cascade' => 'pass'},
84-
:message => "404 API Version Not Found"
84+
:message => "406 API Version Not Found"
8585
)
8686
end
8787

88-
it 'should return a 404 when incorrect header format is used' do
88+
it 'should return a 406 when incorrect header format is used' do
8989
@options = {
9090
:versions => ['v1'],
9191
:version_options => {:using => :header, :strict => true}
@@ -94,9 +94,9 @@
9494
env = subject.call('HTTP_ACCEPT' => '*/*').last
9595
}.to throw_symbol(
9696
:error,
97-
:status => 404,
97+
:status => 406,
9898
:headers => {'X-Cascade' => 'pass'},
99-
:message => "404 API Version Not Found"
99+
:message => "406 API Version Not Found"
100100
)
101101
end
102102

@@ -127,7 +127,7 @@
127127
it 'should not match with an incorrect vendor' do
128128
expect {
129129
env = subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor-v1+json').last
130-
}.to throw_symbol(:error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found")
130+
}.to throw_symbol(:error, :status => 406, :headers => {'X-Cascade' => 'pass'}, :message => "406 API Version Not Found")
131131
end
132132
end
133133

@@ -139,10 +139,10 @@
139139
}
140140
end
141141

142-
it 'should throw 404 error with X-Cascade header set to pass' do
142+
it 'should throw 406 error with X-Cascade header set to pass' do
143143
expect {
144144
env = subject.call('HTTP_ACCEPT' => accept).last
145-
}.to throw_symbol(:error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found")
145+
}.to throw_symbol(:error, :status => 406, :headers => {'X-Cascade' => 'pass'}, :message => "406 API Version Not Found")
146146
end
147147
end
148148
end

0 commit comments

Comments
 (0)