api-versions 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/Changes.md +3 -0
- data/Gemfile +4 -0
- data/README.md +1 -15
- data/api-versions.gemspec +2 -2
- data/lib/api-versions.rb +2 -2
- data/lib/api-versions/dsl.rb +2 -0
- data/lib/api-versions/middleware.rb +23 -0
- data/lib/api-versions/railtie.rb +7 -0
- data/lib/api-versions/version.rb +1 -1
- data/lib/api-versions/version_check.rb +11 -7
- data/spec/{lib/api_versions_spec.rb → api_versions_spec.rb} +0 -0
- data/spec/dummy/app/controllers/api/v1/bar_controller.rb +3 -0
- data/spec/dummy/app/controllers/api/v2/bar_controller.rb +1 -3
- data/spec/dummy/app/controllers/api/v2/foo_controller.rb +3 -0
- data/spec/dummy/app/controllers/api/v3/foo_controller.rb +1 -1
- data/spec/dummy/app/controllers/errors_controller.rb +5 -0
- data/spec/dummy/config/environments/test.rb +1 -0
- data/spec/dummy/config/routes.rb +2 -0
- data/spec/middleware_spec.rb +24 -0
- data/spec/routing_spec.rb +122 -0
- metadata +21 -17
- data/lib/api-versions/simplify_format.rb +0 -15
- data/spec/controllers/simplified_format_spec.rb +0 -47
- data/spec/routing/routing_spec.rb +0 -121
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daa8e2061260c0cf2b7f4d58d95d9f321ba5796b
|
4
|
+
data.tar.gz: 1de22bfc799fb807ee1e8af14d47ef8707c59a4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e5a9e281b081b71b0caad6c1b71acac5a00ca01040337182e00a3ee68eedf39de6ccb1caecc3b69632772e69f916116c53894995270769de2471f1c5aacf817
|
7
|
+
data.tar.gz: 562696e06899eadf515615ae257357cd3610125559236f9876c1de48824bb85ce026560f5565b4b5492e67c2d1e7ff0f54596197b7ffaa3daf1cd25e957194f5
|
data/.travis.yml
CHANGED
data/Changes.md
ADDED
data/Gemfile
CHANGED
@@ -4,6 +4,10 @@ source "http://rubygems.org"
|
|
4
4
|
gemspec
|
5
5
|
|
6
6
|
case ENV['RAILS_VERSION']
|
7
|
+
when /master/
|
8
|
+
gem "rails", github: "rails/rails"
|
9
|
+
when /4-0-stable/
|
10
|
+
gem "rails", github: "rails/rails", branch: "4-0-stable"
|
7
11
|
when /3-2-stable/
|
8
12
|
gem "rails", github: "rails/rails", branch: "3-2-stable"
|
9
13
|
when /3-1-stable/
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ api-versions is very lightweight. It adds a generator and only one method to the
|
|
26
26
|
## Installation
|
27
27
|
In your Gemfile:
|
28
28
|
|
29
|
-
gem "api-versions", "~>
|
29
|
+
gem "api-versions", "~> 1.0"
|
30
30
|
|
31
31
|
## Versions are specified by header, not by URL
|
32
32
|
A lot of APIs are versioned by changing the URL. `http://test.host/api/v1/some_resource/new` for example.
|
@@ -178,17 +178,3 @@ end
|
|
178
178
|
|
179
179
|
So instead of copying your prior version controllers over to the new ones and duplicating all the code in them, you can redefine specific methods,
|
180
180
|
or start from scratch by removing the inheritance.
|
181
|
-
|
182
|
-
## A word on Rails responders
|
183
|
-
If you use `responds_to` and `responds_with` you'll run into a bit of a problem. Rails doesn't understand the Accept header so you'll get a 406 Unacceptable when you use `respond_to`.
|
184
|
-
To get around this I suggest creating a base API controller and including ApiVersions::SimplifyFormat, which will set the format to the one specified with the + symbol in the Accept header.
|
185
|
-
|
186
|
-
``` ruby
|
187
|
-
class Api::V1::BaseController < ActionController::Base
|
188
|
-
include ApiVersions::SimplifyFormat
|
189
|
-
|
190
|
-
respond_to :json
|
191
|
-
end
|
192
|
-
```
|
193
|
-
|
194
|
-
This will set `request.format` accordingly.
|
data/api-versions.gemspec
CHANGED
@@ -20,8 +20,8 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
s.required_ruby_version = '>= 1.9'
|
22
22
|
|
23
|
-
s.add_dependency
|
24
|
-
s.add_dependency
|
23
|
+
s.add_dependency('actionpack', '>= 3.0')
|
24
|
+
s.add_dependency('activesupport', '>= 3.0')
|
25
25
|
|
26
26
|
s.add_development_dependency "rspec-rails", "~> 2.0"
|
27
27
|
s.add_development_dependency 'ammeter', '0.2.5'
|
data/lib/api-versions.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "api-versions/version"
|
2
2
|
require "api-versions/version_check"
|
3
3
|
require "api-versions/dsl"
|
4
|
-
require
|
5
|
-
|
4
|
+
require 'api-versions/middleware'
|
5
|
+
require 'api-versions/railtie'
|
6
6
|
|
7
7
|
module ApiVersions
|
8
8
|
def api(options = {}, &block)
|
data/lib/api-versions/dsl.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
module ApiVersions
|
2
|
+
class Middleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
accepts = env['HTTP_ACCEPT'].split(',')
|
9
|
+
offset = 0
|
10
|
+
accepts.dup.each_with_index do |accept, i|
|
11
|
+
accept.strip!
|
12
|
+
match = /\Aapplication\/vnd\.#{ApiVersions::VersionCheck.vendor_string}\s*\+\s*(?<format>\w+)\s*/.match(accept)
|
13
|
+
if match
|
14
|
+
accepts.insert i + offset, "application/#{match[:format]}"
|
15
|
+
offset += 1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
env['HTTP_ACCEPT'] = accepts.join(',')
|
20
|
+
@app.call(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/api-versions/version.rb
CHANGED
@@ -9,21 +9,25 @@ module ApiVersions
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def matches?(request)
|
12
|
-
|
12
|
+
accepts = request.headers['Accept'].split(',')
|
13
|
+
accepts.any? do |accept|
|
14
|
+
accept.strip!
|
15
|
+
accepts_proper_format?(accept) && (matches_version?(accept) || unversioned?(accept))
|
16
|
+
end
|
13
17
|
end
|
14
18
|
|
15
19
|
private
|
16
20
|
|
17
|
-
def accepts_proper_format?(
|
18
|
-
|
21
|
+
def accepts_proper_format?(accept_string)
|
22
|
+
accept_string =~ /\Aapplication\/vnd\.#{self.class.vendor_string}\s*\+\s*.+/
|
19
23
|
end
|
20
24
|
|
21
|
-
def matches_version?(
|
22
|
-
|
25
|
+
def matches_version?(accept_string)
|
26
|
+
accept_string =~ /version\s*?=\s*?#{@process_version}\b/
|
23
27
|
end
|
24
28
|
|
25
|
-
def unversioned?(
|
26
|
-
@process_version == self.class.default_version && !(
|
29
|
+
def unversioned?(accept_string)
|
30
|
+
@process_version == self.class.default_version && !(accept_string =~ /version\s*?=\s*?\d*\b/i)
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
File without changes
|
@@ -1,2 +1,2 @@
|
|
1
|
-
class Api::V3::FooController <
|
1
|
+
class Api::V3::FooController < Api::V2::FooController
|
2
2
|
end
|
@@ -7,4 +7,5 @@ Dummy::Application.configure do
|
|
7
7
|
config.action_controller.allow_forgery_protection = false
|
8
8
|
config.active_support.deprecation = :stderr
|
9
9
|
config.secret_key_base = "eb6e66d4263b36e93912a0e0c367059d38c3417058890737c1bed0e52961429817430c38be6acab423cf023fe51c05123727f4dd4782cb2a46966d496d8e951c"
|
10
|
+
config.secret_token = "35c2d7361ce46603d587097392f7397748e94216a275d34f0449fe3db03c36e19e998950317bd2bfc28f1cfa4d106792ff346949ecb8209911995480d4668530"
|
10
11
|
end
|
data/spec/dummy/config/routes.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test'
|
3
|
+
|
4
|
+
describe ApiVersions::Middleware do
|
5
|
+
let(:app) { ->(env) { [200, { "Content-Type" => "text/plain"}, [env["HTTP_ACCEPT"]]] } }
|
6
|
+
|
7
|
+
before do
|
8
|
+
ApiVersions::VersionCheck.vendor_string = "myvendor"
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "the accept header" do
|
12
|
+
it "should not adjust the header" do
|
13
|
+
request = Rack::MockRequest.env_for("/", "HTTP_ACCEPT" => "application/vnd.foobar+json;version=1", lint: true, fatal: true)
|
14
|
+
_, _, response = described_class.new(app).call(request)
|
15
|
+
response.first.should == "application/vnd.foobar+json;version=1"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should adjust the header" do
|
19
|
+
request = Rack::MockRequest.env_for("/", "HTTP_ACCEPT" => "text/plain,application/vnd.myvendor+json;version=1,text/html,application/vnd.myvendor+xml", lint: true, fatal: true)
|
20
|
+
_, _, response = described_class.new(app).call(request)
|
21
|
+
response.last.should == "text/plain,application/json,application/vnd.myvendor+json;version=1,text/html,application/xml,application/vnd.myvendor+xml"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'API Routing' do
|
4
|
+
include RSpec::Rails::RequestExampleGroup
|
5
|
+
|
6
|
+
describe "V1" do
|
7
|
+
it "should not route something from V2" do
|
8
|
+
get new_api_foo_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json;version=1'
|
9
|
+
response.status.should == 404
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should route" do
|
13
|
+
get new_api_bar_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json;version=1'
|
14
|
+
@controller.class.should == Api::V1::BarController
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should default" do
|
18
|
+
get new_api_bar_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json'
|
19
|
+
@controller.class.should == Api::V1::BarController
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should default with nothing after the semi-colon" do
|
23
|
+
get new_api_bar_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json; '
|
24
|
+
@controller.class.should == Api::V1::BarController
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "V2" do
|
29
|
+
it "should copy bar" do
|
30
|
+
get new_api_bar_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json;version=2'
|
31
|
+
@controller.class.should == Api::V2::BarController
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should add foo" do
|
35
|
+
get new_api_foo_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json;version=2'
|
36
|
+
@controller.class.should == Api::V2::FooController
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not default" do
|
40
|
+
get new_api_foo_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json'
|
41
|
+
response.status.should == 404
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should default" do
|
45
|
+
original_version = ApiVersions::VersionCheck.default_version
|
46
|
+
ApiVersions::VersionCheck.default_version = 2
|
47
|
+
get new_api_foo_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json'
|
48
|
+
ApiVersions::VersionCheck.default_version = original_version
|
49
|
+
@controller.class.should == Api::V2::FooController
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "V3" do
|
54
|
+
it "should copy foo" do
|
55
|
+
get new_api_foo_path, nil, 'HTTP_ACCEPT' => 'application/vnd.myvendor+json;version=3'
|
56
|
+
@controller.class.should == Api::V3::FooController
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "Header syntax" do
|
61
|
+
context "when valid" do
|
62
|
+
after(:each) do
|
63
|
+
get new_api_bar_path, nil, 'HTTP_ACCEPT' => @accept_string
|
64
|
+
desired_format = /application\/.*\+\W*(?<format>\w+)\W*/.match(@accept_string)[:format]
|
65
|
+
response.content_type.should == "application/#{desired_format}"
|
66
|
+
response.should be_success
|
67
|
+
end
|
68
|
+
|
69
|
+
context "the semi-colon" do
|
70
|
+
it "should allow spaces after" do
|
71
|
+
@accept_string = 'application/vnd.myvendor+json; version=1'
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should allow spaces before" do
|
75
|
+
@accept_string = 'application/vnd.myvendor + xml ;version=1'
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should allow spaces around" do
|
79
|
+
@accept_string = 'application/vnd.myvendor + xml ; version=1'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "the equal sign" do
|
84
|
+
it "should allow spacing before" do
|
85
|
+
@accept_string = 'application/vnd.myvendor+json; version =1'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should allow spacing after" do
|
89
|
+
@accept_string = 'application/vnd.myvendor+json; version= 1'
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should allow spacing around" do
|
93
|
+
@accept_string = 'application/vnd.myvendor+json; version = 1'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "the plus sign" do
|
98
|
+
it "should allow spacing before" do
|
99
|
+
@accept_string = 'application/vnd.myvendor +xml; version=1'
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should allow spacing after" do
|
103
|
+
@accept_string = 'application/vnd.myvendor+ xml; version=1'
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should allow spacing around" do
|
107
|
+
@accept_string = 'application/vnd.myvendor +xml; version=1'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should not route when invalid" do
|
113
|
+
get new_api_bar_path, nil, 'HTTP_ACCEPT' => 'application/vnd.garbage+xml;version=1'
|
114
|
+
response.status.should == 404
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should not route when no header is specified" do
|
118
|
+
get new_api_bar_path, nil
|
119
|
+
response.status.should == 404
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-versions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erich Menge
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '3'
|
19
|
+
version: '3.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '3'
|
26
|
+
version: '3.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '3'
|
33
|
+
version: '3.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '3'
|
40
|
+
version: '3.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- .gitignore
|
77
77
|
- .rspec
|
78
78
|
- .travis.yml
|
79
|
+
- Changes.md
|
79
80
|
- Gemfile
|
80
81
|
- License.txt
|
81
82
|
- README.md
|
@@ -83,12 +84,13 @@ files:
|
|
83
84
|
- api-versions.gemspec
|
84
85
|
- lib/api-versions.rb
|
85
86
|
- lib/api-versions/dsl.rb
|
86
|
-
- lib/api-versions/
|
87
|
+
- lib/api-versions/middleware.rb
|
88
|
+
- lib/api-versions/railtie.rb
|
87
89
|
- lib/api-versions/version.rb
|
88
90
|
- lib/api-versions/version_check.rb
|
89
91
|
- lib/generators/api_versions/bump_generator.rb
|
90
92
|
- lib/generators/api_versions/templates/controller.rb
|
91
|
-
- spec/
|
93
|
+
- spec/api_versions_spec.rb
|
92
94
|
- spec/dummy/app/controllers/api/v1/bar_controller.rb
|
93
95
|
- spec/dummy/app/controllers/api/v2/bar_controller.rb
|
94
96
|
- spec/dummy/app/controllers/api/v2/foo_controller.rb
|
@@ -96,6 +98,7 @@ files:
|
|
96
98
|
- spec/dummy/app/controllers/api/v3/bar_controller.rb
|
97
99
|
- spec/dummy/app/controllers/api/v3/foo_controller.rb
|
98
100
|
- spec/dummy/app/controllers/api/v3/nests/nested_controller.rb
|
101
|
+
- spec/dummy/app/controllers/errors_controller.rb
|
99
102
|
- spec/dummy/config.ru
|
100
103
|
- spec/dummy/config/application.rb
|
101
104
|
- spec/dummy/config/boot.rb
|
@@ -104,8 +107,8 @@ files:
|
|
104
107
|
- spec/dummy/config/routes.rb
|
105
108
|
- spec/dummy/log/.gitkeep
|
106
109
|
- spec/generators/bump_generator_spec.rb
|
107
|
-
- spec/
|
108
|
-
- spec/
|
110
|
+
- spec/middleware_spec.rb
|
111
|
+
- spec/routing_spec.rb
|
109
112
|
- spec/spec_helper.rb
|
110
113
|
homepage: https://github.com/erichmenge/api-versions
|
111
114
|
licenses: []
|
@@ -131,7 +134,7 @@ signing_key:
|
|
131
134
|
specification_version: 4
|
132
135
|
summary: api-versions helps manage your Rails app API endpoints.
|
133
136
|
test_files:
|
134
|
-
- spec/
|
137
|
+
- spec/api_versions_spec.rb
|
135
138
|
- spec/dummy/app/controllers/api/v1/bar_controller.rb
|
136
139
|
- spec/dummy/app/controllers/api/v2/bar_controller.rb
|
137
140
|
- spec/dummy/app/controllers/api/v2/foo_controller.rb
|
@@ -139,6 +142,7 @@ test_files:
|
|
139
142
|
- spec/dummy/app/controllers/api/v3/bar_controller.rb
|
140
143
|
- spec/dummy/app/controllers/api/v3/foo_controller.rb
|
141
144
|
- spec/dummy/app/controllers/api/v3/nests/nested_controller.rb
|
145
|
+
- spec/dummy/app/controllers/errors_controller.rb
|
142
146
|
- spec/dummy/config.ru
|
143
147
|
- spec/dummy/config/application.rb
|
144
148
|
- spec/dummy/config/boot.rb
|
@@ -147,7 +151,7 @@ test_files:
|
|
147
151
|
- spec/dummy/config/routes.rb
|
148
152
|
- spec/dummy/log/.gitkeep
|
149
153
|
- spec/generators/bump_generator_spec.rb
|
150
|
-
- spec/
|
151
|
-
- spec/
|
154
|
+
- spec/middleware_spec.rb
|
155
|
+
- spec/routing_spec.rb
|
152
156
|
- spec/spec_helper.rb
|
153
157
|
has_rdoc:
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module ApiVersions
|
2
|
-
module SimplifyFormat
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
prepend_before_filter :simplify_format
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def simplify_format
|
12
|
-
request.headers['Accept'] && request.headers['Accept'].match(/\+\s*(\w+)/) { |m| request.format = m[1] }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class SimplifiedFormat < ActionController::Base
|
4
|
-
include ApiVersions::SimplifyFormat
|
5
|
-
|
6
|
-
def index
|
7
|
-
render nothing: true, status: 200
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe SimplifiedFormat do
|
12
|
-
describe "simplify format" do
|
13
|
-
after { request.format.should == 'application/json' }
|
14
|
-
|
15
|
-
it "should set the format" do
|
16
|
-
request.env['Accept'] = 'application/vnd.myvendor+json;version=1'
|
17
|
-
get :index
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should set the format when there is no version specified" do
|
21
|
-
request.env['Accept'] = 'application/vnd.myvendor+json'
|
22
|
-
get :index
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should set the format with spaces" do
|
26
|
-
request.env['Accept'] = 'application/vnd.myvendor + json ; version = 1'
|
27
|
-
get :index
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should set the format with spaces and no version" do
|
31
|
-
request.env['Accept'] = 'application/vnd.myvendor + json'
|
32
|
-
get :index
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "when the header is not set" do
|
37
|
-
it "should be successful" do
|
38
|
-
get :index
|
39
|
-
response.should be_success
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should not change the format" do
|
43
|
-
get :index
|
44
|
-
request.format.should == "text/html"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'API Routing' do
|
4
|
-
def merge_and_stub(path, method, hash)
|
5
|
-
env_hash = Rack::MockRequest.env_for(path, method: method).merge(hash)
|
6
|
-
Rack::MockRequest.stub(:env_for).and_return env_hash
|
7
|
-
end
|
8
|
-
|
9
|
-
describe "V1" do
|
10
|
-
it "should not route something from V2" do
|
11
|
-
merge_and_stub new_api_foo_path, 'get', 'Accept' => 'application/vnd.myvendor+json;version=1'
|
12
|
-
expect(get: new_api_foo_path).to_not be_routable
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should route" do
|
16
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json;version=1'
|
17
|
-
expect(get: new_api_bar_path).to route_to(controller: 'api/v1/bar', action: 'new')
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should default" do
|
21
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json'
|
22
|
-
expect(get: new_api_bar_path).to route_to(controller: 'api/v1/bar', action: 'new')
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should default with nothing after the semi-colon" do
|
26
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json; '
|
27
|
-
expect(get: new_api_bar_path).to route_to(controller: 'api/v1/bar', action: 'new')
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "V2" do
|
32
|
-
it "should copy bar" do
|
33
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json;version=2'
|
34
|
-
expect(get: new_api_bar_path).to route_to(controller: 'api/v2/bar', action: 'new')
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should add foo" do
|
38
|
-
merge_and_stub new_api_foo_path, 'get', 'Accept' => 'application/vnd.myvendor+json;version=2'
|
39
|
-
expect(get: new_api_foo_path).to route_to(controller: 'api/v2/foo', action: 'new')
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should not default" do
|
43
|
-
merge_and_stub new_api_foo_path, 'get', 'Accept' => 'application/vnd.myvendor+json'
|
44
|
-
expect(get: new_api_foo_path).to_not be_routable
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should default" do
|
48
|
-
original_version = ApiVersions::VersionCheck.default_version
|
49
|
-
ApiVersions::VersionCheck.default_version = 2
|
50
|
-
merge_and_stub new_api_foo_path, 'get', 'Accept' => 'application/vnd.myvendor+json'
|
51
|
-
expect(get: new_api_foo_path).to route_to(controller: 'api/v2/foo', action: 'new')
|
52
|
-
ApiVersions::VersionCheck.default_version = original_version
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "V3" do
|
57
|
-
it "should copy foo" do
|
58
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json;version=3'
|
59
|
-
expect(get: new_api_bar_path).to route_to(controller: 'api/v3/bar', action: 'new')
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "Header syntax" do
|
64
|
-
context "when valid" do
|
65
|
-
after(:each) do
|
66
|
-
expect(get: new_api_bar_path).to route_to(controller: 'api/v1/bar', action: 'new')
|
67
|
-
end
|
68
|
-
|
69
|
-
context "the semi-colon" do
|
70
|
-
it "should allow spaces after" do
|
71
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json; version=1'
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should allow spaces before" do
|
75
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor + xml ;version=1'
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should allow spaces around" do
|
79
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor + xml ; version=1'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context "the equal sign" do
|
84
|
-
it "should allow spacing before" do
|
85
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json; version =1'
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should allow spacing after" do
|
89
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json; version= 1'
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should allow spacing around" do
|
93
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+json; version = 1'
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context "the plus sign" do
|
98
|
-
it "should allow spacing before" do
|
99
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor +xml; version=1'
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should allow spacing after" do
|
103
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor+ xml; version=1'
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should allow spacing around" do
|
107
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.myvendor +xml; version=1'
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
it "should not route when invalid" do
|
113
|
-
merge_and_stub new_api_bar_path, 'get', 'Accept' => 'application/vnd.garbage+xml;version=1'
|
114
|
-
expect(get: new_api_bar_path).to_not route_to(controller: 'api/v1/bar', action: 'new')
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should not route when no header is specified" do
|
118
|
-
expect(get: new_api_bar_path).to_not be_routable
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|