Skip to content

Commit 1e9def6

Browse files
author
Michael Bleigh
committed
Merge branch 'master' into lazy_compile
2 parents 4a2febb + 12e4478 commit 1e9def6

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

README.markdown

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ request:
8181

8282
curl -H Accept=application/vnd.twitter-v1+json http://localhost:9292/statuses/public_timeline
8383

84+
By default, the first matching version is used when no Accept header is supplied. This behavior is similar to routing in Rails.
85+
To circumvent this default behaviour, one could use the `:strict` option. When this option is set to `true`, a `404 Not found` error is returned when no correct Accept header is supplied.
86+
8487
Serialization takes place automatically. For more detailed usage information, please visit the [Grape Wiki](http://github.com/intridea/grape/wiki).
8588

8689
## Working with Entities
@@ -105,7 +108,7 @@ class API < Grape::API
105108
version 'v1', 'v2'
106109

107110
get '/users/:id' do
108-
present User.find(params[:id]),
111+
present User.find(params[:id]),
109112
:with => Entities::User,
110113
:authenticated => env.key?('api.token')
111114
end
@@ -191,9 +194,9 @@ You can test a Grape API with RSpec. Tests make HTTP requests, therefore they mu
191194

192195
```ruby
193196
RSpec.configure do |config|
194-
config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
197+
config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
195198
:file_path => /spec\/api/
196-
}
199+
}
197200
end
198201
```
199202

@@ -218,10 +221,10 @@ end
218221
Grape exposes arrays of API versions and compiled routes. Each route contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`, `route_path` and `route_params`.
219222

220223
```ruby
221-
class TwitterAPI < Grape::API
224+
class TwitterAPI < Grape::API
222225

223226
version 'v1'
224-
get "version" do
227+
get "version" do
225228
api.version
226229
end
227230

@@ -242,7 +245,7 @@ Grape also supports storing additional parameters with the route information. Th
242245

243246
```ruby
244247
class StringAPI < Grape::API
245-
get "split/:string", { :params => [ "token" ], :optional_params => [ "limit" ] } do
248+
get "split/:string", { :params => [ "token" ], :optional_params => [ "limit" ] } do
246249
params[:string].split(params[:token], (params[:limit] || 0))
247250
end
248251
end

lib/grape/middleware/versioner/header.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ module Versioner
2323
# route.
2424
class Header < Base
2525
def before
26-
accept = env['HTTP_ACCEPT'] || ""
26+
accept = env['HTTP_ACCEPT']
27+
28+
if options[:version_options] && options[:version_options].keys.include?(:strict) && options[:version_options][:strict]
29+
if (accept.nil? || accept.empty?) && options[:versions] && options[:version_options][:using] == :header
30+
throw :error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found"
31+
end
32+
end
2733
accept.strip.scan(/^(.+?)\/(.+?)$/) do |type, subtype|
2834
env['api.type'] = type
2935
env['api.subtype'] = subtype
@@ -42,4 +48,4 @@ def before
4248
end
4349
end
4450
end
45-
end
51+
end

spec/grape/middleware/versioner/header_spec.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@
5050
end
5151
end
5252

53+
context 'no header' do
54+
it 'should return a 200 when no header is set and no strict setting is done' do
55+
@options = {
56+
:versions => ['v1'],
57+
:version_options => {:using => :header}
58+
}
59+
subject.call('HTTP_ACCEPT' => '').first.should == 200
60+
end
61+
62+
it 'should return a 200 when no header is set but strict header based versioning is disabled' do
63+
@options = {
64+
:versions => ['v1'],
65+
:version_options => {:using => :header, :strict => false}
66+
}
67+
subject.call('HTTP_ACCEPT' => '').first.should == 200
68+
end
69+
70+
it 'should return a 404 when no header is set but strict header based versioning is used' do
71+
@options = {
72+
:versions => ['v1'],
73+
:version_options => {:using => :header, :strict => true}
74+
}
75+
expect {
76+
env = subject.call('HTTP_ACCEPT' => '').last
77+
}.to throw_symbol(:error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found")
78+
end
79+
end
80+
5381
context 'no matched version' do
5482
before do
5583
@options = {
@@ -64,4 +92,4 @@
6492
}.to throw_symbol(:error, :status => 404, :headers => {'X-Cascade' => 'pass'}, :message => "404 API Version Not Found")
6593
end
6694
end
67-
end
95+
end

0 commit comments

Comments
 (0)