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