-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrouting_spec.rb
271 lines (225 loc) · 7.92 KB
/
routing_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# frozen_string_literal: true
describe Grape::DSL::Routing do
subject { dummy_class }
let(:dummy_class) do
Class.new do
include Grape::DSL::Routing
end
end
let(:proc) { -> {} }
let(:options) { { a: :b } }
let(:path) { '/dummy' }
describe '.version' do
it 'sets a version for route' do
version = 'v1'
expect(subject).to receive(:namespace_inheritable).with(:version, [version])
expect(subject).to receive(:namespace_inheritable).with(:version_options, { using: :path })
expect(subject.version(version)).to eq(version)
end
end
describe '.prefix' do
it 'sets a prefix for route' do
prefix = '/api'
expect(subject).to receive(:namespace_inheritable).with(:root_prefix, prefix)
subject.prefix prefix
end
end
describe '.scope' do
it 'create a scope without affecting the URL' do
expect(subject).to receive(:within_namespace)
subject.scope {}
end
end
describe '.do_not_route_head!' do
it 'sets do not route head option' do
expect(subject).to receive(:namespace_inheritable).with(:do_not_route_head, true)
subject.do_not_route_head!
end
end
describe '.do_not_route_options!' do
it 'sets do not route options option' do
expect(subject).to receive(:namespace_inheritable).with(:do_not_route_options, true)
subject.do_not_route_options!
end
end
describe '.mount' do
it 'mounts on a nested path' do
subject = Class.new(Grape::API)
app1 = Class.new(Grape::API)
app2 = Class.new(Grape::API)
app2.get '/nice' do
'play'
end
subject.mount app1 => '/app1'
app1.mount app2 => '/app2'
expect(subject.inheritable_setting.to_hash[:namespace]).to eq({})
expect(subject.inheritable_setting.to_hash[:namespace_inheritable]).to eq({})
expect(app1.inheritable_setting.to_hash[:namespace_stackable]).to eq(mount_path: ['/app1'])
expect(app2.inheritable_setting.to_hash[:namespace_stackable]).to eq(mount_path: ['/app1', '/app2'])
end
it 'mounts multiple routes at once' do
base_app = Class.new(Grape::API)
app1 = Class.new(Grape::API)
app2 = Class.new(Grape::API)
base_app.mount(app1 => '/app1', app2 => '/app2')
expect(app1.inheritable_setting.to_hash[:namespace_stackable]).to eq(mount_path: ['/app1'])
expect(app2.inheritable_setting.to_hash[:namespace_stackable]).to eq(mount_path: ['/app2'])
end
end
describe '.route' do
before do
allow(subject).to receive(:endpoints).and_return([])
allow(subject).to receive(:route_end)
allow(subject).to receive(:reset_validations!)
end
it 'marks end of the route' do
expect(subject).to receive(:route_end)
subject.route(:any)
end
it 'resets validations' do
expect(subject).to receive(:reset_validations!)
subject.route(:any)
end
it 'defines a new endpoint' do
expect { subject.route(:any) }
.to change { subject.endpoints.count }.from(0).to(1)
end
it 'does not duplicate identical endpoints' do
subject.route(:any)
expect { subject.route(:any) }
.not_to change(subject.endpoints, :count)
end
it 'generates correct endpoint options' do
allow(subject).to receive(:route_setting).with(:description).and_return(fiz: 'baz')
allow(subject).to receive(:namespace_stackable_with_hash).and_return(nuz: 'naz')
expect(Grape::Endpoint).to receive(:new) do |_inheritable_setting, endpoint_options|
expect(endpoint_options[:method]).to eq :get
expect(endpoint_options[:path]).to eq '/foo'
expect(endpoint_options[:for]).to eq subject
expect(endpoint_options[:route_options]).to eq(foo: 'bar', fiz: 'baz', params: { nuz: 'naz' })
end.and_yield
subject.route(:get, '/foo', { foo: 'bar' }, &proc {})
end
end
describe '.get' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::GET, path, options)
subject.get path, options, &proc
end
end
describe '.post' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::POST, path, options)
subject.post path, options, &proc
end
end
describe '.put' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::PUT, path, options)
subject.put path, options, &proc
end
end
describe '.head' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::HEAD, path, options)
subject.head path, options, &proc
end
end
describe '.delete' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::DELETE, path, options)
subject.delete path, options, &proc
end
end
describe '.options' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::OPTIONS, path, options)
subject.options path, options, &proc
end
end
describe '.patch' do
it 'delegates to .route' do
expect(subject).to receive(:route).with(Rack::PATCH, path, options)
subject.patch path, options, &proc
end
end
describe '.namespace' do
let(:new_namespace) { Object.new }
it 'creates a new namespace with given name and options' do
expect(subject).to receive(:within_namespace).and_yield
expect(subject).to receive(:nest).and_yield
expect(Grape::Namespace).to receive(:new).with(:foo, { foo: 'bar' }).and_return(new_namespace)
expect(subject).to receive(:namespace_stackable).with(:namespace, new_namespace)
subject.namespace :foo, foo: 'bar', &proc {}
end
it 'calls #joined_space_path on Namespace' do
result_of_namspace_stackable = Object.new
allow(subject).to receive(:namespace_stackable).and_return(result_of_namspace_stackable)
expect(Grape::Namespace).to receive(:joined_space_path).with(result_of_namspace_stackable)
subject.namespace
end
end
describe '.group' do
it 'is alias to #namespace' do
expect(subject.method(:group)).to eq subject.method(:namespace)
end
end
describe '.resource' do
it 'is alias to #namespace' do
expect(subject.method(:resource)).to eq subject.method(:namespace)
end
end
describe '.resources' do
it 'is alias to #namespace' do
expect(subject.method(:resources)).to eq subject.method(:namespace)
end
end
describe '.segment' do
it 'is alias to #namespace' do
expect(subject.method(:segment)).to eq subject.method(:namespace)
end
end
describe '.routes' do
let(:routes) { Object.new }
it 'returns value received from #prepare_routes' do
expect(subject).to receive(:prepare_routes).and_return(routes)
expect(subject.routes).to eq routes
end
context 'when #routes was already called once' do
before do
allow(subject).to receive(:prepare_routes).and_return(routes)
subject.routes
end
it 'does not call prepare_routes again' do
expect(subject).not_to receive(:prepare_routes)
expect(subject.routes).to eq routes
end
end
end
describe '.route_param' do
let!(:options) { { requirements: regex } }
let(:regex) { /(.*)/ }
it 'calls #namespace with given params' do
expect(subject).to receive(:namespace).with(':foo', {}).and_yield
subject.route_param('foo', {}, &proc {})
end
it 'nests requirements option under param name' do
expect(subject).to receive(:namespace) do |_param, options|
expect(options[:requirements][:foo]).to eq regex
end
subject.route_param('foo', options, &proc {})
end
it 'does not modify options parameter' do
allow(subject).to receive(:namespace)
expect { subject.route_param('foo', options, &proc {}) }
.not_to(change { options })
end
end
describe '.versions' do
it 'returns last defined version' do
subject.version 'v1'
subject.version 'v2'
expect(subject.version).to eq('v2')
end
end
end