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

Accept header strict #144

Merged
merged 3 commits into from
Feb 19, 2012
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
13 changes: 9 additions & 4 deletions lib/grape/middleware/versioner/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ module Versioner
# env['api.version] => 'v1'
# env['api.format] => 'format'
#
# If version does not match this route, then a 404 is throw with
# If version does not match this route, then a 406 is throw with
# X-Cascade header to alert Rack::Mount to attempt the next matched
# route.
class Header < Base
def before
accept = env['HTTP_ACCEPT'] || ""

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

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

env['api.version'] = version
Expand All @@ -48,6 +48,11 @@ def before
end
end
end

protected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note, if ... && options[:versions] is redundant with the if above it. This block is executed within the same if already.

def is_accept_header_valid?(header)
(header.strip =~ /application\/vnd\.(.+?)-(.+?)\+(.+?)/).nil?
end
end
end
end
Expand Down
53 changes: 42 additions & 11 deletions spec/grape/middleware/versioner/header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,46 @@
subject.call({}).first.should == 200
end

it 'should return a 404 when no header is set but strict header based versioning is used' do
@options = {
:versions => ['v1'],
:version_options => {:using => :header, :strict => true}
}
expect {
env = subject.call('HTTP_ACCEPT' => '').last
}.to throw_symbol(:error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found")
context 'when strict header versioning is used' do
it 'should return a 406 when no header' do
@options = {
:versions => ['v1'],
:version_options => {:using => :header, :strict => true}
}
expect {
env = subject.call('HTTP_ACCEPT' => '').last
}.to throw_symbol(
:error,
:status => 406,
:headers => {'X-Cascade' => 'pass'},
:message => "406 API Version Not Found"
)
end

it 'should return a 406 when incorrect header format is used' do
@options = {
:versions => ['v1'],
:version_options => {:using => :header, :strict => true}
}
expect {
env = subject.call('HTTP_ACCEPT' => '*/*').last
}.to throw_symbol(
:error,
:status => 406,
:headers => {'X-Cascade' => 'pass'},
:message => "406 API Version Not Found"
)
end

it 'should return a 200 when proper header is set' do
@options = {
:versions => ['v1'],
:version_options => {:using => :header, :strict => true}
}
subject.call('HTTP_ACCEPT' => 'application/vnd.testing-v1+json').first.should == 200
end
end

end

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

Expand All @@ -108,10 +139,10 @@
}
end

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