padrino-core 0.16.0.pre4 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +8 -8
- data/Rakefile +1 -1
- data/bin/padrino +1 -1
- data/lib/padrino-core/application/application_setup.rb +28 -26
- data/lib/padrino-core/application/authenticity_token.rb +3 -2
- data/lib/padrino-core/application/flash.rb +4 -5
- data/lib/padrino-core/application/params_protection.rb +34 -37
- data/lib/padrino-core/application/routing.rb +165 -164
- data/lib/padrino-core/application/show_exceptions.rb +5 -7
- data/lib/padrino-core/application.rb +4 -4
- data/lib/padrino-core/caller.rb +6 -7
- data/lib/padrino-core/cli/adapter.rb +4 -4
- data/lib/padrino-core/cli/base.rb +31 -32
- data/lib/padrino-core/cli/binstub.rb +9 -8
- data/lib/padrino-core/cli/console.rb +1 -1
- data/lib/padrino-core/cli/launcher.rb +45 -42
- data/lib/padrino-core/cli/rake.rb +16 -14
- data/lib/padrino-core/cli/rake_tasks.rb +18 -20
- data/lib/padrino-core/command.rb +1 -1
- data/lib/padrino-core/ext/sinatra.rb +3 -2
- data/lib/padrino-core/filter.rb +3 -3
- data/lib/padrino-core/loader.rb +10 -12
- data/lib/padrino-core/logger.rb +85 -77
- data/lib/padrino-core/mounter/application_extension.rb +2 -2
- data/lib/padrino-core/mounter.rb +33 -34
- data/lib/padrino-core/path_router/compiler.rb +8 -8
- data/lib/padrino-core/path_router/matcher.rb +11 -11
- data/lib/padrino-core/path_router/route.rb +15 -15
- data/lib/padrino-core/path_router.rb +4 -3
- data/lib/padrino-core/reloader/rack.rb +1 -1
- data/lib/padrino-core/reloader/storage.rb +12 -11
- data/lib/padrino-core/reloader.rb +18 -19
- data/lib/padrino-core/router.rb +14 -14
- data/lib/padrino-core/server.rb +20 -24
- data/lib/padrino-core/tasks.rb +0 -1
- data/lib/padrino-core/version.rb +1 -1
- data/lib/padrino-core.rb +9 -10
- data/padrino-core.gemspec +18 -18
- data/test/fixtures/app_gem/app_gem.gemspec +8 -7
- data/test/fixtures/app_gem/lib/app_gem/version.rb +1 -1
- data/test/fixtures/apps/complex.rb +6 -6
- data/test/fixtures/apps/concerned/app.rb +1 -1
- data/test/fixtures/apps/custom_dependencies/custom_dependencies.rb +3 -3
- data/test/fixtures/apps/demo_app.rb +1 -1
- data/test/fixtures/apps/demo_demo.rb +1 -1
- data/test/fixtures/apps/demo_project/api/app.rb +1 -1
- data/test/fixtures/apps/demo_project/app.rb +1 -1
- data/test/fixtures/apps/helpers/class_methods_helpers.rb +1 -0
- data/test/fixtures/apps/helpers/instance_methods_helpers.rb +1 -0
- data/test/fixtures/apps/helpers/system_helpers.rb +0 -1
- data/test/fixtures/apps/lib/myklass/mysubklass.rb +2 -2
- data/test/fixtures/apps/mountable_apps/rack_apps.rb +7 -7
- data/test/fixtures/apps/precompiled_app.rb +6 -5
- data/test/fixtures/apps/simple.rb +5 -5
- data/test/fixtures/apps/static.rb +2 -2
- data/test/fixtures/apps/stealthy/app.rb +1 -1
- data/test/fixtures/apps/stealthy/helpers/stealthy_class_helpers.rb +1 -1
- data/test/fixtures/apps/system.rb +1 -1
- data/test/fixtures/apps/system_class_methods_demo.rb +1 -1
- data/test/fixtures/apps/system_instance_methods_demo.rb +1 -1
- data/test/fixtures/dependencies/a.rb +1 -1
- data/test/fixtures/dependencies/b.rb +1 -1
- data/test/fixtures/dependencies/c.rb +1 -1
- data/test/fixtures/dependencies/circular/e.rb +2 -1
- data/test/fixtures/dependencies/d.rb +1 -1
- data/test/fixtures/dependencies/linear/i.rb +1 -1
- data/test/fixtures/dependencies/nested/l.rb +2 -2
- data/test/fixtures/dependencies/nested/m.rb +1 -1
- data/test/fixtures/dependencies/nested/qqq.rb +2 -2
- data/test/fixtures/dependencies/nested/rrr.rb +1 -1
- data/test/fixtures/dependencies/nested/sss.rb +1 -1
- data/test/fixtures/reloadable_apps/external/app/app.rb +0 -1
- data/test/fixtures/reloadable_apps/external/app/controllers/base.rb +1 -2
- data/test/fixtures/reloadable_apps/main/app.rb +2 -2
- data/test/helper.rb +2 -2
- data/test/test_application.rb +59 -60
- data/test/test_configuration.rb +2 -2
- data/test/test_core.rb +13 -13
- data/test/test_csrf_protection.rb +67 -63
- data/test/test_dependencies.rb +33 -34
- data/test/test_filters.rb +46 -47
- data/test/test_flash.rb +14 -14
- data/test/test_locale.rb +2 -2
- data/test/test_logger.rb +64 -64
- data/test/test_mounter.rb +133 -123
- data/test/test_params_protection.rb +40 -40
- data/test/test_reloader_complex.rb +23 -23
- data/test/test_reloader_external.rb +10 -10
- data/test/test_reloader_simple.rb +26 -23
- data/test/test_reloader_storage.rb +9 -12
- data/test/test_reloader_system.rb +29 -29
- data/test/test_restful_routing.rb +19 -19
- data/test/test_router.rb +126 -145
- data/test/test_routing.rb +882 -894
- metadata +6 -6
data/test/test_routing.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative 'helper'
|
|
2
2
|
|
|
3
3
|
class FooError < RuntimeError; end
|
|
4
4
|
|
|
@@ -6,28 +6,28 @@ class RegexpLookAlike
|
|
|
6
6
|
# RegexpLookAlike#to_s, RegexpLookAlike#names and MatchData#names must be defined.
|
|
7
7
|
class MatchData
|
|
8
8
|
def captures
|
|
9
|
-
[
|
|
9
|
+
%w[this is a test]
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def names
|
|
13
|
-
[
|
|
13
|
+
%w[one two three four]
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def names
|
|
18
|
-
[
|
|
18
|
+
%w[one two three four]
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def to_s
|
|
22
|
-
|
|
22
|
+
'/this/is/a/test/'
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def match(string)
|
|
26
|
-
::RegexpLookAlike::MatchData.new if string ==
|
|
26
|
+
::RegexpLookAlike::MatchData.new if string == '/this/is/a/test/'
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
describe
|
|
30
|
+
describe 'Routing' do
|
|
31
31
|
before do
|
|
32
32
|
Padrino.clear!
|
|
33
33
|
ENV['RACK_BASE_URI'] = nil
|
|
@@ -36,30 +36,30 @@ describe "Routing" do
|
|
|
36
36
|
it 'should serve static files with simple cache control' do
|
|
37
37
|
mock_app do
|
|
38
38
|
set :static_cache_control, :public
|
|
39
|
-
set :public_folder,
|
|
39
|
+
set :public_folder, __dir__
|
|
40
40
|
end
|
|
41
41
|
get "/#{File.basename(__FILE__)}"
|
|
42
|
-
assert headers.
|
|
42
|
+
assert headers.key?('Cache-Control')
|
|
43
43
|
assert_equal headers['Cache-Control'], 'public'
|
|
44
44
|
end # static simple
|
|
45
45
|
|
|
46
46
|
it 'should serve static files with cache control and max_age' do
|
|
47
47
|
mock_app do
|
|
48
|
-
set :static_cache_control, [:public, :must_revalidate, {:
|
|
49
|
-
set :public_folder,
|
|
48
|
+
set :static_cache_control, [:public, :must_revalidate, { max_age: 300 }]
|
|
49
|
+
set :public_folder, __dir__
|
|
50
50
|
end
|
|
51
51
|
get "/#{File.basename(__FILE__)}"
|
|
52
|
-
assert headers.
|
|
52
|
+
assert headers.key?('Cache-Control')
|
|
53
53
|
assert_equal headers['Cache-Control'], 'public, must-revalidate, max-age=300'
|
|
54
54
|
end # static max_age
|
|
55
55
|
|
|
56
56
|
it 'should render static files with custom status via options' do
|
|
57
57
|
mock_app do
|
|
58
58
|
set :static, true
|
|
59
|
-
set :public_folder,
|
|
59
|
+
set :public_folder, __dir__
|
|
60
60
|
|
|
61
61
|
post '/*' do
|
|
62
|
-
static!(:
|
|
62
|
+
static!(status: params[:status])
|
|
63
63
|
end
|
|
64
64
|
end
|
|
65
65
|
|
|
@@ -71,82 +71,82 @@ describe "Routing" do
|
|
|
71
71
|
|
|
72
72
|
it 'should ignore trailing delimiters for basic route' do
|
|
73
73
|
mock_app do
|
|
74
|
-
get(
|
|
75
|
-
get(:test) {
|
|
74
|
+
get('/foo') { 'okey' }
|
|
75
|
+
get(:test) { 'tester' }
|
|
76
76
|
end
|
|
77
|
-
get
|
|
78
|
-
assert_equal
|
|
79
|
-
get
|
|
80
|
-
assert_equal
|
|
81
|
-
get
|
|
82
|
-
assert_equal
|
|
83
|
-
get
|
|
84
|
-
assert_equal
|
|
77
|
+
get '/foo'
|
|
78
|
+
assert_equal 'okey', body
|
|
79
|
+
get '/foo/'
|
|
80
|
+
assert_equal 'okey', body
|
|
81
|
+
get '/test'
|
|
82
|
+
assert_equal 'tester', body
|
|
83
|
+
get '/test/'
|
|
84
|
+
assert_equal 'tester', body
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
it 'should recognize route even if paths are duplicated' do
|
|
88
88
|
mock_app do
|
|
89
89
|
get(:index) {}
|
|
90
|
-
get(:index, :
|
|
91
|
-
get(:index, :
|
|
90
|
+
get(:index, with: :id) {}
|
|
91
|
+
get(:index, with: :id, provides: :json) {}
|
|
92
92
|
end
|
|
93
|
-
assert_equal
|
|
94
|
-
assert_equal
|
|
95
|
-
assert_equal
|
|
93
|
+
assert_equal '/', @app.url_for(:index)
|
|
94
|
+
assert_equal '/1234', @app.url_for(:index, id: '1234')
|
|
95
|
+
assert_equal '/1234.json?baz=baz', @app.url_for(:index, id: '1234', format: 'json', baz: 'baz')
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
it 'should recognize route even if paths are duplicated, in reverse order' do
|
|
99
99
|
mock_app do
|
|
100
|
-
get(:index, :
|
|
101
|
-
get(:index, :
|
|
100
|
+
get(:index, with: :id, provides: :json) {}
|
|
101
|
+
get(:index, with: :id) {}
|
|
102
102
|
get(:index) {}
|
|
103
103
|
end
|
|
104
|
-
assert_equal
|
|
105
|
-
assert_equal
|
|
106
|
-
assert_equal
|
|
104
|
+
assert_equal '/', @app.url_for(:index)
|
|
105
|
+
assert_equal '/1234', @app.url_for(:index, id: '1234')
|
|
106
|
+
assert_equal '/1234.json?baz=baz', @app.url_for(:index, id: '1234', format: 'json', baz: 'baz')
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
it 'should fail with unrecognized route exception when not found' do
|
|
110
110
|
mock_app do
|
|
111
|
-
get(:index){
|
|
111
|
+
get(:index) { 'okey' }
|
|
112
112
|
end
|
|
113
113
|
get @app.url_for(:index)
|
|
114
|
-
assert_equal
|
|
115
|
-
assert_raises(Padrino::Routing::UnrecognizedException)
|
|
114
|
+
assert_equal 'okey', body
|
|
115
|
+
assert_raises(Padrino::Routing::UnrecognizedException) do
|
|
116
116
|
get @app.url_for(:fake)
|
|
117
|
-
|
|
117
|
+
end
|
|
118
118
|
end
|
|
119
119
|
|
|
120
120
|
it 'should fail with unrecognized route exception when namespace is invalid' do
|
|
121
121
|
mock_app do
|
|
122
122
|
controller :foo_bar do
|
|
123
|
-
get(:index){
|
|
124
|
-
get(:test_baz){
|
|
123
|
+
get(:index) { 'okey' }
|
|
124
|
+
get(:test_baz) { 'okey' }
|
|
125
125
|
end
|
|
126
126
|
end
|
|
127
|
-
assert_equal
|
|
128
|
-
assert_raises(Padrino::Routing::UnrecognizedException)
|
|
127
|
+
assert_equal '/foo_bar', @app.url_for(:foo_bar, :index)
|
|
128
|
+
assert_raises(Padrino::Routing::UnrecognizedException) do
|
|
129
129
|
get @app.url_for(:foo, :bar, :index)
|
|
130
|
-
|
|
131
|
-
assert_raises(Padrino::Routing::UnrecognizedException)
|
|
130
|
+
end
|
|
131
|
+
assert_raises(Padrino::Routing::UnrecognizedException) do
|
|
132
132
|
get @app.url_for(:foo, :bar_index)
|
|
133
|
-
|
|
134
|
-
assert_equal
|
|
135
|
-
assert_raises(Padrino::Routing::UnrecognizedException)
|
|
133
|
+
end
|
|
134
|
+
assert_equal '/foo_bar/test_baz', @app.url_for(:foo_bar, :test_baz)
|
|
135
|
+
assert_raises(Padrino::Routing::UnrecognizedException) do
|
|
136
136
|
get @app.url_for(:foo_bar, :test, :baz)
|
|
137
|
-
|
|
138
|
-
assert_raises(Padrino::Routing::UnrecognizedException)
|
|
137
|
+
end
|
|
138
|
+
assert_raises(Padrino::Routing::UnrecognizedException) do
|
|
139
139
|
get @app.url_for(:foo, :bar_test, :baz)
|
|
140
|
-
|
|
141
|
-
assert_raises(Padrino::Routing::UnrecognizedException)
|
|
140
|
+
end
|
|
141
|
+
assert_raises(Padrino::Routing::UnrecognizedException) do
|
|
142
142
|
get @app.url_for(:foo, :bar_test_baz)
|
|
143
|
-
|
|
143
|
+
end
|
|
144
144
|
end
|
|
145
145
|
|
|
146
146
|
it 'should accept regexp routes' do
|
|
147
147
|
mock_app do
|
|
148
|
-
get(%r{/fob|/baz}) {
|
|
149
|
-
get(
|
|
148
|
+
get(%r{/fob|/baz}) { 'regexp' }
|
|
149
|
+
get('/foo') { 'str' }
|
|
150
150
|
get %r{/([0-9]+)/} do |num|
|
|
151
151
|
"Your lucky number: #{num} #{params[:captures].first}"
|
|
152
152
|
end
|
|
@@ -154,66 +154,66 @@ describe "Routing" do
|
|
|
154
154
|
"My lucky number: #{num} #{params[:captures].first}"
|
|
155
155
|
end
|
|
156
156
|
end
|
|
157
|
-
get
|
|
158
|
-
assert_equal
|
|
159
|
-
get
|
|
160
|
-
assert_equal
|
|
161
|
-
get
|
|
162
|
-
assert_equal
|
|
163
|
-
get
|
|
164
|
-
assert_equal
|
|
165
|
-
get
|
|
166
|
-
assert_equal
|
|
157
|
+
get '/foo'
|
|
158
|
+
assert_equal 'str', body
|
|
159
|
+
get '/fob'
|
|
160
|
+
assert_equal 'regexp', body
|
|
161
|
+
get '/baz'
|
|
162
|
+
assert_equal 'regexp', body
|
|
163
|
+
get '/321/'
|
|
164
|
+
assert_equal 'Your lucky number: 321 321', body
|
|
165
|
+
get '/page/99'
|
|
166
|
+
assert_equal 'My lucky number: 99 99', body
|
|
167
167
|
end
|
|
168
168
|
|
|
169
169
|
it 'should ignore trailing slashes' do
|
|
170
170
|
mock_app do
|
|
171
|
-
get(%r{/trailing}) {
|
|
171
|
+
get(%r{/trailing}) { 'slash' }
|
|
172
172
|
end
|
|
173
|
-
get
|
|
174
|
-
assert_equal
|
|
175
|
-
get
|
|
176
|
-
assert_equal
|
|
173
|
+
get '/trailing'
|
|
174
|
+
assert_equal 'slash', body
|
|
175
|
+
get '/trailing/'
|
|
176
|
+
assert_equal 'slash', body
|
|
177
177
|
end
|
|
178
178
|
|
|
179
179
|
it 'should accept regexp routes with generate with :generate_with' do
|
|
180
180
|
mock_app do
|
|
181
|
-
get(%r{/fob|/baz}, :
|
|
181
|
+
get(%r{/fob|/baz}, name: :foo, generate_with: '/fob') { 'regexp' }
|
|
182
182
|
end
|
|
183
|
-
assert_equal
|
|
183
|
+
assert_equal '/fob', @app.url(:foo)
|
|
184
184
|
end
|
|
185
185
|
|
|
186
186
|
it 'should parse routes with question marks' do
|
|
187
187
|
mock_app do
|
|
188
|
-
get(
|
|
189
|
-
post('/unauthenticated/?') {
|
|
188
|
+
get('/foo/?') { 'okey' }
|
|
189
|
+
post('/unauthenticated/?') { 'no access' }
|
|
190
190
|
end
|
|
191
|
-
get
|
|
192
|
-
assert_equal
|
|
193
|
-
get
|
|
194
|
-
assert_equal
|
|
195
|
-
post
|
|
196
|
-
assert_equal
|
|
197
|
-
post
|
|
198
|
-
assert_equal
|
|
191
|
+
get '/foo'
|
|
192
|
+
assert_equal 'okey', body
|
|
193
|
+
get '/foo/'
|
|
194
|
+
assert_equal 'okey', body
|
|
195
|
+
post '/unauthenticated'
|
|
196
|
+
assert_equal 'no access', body
|
|
197
|
+
post '/unauthenticated/'
|
|
198
|
+
assert_equal 'no access', body
|
|
199
199
|
end
|
|
200
200
|
|
|
201
201
|
it 'should parse routes that are encoded' do
|
|
202
202
|
mock_app do
|
|
203
203
|
get('/щч') { 'success!' }
|
|
204
204
|
end
|
|
205
|
-
get(
|
|
205
|
+
get("/#{CGI.escape('щч')}")
|
|
206
206
|
assert_equal 'success!', body
|
|
207
207
|
end
|
|
208
208
|
|
|
209
209
|
it 'should parse routes that include encoded slash' do
|
|
210
210
|
mock_app do
|
|
211
|
-
get('/:drive_alias/:path', :
|
|
211
|
+
get('/:drive_alias/:path', path: /.*/) do
|
|
212
212
|
"Show #{params[:drive_alias]} and #{params[:path]}"
|
|
213
|
-
|
|
213
|
+
end
|
|
214
214
|
end
|
|
215
|
-
get(
|
|
216
|
-
assert_equal
|
|
215
|
+
get('/drive%2Ffoo/some/path')
|
|
216
|
+
assert_equal 'Show drive/foo and some/path', body
|
|
217
217
|
end
|
|
218
218
|
|
|
219
219
|
it 'should encode params using UTF-8' do
|
|
@@ -226,177 +226,177 @@ describe "Routing" do
|
|
|
226
226
|
|
|
227
227
|
it 'should match correctly similar paths' do
|
|
228
228
|
mock_app do
|
|
229
|
-
get(
|
|
230
|
-
get(
|
|
229
|
+
get('/my/:foo_id') { params[:foo_id] }
|
|
230
|
+
get('/my/:bar_id/bar') { params[:bar_id] }
|
|
231
231
|
end
|
|
232
|
-
get
|
|
233
|
-
assert_equal
|
|
234
|
-
get
|
|
235
|
-
assert_equal
|
|
232
|
+
get '/my/1'
|
|
233
|
+
assert_equal '1', body
|
|
234
|
+
get '/my/2/bar'
|
|
235
|
+
assert_equal '2', body
|
|
236
236
|
end
|
|
237
237
|
|
|
238
238
|
it 'should match user agents' do
|
|
239
|
-
|
|
240
|
-
get(
|
|
241
|
-
get(
|
|
239
|
+
mock_app do
|
|
240
|
+
get('/main', agent: /IE/) { 'hello IE' }
|
|
241
|
+
get('/main') { 'hello' }
|
|
242
242
|
end
|
|
243
|
-
get
|
|
244
|
-
assert_equal
|
|
245
|
-
get
|
|
246
|
-
assert_equal
|
|
243
|
+
get '/main'
|
|
244
|
+
assert_equal 'hello', body
|
|
245
|
+
get '/main', {}, { 'HTTP_USER_AGENT' => 'This is IE' }
|
|
246
|
+
assert_equal 'hello IE', body
|
|
247
247
|
end
|
|
248
248
|
|
|
249
249
|
it 'should use regex for parts of a route' do
|
|
250
|
-
|
|
251
|
-
get(
|
|
250
|
+
mock_app do
|
|
251
|
+
get('/main/:id', id: /\d+/) { "hello #{params[:id]}" }
|
|
252
252
|
end
|
|
253
|
-
get
|
|
254
|
-
assert_equal
|
|
255
|
-
get
|
|
253
|
+
get '/main/123'
|
|
254
|
+
assert_equal 'hello 123', body
|
|
255
|
+
get '/main/asd'
|
|
256
256
|
assert_equal 404, status
|
|
257
257
|
end
|
|
258
258
|
|
|
259
259
|
it 'should parse params when use regex for parts of a route' do
|
|
260
260
|
mock_app do
|
|
261
|
-
post :index, :
|
|
261
|
+
post :index, with: %i[foo bar], bar: /.+/ do
|
|
262
262
|
"show #{params[:foo]}"
|
|
263
263
|
end
|
|
264
264
|
|
|
265
|
-
get :index, :
|
|
265
|
+
get :index, map: '/mystuff/:a_id/boing/:boing_id' do
|
|
266
266
|
"show #{params[:a_id]} and #{params[:boing_id]}"
|
|
267
267
|
end
|
|
268
268
|
end
|
|
269
|
-
get
|
|
270
|
-
assert_equal
|
|
269
|
+
get '/mystuff/5/boing/2'
|
|
270
|
+
assert_equal 'show 5 and 2', body
|
|
271
271
|
end
|
|
272
272
|
|
|
273
273
|
it 'should not generate overlapping head urls' do
|
|
274
274
|
app = mock_app do
|
|
275
|
-
get(
|
|
276
|
-
post(
|
|
275
|
+
get('/main') { 'hello' }
|
|
276
|
+
post('/main') { 'hello' }
|
|
277
277
|
end
|
|
278
|
-
assert_equal 3, app.routes.size,
|
|
279
|
-
assert_equal
|
|
280
|
-
assert_equal
|
|
281
|
-
assert_equal
|
|
278
|
+
assert_equal 3, app.routes.size, 'should generate GET, HEAD and PUT'
|
|
279
|
+
assert_equal 'GET', app.routes[0].request_methods.first
|
|
280
|
+
assert_equal 'HEAD', app.routes[1].request_methods.first
|
|
281
|
+
assert_equal 'POST', app.routes[2].request_methods.first
|
|
282
282
|
end
|
|
283
283
|
|
|
284
284
|
it 'should generate basic urls' do
|
|
285
285
|
mock_app do
|
|
286
|
-
get(:foo){
|
|
287
|
-
get(:foo, :
|
|
288
|
-
get([
|
|
289
|
-
get(:hash, :
|
|
290
|
-
get(:anchor) { url(:anchor, :
|
|
291
|
-
get(:fragment) { url(:anchor, :
|
|
292
|
-
get(:fragment2) { url(:anchor, :
|
|
293
|
-
get(:gangsta) { url(:gangsta, :
|
|
294
|
-
get([
|
|
295
|
-
get(:array, :
|
|
296
|
-
get([
|
|
297
|
-
get(:hash_with_extra, :
|
|
298
|
-
get([
|
|
299
|
-
get(:array_with_extra, :
|
|
300
|
-
get([
|
|
301
|
-
get(
|
|
302
|
-
post(:mix, :
|
|
303
|
-
get(:mix, :
|
|
304
|
-
get(:foo, '', :
|
|
305
|
-
post(:foo, '', :
|
|
306
|
-
delete(:drugs, :
|
|
307
|
-
delete(:drugs, '', :
|
|
308
|
-
get(:splatter,
|
|
309
|
-
end
|
|
310
|
-
get
|
|
311
|
-
assert_equal
|
|
312
|
-
get
|
|
313
|
-
assert_equal
|
|
314
|
-
get
|
|
315
|
-
assert_equal
|
|
316
|
-
get
|
|
317
|
-
assert_equal
|
|
318
|
-
get
|
|
319
|
-
assert_equal
|
|
320
|
-
get
|
|
321
|
-
assert_equal
|
|
322
|
-
get
|
|
323
|
-
assert_equal
|
|
324
|
-
get
|
|
325
|
-
assert_equal
|
|
326
|
-
get
|
|
327
|
-
assert_equal
|
|
328
|
-
get
|
|
329
|
-
assert_equal
|
|
330
|
-
post
|
|
331
|
-
assert_equal
|
|
332
|
-
get
|
|
333
|
-
assert_equal
|
|
334
|
-
get
|
|
335
|
-
assert_equal
|
|
336
|
-
post
|
|
337
|
-
assert_equal
|
|
338
|
-
delete
|
|
339
|
-
assert_equal
|
|
340
|
-
delete
|
|
341
|
-
assert_equal
|
|
342
|
-
get
|
|
343
|
-
assert_equal
|
|
344
|
-
get
|
|
345
|
-
assert_equal
|
|
286
|
+
get(:foo) { '/foo' }
|
|
287
|
+
get(:foo, with: :id) { |id| "/foo/#{id}" }
|
|
288
|
+
get(%i[foo id]) { |id| "/foo/#{id}" }
|
|
289
|
+
get(:hash, with: :id) { url(:hash, id: 1) }
|
|
290
|
+
get(:anchor) { url(:anchor, anchor: 'comments') }
|
|
291
|
+
get(:fragment) { url(:anchor, fragment: 'comments') }
|
|
292
|
+
get(:fragment2) { url(:anchor, fragment: :comments) }
|
|
293
|
+
get(:gangsta) { url(:gangsta, foo: { bar: :baz }, hoge: :fuga) }
|
|
294
|
+
get(%i[hash id]) { url(:hash, id: 1) }
|
|
295
|
+
get(:array, with: :id) { url(:array, 23) }
|
|
296
|
+
get(%i[array id]) { url(:array, 23) }
|
|
297
|
+
get(:hash_with_extra, with: :id) { url(:hash_with_extra, id: 1, query: 'string') }
|
|
298
|
+
get(%i[hash_with_extra id]) { url(:hash_with_extra, id: 1, query: 'string') }
|
|
299
|
+
get(:array_with_extra, with: :id) { url(:array_with_extra, 23, query: 'string') }
|
|
300
|
+
get(%i[array_with_extra id]) { url(:array_with_extra, 23, query: 'string') }
|
|
301
|
+
get('/old-bar/:id') { params[:id] }
|
|
302
|
+
post(:mix, map: '/mix-bar/:id') { params[:id] }
|
|
303
|
+
get(:mix, map: '/mix-bar/:id') { params[:id] }
|
|
304
|
+
get(:foo, '', with: :id) { |id| "/#{id}" }
|
|
305
|
+
post(:foo, '', with: :id) { |id| "/#{id}" }
|
|
306
|
+
delete(:drugs, with: [:id, 'destroy']) { |id| "/drugs/#{id}/destroy" }
|
|
307
|
+
delete(:drugs, '', with: [:id, 'destroy']) { |id| "/#{id}/destroy" }
|
|
308
|
+
get(:splatter, '/splatter/*/*') { |_a, _b| url(:splatter, splat: %w[123 456]) }
|
|
309
|
+
end
|
|
310
|
+
get '/foo'
|
|
311
|
+
assert_equal '/foo', body
|
|
312
|
+
get '/foo/123'
|
|
313
|
+
assert_equal '/foo/123', body
|
|
314
|
+
get '/hash/2'
|
|
315
|
+
assert_equal '/hash/1', body
|
|
316
|
+
get '/anchor'
|
|
317
|
+
assert_equal '/anchor#comments', body
|
|
318
|
+
get '/fragment'
|
|
319
|
+
assert_equal '/anchor#comments', body
|
|
320
|
+
get '/fragment2'
|
|
321
|
+
assert_equal '/anchor#comments', body
|
|
322
|
+
get '/array/23'
|
|
323
|
+
assert_equal '/array/23', body
|
|
324
|
+
get '/hash_with_extra/1'
|
|
325
|
+
assert_equal '/hash_with_extra/1?query=string', body
|
|
326
|
+
get '/array_with_extra/23'
|
|
327
|
+
assert_equal '/array_with_extra/23?query=string', body
|
|
328
|
+
get '/old-bar/3'
|
|
329
|
+
assert_equal '3', body
|
|
330
|
+
post '/mix-bar/4'
|
|
331
|
+
assert_equal '4', body
|
|
332
|
+
get '/mix-bar/4'
|
|
333
|
+
assert_equal '4', body
|
|
334
|
+
get '/123'
|
|
335
|
+
assert_equal '/123', body
|
|
336
|
+
post '/123'
|
|
337
|
+
assert_equal '/123', body
|
|
338
|
+
delete '/drugs/123/destroy'
|
|
339
|
+
assert_equal '/drugs/123/destroy', body
|
|
340
|
+
delete '/123/destroy'
|
|
341
|
+
assert_equal '/123/destroy', body
|
|
342
|
+
get '/gangsta'
|
|
343
|
+
assert_equal '/gangsta?foo%5Bbar%5D=baz&hoge=fuga', body
|
|
344
|
+
get '/splatter/123/456'
|
|
345
|
+
assert_equal '/splatter/123/456', body
|
|
346
346
|
end
|
|
347
347
|
|
|
348
348
|
it 'should generate url with format' do
|
|
349
349
|
mock_app do
|
|
350
|
-
get(:a, :
|
|
351
|
-
get(:b, :
|
|
352
|
-
get(:c, :
|
|
353
|
-
get(:d, :
|
|
350
|
+
get(:a, provides: :any) { url(:a, format: :json) }
|
|
351
|
+
get(:b, provides: :js) { url(:b, format: :js) }
|
|
352
|
+
get(:c, provides: %i[js json]) { url(:c, format: :json) }
|
|
353
|
+
get(:d, provides: %i[html js]) { url(:d, format: :js, foo: :bar) }
|
|
354
354
|
end
|
|
355
|
-
get
|
|
356
|
-
assert_equal
|
|
357
|
-
get
|
|
358
|
-
assert_equal
|
|
359
|
-
get
|
|
355
|
+
get '/a.js'
|
|
356
|
+
assert_equal '/a.json', body
|
|
357
|
+
get '/b.js'
|
|
358
|
+
assert_equal '/b.js', body
|
|
359
|
+
get '/b.ru'
|
|
360
360
|
assert_equal 404, status
|
|
361
|
-
get
|
|
362
|
-
assert_equal
|
|
363
|
-
get
|
|
364
|
-
assert_equal
|
|
365
|
-
get
|
|
361
|
+
get '/c.js'
|
|
362
|
+
assert_equal '/c.json', body
|
|
363
|
+
get '/c.json'
|
|
364
|
+
assert_equal '/c.json', body
|
|
365
|
+
get '/c.ru'
|
|
366
366
|
assert_equal 404, status
|
|
367
|
-
get
|
|
368
|
-
assert_equal
|
|
369
|
-
get
|
|
370
|
-
assert_equal
|
|
371
|
-
get
|
|
367
|
+
get '/d'
|
|
368
|
+
assert_equal '/d.js?foo=bar', body
|
|
369
|
+
get '/d.js'
|
|
370
|
+
assert_equal '/d.js?foo=bar', body
|
|
371
|
+
get '/e.xml'
|
|
372
372
|
assert_equal 404, status
|
|
373
373
|
end
|
|
374
374
|
|
|
375
375
|
it 'should generate absolute urls' do
|
|
376
376
|
mock_app do
|
|
377
|
-
get(:hash, :
|
|
377
|
+
get(:hash, with: :id) { absolute_url(:hash, id: 1) }
|
|
378
378
|
end
|
|
379
|
-
get
|
|
380
|
-
assert_equal
|
|
381
|
-
get
|
|
382
|
-
assert_equal
|
|
379
|
+
get '/hash/2'
|
|
380
|
+
assert_equal 'http://example.org/hash/1', body
|
|
381
|
+
get 'https://example.org/hash/2'
|
|
382
|
+
assert_equal 'https://example.org/hash/1', body
|
|
383
383
|
end
|
|
384
384
|
|
|
385
385
|
it 'should generate absolute urls from stringified keys' do
|
|
386
386
|
mock_app do
|
|
387
|
-
get(:hash, with: :id) { absolute_url(:hash,
|
|
387
|
+
get(:hash, with: :id) { absolute_url(:hash, 'id' => 1) }
|
|
388
388
|
end
|
|
389
|
-
get
|
|
390
|
-
assert_equal
|
|
389
|
+
get '/hash/2'
|
|
390
|
+
assert_equal 'http://example.org/hash/1', body
|
|
391
391
|
end
|
|
392
392
|
|
|
393
393
|
it 'should generate proper absolute urls for mounted apps' do
|
|
394
394
|
class Test < Padrino::Application
|
|
395
395
|
get :foo do
|
|
396
|
-
absolute_url(:foo, :
|
|
396
|
+
absolute_url(:foo, id: 1)
|
|
397
397
|
end
|
|
398
398
|
end
|
|
399
|
-
Padrino.mount(
|
|
399
|
+
Padrino.mount('Test').to('/test')
|
|
400
400
|
@app = Padrino.application
|
|
401
401
|
get('/test/foo')
|
|
402
402
|
assert_equal 'http://example.org/test/foo?id=1', body
|
|
@@ -405,27 +405,27 @@ describe "Routing" do
|
|
|
405
405
|
it 'should rebase simple string urls to app uri_root' do
|
|
406
406
|
mock_app do
|
|
407
407
|
set :uri_root, '/app'
|
|
408
|
-
get(:a){ url('/foo') }
|
|
409
|
-
get(:b){ url('bar') }
|
|
410
|
-
get(:c){ absolute_url('/foo') }
|
|
411
|
-
get(:d, :
|
|
408
|
+
get(:a) { url('/foo') }
|
|
409
|
+
get(:b) { url('bar') }
|
|
410
|
+
get(:c) { absolute_url('/foo') }
|
|
411
|
+
get(:d, map: '/d/e/f') { absolute_url('bar') }
|
|
412
412
|
end
|
|
413
|
-
get
|
|
414
|
-
assert_equal
|
|
415
|
-
get
|
|
416
|
-
assert_equal
|
|
417
|
-
get
|
|
418
|
-
assert_equal
|
|
419
|
-
get
|
|
420
|
-
assert_equal
|
|
413
|
+
get '/a'
|
|
414
|
+
assert_equal '/app/foo', body
|
|
415
|
+
get '/b'
|
|
416
|
+
assert_equal 'bar', body
|
|
417
|
+
get '/c'
|
|
418
|
+
assert_equal 'http://example.org/app/foo', body
|
|
419
|
+
get '/d/e/f'
|
|
420
|
+
assert_equal 'http://example.org/app/d/e/bar', body
|
|
421
421
|
end
|
|
422
422
|
|
|
423
423
|
it 'should allow regex url with format' do
|
|
424
424
|
mock_app do
|
|
425
|
-
get(/.*/, :
|
|
425
|
+
get(/.*/, provides: :any) { 'regexp' }
|
|
426
426
|
end
|
|
427
|
-
get
|
|
428
|
-
assert_equal
|
|
427
|
+
get '/anything'
|
|
428
|
+
assert_equal 'regexp', body
|
|
429
429
|
end
|
|
430
430
|
|
|
431
431
|
it 'should use padrino url method' do
|
|
@@ -438,60 +438,60 @@ describe "Routing" do
|
|
|
438
438
|
it 'should work correctly with sinatra redirects' do
|
|
439
439
|
mock_app do
|
|
440
440
|
get(:index) { redirect url(:index) }
|
|
441
|
-
get(:google) { redirect
|
|
442
|
-
get(
|
|
443
|
-
get(
|
|
441
|
+
get(:google) { redirect 'http://google.com' }
|
|
442
|
+
get('/foo') { redirect '/bar' }
|
|
443
|
+
get('/bar') { 'Bar' }
|
|
444
444
|
end
|
|
445
445
|
|
|
446
|
-
get
|
|
447
|
-
assert_equal
|
|
448
|
-
get
|
|
449
|
-
assert_equal
|
|
450
|
-
get
|
|
451
|
-
assert_equal
|
|
446
|
+
get '/'
|
|
447
|
+
assert_equal 'http://example.org/', headers['Location']
|
|
448
|
+
get '/google'
|
|
449
|
+
assert_equal 'http://google.com', headers['Location']
|
|
450
|
+
get '/foo'
|
|
451
|
+
assert_equal 'http://example.org/bar', headers['Location']
|
|
452
452
|
end
|
|
453
453
|
|
|
454
454
|
it 'should return 406 on Accept-Headers it does not provide' do
|
|
455
455
|
mock_app do
|
|
456
|
-
get(:a, :
|
|
456
|
+
get(:a, provides: %i[html js]) { content_type }
|
|
457
457
|
end
|
|
458
458
|
|
|
459
|
-
get
|
|
459
|
+
get '/a', {}, { 'HTTP_ACCEPT' => 'application/yaml' }
|
|
460
460
|
assert_equal 406, status
|
|
461
461
|
end
|
|
462
462
|
|
|
463
463
|
it 'should return 406 on file extensions it does not provide and flag is set' do
|
|
464
464
|
mock_app do
|
|
465
465
|
enable :treat_format_as_accept
|
|
466
|
-
get(:a, :
|
|
466
|
+
get(:a, provides: %i[html js]) { content_type }
|
|
467
467
|
end
|
|
468
468
|
|
|
469
|
-
get
|
|
469
|
+
get '/a.xml', {}, {}
|
|
470
470
|
assert_equal 406, status
|
|
471
471
|
end
|
|
472
472
|
|
|
473
473
|
it 'should provide proper content when :provides is specified and Accept: `*/*` requested' do
|
|
474
474
|
mock_app do
|
|
475
|
-
get(:text, :
|
|
475
|
+
get(:text, provides: :text) { 'text' }
|
|
476
476
|
end
|
|
477
477
|
header 'Accept', '*/*'
|
|
478
|
-
get
|
|
478
|
+
get '/text'
|
|
479
479
|
assert_equal 200, status
|
|
480
|
-
assert_equal
|
|
480
|
+
assert_equal 'text', body
|
|
481
481
|
end
|
|
482
482
|
|
|
483
483
|
it 'should return 404 on file extensions it does not provide and flag is not set' do
|
|
484
484
|
mock_app do
|
|
485
|
-
get(:a, :
|
|
485
|
+
get(:a, provides: %i[html js]) { content_type }
|
|
486
486
|
end
|
|
487
487
|
|
|
488
|
-
get
|
|
488
|
+
get '/a.xml', {}, {}
|
|
489
489
|
assert_equal 404, status
|
|
490
490
|
end
|
|
491
491
|
|
|
492
492
|
it 'should not set content_type to :html if Accept */* and html not in provides' do
|
|
493
493
|
mock_app do
|
|
494
|
-
get(
|
|
494
|
+
get('/foo', provides: %i[json xml]) { content_type.to_s }
|
|
495
495
|
end
|
|
496
496
|
|
|
497
497
|
get '/foo', {}, { 'HTTP_ACCEPT' => '*/*;q=0.5' }
|
|
@@ -500,7 +500,7 @@ describe "Routing" do
|
|
|
500
500
|
|
|
501
501
|
it 'should set content_type to :json if Accept contains */*' do
|
|
502
502
|
mock_app do
|
|
503
|
-
get(
|
|
503
|
+
get('/foo', provides: [:json]) { content_type.to_s }
|
|
504
504
|
end
|
|
505
505
|
|
|
506
506
|
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }
|
|
@@ -509,16 +509,16 @@ describe "Routing" do
|
|
|
509
509
|
|
|
510
510
|
it 'should set and get content_type' do
|
|
511
511
|
mock_app do
|
|
512
|
-
get(
|
|
512
|
+
get('/foo') { content_type(:json); content_type.to_s }
|
|
513
513
|
end
|
|
514
|
-
get
|
|
514
|
+
get '/foo'
|
|
515
515
|
assert_equal 'application/json', content_type
|
|
516
516
|
assert_equal 'json', body
|
|
517
517
|
end
|
|
518
518
|
|
|
519
519
|
it 'should send the appropriate number of params' do
|
|
520
520
|
mock_app do
|
|
521
|
-
get('/id/:user_id', :
|
|
521
|
+
get('/id/:user_id', provides: [:json]) { |user_id, _format| user_id }
|
|
522
522
|
end
|
|
523
523
|
get '/id/5.json'
|
|
524
524
|
assert_equal '5', body
|
|
@@ -526,7 +526,7 @@ describe "Routing" do
|
|
|
526
526
|
|
|
527
527
|
it 'should allow "." in param values' do
|
|
528
528
|
mock_app do
|
|
529
|
-
get('/id/:email', :
|
|
529
|
+
get('/id/:email', provides: [:json]) { |email, format| [email, format].join('/') }
|
|
530
530
|
end
|
|
531
531
|
get '/id/foo@bar.com.json'
|
|
532
532
|
assert_equal 'foo@bar.com/json', body
|
|
@@ -534,7 +534,7 @@ describe "Routing" do
|
|
|
534
534
|
|
|
535
535
|
it 'should set correct content_type for Accept not equal to */* even if */* also provided' do
|
|
536
536
|
mock_app do
|
|
537
|
-
get(
|
|
537
|
+
get('/foo', provides: %i[html js xml]) { content_type.to_s }
|
|
538
538
|
end
|
|
539
539
|
|
|
540
540
|
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/javascript, */*;q=0.5' }
|
|
@@ -543,38 +543,38 @@ describe "Routing" do
|
|
|
543
543
|
|
|
544
544
|
it 'should return the first content type in provides if accept header is empty' do
|
|
545
545
|
mock_app do
|
|
546
|
-
get(:a, :
|
|
546
|
+
get(:a, provides: [:js]) { content_type.to_s }
|
|
547
547
|
end
|
|
548
548
|
|
|
549
|
-
get
|
|
550
|
-
assert_equal
|
|
549
|
+
get '/a', {}, {}
|
|
550
|
+
assert_equal 'js', body
|
|
551
551
|
end
|
|
552
552
|
|
|
553
553
|
it 'should not default to HTML if HTML is not provided and no type is given' do
|
|
554
554
|
mock_app do
|
|
555
|
-
get(:a, :
|
|
555
|
+
get(:a, provides: [:js]) { content_type }
|
|
556
556
|
end
|
|
557
557
|
|
|
558
|
-
get
|
|
559
|
-
assert_equal
|
|
558
|
+
get '/a', {}, {}
|
|
559
|
+
assert_equal 'text/javascript;charset=utf-8', content_type
|
|
560
560
|
end
|
|
561
561
|
|
|
562
562
|
it 'should not match routes if url_format and http_accept is provided but not included' do
|
|
563
563
|
mock_app do
|
|
564
|
-
get(:a, :
|
|
564
|
+
get(:a, provides: %i[js html]) { content_type }
|
|
565
565
|
end
|
|
566
566
|
|
|
567
|
-
get
|
|
567
|
+
get '/a.xml', {}, { 'HTTP_ACCEPT' => 'text/html' }
|
|
568
568
|
assert_equal 404, status
|
|
569
569
|
end
|
|
570
570
|
|
|
571
571
|
it 'should generate routes for format simple' do
|
|
572
572
|
mock_app do
|
|
573
|
-
get(:foo, :
|
|
573
|
+
get(:foo, provides: %i[html rss]) { "Test\n" }
|
|
574
574
|
end
|
|
575
|
-
get
|
|
575
|
+
get '/foo'
|
|
576
576
|
assert_equal "Test\n", body
|
|
577
|
-
get
|
|
577
|
+
get '/foo.rss'
|
|
578
578
|
assert_equal "Test\n", body
|
|
579
579
|
end
|
|
580
580
|
|
|
@@ -587,10 +587,10 @@ describe "Routing" do
|
|
|
587
587
|
end
|
|
588
588
|
end
|
|
589
589
|
end
|
|
590
|
-
get
|
|
591
|
-
assert_equal
|
|
592
|
-
get
|
|
593
|
-
assert_equal
|
|
590
|
+
get '/posts'
|
|
591
|
+
assert_equal 'posts', body
|
|
592
|
+
get '/mini'
|
|
593
|
+
assert_equal 'mini', body
|
|
594
594
|
end
|
|
595
595
|
|
|
596
596
|
it 'should inject the action name into the request' do
|
|
@@ -598,21 +598,21 @@ describe "Routing" do
|
|
|
598
598
|
controller :posts do
|
|
599
599
|
get('/omnomnom(/:id)?') { request.action.inspect }
|
|
600
600
|
controller :mini do
|
|
601
|
-
get([
|
|
601
|
+
get(%i[a b c]) { request.action.inspect }
|
|
602
602
|
end
|
|
603
603
|
end
|
|
604
604
|
end
|
|
605
|
-
get
|
|
606
|
-
assert_equal "
|
|
607
|
-
get
|
|
608
|
-
assert_equal
|
|
605
|
+
get '/posts/omnomnom'
|
|
606
|
+
assert_equal '"/omnomnom(/:id)?"', body
|
|
607
|
+
get '/mini/a/b/c'
|
|
608
|
+
assert_equal ':a', body
|
|
609
609
|
end
|
|
610
610
|
|
|
611
611
|
it 'should support not_found' do
|
|
612
612
|
mock_app do
|
|
613
613
|
not_found { 'whatever' }
|
|
614
614
|
|
|
615
|
-
get :index, :
|
|
615
|
+
get :index, map: '/' do
|
|
616
616
|
'index'
|
|
617
617
|
end
|
|
618
618
|
end
|
|
@@ -630,361 +630,356 @@ describe "Routing" do
|
|
|
630
630
|
get(:index) { request.route_obj.name.to_s }
|
|
631
631
|
end
|
|
632
632
|
end
|
|
633
|
-
get
|
|
634
|
-
assert_equal
|
|
633
|
+
get '/posts'
|
|
634
|
+
assert_equal 'posts index', body
|
|
635
635
|
end
|
|
636
636
|
|
|
637
637
|
it 'should preserve the format if you set it manually' do
|
|
638
638
|
mock_app do
|
|
639
639
|
before do
|
|
640
|
-
params[:format] =
|
|
640
|
+
params[:format] = 'json'
|
|
641
641
|
end
|
|
642
642
|
|
|
643
|
-
get
|
|
643
|
+
get 'test', provides: %i[html json] do
|
|
644
644
|
content_type.inspect
|
|
645
645
|
end
|
|
646
646
|
end
|
|
647
|
-
get
|
|
648
|
-
assert_equal
|
|
649
|
-
get
|
|
650
|
-
assert_equal
|
|
651
|
-
get
|
|
652
|
-
assert_equal
|
|
647
|
+
get '/test'
|
|
648
|
+
assert_equal ':json', body
|
|
649
|
+
get '/test.html'
|
|
650
|
+
assert_equal ':json', body
|
|
651
|
+
get '/test.php'
|
|
652
|
+
assert_equal ':json', body
|
|
653
653
|
end
|
|
654
654
|
|
|
655
655
|
it 'should correctly accept "." in the route' do
|
|
656
656
|
mock_app do
|
|
657
|
-
get
|
|
657
|
+
get 'test.php', provides: %i[html json] do
|
|
658
658
|
content_type.inspect
|
|
659
659
|
end
|
|
660
660
|
end
|
|
661
|
-
get
|
|
662
|
-
assert_equal
|
|
663
|
-
get
|
|
664
|
-
assert_equal
|
|
661
|
+
get '/test.php'
|
|
662
|
+
assert_equal ':html', body
|
|
663
|
+
get '/test.php.json'
|
|
664
|
+
assert_equal ':json', body
|
|
665
665
|
end
|
|
666
666
|
|
|
667
667
|
it 'should correctly accept priority of format' do
|
|
668
668
|
mock_app do
|
|
669
|
-
get
|
|
669
|
+
get 'test.php', provides: %i[html json xml] do
|
|
670
670
|
content_type.inspect
|
|
671
671
|
end
|
|
672
672
|
end
|
|
673
673
|
|
|
674
|
-
get
|
|
675
|
-
assert_equal
|
|
676
|
-
get
|
|
677
|
-
assert_equal
|
|
678
|
-
get
|
|
679
|
-
assert_equal
|
|
680
|
-
get
|
|
681
|
-
assert_equal
|
|
674
|
+
get '/test.php'
|
|
675
|
+
assert_equal ':html', body
|
|
676
|
+
get '/test.php', {}, { 'HTTP_ACCEPT' => 'application/xml' }
|
|
677
|
+
assert_equal ':xml', body
|
|
678
|
+
get '/test.php?format=json', { 'HTTP_ACCEPT' => 'application/xml' }
|
|
679
|
+
assert_equal ':json', body
|
|
680
|
+
get '/test.php.json?format=html', { 'HTTP_ACCEPT' => 'application/xml' }
|
|
681
|
+
assert_equal ':json', body
|
|
682
682
|
end
|
|
683
683
|
|
|
684
684
|
it 'should generate routes for format with controller' do
|
|
685
685
|
mock_app do
|
|
686
686
|
controller :posts do
|
|
687
|
-
get(:index, :
|
|
688
|
-
get(:show, :
|
|
687
|
+
get(:index, provides: %i[html rss atom js]) { "Index.#{content_type}\n" }
|
|
688
|
+
get(:show, with: :id, provides: %i[html rss atom]) { "Show.#{content_type}\n" }
|
|
689
689
|
end
|
|
690
690
|
end
|
|
691
|
-
get
|
|
691
|
+
get '/posts'
|
|
692
692
|
assert_equal "Index.html\n", body
|
|
693
|
-
get
|
|
693
|
+
get '/posts.rss'
|
|
694
694
|
assert_equal "Index.rss\n", body
|
|
695
|
-
get
|
|
695
|
+
get '/posts.atom'
|
|
696
696
|
assert_equal "Index.atom\n", body
|
|
697
|
-
get
|
|
697
|
+
get '/posts.js'
|
|
698
698
|
assert_equal "Index.js\n", body
|
|
699
|
-
get
|
|
699
|
+
get '/posts/show/5'
|
|
700
700
|
assert_equal "Show.html\n", body
|
|
701
|
-
get
|
|
701
|
+
get '/posts/show/5.rss'
|
|
702
702
|
assert_equal "Show.rss\n", body
|
|
703
|
-
get
|
|
703
|
+
get '/posts/show/10.atom'
|
|
704
704
|
assert_equal "Show.atom\n", body
|
|
705
705
|
end
|
|
706
706
|
|
|
707
707
|
it 'should map routes' do
|
|
708
708
|
mock_app do
|
|
709
|
-
get(:bar){
|
|
709
|
+
get(:bar) { 'bar' }
|
|
710
710
|
end
|
|
711
|
-
get
|
|
712
|
-
assert_equal
|
|
713
|
-
assert_equal
|
|
711
|
+
get '/bar'
|
|
712
|
+
assert_equal 'bar', body
|
|
713
|
+
assert_equal '/bar', @app.url(:bar)
|
|
714
714
|
end
|
|
715
715
|
|
|
716
716
|
it 'should remove index from path' do
|
|
717
717
|
mock_app do
|
|
718
|
-
get(:index){
|
|
719
|
-
get(
|
|
718
|
+
get(:index) { 'index' }
|
|
719
|
+
get('/accounts/index') { 'accounts' }
|
|
720
720
|
end
|
|
721
|
-
get
|
|
722
|
-
assert_equal
|
|
723
|
-
assert_equal
|
|
724
|
-
get
|
|
725
|
-
assert_equal
|
|
721
|
+
get '/'
|
|
722
|
+
assert_equal 'index', body
|
|
723
|
+
assert_equal '/', @app.url(:index)
|
|
724
|
+
get '/accounts/index'
|
|
725
|
+
assert_equal 'accounts', body
|
|
726
726
|
end
|
|
727
727
|
|
|
728
728
|
it 'should remove index from path with params' do
|
|
729
729
|
mock_app do
|
|
730
|
-
get(:index, :
|
|
730
|
+
get(:index, with: :name) { "index with #{params[:name]}" }
|
|
731
731
|
end
|
|
732
|
-
get
|
|
733
|
-
assert_equal
|
|
734
|
-
assert_equal
|
|
732
|
+
get '/bobby'
|
|
733
|
+
assert_equal 'index with bobby', body
|
|
734
|
+
assert_equal '/john', @app.url(:index, name: 'john')
|
|
735
735
|
end
|
|
736
736
|
|
|
737
737
|
it 'should parse named params' do
|
|
738
738
|
mock_app do
|
|
739
|
-
get(:print, :
|
|
739
|
+
get(:print, with: :id) { "Im #{params[:id]}" }
|
|
740
740
|
end
|
|
741
|
-
get
|
|
742
|
-
assert_equal
|
|
743
|
-
assert_equal
|
|
741
|
+
get '/print/9'
|
|
742
|
+
assert_equal 'Im 9', body
|
|
743
|
+
assert_equal '/print/9', @app.url(:print, id: 9)
|
|
744
744
|
end
|
|
745
745
|
|
|
746
746
|
it 'should 405 on wrong request_method' do
|
|
747
747
|
mock_app do
|
|
748
|
-
post('/bar'){
|
|
748
|
+
post('/bar') { 'bar' }
|
|
749
749
|
end
|
|
750
|
-
get
|
|
750
|
+
get '/bar'
|
|
751
751
|
assert_equal 405, status
|
|
752
752
|
end
|
|
753
753
|
|
|
754
754
|
it 'should respond to' do
|
|
755
755
|
mock_app do
|
|
756
|
-
get(:a, :
|
|
757
|
-
get(:b, :
|
|
758
|
-
get(:c, :
|
|
759
|
-
get(:d, :
|
|
756
|
+
get(:a, provides: :js) { 'js' }
|
|
757
|
+
get(:b, provides: :any) { 'any' }
|
|
758
|
+
get(:c, provides: %i[js json]) { 'js,json' }
|
|
759
|
+
get(:d, provides: %i[html js]) { 'html,js' }
|
|
760
760
|
end
|
|
761
|
-
get
|
|
761
|
+
get '/a'
|
|
762
762
|
assert_equal 200, status
|
|
763
|
-
assert_equal
|
|
764
|
-
get
|
|
765
|
-
assert_equal
|
|
766
|
-
get
|
|
767
|
-
assert_equal
|
|
763
|
+
assert_equal 'js', body
|
|
764
|
+
get '/a.js'
|
|
765
|
+
assert_equal 'js', body
|
|
766
|
+
get '/b'
|
|
767
|
+
assert_equal 'any', body
|
|
768
768
|
# TODO: randomly fails in minitest :(
|
|
769
769
|
# assert_raises(RuntimeError) { get "/b.foo" }
|
|
770
|
-
get
|
|
770
|
+
get '/c'
|
|
771
771
|
assert_equal 200, status
|
|
772
|
-
assert_equal
|
|
773
|
-
get
|
|
774
|
-
assert_equal
|
|
775
|
-
get
|
|
776
|
-
assert_equal
|
|
777
|
-
get
|
|
778
|
-
assert_equal
|
|
779
|
-
get
|
|
780
|
-
assert_equal
|
|
772
|
+
assert_equal 'js,json', body
|
|
773
|
+
get '/c.js'
|
|
774
|
+
assert_equal 'js,json', body
|
|
775
|
+
get '/c.json'
|
|
776
|
+
assert_equal 'js,json', body
|
|
777
|
+
get '/d'
|
|
778
|
+
assert_equal 'html,js', body
|
|
779
|
+
get '/d.js'
|
|
780
|
+
assert_equal 'html,js', body
|
|
781
781
|
end
|
|
782
782
|
|
|
783
783
|
it 'should respond_to and set content_type' do
|
|
784
784
|
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
|
785
785
|
mock_app do
|
|
786
|
-
get :a, :
|
|
787
|
-
|
|
788
|
-
when :js then "js"
|
|
789
|
-
when :json then "json"
|
|
790
|
-
when :foo then "foo"
|
|
791
|
-
when :html then "html"
|
|
792
|
-
end
|
|
786
|
+
get :a, provides: :any do
|
|
787
|
+
content_type.to_s if %i[js json foo html].include?(content_type)
|
|
793
788
|
end
|
|
794
789
|
end
|
|
795
|
-
get
|
|
796
|
-
assert_equal
|
|
797
|
-
assert_equal 'text/javascript;charset=utf-8', response[
|
|
798
|
-
get
|
|
799
|
-
assert_equal
|
|
800
|
-
assert_equal 'application/json', response[
|
|
801
|
-
get
|
|
802
|
-
assert_equal
|
|
803
|
-
assert_equal 'application/foo', response[
|
|
804
|
-
get
|
|
805
|
-
assert_equal
|
|
806
|
-
assert_equal 'text/html;charset=utf-8', response[
|
|
790
|
+
get '/a.js'
|
|
791
|
+
assert_equal 'js', body
|
|
792
|
+
assert_equal 'text/javascript;charset=utf-8', response['Content-Type']
|
|
793
|
+
get '/a.json'
|
|
794
|
+
assert_equal 'json', body
|
|
795
|
+
assert_equal 'application/json', response['Content-Type']
|
|
796
|
+
get '/a.foo'
|
|
797
|
+
assert_equal 'foo', body
|
|
798
|
+
assert_equal 'application/foo', response['Content-Type']
|
|
799
|
+
get '/a'
|
|
800
|
+
assert_equal 'html', body
|
|
801
|
+
assert_equal 'text/html;charset=utf-8', response['Content-Type']
|
|
807
802
|
end
|
|
808
803
|
|
|
809
804
|
it 'should not drop json charset' do
|
|
810
805
|
mock_app do
|
|
811
806
|
get '/' do
|
|
812
|
-
content_type :json, :
|
|
807
|
+
content_type :json, charset: 'utf-16'
|
|
813
808
|
end
|
|
814
809
|
get '/a' do
|
|
815
810
|
content_type :json, 'charset' => 'utf-16'
|
|
816
811
|
end
|
|
817
812
|
end
|
|
818
813
|
get '/'
|
|
819
|
-
assert_equal 'application/json;charset=utf-16', response[
|
|
814
|
+
assert_equal 'application/json;charset=utf-16', response['Content-Type']
|
|
820
815
|
get '/a'
|
|
821
|
-
assert_equal 'application/json;charset=utf-16', response[
|
|
816
|
+
assert_equal 'application/json;charset=utf-16', response['Content-Type']
|
|
822
817
|
end
|
|
823
818
|
|
|
824
819
|
it 'should use controllers' do
|
|
825
820
|
mock_app do
|
|
826
|
-
controller
|
|
827
|
-
get(
|
|
828
|
-
get(
|
|
821
|
+
controller '/admin' do
|
|
822
|
+
get('/') { 'index' }
|
|
823
|
+
get('/show/:id') { "show #{params[:id]}" }
|
|
829
824
|
end
|
|
830
825
|
end
|
|
831
|
-
get
|
|
832
|
-
assert_equal
|
|
833
|
-
get
|
|
834
|
-
assert_equal
|
|
826
|
+
get '/admin'
|
|
827
|
+
assert_equal 'index', body
|
|
828
|
+
get '/admin/show/1'
|
|
829
|
+
assert_equal 'show 1', body
|
|
835
830
|
end
|
|
836
831
|
|
|
837
832
|
it 'should use named controllers' do
|
|
838
833
|
mock_app do
|
|
839
834
|
controller :admin do
|
|
840
|
-
get(:index, :
|
|
841
|
-
get(:show, :
|
|
835
|
+
get(:index, with: :id) { params[:id] }
|
|
836
|
+
get(:show, with: :id) { "show #{params[:id]}" }
|
|
842
837
|
end
|
|
843
838
|
controllers :foo, :bar do
|
|
844
|
-
get(:index){
|
|
839
|
+
get(:index) { 'foo_bar_index' }
|
|
845
840
|
end
|
|
846
841
|
end
|
|
847
|
-
get
|
|
848
|
-
assert_equal
|
|
849
|
-
get
|
|
850
|
-
assert_equal
|
|
851
|
-
assert_equal
|
|
852
|
-
assert_equal
|
|
853
|
-
get
|
|
854
|
-
assert_equal
|
|
842
|
+
get '/admin/1'
|
|
843
|
+
assert_equal '1', body
|
|
844
|
+
get '/admin/show/1'
|
|
845
|
+
assert_equal 'show 1', body
|
|
846
|
+
assert_equal '/admin/1', @app.url(:admin, :index, id: 1)
|
|
847
|
+
assert_equal '/admin/show/1', @app.url(:admin, :show, id: 1)
|
|
848
|
+
get '/foo/bar'
|
|
849
|
+
assert_equal 'foo_bar_index', body
|
|
855
850
|
end
|
|
856
851
|
|
|
857
852
|
it 'should use map and with' do
|
|
858
853
|
mock_app do
|
|
859
|
-
get :index, :
|
|
854
|
+
get :index, map: '/bugs', with: :id do
|
|
860
855
|
params[:id]
|
|
861
856
|
end
|
|
862
857
|
end
|
|
863
858
|
get '/bugs/4'
|
|
864
859
|
assert_equal '4', body
|
|
865
|
-
assert_equal
|
|
860
|
+
assert_equal '/bugs/4', @app.url(:index, id: 4)
|
|
866
861
|
end
|
|
867
862
|
|
|
868
863
|
it 'should ignore trailing delimiters within a named controller' do
|
|
869
864
|
mock_app do
|
|
870
865
|
controller :posts do
|
|
871
|
-
get(:index, :
|
|
872
|
-
get(:new) {
|
|
873
|
-
get(:show, :
|
|
866
|
+
get(:index, provides: %i[html js]) { 'index' }
|
|
867
|
+
get(:new) { 'new' }
|
|
868
|
+
get(:show, with: :id) { "show #{params[:id]}" }
|
|
874
869
|
end
|
|
875
870
|
end
|
|
876
|
-
get
|
|
877
|
-
assert_equal
|
|
878
|
-
get
|
|
879
|
-
assert_equal
|
|
880
|
-
get
|
|
881
|
-
assert_equal
|
|
882
|
-
get
|
|
883
|
-
assert_equal
|
|
884
|
-
get
|
|
885
|
-
assert_equal
|
|
886
|
-
get
|
|
887
|
-
assert_equal
|
|
871
|
+
get '/posts'
|
|
872
|
+
assert_equal 'index', body
|
|
873
|
+
get '/posts/'
|
|
874
|
+
assert_equal 'index', body
|
|
875
|
+
get '/posts.js'
|
|
876
|
+
assert_equal 'index', body
|
|
877
|
+
get '/posts.js/'
|
|
878
|
+
assert_equal 'index', body
|
|
879
|
+
get '/posts/new'
|
|
880
|
+
assert_equal 'new', body
|
|
881
|
+
get '/posts/new/'
|
|
882
|
+
assert_equal 'new', body
|
|
888
883
|
end
|
|
889
884
|
|
|
890
885
|
it 'should ignore trailing delimiters within a named controller for unnamed actions' do
|
|
891
886
|
mock_app do
|
|
892
887
|
controller :accounts do
|
|
893
|
-
get(
|
|
894
|
-
get(
|
|
888
|
+
get('/') { 'account_index' }
|
|
889
|
+
get('/new') { 'new' }
|
|
895
890
|
end
|
|
896
891
|
controller :votes do
|
|
897
|
-
get(
|
|
892
|
+
get('/') { 'vote_index' }
|
|
898
893
|
end
|
|
899
894
|
end
|
|
900
|
-
get
|
|
901
|
-
assert_equal
|
|
902
|
-
get
|
|
903
|
-
assert_equal
|
|
904
|
-
get
|
|
905
|
-
assert_equal
|
|
906
|
-
get
|
|
907
|
-
assert_equal
|
|
908
|
-
get
|
|
909
|
-
assert_equal
|
|
910
|
-
get
|
|
911
|
-
assert_equal
|
|
895
|
+
get '/accounts'
|
|
896
|
+
assert_equal 'account_index', body
|
|
897
|
+
get '/accounts/'
|
|
898
|
+
assert_equal 'account_index', body
|
|
899
|
+
get '/accounts/new'
|
|
900
|
+
assert_equal 'new', body
|
|
901
|
+
get '/accounts/new/'
|
|
902
|
+
assert_equal 'new', body
|
|
903
|
+
get '/votes'
|
|
904
|
+
assert_equal 'vote_index', body
|
|
905
|
+
get '/votes/'
|
|
906
|
+
assert_equal 'vote_index', body
|
|
912
907
|
end
|
|
913
908
|
|
|
914
909
|
it 'should use named controllers with array routes' do
|
|
915
910
|
mock_app do
|
|
916
911
|
controller :admin do
|
|
917
|
-
get(:index){
|
|
918
|
-
get(:show, :
|
|
912
|
+
get(:index) { 'index' }
|
|
913
|
+
get(:show, with: :id) { "show #{params[:id]}" }
|
|
919
914
|
end
|
|
920
915
|
controllers :foo, :bar do
|
|
921
|
-
get(:index){
|
|
916
|
+
get(:index) { 'foo_bar_index' }
|
|
922
917
|
end
|
|
923
918
|
end
|
|
924
|
-
get
|
|
925
|
-
assert_equal
|
|
926
|
-
get
|
|
927
|
-
assert_equal
|
|
928
|
-
assert_equal
|
|
929
|
-
assert_equal
|
|
930
|
-
get
|
|
931
|
-
assert_equal
|
|
919
|
+
get '/admin'
|
|
920
|
+
assert_equal 'index', body
|
|
921
|
+
get '/admin/show/1'
|
|
922
|
+
assert_equal 'show 1', body
|
|
923
|
+
assert_equal '/admin', @app.url(:admin, :index)
|
|
924
|
+
assert_equal '/admin/show/1', @app.url(:admin, :show, id: 1)
|
|
925
|
+
get '/foo/bar'
|
|
926
|
+
assert_equal 'foo_bar_index', body
|
|
932
927
|
end
|
|
933
928
|
|
|
934
929
|
it 'should support a reindex action and remove index inside controller' do
|
|
935
930
|
mock_app do
|
|
936
931
|
controller :posts do
|
|
937
|
-
get(:index){
|
|
938
|
-
get(:reindex){
|
|
932
|
+
get(:index) { 'index' }
|
|
933
|
+
get(:reindex) { 'reindex' }
|
|
939
934
|
end
|
|
940
935
|
end
|
|
941
|
-
get
|
|
942
|
-
assert_equal
|
|
943
|
-
get
|
|
944
|
-
assert_equal
|
|
945
|
-
assert_equal
|
|
936
|
+
get '/posts'
|
|
937
|
+
assert_equal 'index', body
|
|
938
|
+
get '/posts/reindex'
|
|
939
|
+
assert_equal '/posts/reindex', @app.url(:posts, :reindex)
|
|
940
|
+
assert_equal 'reindex', body
|
|
946
941
|
end
|
|
947
942
|
|
|
948
943
|
it 'should use uri_root' do
|
|
949
944
|
mock_app do
|
|
950
|
-
get(:foo){
|
|
945
|
+
get(:foo) { 'foo' }
|
|
951
946
|
end
|
|
952
947
|
@app.uri_root = '/'
|
|
953
|
-
assert_equal
|
|
948
|
+
assert_equal '/foo', @app.url(:foo)
|
|
954
949
|
@app.uri_root = '/testing'
|
|
955
|
-
assert_equal
|
|
950
|
+
assert_equal '/testing/foo', @app.url(:foo)
|
|
956
951
|
@app.uri_root = '/testing/'
|
|
957
|
-
assert_equal
|
|
952
|
+
assert_equal '/testing/foo', @app.url(:foo)
|
|
958
953
|
@app.uri_root = 'testing/bar///'
|
|
959
|
-
assert_equal
|
|
954
|
+
assert_equal '/testing/bar/foo', @app.url(:foo)
|
|
960
955
|
end
|
|
961
956
|
|
|
962
957
|
it 'should use uri_root with controllers' do
|
|
963
958
|
mock_app do
|
|
964
959
|
controller :foo do
|
|
965
|
-
get(:bar){
|
|
960
|
+
get(:bar) { 'bar' }
|
|
966
961
|
end
|
|
967
962
|
end
|
|
968
963
|
@app.uri_root = '/testing'
|
|
969
|
-
assert_equal
|
|
964
|
+
assert_equal '/testing/foo/bar', @app.url(:foo, :bar)
|
|
970
965
|
end
|
|
971
966
|
|
|
972
967
|
it 'should use RACK_BASE_URI' do
|
|
973
968
|
mock_app do
|
|
974
|
-
get(:foo){
|
|
969
|
+
get(:foo) { 'foo' }
|
|
975
970
|
end
|
|
976
971
|
# Wish there was a side-effect free way to test this...
|
|
977
972
|
ENV['RACK_BASE_URI'] = '/'
|
|
978
|
-
assert_equal
|
|
973
|
+
assert_equal '/foo', @app.url(:foo)
|
|
979
974
|
ENV['RACK_BASE_URI'] = '/testing'
|
|
980
|
-
assert_equal
|
|
975
|
+
assert_equal '/testing/foo', @app.url(:foo)
|
|
981
976
|
ENV['RACK_BASE_URI'] = nil
|
|
982
977
|
end
|
|
983
978
|
|
|
984
979
|
it 'should use uri_root and RACK_BASE_URI' do
|
|
985
980
|
mock_app do
|
|
986
981
|
controller :foo do
|
|
987
|
-
get(:bar){
|
|
982
|
+
get(:bar) { 'bar' }
|
|
988
983
|
end
|
|
989
984
|
end
|
|
990
985
|
ENV['RACK_BASE_URI'] = '/base'
|
|
@@ -995,20 +990,20 @@ describe "Routing" do
|
|
|
995
990
|
|
|
996
991
|
it 'should reset routes' do
|
|
997
992
|
mock_app do
|
|
998
|
-
get(
|
|
993
|
+
get('/') { 'foo' }
|
|
999
994
|
reset_router!
|
|
1000
995
|
end
|
|
1001
|
-
get
|
|
996
|
+
get '/'
|
|
1002
997
|
assert_equal 404, status
|
|
1003
998
|
end
|
|
1004
999
|
|
|
1005
1000
|
it 'should match params and format' do
|
|
1006
|
-
|
|
1007
|
-
get '/:id', :
|
|
1001
|
+
mock_app do
|
|
1002
|
+
get '/:id', provides: %i[json html] do |id, _|
|
|
1008
1003
|
id
|
|
1009
1004
|
end
|
|
1010
1005
|
|
|
1011
|
-
get 'format/:id', :
|
|
1006
|
+
get 'format/:id', provides: %i[json html] do |_id, format|
|
|
1012
1007
|
format
|
|
1013
1008
|
end
|
|
1014
1009
|
end
|
|
@@ -1020,251 +1015,250 @@ describe "Routing" do
|
|
|
1020
1015
|
assert_equal 'html', body
|
|
1021
1016
|
end
|
|
1022
1017
|
|
|
1023
|
-
|
|
1024
1018
|
it 'should respect priorities' do
|
|
1025
1019
|
route_order = []
|
|
1026
1020
|
mock_app do
|
|
1027
|
-
get(:index, :
|
|
1028
|
-
get(:index, :
|
|
1029
|
-
get(:index, :
|
|
1021
|
+
get(:index, priority: :normal) { route_order << :normal; pass }
|
|
1022
|
+
get(:index, priority: :low) { route_order << :low; 'hello' }
|
|
1023
|
+
get(:index, priority: :high) { route_order << :high; pass }
|
|
1030
1024
|
end
|
|
1031
1025
|
get '/'
|
|
1032
|
-
assert_equal [
|
|
1033
|
-
assert_equal
|
|
1026
|
+
assert_equal %i[high normal low], route_order
|
|
1027
|
+
assert_equal 'hello', body
|
|
1034
1028
|
end
|
|
1035
1029
|
|
|
1036
1030
|
it 'should set the params correctly even if using prioritized routes' do
|
|
1037
1031
|
mock_app do
|
|
1038
|
-
get(
|
|
1039
|
-
get
|
|
1032
|
+
get('*__sinatra__/:image.png') {}
|
|
1033
|
+
get '/:primary/:secondary', priority: :low do
|
|
1040
1034
|
"#{params[:primary]} #{params[:secondary]}"
|
|
1041
1035
|
end
|
|
1042
1036
|
end
|
|
1043
|
-
get
|
|
1044
|
-
assert_equal
|
|
1037
|
+
get '/abc/def'
|
|
1038
|
+
assert_equal 'abc def', body
|
|
1045
1039
|
end
|
|
1046
1040
|
|
|
1047
1041
|
it 'should catch all after controllers' do
|
|
1048
1042
|
mock_app do
|
|
1049
|
-
get(:index, :
|
|
1043
|
+
get(:index, with: :slug, priority: :low) { 'catch all' }
|
|
1050
1044
|
controllers :contact do
|
|
1051
|
-
get(:index) {
|
|
1045
|
+
get(:index) { 'contact' }
|
|
1052
1046
|
end
|
|
1053
1047
|
end
|
|
1054
|
-
get
|
|
1055
|
-
assert_equal
|
|
1056
|
-
get
|
|
1057
|
-
assert_equal
|
|
1048
|
+
get '/contact'
|
|
1049
|
+
assert_equal 'contact', body
|
|
1050
|
+
get '/foo'
|
|
1051
|
+
assert_equal 'catch all', body
|
|
1058
1052
|
end
|
|
1059
1053
|
|
|
1060
1054
|
it 'should allow optionals' do
|
|
1061
1055
|
mock_app do
|
|
1062
|
-
get(:show, :
|
|
1056
|
+
get(:show, map: '/stories/:type(/:category)?') do
|
|
1063
1057
|
"#{params[:type]}/#{params[:category]}"
|
|
1064
1058
|
end
|
|
1065
1059
|
end
|
|
1066
|
-
get
|
|
1067
|
-
assert_equal
|
|
1068
|
-
get
|
|
1069
|
-
assert_equal
|
|
1060
|
+
get '/stories/foo'
|
|
1061
|
+
assert_equal 'foo/', body
|
|
1062
|
+
get '/stories/foo/bar'
|
|
1063
|
+
assert_equal 'foo/bar', body
|
|
1070
1064
|
end
|
|
1071
1065
|
|
|
1072
1066
|
it 'should apply maps' do
|
|
1073
1067
|
mock_app do
|
|
1074
1068
|
controllers :admin do
|
|
1075
|
-
get(:index, :
|
|
1076
|
-
get(:show, :
|
|
1077
|
-
get(:edit, :
|
|
1078
|
-
get(:wacky, :
|
|
1069
|
+
get(:index, map: '/') { 'index' }
|
|
1070
|
+
get(:show, with: :id, map: '/show') { "show #{params[:id]}" }
|
|
1071
|
+
get(:edit, map: '/edit/:id/product') { "edit #{params[:id]}" }
|
|
1072
|
+
get(:wacky, map: '/wacky-:id-:product_id') { "wacky #{params[:id]}-#{params[:product_id]}" }
|
|
1079
1073
|
end
|
|
1080
1074
|
end
|
|
1081
|
-
get
|
|
1082
|
-
assert_equal
|
|
1075
|
+
get '/'
|
|
1076
|
+
assert_equal 'index', body
|
|
1083
1077
|
get @app.url(:admin, :index)
|
|
1084
|
-
assert_equal
|
|
1085
|
-
get
|
|
1086
|
-
assert_equal
|
|
1087
|
-
get
|
|
1088
|
-
assert_equal
|
|
1089
|
-
get
|
|
1090
|
-
assert_equal
|
|
1078
|
+
assert_equal 'index', body
|
|
1079
|
+
get '/show/1'
|
|
1080
|
+
assert_equal 'show 1', body
|
|
1081
|
+
get '/edit/1/product'
|
|
1082
|
+
assert_equal 'edit 1', body
|
|
1083
|
+
get '/wacky-1-2'
|
|
1084
|
+
assert_equal 'wacky 1-2', body
|
|
1091
1085
|
end
|
|
1092
1086
|
|
|
1093
1087
|
it 'should apply maps when given path is kind of hash' do
|
|
1094
1088
|
mock_app do
|
|
1095
1089
|
controllers :admin do
|
|
1096
|
-
get(:foobar,
|
|
1090
|
+
get(:foobar, '/foo/bar') { 'foobar' }
|
|
1097
1091
|
end
|
|
1098
1092
|
end
|
|
1099
|
-
get
|
|
1100
|
-
assert_equal
|
|
1093
|
+
get '/foo/bar'
|
|
1094
|
+
assert_equal 'foobar', body
|
|
1101
1095
|
end
|
|
1102
1096
|
|
|
1103
1097
|
it 'should apply parent to route' do
|
|
1104
1098
|
mock_app do
|
|
1105
1099
|
controllers :project do
|
|
1106
|
-
get(:index, :
|
|
1107
|
-
get(:index, :
|
|
1108
|
-
get(:edit, :
|
|
1109
|
-
get(:show, :
|
|
1100
|
+
get(:index, parent: :user) { "index #{params[:user_id]}" }
|
|
1101
|
+
get(:index, parent: %i[user section]) { "index #{params[:user_id]} #{params[:section_id]}" }
|
|
1102
|
+
get(:edit, with: :id, parent: :user) { "edit #{params[:id]} #{params[:user_id]}" }
|
|
1103
|
+
get(:show, with: :id, parent: %i[user product]) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}" }
|
|
1110
1104
|
end
|
|
1111
1105
|
end
|
|
1112
|
-
get
|
|
1113
|
-
assert_equal
|
|
1114
|
-
get
|
|
1115
|
-
assert_equal
|
|
1116
|
-
get
|
|
1117
|
-
assert_equal
|
|
1118
|
-
get
|
|
1119
|
-
assert_equal
|
|
1106
|
+
get '/user/1/project'
|
|
1107
|
+
assert_equal 'index 1', body
|
|
1108
|
+
get '/user/1/section/3/project'
|
|
1109
|
+
assert_equal 'index 1 3', body
|
|
1110
|
+
get '/user/1/project/edit/2'
|
|
1111
|
+
assert_equal 'edit 2 1', body
|
|
1112
|
+
get '/user/1/product/2/project/show/3'
|
|
1113
|
+
assert_equal 'show 3 1 2', body
|
|
1120
1114
|
end
|
|
1121
1115
|
|
|
1122
1116
|
it 'should respect parent precedence: controllers parents go before route parents' do
|
|
1123
1117
|
mock_app do
|
|
1124
1118
|
controllers :project do
|
|
1125
|
-
get(:index, :
|
|
1119
|
+
get(:index, parent: :user) { "index #{params[:user_id]}" }
|
|
1126
1120
|
end
|
|
1127
1121
|
|
|
1128
|
-
controllers :bar, :
|
|
1122
|
+
controllers :bar, parent: :foo do
|
|
1129
1123
|
get(:index) { "index on foo #{params[:foo_id]} @ bar" }
|
|
1130
|
-
get(:index, :
|
|
1124
|
+
get(:index, parent: :baz) { "index on foo #{params[:foo_id]} @ baz #{params[:baz_id]} @ bar" }
|
|
1131
1125
|
end
|
|
1132
1126
|
end
|
|
1133
1127
|
|
|
1134
|
-
get
|
|
1135
|
-
assert_equal
|
|
1136
|
-
get
|
|
1137
|
-
assert_equal
|
|
1138
|
-
get
|
|
1139
|
-
assert_equal
|
|
1128
|
+
get '/user/1/project'
|
|
1129
|
+
assert_equal 'index 1', body
|
|
1130
|
+
get '/foo/1/bar'
|
|
1131
|
+
assert_equal 'index on foo 1 @ bar', body
|
|
1132
|
+
get '/foo/1/baz/2/bar'
|
|
1133
|
+
assert_equal 'index on foo 1 @ baz 2 @ bar', body
|
|
1140
1134
|
end
|
|
1141
1135
|
|
|
1142
1136
|
it 'should keep a reference to the parent on the route' do
|
|
1143
1137
|
mock_app do
|
|
1144
1138
|
controllers :project do
|
|
1145
|
-
get(:index, :
|
|
1146
|
-
get(:index, :
|
|
1147
|
-
get(:edit, :
|
|
1148
|
-
get(:show, :
|
|
1139
|
+
get(:index, parent: :user) { "index #{params[:user_id]}" }
|
|
1140
|
+
get(:index, parent: %i[user section]) { "index #{params[:user_id]} #{params[:section_id]}" }
|
|
1141
|
+
get(:edit, with: :id, parent: :user) { "edit #{params[:id]} #{params[:user_id]}" }
|
|
1142
|
+
get(:show, with: :id, parent: %i[user product]) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}" }
|
|
1149
1143
|
end
|
|
1150
1144
|
|
|
1151
|
-
controllers :bar, :
|
|
1152
|
-
get(:index) {
|
|
1153
|
-
get(:index, :
|
|
1145
|
+
controllers :bar, parent: :foo do
|
|
1146
|
+
get(:index) { 'index on foo/bar' }
|
|
1147
|
+
get(:index, parent: :baz) { 'index on foo/baz/bar' }
|
|
1154
1148
|
end
|
|
1155
1149
|
end
|
|
1156
1150
|
|
|
1157
1151
|
# get "/user/1/project"
|
|
1158
1152
|
assert_equal :user, @app.routes[0].parent
|
|
1159
1153
|
# get "/user/1/section/3/project"
|
|
1160
|
-
assert_equal [
|
|
1154
|
+
assert_equal %i[user section], @app.routes[2].parent
|
|
1161
1155
|
# get "/user/1/project/edit/2"
|
|
1162
1156
|
assert_equal :user, @app.routes[4].parent
|
|
1163
1157
|
# get "/user/1/product/2/project/show/3"
|
|
1164
|
-
assert_equal [
|
|
1158
|
+
assert_equal %i[user product], @app.routes[6].parent
|
|
1165
1159
|
# get "/foo/1/bar"
|
|
1166
1160
|
assert_equal :foo, @app.routes[8].parent
|
|
1167
1161
|
# get "/foo/1/baz/2/bar"
|
|
1168
|
-
assert_equal [
|
|
1162
|
+
assert_equal %i[foo baz], @app.routes[10].parent
|
|
1169
1163
|
end
|
|
1170
1164
|
|
|
1171
1165
|
it 'should apply parent to controller' do
|
|
1172
1166
|
mock_app do
|
|
1173
|
-
controller :project, :
|
|
1174
|
-
get(:index) { "index #{params[:user_id]}"}
|
|
1175
|
-
get(:edit, :
|
|
1176
|
-
get(:show, :
|
|
1167
|
+
controller :project, parent: :user do
|
|
1168
|
+
get(:index) { "index #{params[:user_id]}" }
|
|
1169
|
+
get(:edit, with: :id, parent: :user) { "edit #{params[:id]} #{params[:user_id]}" }
|
|
1170
|
+
get(:show, with: :id, parent: :product) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}" }
|
|
1177
1171
|
end
|
|
1178
1172
|
end
|
|
1179
1173
|
|
|
1180
|
-
user_project_url =
|
|
1174
|
+
user_project_url = '/user/1/project'
|
|
1181
1175
|
get user_project_url
|
|
1182
|
-
assert_equal
|
|
1183
|
-
assert_equal user_project_url, @app.url(:project, :index, :
|
|
1176
|
+
assert_equal 'index 1', body
|
|
1177
|
+
assert_equal user_project_url, @app.url(:project, :index, user_id: 1)
|
|
1184
1178
|
|
|
1185
|
-
user_project_edit_url =
|
|
1179
|
+
user_project_edit_url = '/user/1/project/edit/2'
|
|
1186
1180
|
get user_project_edit_url
|
|
1187
|
-
assert_equal
|
|
1188
|
-
assert_equal user_project_edit_url, @app.url(:project, :edit, :
|
|
1181
|
+
assert_equal 'edit 2 1', body
|
|
1182
|
+
assert_equal user_project_edit_url, @app.url(:project, :edit, user_id: 1, id: 2)
|
|
1189
1183
|
|
|
1190
|
-
user_product_project_url =
|
|
1184
|
+
user_product_project_url = '/user/1/product/2/project/show/3'
|
|
1191
1185
|
get user_product_project_url
|
|
1192
|
-
assert_equal
|
|
1193
|
-
assert_equal user_product_project_url, @app.url(:project, :show, :
|
|
1186
|
+
assert_equal 'show 3 1 2', body
|
|
1187
|
+
assert_equal user_product_project_url, @app.url(:project, :show, user_id: 1, product_id: 2, id: 3)
|
|
1194
1188
|
end
|
|
1195
1189
|
|
|
1196
1190
|
it 'should apply parent with shallowing to controller' do
|
|
1197
1191
|
mock_app do
|
|
1198
1192
|
controller :project do
|
|
1199
1193
|
parent :user
|
|
1200
|
-
parent :shop, :
|
|
1194
|
+
parent :shop, optional: true
|
|
1201
1195
|
get(:index) { "index #{params[:user_id]} #{params[:shop_id]}" }
|
|
1202
|
-
get(:edit, :
|
|
1203
|
-
get(:show, :
|
|
1196
|
+
get(:edit, with: :id) { "edit #{params[:id]} #{params[:user_id]} #{params[:shop_id]}" }
|
|
1197
|
+
get(:show, with: :id, parent: :product) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]} #{params[:shop_id]}" }
|
|
1204
1198
|
end
|
|
1205
1199
|
end
|
|
1206
1200
|
|
|
1207
|
-
assert_equal
|
|
1208
|
-
assert_equal
|
|
1201
|
+
assert_equal '/user/1/project', @app.url(:project, :index, user_id: 1, shop_id: nil)
|
|
1202
|
+
assert_equal '/user/1/shop/23/project', @app.url(:project, :index, user_id: 1, shop_id: 23)
|
|
1209
1203
|
|
|
1210
|
-
user_project_url =
|
|
1204
|
+
user_project_url = '/user/1/project'
|
|
1211
1205
|
get user_project_url
|
|
1212
|
-
assert_equal
|
|
1213
|
-
assert_equal user_project_url, @app.url(:project, :index, :
|
|
1206
|
+
assert_equal 'index 1 ', body
|
|
1207
|
+
assert_equal user_project_url, @app.url(:project, :index, user_id: 1)
|
|
1214
1208
|
|
|
1215
|
-
user_project_edit_url =
|
|
1209
|
+
user_project_edit_url = '/user/1/project/edit/2'
|
|
1216
1210
|
get user_project_edit_url
|
|
1217
|
-
assert_equal
|
|
1218
|
-
assert_equal user_project_edit_url, @app.url(:project, :edit, :
|
|
1211
|
+
assert_equal 'edit 2 1 ', body
|
|
1212
|
+
assert_equal user_project_edit_url, @app.url(:project, :edit, user_id: 1, id: 2)
|
|
1219
1213
|
|
|
1220
|
-
user_product_project_url =
|
|
1214
|
+
user_product_project_url = '/user/1/product/2/project/show/3'
|
|
1221
1215
|
get user_product_project_url
|
|
1222
|
-
assert_equal
|
|
1223
|
-
assert_equal user_product_project_url, @app.url(:project, :show, :
|
|
1216
|
+
assert_equal 'show 3 1 2 ', body
|
|
1217
|
+
assert_equal user_product_project_url, @app.url(:project, :show, user_id: 1, product_id: 2, id: 3)
|
|
1224
1218
|
|
|
1225
|
-
user_project_url =
|
|
1219
|
+
user_project_url = '/user/1/shop/1/project'
|
|
1226
1220
|
get user_project_url
|
|
1227
|
-
assert_equal
|
|
1228
|
-
assert_equal user_project_url, @app.url(:project, :index, :
|
|
1221
|
+
assert_equal 'index 1 1', body
|
|
1222
|
+
assert_equal user_project_url, @app.url(:project, :index, user_id: 1, shop_id: 1)
|
|
1229
1223
|
|
|
1230
|
-
user_project_edit_url =
|
|
1224
|
+
user_project_edit_url = '/user/1/shop/1/project/edit/2'
|
|
1231
1225
|
get user_project_edit_url
|
|
1232
|
-
assert_equal
|
|
1233
|
-
assert_equal user_project_edit_url, @app.url(:project, :edit, :
|
|
1226
|
+
assert_equal 'edit 2 1 1', body
|
|
1227
|
+
assert_equal user_project_edit_url, @app.url(:project, :edit, user_id: 1, id: 2, shop_id: 1)
|
|
1234
1228
|
|
|
1235
|
-
user_product_project_url =
|
|
1229
|
+
user_product_project_url = '/user/1/shop/1/product/2/project/show/3'
|
|
1236
1230
|
get user_product_project_url
|
|
1237
|
-
assert_equal
|
|
1238
|
-
assert_equal user_product_project_url, @app.url(:project, :show, :
|
|
1231
|
+
assert_equal 'show 3 1 2 1', body
|
|
1232
|
+
assert_equal user_product_project_url, @app.url(:project, :show, user_id: 1, product_id: 2, id: 3, shop_id: 1)
|
|
1239
1233
|
end
|
|
1240
1234
|
|
|
1241
1235
|
it 'should respect map in parents with shallowing' do
|
|
1242
1236
|
mock_app do
|
|
1243
1237
|
controller :project do
|
|
1244
|
-
parent :shop, :
|
|
1238
|
+
parent :shop, map: '/foo/bar'
|
|
1245
1239
|
get(:index) { "index #{params[:shop_id]}" }
|
|
1246
1240
|
end
|
|
1247
1241
|
end
|
|
1248
1242
|
|
|
1249
|
-
shop_project_url =
|
|
1243
|
+
shop_project_url = '/foo/bar/1/project'
|
|
1250
1244
|
get shop_project_url
|
|
1251
|
-
assert_equal
|
|
1252
|
-
assert_equal shop_project_url, @app.url(:project, :index, :
|
|
1245
|
+
assert_equal 'index 1', body
|
|
1246
|
+
assert_equal shop_project_url, @app.url(:project, :index, shop_id: 1)
|
|
1253
1247
|
end
|
|
1254
1248
|
|
|
1255
1249
|
it 'should use default values' do
|
|
1256
1250
|
mock_app do
|
|
1257
|
-
controller :
|
|
1258
|
-
get(:index, :
|
|
1251
|
+
controller lang: :it do
|
|
1252
|
+
get(:index, map: '/:lang') { "lang is #{params[:lang]}" }
|
|
1259
1253
|
end
|
|
1260
1254
|
# This is only for be sure that default values
|
|
1261
1255
|
# work only for the given controller
|
|
1262
|
-
get(:foo, :
|
|
1256
|
+
get(:foo, map: '/foo') {}
|
|
1263
1257
|
end
|
|
1264
|
-
assert_equal
|
|
1265
|
-
assert_equal
|
|
1266
|
-
get
|
|
1267
|
-
assert_equal
|
|
1258
|
+
assert_equal '/it', @app.url(:index)
|
|
1259
|
+
assert_equal '/foo', @app.url(:foo)
|
|
1260
|
+
get '/en'
|
|
1261
|
+
assert_equal 'lang is en', body
|
|
1268
1262
|
end
|
|
1269
1263
|
|
|
1270
1264
|
it 'should override default values when parameters are passed' do
|
|
@@ -1295,7 +1289,7 @@ describe "Routing" do
|
|
|
1295
1289
|
|
|
1296
1290
|
it 'should filters by media type' do
|
|
1297
1291
|
mock_app do
|
|
1298
|
-
get '/foo', :
|
|
1292
|
+
get '/foo', accepts: %i[xml json] do
|
|
1299
1293
|
request.env['CONTENT_TYPE']
|
|
1300
1294
|
end
|
|
1301
1295
|
end
|
|
@@ -1317,7 +1311,7 @@ describe "Routing" do
|
|
|
1317
1311
|
|
|
1318
1312
|
it 'should filters by media type when using :accepts as controller option' do
|
|
1319
1313
|
mock_app do
|
|
1320
|
-
controller accepts: [
|
|
1314
|
+
controller accepts: %i[xml js] do
|
|
1321
1315
|
get '/foo' do
|
|
1322
1316
|
request.env['CONTENT_TYPE']
|
|
1323
1317
|
end
|
|
@@ -1331,7 +1325,7 @@ describe "Routing" do
|
|
|
1331
1325
|
|
|
1332
1326
|
it 'should filters by accept header' do
|
|
1333
1327
|
mock_app do
|
|
1334
|
-
get '/foo', :
|
|
1328
|
+
get '/foo', provides: %i[xml js] do
|
|
1335
1329
|
request.env['HTTP_ACCEPT']
|
|
1336
1330
|
end
|
|
1337
1331
|
end
|
|
@@ -1354,7 +1348,7 @@ describe "Routing" do
|
|
|
1354
1348
|
assert ok?
|
|
1355
1349
|
assert_equal 'text/javascript;charset=utf-8', response.headers['Content-Type']
|
|
1356
1350
|
|
|
1357
|
-
get '/foo', {}, {
|
|
1351
|
+
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html' }
|
|
1358
1352
|
assert_equal 406, status
|
|
1359
1353
|
end
|
|
1360
1354
|
|
|
@@ -1362,8 +1356,8 @@ describe "Routing" do
|
|
|
1362
1356
|
mock_app do
|
|
1363
1357
|
provides :xml
|
|
1364
1358
|
|
|
1365
|
-
get(
|
|
1366
|
-
get(
|
|
1359
|
+
get('/foo') { "Foo in #{content_type.inspect}" }
|
|
1360
|
+
get('/bar') { "Bar in #{content_type.inspect}" }
|
|
1367
1361
|
end
|
|
1368
1362
|
|
|
1369
1363
|
get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
|
|
@@ -1380,8 +1374,8 @@ describe "Routing" do
|
|
|
1380
1374
|
controller :base do
|
|
1381
1375
|
provides :xml
|
|
1382
1376
|
|
|
1383
|
-
get(:foo,
|
|
1384
|
-
get(:bar,
|
|
1377
|
+
get(:foo, '/foo') { "Foo in #{content_type.inspect}" }
|
|
1378
|
+
get(:bar, '/bar') { "Bar in #{content_type.inspect}" }
|
|
1385
1379
|
end
|
|
1386
1380
|
end
|
|
1387
1381
|
|
|
@@ -1397,14 +1391,14 @@ describe "Routing" do
|
|
|
1397
1391
|
it 'should map non named routes in controllers' do
|
|
1398
1392
|
mock_app do
|
|
1399
1393
|
controller :base do
|
|
1400
|
-
get(
|
|
1401
|
-
get(
|
|
1394
|
+
get('/foo') { 'ok' }
|
|
1395
|
+
get('/bar') { 'ok' }
|
|
1402
1396
|
end
|
|
1403
1397
|
end
|
|
1404
1398
|
|
|
1405
|
-
get
|
|
1399
|
+
get '/base/foo'
|
|
1406
1400
|
assert ok?
|
|
1407
|
-
get
|
|
1401
|
+
get '/base/bar'
|
|
1408
1402
|
assert ok?
|
|
1409
1403
|
end
|
|
1410
1404
|
|
|
@@ -1412,7 +1406,7 @@ describe "Routing" do
|
|
|
1412
1406
|
mock_app do
|
|
1413
1407
|
provides :html
|
|
1414
1408
|
|
|
1415
|
-
get(
|
|
1409
|
+
get('/foo') { content_type.to_s }
|
|
1416
1410
|
end
|
|
1417
1411
|
|
|
1418
1412
|
get '/foo', {}, {}
|
|
@@ -1424,7 +1418,7 @@ describe "Routing" do
|
|
|
1424
1418
|
|
|
1425
1419
|
it 'should set content_type to :html if Accept */*' do
|
|
1426
1420
|
mock_app do
|
|
1427
|
-
get(
|
|
1421
|
+
get('/foo', provides: %i[html js]) { content_type.to_s }
|
|
1428
1422
|
end
|
|
1429
1423
|
get '/foo', {}, {}
|
|
1430
1424
|
assert_equal 'html', body
|
|
@@ -1435,7 +1429,7 @@ describe "Routing" do
|
|
|
1435
1429
|
|
|
1436
1430
|
it 'should set content_type to :js if Accept includes both text/javascript and */*;q=0.5' do
|
|
1437
1431
|
mock_app do
|
|
1438
|
-
get(
|
|
1432
|
+
get('/foo', provides: %i[html js]) { content_type.to_s }
|
|
1439
1433
|
end
|
|
1440
1434
|
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/javascript, */*;q=0.5' }
|
|
1441
1435
|
assert_equal 'js', body
|
|
@@ -1443,7 +1437,7 @@ describe "Routing" do
|
|
|
1443
1437
|
|
|
1444
1438
|
it 'should set content_type to :html if Accept */* and provides of :any' do
|
|
1445
1439
|
mock_app do
|
|
1446
|
-
get(
|
|
1440
|
+
get('/foo', provides: :any) { content_type.to_s }
|
|
1447
1441
|
end
|
|
1448
1442
|
|
|
1449
1443
|
get '/foo', {}, { 'HTTP_ACCEPT' => '*/*' }
|
|
@@ -1452,7 +1446,7 @@ describe "Routing" do
|
|
|
1452
1446
|
|
|
1453
1447
|
it 'should set content_type to :js if Accept includes both text/javascript, */*;q=0.5 and provides of :any' do
|
|
1454
1448
|
mock_app do
|
|
1455
|
-
get(
|
|
1449
|
+
get('/foo', provides: :any) { content_type.to_s }
|
|
1456
1450
|
end
|
|
1457
1451
|
|
|
1458
1452
|
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/javascript, */*;q=0.5' }
|
|
@@ -1462,11 +1456,7 @@ describe "Routing" do
|
|
|
1462
1456
|
it 'should allows custom route-conditions to be set via route options and halt' do
|
|
1463
1457
|
protector = Module.new do
|
|
1464
1458
|
def protect(*args)
|
|
1465
|
-
condition {
|
|
1466
|
-
unless authorize(params["user"], params["password"])
|
|
1467
|
-
halt 403, "go away"
|
|
1468
|
-
end
|
|
1469
|
-
}
|
|
1459
|
+
condition { halt(403, 'go away') unless authorize(params['user'], params['password']) }
|
|
1470
1460
|
end
|
|
1471
1461
|
end
|
|
1472
1462
|
|
|
@@ -1475,28 +1465,28 @@ describe "Routing" do
|
|
|
1475
1465
|
|
|
1476
1466
|
helpers do
|
|
1477
1467
|
def authorize(username, password)
|
|
1478
|
-
username ==
|
|
1468
|
+
username == 'foo' && password == 'bar'
|
|
1479
1469
|
end
|
|
1480
1470
|
end
|
|
1481
1471
|
|
|
1482
|
-
get
|
|
1483
|
-
|
|
1472
|
+
get '/', protect: true do
|
|
1473
|
+
'hey'
|
|
1484
1474
|
end
|
|
1485
1475
|
end
|
|
1486
1476
|
|
|
1487
|
-
get
|
|
1477
|
+
get '/'
|
|
1488
1478
|
assert forbidden?
|
|
1489
|
-
assert_equal
|
|
1479
|
+
assert_equal 'go away', body
|
|
1490
1480
|
|
|
1491
|
-
get
|
|
1481
|
+
get '/', user: 'foo', password: 'bar'
|
|
1492
1482
|
assert ok?
|
|
1493
|
-
assert_equal
|
|
1483
|
+
assert_equal 'hey', body
|
|
1494
1484
|
end
|
|
1495
1485
|
|
|
1496
1486
|
it 'should allows custom route-conditions to be set via route options using two routes' do
|
|
1497
1487
|
protector = Module.new do
|
|
1498
1488
|
def protect(*args)
|
|
1499
|
-
condition { authorize(params[
|
|
1489
|
+
condition { authorize(params['user'], params['password']) }
|
|
1500
1490
|
end
|
|
1501
1491
|
end
|
|
1502
1492
|
|
|
@@ -1505,57 +1495,57 @@ describe "Routing" do
|
|
|
1505
1495
|
|
|
1506
1496
|
helpers do
|
|
1507
1497
|
def authorize(username, password)
|
|
1508
|
-
username ==
|
|
1498
|
+
username == 'foo' && password == 'bar'
|
|
1509
1499
|
end
|
|
1510
1500
|
end
|
|
1511
1501
|
|
|
1512
|
-
get
|
|
1513
|
-
|
|
1502
|
+
get '/', protect: true do
|
|
1503
|
+
'hey'
|
|
1514
1504
|
end
|
|
1515
1505
|
|
|
1516
|
-
get
|
|
1517
|
-
|
|
1506
|
+
get '/' do
|
|
1507
|
+
'go away'
|
|
1518
1508
|
end
|
|
1519
1509
|
end
|
|
1520
1510
|
|
|
1521
|
-
get
|
|
1522
|
-
assert_equal
|
|
1511
|
+
get '/'
|
|
1512
|
+
assert_equal 'go away', body
|
|
1523
1513
|
|
|
1524
|
-
get
|
|
1514
|
+
get '/', user: 'foo', password: 'bar'
|
|
1525
1515
|
assert ok?
|
|
1526
|
-
assert_equal
|
|
1516
|
+
assert_equal 'hey', body
|
|
1527
1517
|
end
|
|
1528
1518
|
|
|
1529
1519
|
it 'should allow concise routing' do
|
|
1530
1520
|
mock_app do
|
|
1531
|
-
get :index,
|
|
1521
|
+
get :index, ':id' do
|
|
1532
1522
|
params[:id]
|
|
1533
1523
|
end
|
|
1534
1524
|
|
|
1535
|
-
get :map,
|
|
1525
|
+
get :map, 'route/:id' do
|
|
1536
1526
|
params[:id]
|
|
1537
1527
|
end
|
|
1538
1528
|
end
|
|
1539
1529
|
|
|
1540
|
-
get
|
|
1541
|
-
assert_equal
|
|
1530
|
+
get '/123'
|
|
1531
|
+
assert_equal '123', body
|
|
1542
1532
|
|
|
1543
|
-
get
|
|
1544
|
-
assert_equal
|
|
1533
|
+
get '/route/123'
|
|
1534
|
+
assert_equal '123', body
|
|
1545
1535
|
end
|
|
1546
1536
|
|
|
1547
1537
|
it 'should support halting with 404 and message' do
|
|
1548
1538
|
mock_app do
|
|
1549
1539
|
controller do
|
|
1550
1540
|
get :index do
|
|
1551
|
-
halt 404,
|
|
1541
|
+
halt 404, 'not found'
|
|
1552
1542
|
end
|
|
1553
1543
|
end
|
|
1554
1544
|
end
|
|
1555
1545
|
|
|
1556
|
-
get
|
|
1546
|
+
get '/'
|
|
1557
1547
|
assert_equal 404, status
|
|
1558
|
-
assert_equal
|
|
1548
|
+
assert_equal 'not found', body
|
|
1559
1549
|
end
|
|
1560
1550
|
|
|
1561
1551
|
it 'should allow passing & halting in before filters' do
|
|
@@ -1563,27 +1553,27 @@ describe "Routing" do
|
|
|
1563
1553
|
controller do
|
|
1564
1554
|
before { env['QUERY_STRING'] == 'secret' or pass }
|
|
1565
1555
|
get :index do
|
|
1566
|
-
|
|
1556
|
+
'secret index'
|
|
1567
1557
|
end
|
|
1568
1558
|
end
|
|
1569
1559
|
|
|
1570
1560
|
controller do
|
|
1571
1561
|
before { env['QUERY_STRING'] == 'halt' and halt 401, 'go away!' }
|
|
1572
1562
|
get :index do
|
|
1573
|
-
|
|
1563
|
+
'index'
|
|
1574
1564
|
end
|
|
1575
1565
|
end
|
|
1576
1566
|
end
|
|
1577
1567
|
|
|
1578
|
-
get
|
|
1579
|
-
assert_equal
|
|
1568
|
+
get '/?secret'
|
|
1569
|
+
assert_equal 'secret index', body
|
|
1580
1570
|
|
|
1581
|
-
get
|
|
1582
|
-
assert_equal
|
|
1571
|
+
get '/?halt'
|
|
1572
|
+
assert_equal 'go away!', body
|
|
1583
1573
|
assert_equal 401, status
|
|
1584
1574
|
|
|
1585
|
-
get
|
|
1586
|
-
assert_equal
|
|
1575
|
+
get '/'
|
|
1576
|
+
assert_equal 'index', body
|
|
1587
1577
|
end
|
|
1588
1578
|
|
|
1589
1579
|
it 'should scope filters in the given controller' do
|
|
@@ -1594,51 +1584,51 @@ describe "Routing" do
|
|
|
1594
1584
|
controller :foo do
|
|
1595
1585
|
before { @foo = :foo }
|
|
1596
1586
|
after { @foo = nil }
|
|
1597
|
-
get(
|
|
1587
|
+
get('/') { [@foo, @bar, @global].compact.join(' ') }
|
|
1598
1588
|
end
|
|
1599
1589
|
|
|
1600
|
-
get(
|
|
1590
|
+
get('/') { [@foo, @bar, @global].compact.join(' ') }
|
|
1601
1591
|
|
|
1602
1592
|
controller :bar do
|
|
1603
1593
|
before { @bar = :bar }
|
|
1604
1594
|
after { @bar = nil }
|
|
1605
|
-
get(
|
|
1595
|
+
get('/') { [@foo, @bar, @global].compact.join(' ') }
|
|
1606
1596
|
end
|
|
1607
1597
|
end
|
|
1608
1598
|
|
|
1609
|
-
get
|
|
1610
|
-
assert_equal
|
|
1599
|
+
get '/bar'
|
|
1600
|
+
assert_equal 'bar global', body
|
|
1611
1601
|
|
|
1612
|
-
get
|
|
1613
|
-
assert_equal
|
|
1602
|
+
get '/foo'
|
|
1603
|
+
assert_equal 'foo global', body
|
|
1614
1604
|
|
|
1615
|
-
get
|
|
1616
|
-
assert_equal
|
|
1605
|
+
get '/'
|
|
1606
|
+
assert_equal 'global', body
|
|
1617
1607
|
end
|
|
1618
1608
|
|
|
1619
1609
|
it 'should works with optionals params' do
|
|
1620
1610
|
mock_app do
|
|
1621
|
-
get(
|
|
1611
|
+
get('/foo(/:bar)?') { params[:bar] }
|
|
1622
1612
|
end
|
|
1623
1613
|
|
|
1624
|
-
get
|
|
1625
|
-
assert_equal
|
|
1614
|
+
get '/foo/bar'
|
|
1615
|
+
assert_equal 'bar', body
|
|
1626
1616
|
|
|
1627
|
-
get
|
|
1628
|
-
assert_equal
|
|
1617
|
+
get '/foo'
|
|
1618
|
+
assert_equal '', body
|
|
1629
1619
|
end
|
|
1630
1620
|
|
|
1631
1621
|
it 'should work with multiple dashed params' do
|
|
1632
1622
|
mock_app do
|
|
1633
|
-
get
|
|
1623
|
+
get '/route/:foo/:bar/:baz', provides: :html do
|
|
1634
1624
|
"#{params[:foo]};#{params[:bar]};#{params[:baz]}"
|
|
1635
1625
|
end
|
|
1636
1626
|
end
|
|
1637
1627
|
|
|
1638
|
-
get
|
|
1628
|
+
get '/route/foo/bar/baz'
|
|
1639
1629
|
assert_equal 'foo;bar;baz', body
|
|
1640
1630
|
|
|
1641
|
-
get
|
|
1631
|
+
get '/route/foo/bar-whatever/baz'
|
|
1642
1632
|
assert_equal 'foo;bar-whatever;baz', body
|
|
1643
1633
|
end
|
|
1644
1634
|
|
|
@@ -1647,19 +1637,19 @@ describe "Routing" do
|
|
|
1647
1637
|
get(:testing) { params[:foo] }
|
|
1648
1638
|
end
|
|
1649
1639
|
|
|
1650
|
-
url = @app.url(:testing, :
|
|
1651
|
-
assert_equal
|
|
1640
|
+
url = @app.url(:testing, foo: 'bar')
|
|
1641
|
+
assert_equal '/testing?foo=bar', url
|
|
1652
1642
|
get url
|
|
1653
|
-
assert_equal
|
|
1643
|
+
assert_equal 'bar', body
|
|
1654
1644
|
end
|
|
1655
1645
|
|
|
1656
1646
|
it 'should ignore nil params' do
|
|
1657
1647
|
mock_app do
|
|
1658
|
-
get(:testing, :
|
|
1648
|
+
get(:testing, provides: %i[html json]) do
|
|
1659
1649
|
end
|
|
1660
1650
|
end
|
|
1661
|
-
assert_equal '/testing.html', @app.url(:testing, :
|
|
1662
|
-
assert_equal '/testing', @app.url(:testing, :
|
|
1651
|
+
assert_equal '/testing.html', @app.url(:testing, format: :html)
|
|
1652
|
+
assert_equal '/testing', @app.url(:testing, format: nil)
|
|
1663
1653
|
end
|
|
1664
1654
|
|
|
1665
1655
|
it 'should be able to access params in a before filter' do
|
|
@@ -1670,7 +1660,7 @@ describe "Routing" do
|
|
|
1670
1660
|
username_from_before_filter = params[:username]
|
|
1671
1661
|
end
|
|
1672
1662
|
|
|
1673
|
-
get :users, :
|
|
1663
|
+
get :users, with: :username do
|
|
1674
1664
|
end
|
|
1675
1665
|
end
|
|
1676
1666
|
get '/users/josh'
|
|
@@ -1679,7 +1669,7 @@ describe "Routing" do
|
|
|
1679
1669
|
|
|
1680
1670
|
it 'should be able to access params normally when a before filter is specified' do
|
|
1681
1671
|
mock_app do
|
|
1682
|
-
before {
|
|
1672
|
+
before {}
|
|
1683
1673
|
get :index do
|
|
1684
1674
|
params.to_json
|
|
1685
1675
|
end
|
|
@@ -1691,7 +1681,7 @@ describe "Routing" do
|
|
|
1691
1681
|
it 'should work only for the given controller and route when using before-filter with route name' do
|
|
1692
1682
|
mock_app do
|
|
1693
1683
|
controller :foo do
|
|
1694
|
-
before(:index) { @a =
|
|
1684
|
+
before(:index) { @a = 'only to :index' }
|
|
1695
1685
|
get(:index) { @a }
|
|
1696
1686
|
get(:main) { @a }
|
|
1697
1687
|
end
|
|
@@ -1705,7 +1695,7 @@ describe "Routing" do
|
|
|
1705
1695
|
it 'should work only for the given controller and route when using after-filter with route name' do
|
|
1706
1696
|
mock_app do
|
|
1707
1697
|
controller :after_controller do
|
|
1708
|
-
global =
|
|
1698
|
+
global = 'global variable'
|
|
1709
1699
|
get(:index) { global }
|
|
1710
1700
|
get(:main) { global }
|
|
1711
1701
|
after(:index) { global = nil }
|
|
@@ -1720,11 +1710,11 @@ describe "Routing" do
|
|
|
1720
1710
|
it 'should execute the before/after filters when they are inserted after the target route' do
|
|
1721
1711
|
mock_app do
|
|
1722
1712
|
controller :after_test do
|
|
1723
|
-
global =
|
|
1713
|
+
global = 'global variable'
|
|
1724
1714
|
get(:index) { global }
|
|
1725
1715
|
get(:foo) { global }
|
|
1726
|
-
before(:index) { global.delete!(
|
|
1727
|
-
after(:index) { global =
|
|
1716
|
+
before(:index) { global.delete!(' ') }
|
|
1717
|
+
after(:index) { global = 'after' }
|
|
1728
1718
|
end
|
|
1729
1719
|
end
|
|
1730
1720
|
get '/after_test'
|
|
@@ -1738,103 +1728,103 @@ describe "Routing" do
|
|
|
1738
1728
|
get(:testing) { params[:foo] }
|
|
1739
1729
|
controller :test1 do
|
|
1740
1730
|
get(:url1) { params[:foo] }
|
|
1741
|
-
get(:url2, :
|
|
1731
|
+
get(:url2, provides: %i[html json]) { params[:foo] }
|
|
1742
1732
|
end
|
|
1743
1733
|
end
|
|
1744
1734
|
|
|
1745
|
-
url = @app.url(:test1, :url1, :
|
|
1746
|
-
assert_equal
|
|
1735
|
+
url = @app.url(:test1, :url1, foo: 'bar1')
|
|
1736
|
+
assert_equal '/test1/url1?foo=bar1', url
|
|
1747
1737
|
get url
|
|
1748
|
-
assert_equal
|
|
1738
|
+
assert_equal 'bar1', body
|
|
1749
1739
|
|
|
1750
|
-
url = @app.url(:test1, :url2, :
|
|
1751
|
-
assert_equal
|
|
1740
|
+
url = @app.url(:test1, :url2, foo: 'bar2')
|
|
1741
|
+
assert_equal '/test1/url2?foo=bar2', url
|
|
1752
1742
|
get url
|
|
1753
|
-
assert_equal
|
|
1743
|
+
assert_equal 'bar2', body
|
|
1754
1744
|
end
|
|
1755
1745
|
|
|
1756
1746
|
it 'should parse two routes with the same path but different http verbs' do
|
|
1757
1747
|
mock_app do
|
|
1758
|
-
get(:index) {
|
|
1759
|
-
post(:index) {
|
|
1748
|
+
get(:index) { 'This is the get index' }
|
|
1749
|
+
post(:index) { 'This is the post index' }
|
|
1760
1750
|
end
|
|
1761
|
-
get
|
|
1762
|
-
assert_equal
|
|
1763
|
-
post
|
|
1764
|
-
assert_equal
|
|
1751
|
+
get '/'
|
|
1752
|
+
assert_equal 'This is the get index', body
|
|
1753
|
+
post '/'
|
|
1754
|
+
assert_equal 'This is the post index', body
|
|
1765
1755
|
end
|
|
1766
1756
|
|
|
1767
1757
|
it 'should use optionals params' do
|
|
1768
1758
|
mock_app do
|
|
1769
|
-
get(:index, :
|
|
1759
|
+
get(:index, map: '/:foo(/:bar)?') { "#{params[:foo]}-#{params[:bar]}" }
|
|
1770
1760
|
end
|
|
1771
|
-
get
|
|
1772
|
-
assert_equal
|
|
1773
|
-
get
|
|
1774
|
-
assert_equal
|
|
1761
|
+
get '/foo'
|
|
1762
|
+
assert_equal 'foo-', body
|
|
1763
|
+
get '/foo/bar'
|
|
1764
|
+
assert_equal 'foo-bar', body
|
|
1775
1765
|
end
|
|
1776
1766
|
|
|
1777
1767
|
it 'should parse two routes with the same path but different http verbs and provides' do
|
|
1778
1768
|
mock_app do
|
|
1779
|
-
get(:index, :
|
|
1780
|
-
post(:index, :
|
|
1769
|
+
get(:index, provides: %i[html json]) { "This is the get index.#{content_type}" }
|
|
1770
|
+
post(:index, provides: %i[html json]) { "This is the post index.#{content_type}" }
|
|
1781
1771
|
end
|
|
1782
|
-
get
|
|
1783
|
-
assert_equal
|
|
1784
|
-
post
|
|
1785
|
-
assert_equal
|
|
1786
|
-
get
|
|
1787
|
-
assert_equal
|
|
1788
|
-
get
|
|
1772
|
+
get '/'
|
|
1773
|
+
assert_equal 'This is the get index.html', body
|
|
1774
|
+
post '/'
|
|
1775
|
+
assert_equal 'This is the post index.html', body
|
|
1776
|
+
get '/.json'
|
|
1777
|
+
assert_equal 'This is the get index.json', body
|
|
1778
|
+
get '/.js'
|
|
1789
1779
|
assert_equal 404, status
|
|
1790
|
-
post
|
|
1791
|
-
assert_equal
|
|
1792
|
-
post
|
|
1780
|
+
post '/.json'
|
|
1781
|
+
assert_equal 'This is the post index.json', body
|
|
1782
|
+
post '/.js'
|
|
1793
1783
|
assert_equal 404, status
|
|
1794
1784
|
end
|
|
1795
1785
|
|
|
1796
1786
|
it 'should allow controller level mapping' do
|
|
1797
1787
|
mock_app do
|
|
1798
|
-
controller :
|
|
1799
|
-
get(:url3) {
|
|
1800
|
-
get(:url4, :
|
|
1788
|
+
controller map: 'controller-:id' do
|
|
1789
|
+
get(:url3) { params[:id].to_s }
|
|
1790
|
+
get(:url4, map: 'test-:id2') { "#{params[:id]}, #{params[:id2]}" }
|
|
1801
1791
|
end
|
|
1802
1792
|
end
|
|
1803
1793
|
|
|
1804
|
-
url = @app.url(:url3, :
|
|
1805
|
-
assert_equal
|
|
1794
|
+
url = @app.url(:url3, id: 1)
|
|
1795
|
+
assert_equal '/controller-1/url3', url
|
|
1806
1796
|
get url
|
|
1807
|
-
assert_equal
|
|
1797
|
+
assert_equal '1', body
|
|
1808
1798
|
|
|
1809
1799
|
url = @app.url(:url4, 1, 2)
|
|
1810
|
-
assert_equal
|
|
1800
|
+
assert_equal '/controller-1/test-2', url
|
|
1811
1801
|
get url
|
|
1812
|
-
assert_equal
|
|
1802
|
+
assert_equal '1, 2', body
|
|
1813
1803
|
end
|
|
1814
1804
|
|
|
1815
1805
|
it 'should replace name of named controller with mapping path' do
|
|
1816
1806
|
mock_app do
|
|
1817
|
-
controller :ugly, :
|
|
1818
|
-
get(:url3) {
|
|
1819
|
-
get(:url4, :
|
|
1807
|
+
controller :ugly, map: '/pretty/:id' do
|
|
1808
|
+
get(:url3) { params[:id].to_s }
|
|
1809
|
+
get(:url4, map: 'test-:id2') { "#{params[:id]}, #{params[:id2]}" }
|
|
1820
1810
|
end
|
|
1821
|
-
controller :voldemort, :
|
|
1822
|
-
get(:url5) {
|
|
1811
|
+
controller :voldemort, map: '' do
|
|
1812
|
+
get(:url5) { 'okay' }
|
|
1823
1813
|
end
|
|
1824
1814
|
end
|
|
1825
1815
|
|
|
1826
|
-
url = @app.url(:ugly, :url3, :
|
|
1827
|
-
assert_equal
|
|
1816
|
+
url = @app.url(:ugly, :url3, id: 1)
|
|
1817
|
+
assert_equal '/pretty/1/url3', url
|
|
1828
1818
|
get url
|
|
1829
|
-
assert_equal
|
|
1819
|
+
assert_equal '1', body
|
|
1830
1820
|
|
|
1831
1821
|
url = @app.url(:ugly, :url4, 3, 5)
|
|
1832
|
-
assert_equal
|
|
1822
|
+
assert_equal '/pretty/3/test-5', url
|
|
1833
1823
|
get url
|
|
1834
|
-
assert_equal
|
|
1824
|
+
assert_equal '3, 5', body
|
|
1835
1825
|
|
|
1836
1826
|
url = @app.url(:voldemort, :url5)
|
|
1837
|
-
assert_equal
|
|
1827
|
+
assert_equal '/url5', url
|
|
1838
1828
|
get url
|
|
1839
1829
|
assert_equal 'okay', body
|
|
1840
1830
|
end
|
|
@@ -1843,105 +1833,105 @@ describe "Routing" do
|
|
|
1843
1833
|
mock_app do
|
|
1844
1834
|
controller :one do
|
|
1845
1835
|
parent :three
|
|
1846
|
-
get :index, :
|
|
1847
|
-
get :index2, :
|
|
1836
|
+
get :index, map: 'one' do; end
|
|
1837
|
+
get :index2, map: '/one' do; end
|
|
1848
1838
|
end
|
|
1849
1839
|
|
|
1850
|
-
controller :two, :
|
|
1840
|
+
controller :two, map: 'two' do
|
|
1851
1841
|
parent :three
|
|
1852
|
-
get :index, :
|
|
1853
|
-
get :index2, :
|
|
1842
|
+
get :index, map: 'two' do; end
|
|
1843
|
+
get :index2, map: '/two', with: :id do; end
|
|
1854
1844
|
end
|
|
1855
1845
|
end
|
|
1856
|
-
assert_equal
|
|
1857
|
-
assert_equal
|
|
1858
|
-
assert_equal
|
|
1859
|
-
assert_equal
|
|
1846
|
+
assert_equal '/three/three_id/one', @app.url(:one, :index, 'three_id')
|
|
1847
|
+
assert_equal '/one', @app.url(:one, :index2)
|
|
1848
|
+
assert_equal '/two/three/three_id/two', @app.url(:two, :index, 'three_id')
|
|
1849
|
+
assert_equal '/two/four_id', @app.url(:two, :index2, 'four_id')
|
|
1860
1850
|
end
|
|
1861
1851
|
|
|
1862
1852
|
it 'should work with params and parent options' do
|
|
1863
1853
|
mock_app do
|
|
1864
|
-
controller :test2, :
|
|
1854
|
+
controller :test2, parent: :parent1, parent1_id: 1 do
|
|
1865
1855
|
get(:url3) { params[:foo] }
|
|
1866
|
-
get(:url4, :
|
|
1867
|
-
get(:url5, :
|
|
1856
|
+
get(:url4, with: :with1) { params[:foo] }
|
|
1857
|
+
get(:url5, with: :with2, provides: [:html]) { params[:foo] }
|
|
1868
1858
|
end
|
|
1869
1859
|
end
|
|
1870
1860
|
|
|
1871
|
-
url = @app.url(:test2, :url3, :
|
|
1872
|
-
assert_equal
|
|
1861
|
+
url = @app.url(:test2, :url3, foo: 'bar3')
|
|
1862
|
+
assert_equal '/parent1/1/test2/url3?foo=bar3', url
|
|
1873
1863
|
get url
|
|
1874
|
-
assert_equal
|
|
1864
|
+
assert_equal 'bar3', body
|
|
1875
1865
|
|
|
1876
|
-
url = @app.url(:test2, :url4, :
|
|
1877
|
-
assert_equal
|
|
1866
|
+
url = @app.url(:test2, :url4, with1: 'awith1', foo: 'bar4')
|
|
1867
|
+
assert_equal '/parent1/1/test2/url4/awith1?foo=bar4', url
|
|
1878
1868
|
get url
|
|
1879
|
-
assert_equal
|
|
1869
|
+
assert_equal 'bar4', body
|
|
1880
1870
|
|
|
1881
|
-
url = @app.url(:test2, :url5, :
|
|
1882
|
-
assert_equal
|
|
1871
|
+
url = @app.url(:test2, :url5, with2: 'awith1', foo: 'bar5')
|
|
1872
|
+
assert_equal '/parent1/1/test2/url5/awith1?foo=bar5', url
|
|
1883
1873
|
get url
|
|
1884
|
-
assert_equal
|
|
1874
|
+
assert_equal 'bar5', body
|
|
1885
1875
|
end
|
|
1886
1876
|
|
|
1887
1877
|
it 'should parse params without explicit provides for every matching route' do
|
|
1888
1878
|
mock_app do
|
|
1889
|
-
get(:index, :
|
|
1890
|
-
post :create, :
|
|
1879
|
+
get(:index, map: '/foos/:bar') { "get bar = #{params[:bar]}" }
|
|
1880
|
+
post :create, map: '/foos/:bar', provides: %i[html js] do
|
|
1891
1881
|
"post bar = #{params[:bar]}"
|
|
1892
1882
|
end
|
|
1893
1883
|
end
|
|
1894
1884
|
|
|
1895
|
-
get
|
|
1896
|
-
assert_equal
|
|
1897
|
-
post
|
|
1898
|
-
assert_equal
|
|
1899
|
-
post
|
|
1900
|
-
assert_equal
|
|
1885
|
+
get '/foos/hello'
|
|
1886
|
+
assert_equal 'get bar = hello', body
|
|
1887
|
+
post '/foos/hello'
|
|
1888
|
+
assert_equal 'post bar = hello', body
|
|
1889
|
+
post '/foos/hello.js'
|
|
1890
|
+
assert_equal 'post bar = hello', body
|
|
1901
1891
|
end
|
|
1902
1892
|
|
|
1903
1893
|
it 'should properly route to first foo with two similar routes' do
|
|
1904
1894
|
mock_app do
|
|
1905
1895
|
controllers do
|
|
1906
|
-
get('/foo/') {
|
|
1907
|
-
get(:show, :
|
|
1896
|
+
get('/foo/') { 'this is foo' }
|
|
1897
|
+
get(:show, map: '/foo/:bar/:id') { "/foo/#{params[:bar]}/#{params[:id]}" }
|
|
1908
1898
|
end
|
|
1909
1899
|
end
|
|
1910
|
-
get
|
|
1911
|
-
assert_equal
|
|
1912
|
-
get
|
|
1913
|
-
assert_equal
|
|
1900
|
+
get '/foo'
|
|
1901
|
+
assert_equal 'this is foo', body
|
|
1902
|
+
get '/foo/'
|
|
1903
|
+
assert_equal 'this is foo', body
|
|
1914
1904
|
get '/foo/5/10'
|
|
1915
|
-
assert_equal
|
|
1905
|
+
assert_equal '/foo/5/10', body
|
|
1916
1906
|
end
|
|
1917
1907
|
|
|
1918
1908
|
it 'should index routes should be optional when nested' do
|
|
1919
1909
|
mock_app do
|
|
1920
|
-
controller '/users', :
|
|
1910
|
+
controller '/users', provides: [:json] do
|
|
1921
1911
|
get '/' do
|
|
1922
|
-
|
|
1912
|
+
'foo'
|
|
1923
1913
|
end
|
|
1924
1914
|
end
|
|
1925
1915
|
end
|
|
1926
|
-
get
|
|
1927
|
-
assert_equal
|
|
1916
|
+
get '/users.json'
|
|
1917
|
+
assert_equal 'foo', body
|
|
1928
1918
|
end
|
|
1929
1919
|
|
|
1930
1920
|
it 'should use provides as conditional' do
|
|
1931
1921
|
mock_app do
|
|
1932
1922
|
provides :json
|
|
1933
|
-
get
|
|
1934
|
-
|
|
1923
|
+
get '/' do
|
|
1924
|
+
'foo'
|
|
1935
1925
|
end
|
|
1936
1926
|
end
|
|
1937
|
-
get
|
|
1938
|
-
assert_equal
|
|
1927
|
+
get '/.json'
|
|
1928
|
+
assert_equal 'foo', body
|
|
1939
1929
|
end
|
|
1940
1930
|
|
|
1941
1931
|
it 'should reset provides for routes that did not use it' do
|
|
1942
1932
|
mock_app do
|
|
1943
|
-
get('/foo', :
|
|
1944
|
-
get('/bar'){}
|
|
1933
|
+
get('/foo', provides: :js) {}
|
|
1934
|
+
get('/bar') {}
|
|
1945
1935
|
end
|
|
1946
1936
|
get '/foo'
|
|
1947
1937
|
assert ok?
|
|
@@ -1961,15 +1951,14 @@ describe "Routing" do
|
|
|
1961
1951
|
condition { counter += 1 }
|
|
1962
1952
|
end
|
|
1963
1953
|
|
|
1964
|
-
controller :posts, :
|
|
1965
|
-
get(
|
|
1966
|
-
get(
|
|
1954
|
+
controller :posts, conditions: { increment!: true } do
|
|
1955
|
+
get('/foo') { 'foo' }
|
|
1956
|
+
get('/bar') { 'bar' }
|
|
1967
1957
|
end
|
|
1968
|
-
|
|
1969
1958
|
end
|
|
1970
1959
|
|
|
1971
|
-
get
|
|
1972
|
-
get
|
|
1960
|
+
get '/posts/foo'
|
|
1961
|
+
get '/posts/bar'
|
|
1973
1962
|
assert_equal 2, counter
|
|
1974
1963
|
end
|
|
1975
1964
|
|
|
@@ -1981,68 +1970,67 @@ describe "Routing" do
|
|
|
1981
1970
|
condition { counter += 1 } if increment
|
|
1982
1971
|
end
|
|
1983
1972
|
|
|
1984
|
-
controller :posts, :
|
|
1985
|
-
get(
|
|
1986
|
-
get(
|
|
1973
|
+
controller :posts, conditions: { increment!: true } do
|
|
1974
|
+
get('/foo') { 'foo' }
|
|
1975
|
+
get('/bar', increment!: false) { 'bar' }
|
|
1987
1976
|
end
|
|
1988
|
-
|
|
1989
1977
|
end
|
|
1990
1978
|
|
|
1991
|
-
get
|
|
1992
|
-
get
|
|
1979
|
+
get '/posts/foo'
|
|
1980
|
+
get '/posts/bar'
|
|
1993
1981
|
assert_equal 1, counter
|
|
1994
1982
|
end
|
|
1995
1983
|
|
|
1996
1984
|
it 'should parse params with class level provides' do
|
|
1997
1985
|
mock_app do
|
|
1998
|
-
controllers :posts, :
|
|
1999
|
-
post(:create, :
|
|
1986
|
+
controllers :posts, provides: %i[html js] do
|
|
1987
|
+
post(:create, map: '/foo/:bar/:baz/:id') do
|
|
2000
1988
|
"POST CREATE #{params[:bar]} - #{params[:baz]} - #{params[:id]}"
|
|
2001
|
-
|
|
1989
|
+
end
|
|
2002
1990
|
end
|
|
2003
|
-
controllers :topics, :
|
|
2004
|
-
get(:show, :
|
|
2005
|
-
post(:create, :
|
|
1991
|
+
controllers :topics, provides: %i[js html] do
|
|
1992
|
+
get(:show, map: '/foo/:bar/:baz/:id') { render 'topics/show' }
|
|
1993
|
+
post(:create, map: '/foo/:bar/:baz') { "TOPICS CREATE #{params[:bar]} - #{params[:baz]}" }
|
|
2006
1994
|
end
|
|
2007
1995
|
end
|
|
2008
|
-
post
|
|
2009
|
-
assert_equal
|
|
2010
|
-
post @app.url(:topics, :create, :
|
|
2011
|
-
assert_equal
|
|
2012
|
-
post
|
|
2013
|
-
assert_equal
|
|
2014
|
-
post @app.url(:posts, :create, :
|
|
2015
|
-
assert_equal
|
|
1996
|
+
post '/foo/bar/baz.js'
|
|
1997
|
+
assert_equal 'TOPICS CREATE bar - baz', body, 'should parse params with explicit .js'
|
|
1998
|
+
post @app.url(:topics, :create, format: :js, bar: 'bar', baz: 'baz')
|
|
1999
|
+
assert_equal 'TOPICS CREATE bar - baz', body, 'should parse params from generated url'
|
|
2000
|
+
post '/foo/bar/baz/5.js'
|
|
2001
|
+
assert_equal 'POST CREATE bar - baz - 5', body
|
|
2002
|
+
post @app.url(:posts, :create, format: :js, bar: 'bar', baz: 'baz', id: 5)
|
|
2003
|
+
assert_equal 'POST CREATE bar - baz - 5', body
|
|
2016
2004
|
end
|
|
2017
2005
|
|
|
2018
2006
|
it 'should parse params properly with inline provides' do
|
|
2019
2007
|
mock_app do
|
|
2020
2008
|
controllers :posts do
|
|
2021
|
-
post(:create, :
|
|
2009
|
+
post(:create, map: '/foo/:bar/:baz/:id', provides: %i[html js]) do
|
|
2022
2010
|
"POST CREATE #{params[:bar]} - #{params[:baz]} - #{params[:id]}"
|
|
2023
|
-
|
|
2011
|
+
end
|
|
2024
2012
|
end
|
|
2025
2013
|
controllers :topics do
|
|
2026
|
-
get(:show, :
|
|
2027
|
-
post(:create, :
|
|
2014
|
+
get(:show, map: '/foo/:bar/:baz/:id', provides: %i[html js]) { render 'topics/show' }
|
|
2015
|
+
post(:create, map: '/foo/:bar/:baz', provides: %i[html js]) { "TOPICS CREATE #{params[:bar]} - #{params[:baz]}" }
|
|
2028
2016
|
end
|
|
2029
2017
|
end
|
|
2030
|
-
post @app.url(:topics, :create, :
|
|
2031
|
-
assert_equal
|
|
2032
|
-
post @app.url(:posts, :create, :
|
|
2033
|
-
assert_equal
|
|
2018
|
+
post @app.url(:topics, :create, format: :js, bar: 'bar', baz: 'baz')
|
|
2019
|
+
assert_equal 'TOPICS CREATE bar - baz', body, 'should properly post to topics create action'
|
|
2020
|
+
post @app.url(:posts, :create, format: :js, bar: 'bar', baz: 'baz', id: 5)
|
|
2021
|
+
assert_equal 'POST CREATE bar - baz - 5', body, 'should properly post to create action'
|
|
2034
2022
|
end
|
|
2035
2023
|
|
|
2036
2024
|
it 'should have overideable format' do
|
|
2037
|
-
::Rack::Mime::MIME_TYPES[
|
|
2025
|
+
::Rack::Mime::MIME_TYPES['.other'] = 'text/html'
|
|
2038
2026
|
mock_app do
|
|
2039
2027
|
before do
|
|
2040
2028
|
params[:format] ||= :other
|
|
2041
2029
|
end
|
|
2042
|
-
get(
|
|
2030
|
+
get('/format_test', provides: %i[html other]) { content_type.to_s }
|
|
2043
2031
|
end
|
|
2044
|
-
get
|
|
2045
|
-
assert_equal
|
|
2032
|
+
get '/format_test'
|
|
2033
|
+
assert_equal 'other', body
|
|
2046
2034
|
::Rack::Mime::MIME_TYPES.delete('.other')
|
|
2047
2035
|
end
|
|
2048
2036
|
|
|
@@ -2064,54 +2052,54 @@ describe "Routing" do
|
|
|
2064
2052
|
put('/') { 'okay' }
|
|
2065
2053
|
end
|
|
2066
2054
|
assert @app.method_override?
|
|
2067
|
-
post '/', {'_method'=>'PUT'}, {}
|
|
2055
|
+
post '/', { '_method' => 'PUT' }, {}
|
|
2068
2056
|
assert_equal 200, status
|
|
2069
2057
|
assert_equal 'okay', body
|
|
2070
2058
|
end
|
|
2071
2059
|
|
|
2072
2060
|
it 'should return value from params' do
|
|
2073
2061
|
mock_app do
|
|
2074
|
-
get(
|
|
2062
|
+
get('/foo/:bar') { raise "'bar' should be a string" unless params[:bar].is_a? String }
|
|
2075
2063
|
end
|
|
2076
|
-
get
|
|
2064
|
+
get '/foo/50'
|
|
2077
2065
|
assert ok?
|
|
2078
2066
|
end
|
|
2079
2067
|
|
|
2080
2068
|
it 'should return params as a HashWithIndifferentAccess object via GET' do
|
|
2081
2069
|
mock_app do
|
|
2082
|
-
get('/foo/:bar') { "#{params[
|
|
2083
|
-
get(:foo, :
|
|
2070
|
+
get('/foo/:bar') { "#{params['bar']} #{params[:bar]}" }
|
|
2071
|
+
get(:foo, map: '/prefix/:var') { "#{params['var']} #{params[:var]}" }
|
|
2084
2072
|
end
|
|
2085
2073
|
|
|
2086
2074
|
get('/foo/some_text')
|
|
2087
|
-
assert_equal
|
|
2075
|
+
assert_equal 'some_text some_text', body
|
|
2088
2076
|
|
|
2089
2077
|
get('/prefix/var')
|
|
2090
|
-
assert_equal
|
|
2078
|
+
assert_equal 'var var', body
|
|
2091
2079
|
end
|
|
2092
2080
|
|
|
2093
2081
|
it 'should return params as a HashWithIndifferentAccess object via POST' do
|
|
2094
2082
|
mock_app do
|
|
2095
2083
|
post('/user') do
|
|
2096
|
-
"#{params[
|
|
2084
|
+
"#{params['user']['full_name']} #{params[:user][:full_name]}"
|
|
2097
2085
|
end
|
|
2098
2086
|
end
|
|
2099
2087
|
|
|
2100
|
-
post '/user', {:
|
|
2101
|
-
assert_equal
|
|
2088
|
+
post '/user', { user: { full_name: 'example user' } }
|
|
2089
|
+
assert_equal 'example user example user', body
|
|
2102
2090
|
|
|
2103
|
-
post '/user', {
|
|
2104
|
-
assert_equal
|
|
2091
|
+
post '/user', { 'user' => { 'full_name' => 'example user' } }
|
|
2092
|
+
assert_equal 'example user example user', body
|
|
2105
2093
|
end
|
|
2106
2094
|
|
|
2107
2095
|
it 'should have MethodOverride middleware with more options' do
|
|
2108
2096
|
mock_app do
|
|
2109
|
-
put('/hi', :
|
|
2097
|
+
put('/hi', provides: [:json]) { 'hi' }
|
|
2110
2098
|
end
|
|
2111
|
-
post '/hi', {'_method'=>'PUT'}
|
|
2099
|
+
post '/hi', { '_method' => 'PUT' }
|
|
2112
2100
|
assert_equal 200, status
|
|
2113
2101
|
assert_equal 'hi', body
|
|
2114
|
-
post '/hi.json', {'_method'=>'PUT'}
|
|
2102
|
+
post '/hi.json', { '_method' => 'PUT' }
|
|
2115
2103
|
assert_equal 200, status
|
|
2116
2104
|
assert_equal 'hi', body
|
|
2117
2105
|
post '/hi.json'
|
|
@@ -2120,161 +2108,161 @@ describe "Routing" do
|
|
|
2120
2108
|
|
|
2121
2109
|
it 'should parse nested params' do
|
|
2122
2110
|
mock_app do
|
|
2123
|
-
get(:index) {
|
|
2111
|
+
get(:index) { format('%s %s', params[:account][:name], params[:account][:surname]) }
|
|
2124
2112
|
end
|
|
2125
|
-
get "
|
|
2113
|
+
get "/?#{Padrino::Utils.build_uri_query(account: { name: 'foo', surname: 'bar' })}"
|
|
2126
2114
|
assert_equal 'foo bar', body
|
|
2127
|
-
get @app.url(:index,
|
|
2115
|
+
get @app.url(:index, 'account[name]' => 'foo', 'account[surname]' => 'bar')
|
|
2128
2116
|
assert_equal 'foo bar', body
|
|
2129
2117
|
end
|
|
2130
2118
|
|
|
2131
2119
|
it 'should render sinatra NotFound page' do
|
|
2132
2120
|
mock_app { set :environment, :development }
|
|
2133
|
-
get
|
|
2121
|
+
get '/'
|
|
2134
2122
|
assert_equal 404, status
|
|
2135
|
-
assert_match
|
|
2123
|
+
assert_match(/Not Found/, body)
|
|
2136
2124
|
end
|
|
2137
2125
|
|
|
2138
2126
|
it 'should render a custom NotFound page' do
|
|
2139
2127
|
mock_app do
|
|
2140
|
-
error(Sinatra::NotFound) {
|
|
2128
|
+
error(Sinatra::NotFound) { 'not found' }
|
|
2141
2129
|
end
|
|
2142
|
-
get
|
|
2130
|
+
get '/'
|
|
2143
2131
|
assert_equal 404, status
|
|
2144
2132
|
assert_match(/not found/, body)
|
|
2145
2133
|
end
|
|
2146
2134
|
|
|
2147
2135
|
it 'should render a custom 404 page using not_found' do
|
|
2148
2136
|
mock_app do
|
|
2149
|
-
not_found {
|
|
2137
|
+
not_found { 'custom 404 not found' }
|
|
2150
2138
|
end
|
|
2151
|
-
get
|
|
2139
|
+
get '/'
|
|
2152
2140
|
assert_equal 404, status
|
|
2153
|
-
assert_equal
|
|
2141
|
+
assert_equal 'custom 404 not found', body
|
|
2154
2142
|
end
|
|
2155
2143
|
|
|
2156
2144
|
it 'should render a custom error page using error method' do
|
|
2157
2145
|
mock_app do
|
|
2158
|
-
error(404) {
|
|
2146
|
+
error(404) { 'custom 404 error' }
|
|
2159
2147
|
end
|
|
2160
|
-
get
|
|
2148
|
+
get '/'
|
|
2161
2149
|
assert_equal 404, status
|
|
2162
|
-
assert_equal
|
|
2150
|
+
assert_equal 'custom 404 error', body
|
|
2163
2151
|
end
|
|
2164
2152
|
|
|
2165
2153
|
it 'should render a custom 403 page' do
|
|
2166
2154
|
mock_app do
|
|
2167
|
-
error(403) {
|
|
2168
|
-
get(
|
|
2155
|
+
error(403) { 'custom 403 not found' }
|
|
2156
|
+
get('/') { status 403 }
|
|
2169
2157
|
end
|
|
2170
|
-
get
|
|
2158
|
+
get '/'
|
|
2171
2159
|
assert_equal 403, status
|
|
2172
|
-
assert_equal
|
|
2160
|
+
assert_equal 'custom 403 not found', body
|
|
2173
2161
|
end
|
|
2174
2162
|
|
|
2175
2163
|
it 'should recognize paths' do
|
|
2176
2164
|
mock_app do
|
|
2177
2165
|
controller :foo do
|
|
2178
|
-
get(:bar, :
|
|
2166
|
+
get(:bar, map: '/my/:id/custom-route') {}
|
|
2179
2167
|
end
|
|
2180
|
-
get(:simple, :
|
|
2181
|
-
get(:with_format, :
|
|
2168
|
+
get(:simple, map: '/simple/:id') {}
|
|
2169
|
+
get(:with_format, with: :id, provides: :js) {}
|
|
2182
2170
|
end
|
|
2183
|
-
assert_equal [:"foo bar", {
|
|
2184
|
-
assert_equal [:"foo bar", {
|
|
2185
|
-
assert_equal [:simple, {
|
|
2186
|
-
assert_equal [:simple, {
|
|
2187
|
-
assert_equal [:simple, {
|
|
2188
|
-
assert_equal [:with_format, {
|
|
2189
|
-
assert_equal [:with_format, {
|
|
2190
|
-
assert_equal [:with_format, {
|
|
2171
|
+
assert_equal [:"foo bar", { 'id' => 'fantastic' }], @app.recognize_path(@app.url(:foo, :bar, id: :fantastic))
|
|
2172
|
+
assert_equal [:"foo bar", { 'id' => '18' }], @app.recognize_path(@app.url(:foo, :bar, id: 18))
|
|
2173
|
+
assert_equal [:simple, { 'id' => 'bar' }], @app.recognize_path(@app.url(:simple, id: 'bar'))
|
|
2174
|
+
assert_equal [:simple, { 'id' => 'true' }], @app.recognize_path(@app.url(:simple, id: true))
|
|
2175
|
+
assert_equal [:simple, { 'id' => '9' }], @app.recognize_path(@app.url(:simple, id: 9))
|
|
2176
|
+
assert_equal [:with_format, { 'id' => 'bar', 'format' => 'js' }], @app.recognize_path(@app.url(:with_format, id: 'bar', format: :js))
|
|
2177
|
+
assert_equal [:with_format, { 'id' => 'true', 'format' => 'js' }], @app.recognize_path(@app.url(:with_format, id: true, format: 'js'))
|
|
2178
|
+
assert_equal [:with_format, { 'id' => '9', 'format' => 'js' }], @app.recognize_path(@app.url(:with_format, id: 9, format: :js))
|
|
2191
2179
|
end
|
|
2192
2180
|
|
|
2193
2181
|
it 'should have current_path' do
|
|
2194
2182
|
mock_app do
|
|
2195
2183
|
controller :foo do
|
|
2196
2184
|
get(:index) { current_path }
|
|
2197
|
-
get :bar, :
|
|
2185
|
+
get :bar, map: '/paginate/:page' do
|
|
2198
2186
|
current_path
|
|
2199
2187
|
end
|
|
2200
2188
|
get(:after) { current_path }
|
|
2201
2189
|
end
|
|
2202
2190
|
end
|
|
2203
|
-
get
|
|
2204
|
-
assert_equal
|
|
2205
|
-
get
|
|
2206
|
-
assert_equal
|
|
2207
|
-
get
|
|
2208
|
-
assert_equal
|
|
2191
|
+
get '/paginate/10'
|
|
2192
|
+
assert_equal '/paginate/10', body
|
|
2193
|
+
get '/foo/after'
|
|
2194
|
+
assert_equal '/foo/after', body
|
|
2195
|
+
get '/foo'
|
|
2196
|
+
assert_equal '/foo', body
|
|
2209
2197
|
end
|
|
2210
2198
|
|
|
2211
2199
|
it 'should accept :map and :parent' do
|
|
2212
2200
|
mock_app do
|
|
2213
2201
|
controller :posts do
|
|
2214
|
-
get :show, :
|
|
2202
|
+
get :show, parent: :users, map: 'posts/:id' do
|
|
2215
2203
|
"#{params[:user_id]}-#{params[:id]}"
|
|
2216
2204
|
end
|
|
2217
2205
|
end
|
|
2218
2206
|
end
|
|
2219
2207
|
get '/users/123/posts/321'
|
|
2220
|
-
assert_equal
|
|
2208
|
+
assert_equal '123-321', body
|
|
2221
2209
|
end
|
|
2222
2210
|
|
|
2223
2211
|
it 'should change params in current_path' do
|
|
2224
2212
|
mock_app do
|
|
2225
|
-
get :index, :
|
|
2226
|
-
current_path(:
|
|
2213
|
+
get :index, map: '/paginate/:page' do
|
|
2214
|
+
current_path(page: 66)
|
|
2227
2215
|
end
|
|
2228
2216
|
end
|
|
2229
|
-
get @app.url(:index, :
|
|
2230
|
-
assert_equal
|
|
2217
|
+
get @app.url(:index, page: 10)
|
|
2218
|
+
assert_equal '/paginate/66', body
|
|
2231
2219
|
end
|
|
2232
2220
|
|
|
2233
|
-
it 'should not route get :users, :
|
|
2221
|
+
it 'should not route get :users, with: :id to /users//' do
|
|
2234
2222
|
mock_app do
|
|
2235
|
-
get(:users, :
|
|
2223
|
+
get(:users, with: :id) { 'boo' }
|
|
2236
2224
|
end
|
|
2237
2225
|
get '/users//'
|
|
2238
2226
|
assert_equal 404, status
|
|
2239
2227
|
end
|
|
2240
2228
|
|
|
2241
|
-
it
|
|
2229
|
+
it 'should support splat params' do
|
|
2242
2230
|
mock_app do
|
|
2243
|
-
get
|
|
2231
|
+
get '/say/*/to/*' do
|
|
2244
2232
|
params[:splat].inspect
|
|
2245
2233
|
end
|
|
2246
2234
|
end
|
|
2247
|
-
get
|
|
2235
|
+
get '/say/hello/to/world'
|
|
2248
2236
|
assert_equal %(["hello", "world"]), body
|
|
2249
2237
|
end
|
|
2250
2238
|
|
|
2251
|
-
it
|
|
2239
|
+
it 'should recognize the route containing splat params if path is ended with slash' do
|
|
2252
2240
|
mock_app do
|
|
2253
|
-
get
|
|
2254
|
-
|
|
2241
|
+
get '/splat/*' do
|
|
2242
|
+
'slash!'
|
|
2255
2243
|
end
|
|
2256
2244
|
end
|
|
2257
|
-
get
|
|
2245
|
+
get '/splat'
|
|
2258
2246
|
assert_equal 404, status
|
|
2259
|
-
get
|
|
2260
|
-
assert_equal
|
|
2247
|
+
get '/splat/'
|
|
2248
|
+
assert_equal 'slash!', body
|
|
2261
2249
|
end
|
|
2262
2250
|
|
|
2263
|
-
it
|
|
2251
|
+
it 'should match correctly paths even if the free regex route exists' do
|
|
2264
2252
|
mock_app do
|
|
2265
2253
|
get %r{/b/(?<aa>\w+)/(?<bb>\w+)} do
|
|
2266
|
-
|
|
2254
|
+
'free regex'
|
|
2267
2255
|
end
|
|
2268
2256
|
|
|
2269
|
-
put '/b/:b/:c', :
|
|
2257
|
+
put '/b/:b/:c', csrf_protection: false do
|
|
2270
2258
|
params.to_json
|
|
2271
2259
|
end
|
|
2272
2260
|
end
|
|
2273
|
-
put
|
|
2261
|
+
put '/b/x/y'
|
|
2274
2262
|
assert_equal '{"b":"x","c":"y"}', body
|
|
2275
2263
|
end
|
|
2276
2264
|
|
|
2277
|
-
it
|
|
2265
|
+
it 'should support named captures like %r{/hello/(?<person>[^/?#]+)} on Ruby >= 1.9' do
|
|
2278
2266
|
mock_app do
|
|
2279
2267
|
get Regexp.new('/hello/(?<person>[^/?#]+)') do
|
|
2280
2268
|
"Hello #{params['person']}"
|
|
@@ -2287,7 +2275,7 @@ describe "Routing" do
|
|
|
2287
2275
|
it 'supports regular expression look-alike routes' do
|
|
2288
2276
|
mock_app do
|
|
2289
2277
|
get(RegexpLookAlike.new) do
|
|
2290
|
-
[params[:one], params[:two], params[:three], params[:four]].join(
|
|
2278
|
+
[params[:one], params[:two], params[:three], params[:four]].join(' ')
|
|
2291
2279
|
end
|
|
2292
2280
|
end
|
|
2293
2281
|
|
|
@@ -2296,22 +2284,22 @@ describe "Routing" do
|
|
|
2296
2284
|
assert_equal 'this is a test', body
|
|
2297
2285
|
end
|
|
2298
2286
|
|
|
2299
|
-
it
|
|
2287
|
+
it 'uses optional block passed to pass as route block if no other route is found' do
|
|
2300
2288
|
mock_app do
|
|
2301
|
-
get
|
|
2289
|
+
get '/' do
|
|
2302
2290
|
pass do
|
|
2303
|
-
|
|
2291
|
+
'this'
|
|
2304
2292
|
end
|
|
2305
|
-
|
|
2293
|
+
'not this'
|
|
2306
2294
|
end
|
|
2307
2295
|
end
|
|
2308
2296
|
|
|
2309
|
-
get
|
|
2297
|
+
get '/'
|
|
2310
2298
|
assert ok?
|
|
2311
|
-
assert_equal
|
|
2299
|
+
assert_equal 'this', body
|
|
2312
2300
|
end
|
|
2313
2301
|
|
|
2314
|
-
it
|
|
2302
|
+
it 'supports mixing multiple splat params like /*/foo/*/* as block parameters' do
|
|
2315
2303
|
mock_app do
|
|
2316
2304
|
get '/*/foo/*/*' do |foo, bar, baz|
|
|
2317
2305
|
"#{foo}, #{bar}, #{baz}"
|
|
@@ -2323,68 +2311,68 @@ describe "Routing" do
|
|
|
2323
2311
|
assert_equal 'bar, bling, baz/boom', body
|
|
2324
2312
|
end
|
|
2325
2313
|
|
|
2326
|
-
it
|
|
2314
|
+
it 'should be able to use PathRouter#recognize to recognize routes' do
|
|
2327
2315
|
mock_app do
|
|
2328
|
-
get(:sample){}
|
|
2316
|
+
get(:sample) {}
|
|
2329
2317
|
end
|
|
2330
|
-
env = Rack::MockRequest.env_for(
|
|
2318
|
+
env = Rack::MockRequest.env_for('/sample')
|
|
2331
2319
|
request = Rack::Request.new(env)
|
|
2332
2320
|
assert_equal :sample, @app.router.recognize(request).first.name
|
|
2333
2321
|
end
|
|
2334
2322
|
|
|
2335
|
-
it
|
|
2323
|
+
it 'should be able to use PathRouter#recognize to recognize routes by using Rack::MockRequest' do
|
|
2336
2324
|
mock_app do
|
|
2337
|
-
get(:mock_sample){}
|
|
2325
|
+
get(:mock_sample) {}
|
|
2338
2326
|
end
|
|
2339
|
-
env = Rack::MockRequest.env_for(
|
|
2327
|
+
env = Rack::MockRequest.env_for('/mock_sample')
|
|
2340
2328
|
assert_equal :mock_sample, @app.router.recognize(env).first.name
|
|
2341
|
-
env = Rack::MockRequest.env_for(
|
|
2329
|
+
env = Rack::MockRequest.env_for('/invalid')
|
|
2342
2330
|
assert_empty @app.router.recognize(env)
|
|
2343
2331
|
end
|
|
2344
2332
|
|
|
2345
|
-
it
|
|
2346
|
-
|
|
2347
|
-
get(
|
|
2333
|
+
it 'should be able to use params after sending request' do
|
|
2334
|
+
mock_app do
|
|
2335
|
+
get('/foo/:id') { params.inspect }
|
|
2348
2336
|
end
|
|
2349
|
-
get
|
|
2350
|
-
assert_equal({
|
|
2337
|
+
get '/foo/123'
|
|
2338
|
+
assert_equal({ 'id' => '123' }, Thread.current['padrino.instance'].instance_variable_get(:@params))
|
|
2351
2339
|
end
|
|
2352
2340
|
|
|
2353
|
-
it
|
|
2341
|
+
it 'should raise an exception if block arity is not same with captured params size' do
|
|
2354
2342
|
assert_raises(Padrino::Routing::BlockArityError) do
|
|
2355
2343
|
mock_app do
|
|
2356
|
-
get(
|
|
2344
|
+
get('/sample/:a/:b') { |a| }
|
|
2357
2345
|
end
|
|
2358
2346
|
end
|
|
2359
2347
|
end
|
|
2360
2348
|
|
|
2361
|
-
it
|
|
2349
|
+
it 'should pass format value as a block parameter' do
|
|
2362
2350
|
mock_app do
|
|
2363
|
-
get
|
|
2351
|
+
get '/sample/:a/:b', provides: :xml do |a, b, format|
|
|
2364
2352
|
"#{a}, #{b}, #{format}"
|
|
2365
2353
|
end
|
|
2366
2354
|
end
|
|
2367
|
-
get
|
|
2368
|
-
assert_equal
|
|
2369
|
-
get
|
|
2370
|
-
assert_equal
|
|
2355
|
+
get '/sample/foo/bar'
|
|
2356
|
+
assert_equal 'foo, bar, ', body
|
|
2357
|
+
get '/sample/foo/bar.xml'
|
|
2358
|
+
assert_equal 'foo, bar, xml', body
|
|
2371
2359
|
end
|
|
2372
2360
|
|
|
2373
|
-
it
|
|
2361
|
+
it 'should allow negative arity in route block' do
|
|
2374
2362
|
mock_app do
|
|
2375
|
-
get(
|
|
2363
|
+
get('/:a/sample/*/*') { |*all| }
|
|
2376
2364
|
end
|
|
2377
2365
|
end
|
|
2378
2366
|
|
|
2379
|
-
it
|
|
2367
|
+
it 'should be able to use splat and named captues' do
|
|
2380
2368
|
mock_app do
|
|
2381
|
-
get(
|
|
2369
|
+
get('/:a/:b/*/*/*') { |a, b, *splats| "#{a}, #{b}, (#{splats * ','})" }
|
|
2382
2370
|
end
|
|
2383
|
-
get
|
|
2384
|
-
assert_equal
|
|
2371
|
+
get '/123/456/a/b/c'
|
|
2372
|
+
assert_equal '123, 456, (a,b,c)', body
|
|
2385
2373
|
end
|
|
2386
2374
|
|
|
2387
|
-
it
|
|
2375
|
+
it 'can modify the request' do
|
|
2388
2376
|
mock_app do
|
|
2389
2377
|
get('/foo') { request.path_info = '/bar'; pass }
|
|
2390
2378
|
get('/bar') { 'bar' }
|
|
@@ -2411,22 +2399,22 @@ describe "Routing" do
|
|
|
2411
2399
|
|
|
2412
2400
|
it 'should not match if route regexps matches with incorrect_path[0..2]' do
|
|
2413
2401
|
mock_app do
|
|
2414
|
-
get(:index) {
|
|
2415
|
-
get(
|
|
2402
|
+
get(:index) { 'bork' }
|
|
2403
|
+
get('/foo') { 'foo' }
|
|
2416
2404
|
end
|
|
2417
|
-
get
|
|
2405
|
+
get '/'
|
|
2418
2406
|
assert_equal 200, status
|
|
2419
|
-
get
|
|
2407
|
+
get '/a'
|
|
2420
2408
|
assert_equal 404, status
|
|
2421
|
-
get
|
|
2409
|
+
get '/foo'
|
|
2422
2410
|
assert_equal 200, status
|
|
2423
|
-
get
|
|
2411
|
+
get '/fo'
|
|
2424
2412
|
assert_equal 404, status
|
|
2425
2413
|
end
|
|
2426
2414
|
|
|
2427
2415
|
it "should maintain Sinatra's params indifference" do
|
|
2428
2416
|
mock_app do
|
|
2429
|
-
get '/update', :
|
|
2417
|
+
get '/update', with: :id do
|
|
2430
2418
|
"#{params[:product]['title']}==#{params[:product][:title]}"
|
|
2431
2419
|
end
|
|
2432
2420
|
end
|
|
@@ -2434,7 +2422,7 @@ describe "Routing" do
|
|
|
2434
2422
|
assert_equal 'test==test', body
|
|
2435
2423
|
end
|
|
2436
2424
|
|
|
2437
|
-
it
|
|
2425
|
+
it 'prevent overwriting params by given query' do
|
|
2438
2426
|
mock_app do
|
|
2439
2427
|
get '/prohibit/:id' do
|
|
2440
2428
|
params[:id]
|
|
@@ -2444,7 +2432,7 @@ describe "Routing" do
|
|
|
2444
2432
|
assert_equal '123', body
|
|
2445
2433
|
end
|
|
2446
2434
|
|
|
2447
|
-
it
|
|
2435
|
+
it 'functions in a standalone app' do
|
|
2448
2436
|
mock_app(Sinatra::Application) do
|
|
2449
2437
|
register Padrino::Routing
|
|
2450
2438
|
get(:index) { 'Standalone' }
|