Skip to content

Commit 10b2c10

Browse files
Failing test for stacked versions w/ catch-all
#1799
1 parent b88a8df commit 10b2c10

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

spec/grape/version_fallback_spec.rb

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
require 'spec_helper'
2+
require 'shared/versioning_examples'
3+
4+
describe Grape::API do
5+
subject { Class.new(Grape::API) }
6+
7+
def app
8+
subject
9+
end
10+
11+
before do
12+
api1 = Class.new(Grape::API)
13+
api1.version ['v3', 'v2', 'v1'], version_options
14+
api1.get('all') { 'v1' }
15+
api1.get('only_v1') { 'only_v1' }
16+
17+
api2 = Class.new(Grape::API)
18+
api2.version ['v3', 'v2'], version_options
19+
api2.get('all') { 'v2' }
20+
api2.get('only_v2') { 'only_v2' }
21+
22+
api3 = Class.new(Grape::API)
23+
api3.version 'v3', version_options
24+
api3.get('all') { 'v3' }
25+
26+
app.mount api3
27+
app.mount api2
28+
app.mount api1
29+
end
30+
31+
shared_examples 'version fallback' do
32+
it 'returns the correct version' do
33+
versioned_get '/all', 'v1', version_options
34+
expect(last_response.status).to eq(200)
35+
expect(last_response.body).to eq('v1')
36+
37+
versioned_get '/all', 'v2', version_options
38+
expect(last_response.status).to eq(200)
39+
expect(last_response.body).to eq('v2')
40+
41+
versioned_get '/all', 'v3', version_options
42+
expect(last_response.status).to eq(200)
43+
expect(last_response.body).to eq('v3')
44+
45+
versioned_get '/only_v1', 'v2', version_options
46+
expect(last_response.status).to eq(200)
47+
expect(last_response.body).to eq('only_v1')
48+
49+
versioned_get '/only_v1', 'v3', version_options
50+
expect(last_response.status).to eq(200)
51+
expect(last_response.body).to eq('only_v1')
52+
53+
versioned_get '/only_v2', 'v3', version_options
54+
expect(last_response.status).to eq(200)
55+
expect(last_response.body).to eq('only_v2')
56+
end
57+
end
58+
59+
context 'with catch-all' do
60+
before do
61+
app.route :any, '*path' do
62+
error!("Unrecognized request path: #{params[:path]} - #{env['PATH_INFO']}#{env['SCRIPT_NAME']}", 404)
63+
end
64+
end
65+
66+
shared_examples 'catch-all' do
67+
it 'returns a 404' do
68+
get '/foobar'
69+
expect(last_response.status).to eq(404)
70+
expect(last_response.body).to eq('Unrecognized request path: foobar - /foobar')
71+
end
72+
end
73+
74+
context 'using path' do
75+
let(:version_options) { { using: :path } }
76+
77+
it_behaves_like 'version fallback'
78+
it_behaves_like 'catch-all'
79+
end
80+
81+
context 'using param' do
82+
let(:version_options) { { using: :param } }
83+
84+
it_behaves_like 'version fallback'
85+
it_behaves_like 'catch-all'
86+
end
87+
88+
context 'using accept_version_header' do
89+
let(:version_options) { { using: :accept_version_header } }
90+
91+
it_behaves_like 'version fallback'
92+
it_behaves_like 'catch-all'
93+
end
94+
95+
context 'using header' do
96+
let(:version_options) { { using: :header, vendor: 'test' } }
97+
98+
it_behaves_like 'version fallback'
99+
it_behaves_like 'catch-all'
100+
end
101+
end
102+
103+
context 'without catch-all' do
104+
context 'using path' do
105+
let(:version_options) { { using: :path } }
106+
107+
it_behaves_like 'version fallback'
108+
end
109+
110+
context 'using param' do
111+
let(:version_options) { { using: :param } }
112+
113+
it_behaves_like 'version fallback'
114+
end
115+
116+
context 'using accept_version_header' do
117+
let(:version_options) { { using: :accept_version_header } }
118+
119+
it_behaves_like 'version fallback'
120+
end
121+
122+
context 'using header' do
123+
let(:version_options) { { using: :header, vendor: 'test' } }
124+
125+
it_behaves_like 'version fallback'
126+
end
127+
end
128+
end

spec/support/versioned_helpers.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def versioned_get(path, version_name, version_options = {})
4646
headers = versioned_headers(version_options.merge(version: version_name))
4747
params = {}
4848
if version_options[:using] == :param
49-
params = { version_options[:parameter] => version_name }
49+
params = { version_options.fetch(:parameter, 'apiver') => version_name }
5050
end
5151
get path, params, headers
5252
end

0 commit comments

Comments
 (0)