-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrecognize_path_spec.rb
76 lines (63 loc) · 2.37 KB
/
recognize_path_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
# frozen_string_literal: true
describe Grape::API do
describe '.recognize_path' do
subject { Class.new(described_class) }
it 'fetches endpoint by given path' do
subject.get('/foo/:id') {}
subject.get('/bar/:id') {}
subject.get('/baz/:id') {}
actual = subject.recognize_path('/bar/1234').routes[0].origin
expect(actual).to eq('/bar/:id')
end
it 'returns nil if given path does not match with registered routes' do
subject.get {}
expect(subject.recognize_path('/bar/1234')).to be_nil
end
context 'when parametrized route with type specified together with a static route' do
subject do
Class.new(described_class) do
resource :books do
route_param :id, type: Integer do
get do
end
resource :loans do
route_param :loan_id, type: Integer do
get do
end
end
resource :print do
post do
end
end
end
end
resource :share do
post do
end
end
end
end
end
it 'recognizes the static route when the parameter does not match with the specified type' do
actual = subject.recognize_path('/books/share').routes[0].origin
expect(actual).to eq('/books/share')
end
it 'does not recognize any endpoint when there is not other endpoint that matches with the requested path' do
actual = subject.recognize_path('/books/other')
expect(actual).to be_nil
end
it 'recognizes the parametrized route when the parameter matches with the specified type' do
actual = subject.recognize_path('/books/1').routes[0].origin
expect(actual).to eq('/books/:id')
end
it 'recognizes the static nested route when the parameter does not match with the specified type' do
actual = subject.recognize_path('/books/1/loans/print').routes[0].origin
expect(actual).to eq('/books/:id/loans/print')
end
it 'recognizes the nested parametrized route when the parameter matches with the specified type' do
actual = subject.recognize_path('/books/1/loans/33').routes[0].origin
expect(actual).to eq('/books/:id/loans/:loan_id')
end
end
end
end