-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
routes_inspector_test.rb
256 lines (220 loc) · 7.26 KB
/
routes_inspector_test.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
require 'test_helper'
require 'lotus/routing/routes_inspector'
describe Lotus::Routing::RoutesInspector do
describe '#to_s' do
before do
@path = ::File.expand_path(__FILE__)
end
describe 'named routes with procs' do
before do
@router = Lotus::Router.new do
get '/login', to: ->(env) { }, as: :login
get '/logout', to: Proc.new {|env| }, as: :logout
end
end
it 'inspects routes' do
expectations = [
%( login GET, HEAD /login #<Proc@#{ @path }:13 (lambda)>),
%(logout GET, HEAD /logout #<Proc@#{ @path }:14>)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'controller action syntax' do
before do
@router = Lotus::Router.new do
get '/controller/action', to: 'welcome#index'
end
end
it 'inspects routes' do
expectations = [
%(GET, HEAD /controller/action Welcome::Index)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'lazy controller and action' do
before do
@router = Lotus::Router.new do
get '/lazy', to: 'sleepy#index'
end
module Sleepy
class Index
end
end
end
after do
Object.__send__(:remove_const, :Sleepy)
end
it 'inspects routes' do
expectations = [
%(GET, HEAD /lazy Sleepy::Index)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'missing controller and action' do
before do
@router = Lotus::Router.new do
get '/missing', to: 'missing#index'
end
end
it 'inspects routes' do
expectations = [
%(GET, HEAD /missing Missing::Index)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'class' do
before do
@router = Lotus::Router.new do
get '/class', to: RackMiddleware
end
end
it 'inspects routes' do
expectations = [
%(GET, HEAD /class RackMiddleware)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'object' do
before do
@router = Lotus::Router.new do
get '/class', to: RackMiddlewareInstanceMethod
get '/object', to: RackMiddlewareInstanceMethod.new
end
end
it 'inspects routes' do
expectations = [
%(GET, HEAD /class #<RackMiddlewareInstanceMethod>),
%(GET, HEAD /object #<RackMiddlewareInstanceMethod>)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'resource' do
before do
@router = Lotus::Router.new do
resource 'identity'
end
end
it 'inspects routes' do
expectations = [
%( new_identity GET, HEAD /identity/new Identity::New),
%( identity POST /identity Identity::Create),
%( identity GET, HEAD /identity Identity::Show),
%(edit_identity GET, HEAD /identity/edit Identity::Edit),
%( identity PATCH /identity Identity::Update),
%( identity DELETE /identity Identity::Destroy)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'resources' do
before do
@router = Lotus::Router.new do
resources 'books'
end
end
it 'inspects routes' do
expectations = [
%( books GET, HEAD /books Books::Index),
%( new_books GET, HEAD /books/new Books::New),
%( books POST /books Books::Create),
%( books GET, HEAD /books/:id Books::Show),
%(edit_books GET, HEAD /books/:id/edit Books::Edit),
%( books PATCH /books/:id Books::Update),
%( books DELETE /books/:id Books::Destroy)
]
actual = @router.inspector.to_s
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'with custom formatter' do
before do
@router = Lotus::Router.new do
get '/login', to: ->(env) { }, as: :login
end
end
it 'inspects routes' do
formatter = "| %{methods} | %{name} | %{path} | %{endpoint} |\n"
expectations = [
%(| GET, HEAD | login | /login | #<Proc@#{ @path }:189 (lambda)> |)
]
actual = @router.inspector.to_s(formatter)
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
describe 'nested routes' do
before do
class AdminLotusApp
def call(env)
end
def routes
Lotus::Router.new do
get '/home', to: 'home#index'
end
end
end
inner_router = Lotus::Router.new {
get '/comments', to: 'comments#index'
}
nested_router = Lotus::Router.new {
get '/posts', to: 'posts#index'
mount inner_router, at: '/second_mount'
}
@router = Lotus::Router.new do
get '/fakeroute', to: 'fake#index'
mount AdminLotusApp, at: '/admin'
mount nested_router, at: '/api'
mount RackMiddleware, at: '/class'
mount RackMiddlewareInstanceMethod, at: '/instance_from_class'
mount RackMiddlewareInstanceMethod.new, at: '/instance'
end
end
it 'inspect routes' do
formatter = "| %{methods} | %{name} | %{path} | %{endpoint} |\n"
expectations = [
%(| GET, HEAD | | /fakeroute | Fake::Index |),
%(| GET, HEAD | | /admin/home | Home::Index |),
%(| GET, HEAD | | /api/posts | Posts::Index |),
%(| GET, HEAD | | /api/second_mount/comments | Comments::Index |),
%(| | | /class | RackMiddleware |),
%(| | | /instance_from_class | #<RackMiddlewareInstanceMethod> |),
%(| | | /instance | #<RackMiddlewareInstanceMethod> |)
]
actual = @router.inspector.to_s(formatter)
expectations.each do |expectation|
actual.must_include(expectation)
end
end
end
end
end