pendragon 0.6.2 → 1.0.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.
@@ -1,50 +0,0 @@
1
- require 'pendragon/route'
2
-
3
- module Pendragon
4
- module Padrino
5
- class Route < ::Pendragon::Route
6
- attr_accessor :action, :cache, :cache_key, :cache_expires, :parent,
7
- :use_layout, :controller, :user_agent, :path_for_generation
8
-
9
- def before_filters(&block)
10
- @_before_filters ||= []
11
- @_before_filters << block if block_given?
12
- @_before_filters
13
- end
14
-
15
- def after_filters(&block)
16
- @_after_filters ||= []
17
- @_after_filters << block if block_given?
18
- @_after_filters
19
- end
20
-
21
- def custom_conditions(&block)
22
- @_custom_conditions ||= []
23
- @_custom_conditions << block if block_given?
24
- @_custom_conditions
25
- end
26
-
27
- def call(app, *args)
28
- @block.call(app, *args)
29
- end
30
-
31
- def request_methods
32
- [verb.to_s.upcase]
33
- end
34
-
35
- def original_path
36
- @path
37
- end
38
-
39
- def significant_variable_names
40
- @significant_variable_names ||= if @path.is_a?(String)
41
- @path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
42
- elsif @path.is_a?(Regexp) and @path.respond_to?(:named_captures)
43
- @path.named_captures.keys.map(&:to_sym)
44
- else
45
- []
46
- end
47
- end
48
- end
49
- end
50
- end
@@ -1,34 +0,0 @@
1
- require 'pendragon' unless defined?(Pendragon::Router)
2
-
3
- module Pendragon
4
- module Padrino
5
- class Router < Pendragon::Router
6
- attr_accessor :configuration
7
-
8
- def add(verb, path, options = {}, &block)
9
- route = Route.new(path, verb, options, &block)
10
- route.path_for_generation = options[:path_for_generation] if options[:path_for_generation]
11
- route.router = self
12
- routes << route
13
- route
14
- end
15
-
16
- def call(env)
17
- request = Rack::Request.new(env)
18
- [200, {}, recognize(request)]
19
- rescue BadRequest, NotFound, MethodNotAllowed
20
- $!.call
21
- end
22
-
23
- def path(name, *args)
24
- extract_with(name, *args) do |route, params, matcher|
25
- matcher.mustermann? ? matcher.expand(params) : route.path_for_generation
26
- end
27
- end
28
-
29
- def configuration
30
- @configuration || Pendragon::Configuration.new
31
- end
32
- end
33
- end
34
- end
@@ -1,124 +0,0 @@
1
- module Pendragon
2
- # A class for defining the route
3
- #
4
- # @example
5
- # route = Pendragon::Route.new("/:id", "GET", capture: id: /\d+/){|params| params[:id].to_s }
6
- # route.match("/1234") #=> #<MatchData "/category/1234" id:"1234">
7
- # route.arity
8
- class Route
9
-
10
- # The accessors are useful to access from Pendragon::Router
11
- attr_accessor :name, :capture, :order, :options
12
-
13
- # For compile option
14
- attr_accessor :index
15
-
16
- # The verb should be read from Pendragon::Router
17
- attr_reader :verb, :block
18
-
19
- # The router will be treated in this class.
20
- attr_writer :router
21
-
22
- # Constructs a new instance of Pendragon::Route
23
- # @param [String, Regexp] path The path of route
24
- # @param [String, Symbol] verb The verb of route
25
- # @param [Hash] options The options hash
26
- def initialize(path, verb, options = {}, &block)
27
- @block = block if block_given?
28
- @path, @verb = path, verb.to_s.upcase
29
- @capture = {}
30
- @order = 0
31
- merge_with_options!(options)
32
- end
33
-
34
- # Returns an instance of Pendragon::Matcherthat is associated with the route
35
- # @return [Pendragon::Matcher]
36
- def matcher
37
- @matcher ||= Matcher.new(@path, :capture => @capture,
38
- :default_values => options[:default_values])
39
- end
40
-
41
- # Returns arity of route block
42
- # @return [Fixnum]
43
- def arity
44
- @block.arity
45
- end
46
-
47
- # Calls the route block with arguments
48
- # @param [Array] args The arguments are passed to the route block
49
- def call(*args)
50
- @block.call(*args)
51
- end
52
-
53
- # Matches a pattern with the route matcher
54
- # @param [String] pattern The pattern will be matched with route matcher
55
- # @return (see Pendragon::Matcher#match)
56
- def match(pattern)
57
- matcher.match(pattern)
58
- end
59
-
60
- # Associates the block with the route, and increments current order of the router
61
- # @yield The route block
62
- def to(&block)
63
- @block = block if block_given?
64
- @order = @router.current
65
- @router.increment_order!
66
- end
67
-
68
- # Expands a path using parameters
69
- # @param [Array] args
70
- # @example
71
- # pendragon = Pendragon.new
72
- # route = pendragon.get("/category/:name"){}
73
- # route.path(name: "Doraemon") #=> "/category/Doraemon"
74
- # route.path(name: "Doraemon", hey: "Hey") #=> "/category/Doraemon?hey=Hey"
75
- # @return [String] The expanded path
76
- def path(*args)
77
- return @path if args.empty?
78
- params = args[0]
79
- params.delete(:captures)
80
- matcher.expand(params) if matcher.mustermann?
81
- end
82
-
83
- # Matches a pattern with the route matcher, and then returns the route params
84
- # @param [String] pattern The pattern will be matched with the matcher
85
- # @param [Hash] parameters The parameters are base of the route params
86
- # @example
87
- # pendragon = Pendragon.new
88
- # route = pendragon.get("/category/:name"){}
89
- # route.params("/category/Doraemon") #=> {:name=>"Doraemon"}
90
- # route.params("/category/Doraemon", hey: "Hey") #=> {:name=>"Doraemon", :hey=>"Hey"}
91
- # @return [Hash] The params for use in routing engines
92
- def params(pattern, parameters = {})
93
- match_data, params = match(pattern), indifferent_hash
94
- if match_data.names.empty?
95
- params.merge!(:captures => match_data.captures) unless match_data.captures.empty?
96
- params
97
- else
98
- params_from_matcher = matcher.handler.params(pattern, :captures => match_data)
99
- params.merge!(params_from_matcher) if params_from_matcher
100
- params.merge(parameters){|key, old, new| old || new }
101
- end
102
- end
103
-
104
- # @!visibility private
105
- def merge_with_options!(options)
106
- @options = {} unless @options
107
- options.each_pair do |key, value|
108
- accessor?(key) ? __send__("#{key}=", value) : (@options[key] = value)
109
- end
110
- end
111
-
112
- # @!visibility private
113
- def accessor?(key)
114
- respond_to?("#{key}=") && respond_to?(key)
115
- end
116
-
117
- # @!visibility private
118
- def indifferent_hash
119
- Hash.new{|hash, key| hash[key.to_s] if key.instance_of?(Symbol) }
120
- end
121
-
122
- private :merge_with_options!, :accessor?, :indifferent_hash
123
- end
124
- end
@@ -1,3 +0,0 @@
1
- require 'pendragon'
2
-
3
- ENABLE_COMPILER = true
@@ -1,2113 +0,0 @@
1
- # coding: utf-8
2
- require File.expand_path('../../lib/pendragon/padrino', __FILE__)
3
- $:.unshift(File.dirname(__FILE__))
4
- require 'active_support/core_ext/hash/conversions'
5
- require 'helper'
6
-
7
- class FooError < RuntimeError; end
8
-
9
- describe "Pendragon::Padrino" do
10
- setup do
11
- Padrino::Application.send(:register, Pendragon::Padrino)
12
- Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = false
13
- ENV['RACK_BASE_URI'] = nil
14
- end
15
-
16
- should "serve static files with simple cache control" do
17
- mock_app do
18
- set :static_cache_control, :public
19
- set :public_folder, File.dirname(__FILE__)
20
- end
21
- get "/#{File.basename(__FILE__)}"
22
- assert headers.has_key?('Cache-Control')
23
- assert_equal headers['Cache-Control'], 'public'
24
- end # static simple
25
-
26
- should "serve static files with cache control and max_age" do
27
- mock_app do
28
- set :static_cache_control, [:public, :must_revalidate, {:max_age => 300}]
29
- set :public_folder, File.dirname(__FILE__)
30
- end
31
- get "/#{File.basename(__FILE__)}"
32
- assert headers.has_key?('Cache-Control')
33
- assert_equal headers['Cache-Control'], 'public, must-revalidate, max-age=300'
34
- end # static max_age
35
-
36
- should 'ignore trailing delimiters for basic route' do
37
- mock_app do
38
- get("/foo"){ "okey" }
39
- get(:test) { "tester" }
40
- end
41
- get "/foo"
42
- assert_equal "okey", body
43
- get "/foo/"
44
- assert_equal "okey", body
45
- get "/test"
46
- assert_equal "tester", body
47
- get "/test/"
48
- assert_equal "tester", body
49
- end
50
-
51
- should 'fail with unrecognized route exception when not found' do
52
- mock_app do
53
- get(:index){ "okey" }
54
- end
55
- get @app.url_for(:index)
56
- assert_equal "okey", body
57
- assert_raises(Padrino::Routing::UnrecognizedException) {
58
- get @app.url_for(:fake)
59
- }
60
- end
61
-
62
- should 'accept regexp routes' do
63
- mock_app do
64
- get(%r./fob|/baz.) { "regexp" }
65
- get("/foo") { "str" }
66
- get %r./([0-9]+)/. do |num|
67
- "Your lucky number: #{num} #{params[:captures].first}"
68
- end
69
- get %r./page/([0-9]+)|/. do |num|
70
- "My lucky number: #{num} #{params[:captures].first}"
71
- end
72
- end
73
- get "/foo"
74
- assert_equal "str", body
75
- get "/fob"
76
- assert_equal "regexp", body
77
- get "/baz"
78
- assert_equal "regexp", body
79
- get "/321/"
80
- assert_equal "Your lucky number: 321 321", body
81
- get "/page/99"
82
- assert_equal "My lucky number: 99 99", body
83
- end
84
-
85
- should 'accept regexp routes with generate with :generate_with' do
86
- mock_app do
87
- get(%r{/fob|/baz}, :name => :foo, :generate_with => '/fob') { "regexp" }
88
- end
89
- assert_equal "/fob", @app.url(:foo)
90
- end
91
-
92
- should "parse routes with question marks" do
93
- mock_app do
94
- get("/foo/?"){ "okey" }
95
- post('/unauthenticated/?') { "no access" }
96
- end
97
- get "/foo"
98
- assert_equal "okey", body
99
- get "/foo/"
100
- assert_equal "okey", body
101
- post "/unauthenticated"
102
- assert_equal "no access", body
103
- post "/unauthenticated/"
104
- assert_equal "no access", body
105
- end
106
-
107
- should 'parse routes that are encoded' do
108
- mock_app do
109
- get('/щч') { 'success!' }
110
- end
111
- get(URI.escape('/щч'))
112
- assert_equal 'success!', body
113
- end
114
-
115
- should 'parse routes that include encoded slash' do
116
- mock_app do
117
- get('/:drive_alias/:path', :path => /.*/){
118
- "Show #{params[:drive_alias]} and #{params[:path]}"
119
- }
120
- end
121
- get("/drive%2Ffoo/some/path")
122
- assert_equal "Show drive/foo and some/path", body
123
- end
124
-
125
- should 'parse route that contains encoded param.' do
126
- mock_app do
127
- get('/foo/:name'){ params[:name] }
128
- end
129
- get(URI.escape('/foo/あいうえお'))
130
- assert_equal 'あいうえお', body
131
- end
132
-
133
- should 'encode params using UTF-8' do
134
- mock_app do
135
- get('/:foo') { params[:foo].encoding.name }
136
- end
137
- get '/bar'
138
- assert_equal 'UTF-8', body
139
- end
140
-
141
- should 'match correctly similar paths' do
142
- mock_app do
143
- get("/my/:foo_id"){ params[:foo_id] }
144
- get("/my/:bar_id/bar"){ params[:bar_id] }
145
- end
146
- get "/my/1"
147
- assert_equal "1", body
148
- get "/my/2/bar"
149
- assert_equal "2", body
150
- end
151
-
152
- should "match user agents" do
153
- app = mock_app do
154
- get("/main", :agent => /IE/){ "hello IE" }
155
- get("/main"){ "hello" }
156
- end
157
- get "/main"
158
- assert_equal "hello", body
159
- get "/main", {}, {'HTTP_USER_AGENT' => 'This is IE'}
160
- assert_equal "hello IE", body
161
- end
162
-
163
- should "use regex for parts of a route" do
164
- app = mock_app do
165
- get("/main/:id", :id => /\d+/){ "hello #{params[:id]}" }
166
- end
167
- get "/main/123"
168
- assert_equal "hello 123", body
169
- get "/main/asd"
170
- assert_equal 404, status
171
- end
172
-
173
- should "parse params when use regex for parts of a route" do
174
- mock_app do
175
- post :index, :with => [:foo, :bar], :bar => /.+/ do
176
- "show #{params[:foo]}"
177
- end
178
-
179
- get :index, :map => '/mystuff/:a_id/boing/:boing_id' do
180
- "show #{params[:a_id]} and #{params[:boing_id]}"
181
- end
182
- end
183
- get "/mystuff/5/boing/2"
184
- assert_equal "show 5 and 2", body
185
- end
186
-
187
- should "not generate overlapping head urls" do
188
- app = mock_app do
189
- get("/main"){ "hello" }
190
- post("/main"){ "hello" }
191
- end
192
- assert_equal 3, app.routes.size, "should generate GET, HEAD and PUT"
193
- assert_equal "GET", app.routes[0].request_methods.first
194
- assert_equal "HEAD", app.routes[1].request_methods.first
195
- assert_equal "POST", app.routes[2].request_methods.first
196
- end
197
-
198
- should 'generate basic urls' do
199
- mock_app do
200
- get(:foo){ "/foo" }
201
- get(:foo, :with => :id){ |id| "/foo/#{id}" }
202
- get([:foo, :id]){ |id| "/foo/#{id}" }
203
- get(:hash, :with => :id){ url(:hash, :id => 1) }
204
- get([:hash, :id]){ url(:hash, :id => 1) }
205
- get(:array, :with => :id){ url(:array, 23) }
206
- get([:array, :id]){ url(:array, 23) }
207
- get(:hash_with_extra, :with => :id){ url(:hash_with_extra, :id => 1, :query => 'string') }
208
- get([:hash_with_extra, :id]){ url(:hash_with_extra, :id => 1, :query => 'string') }
209
- get(:array_with_extra, :with => :id){ url(:array_with_extra, 23, :query => 'string') }
210
- get([:array_with_extra, :id]){ url(:array_with_extra, 23, :query => 'string') }
211
- get("/old-bar/:id"){ params[:id] }
212
- post(:mix, :map => "/mix-bar/:id"){ params[:id] }
213
- get(:mix, :map => "/mix-bar/:id"){ params[:id] }
214
- end
215
- get "/foo"
216
- assert_equal "/foo", body
217
- get "/foo/123"
218
- assert_equal "/foo/123", body
219
- get "/hash/2"
220
- assert_equal "/hash/1", body
221
- get "/array/23"
222
- assert_equal "/array/23", body
223
- get "/hash_with_extra/1"
224
- assert_equal "/hash_with_extra/1?query=string", body
225
- get "/array_with_extra/23"
226
- assert_equal "/array_with_extra/23?query=string", body
227
- get "/old-bar/3"
228
- assert_equal "3", body
229
- post "/mix-bar/4"
230
- assert_equal "4", body
231
- get "/mix-bar/4"
232
- assert_equal "4", body
233
- end
234
-
235
- should 'generate url with format' do
236
- mock_app do
237
- get(:a, :provides => :any){ url(:a, :format => :json) }
238
- get(:b, :provides => :js){ url(:b, :format => :js) }
239
- get(:c, :provides => [:js, :json]){ url(:c, :format => :json) }
240
- get(:d, :provides => [:html, :js]){ url(:d, :format => :js, :foo => :bar) }
241
- end
242
- get "/a.js"
243
- assert_equal "/a.json", body
244
- get "/b.js"
245
- assert_equal "/b.js", body
246
- get "/b.ru"
247
- assert_equal 404, status
248
- get "/c.js"
249
- assert_equal "/c.json", body
250
- get "/c.json"
251
- assert_equal "/c.json", body
252
- get "/c.ru"
253
- assert_equal 404, status
254
- get "/d"
255
- assert_equal "/d.js?foo=bar", body
256
- get "/d.js"
257
- assert_equal "/d.js?foo=bar", body
258
- get "/e.xml"
259
- assert_equal 404, status
260
- end
261
-
262
- should 'generate absolute urls' do
263
- mock_app do
264
- get(:hash, :with => :id){ absolute_url(:hash, :id => 1) }
265
- end
266
- get "/hash/2"
267
- assert_equal "http://example.org/hash/1", body
268
- get "https://example.org/hash/2"
269
- assert_equal "https://example.org/hash/1", body
270
- end
271
-
272
- should 'generate proper absolute urls for mounted apps' do
273
- class Test < Padrino::Application
274
- get :foo do
275
- absolute_url(:foo, :id => 1)
276
- end
277
- end
278
- Padrino.mount("Test").to("/test")
279
- @app = Padrino.application
280
- get('/test/foo')
281
- assert_equal 'http://example.org/test/foo?id=1', body
282
- end
283
-
284
- should 'allow regex url with format' do
285
- mock_app do
286
- get(/.*/, :provides => :any) { "regexp" }
287
- end
288
- get "/anything"
289
- assert_equal "regexp", body
290
- end
291
-
292
- should 'use padrino url method' do
293
- mock_app do
294
- end
295
-
296
- assert_equal @app.method(:url).owner, Pendragon::Padrino::ClassMethods
297
- end
298
-
299
- should 'work correctly with sinatra redirects' do
300
- mock_app do
301
- get(:index) { redirect url(:index) }
302
- get(:google) { redirect "http://google.com" }
303
- get("/foo") { redirect "/bar" }
304
- get("/bar") { "Bar" }
305
- end
306
-
307
- get "/"
308
- assert_equal "http://example.org/", headers['Location']
309
- get "/google"
310
- assert_equal "http://google.com", headers['Location']
311
- get "/foo"
312
- assert_equal "http://example.org/bar", headers['Location']
313
- end
314
-
315
- should "return 406 on Accept-Headers it does not provide" do
316
- mock_app do
317
- get(:a, :provides => [:html, :js]){ content_type }
318
- end
319
-
320
- get "/a", {}, {"HTTP_ACCEPT" => "application/yaml"}
321
- assert_equal 406, status
322
- end
323
-
324
- should "return 406 on file extensions it does not provide and flag is set" do
325
- mock_app do
326
- enable :treat_format_as_accept
327
- get(:a, :provides => [:html, :js]){ content_type }
328
- end
329
-
330
- get "/a.xml", {}, {}
331
- assert_equal 406, status
332
- end
333
-
334
- should "return 404 on file extensions it does not provide and flag is not set" do
335
- mock_app do
336
- get(:a, :provides => [:html, :js]){ content_type }
337
- end
338
-
339
- get "/a.xml", {}, {}
340
- assert_equal 404, status
341
- end
342
-
343
- should "not set content_type to :html if Accept */* and html not in provides" do
344
- mock_app do
345
- get("/foo", :provides => [:json, :xml]) { content_type.to_s }
346
- end
347
-
348
- get '/foo', {}, { 'HTTP_ACCEPT' => '*/*;q=0.5' }
349
- assert_equal 'json', body
350
- end
351
-
352
- should "set content_type to :json if Accept contains */*" do
353
- mock_app do
354
- get("/foo", :provides => [:json]) { content_type.to_s }
355
- end
356
-
357
- get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }
358
- assert_equal 'json', body
359
- end
360
-
361
- should 'set and get content_type' do
362
- mock_app do
363
- get("/foo"){ content_type(:json); content_type.to_s }
364
- end
365
- get "/foo"
366
- assert_equal 'application/json', content_type
367
- assert_equal 'json', body
368
- end
369
-
370
- should "send the appropriate number of params" do
371
- mock_app do
372
- get('/id/:user_id', :provides => [:json]) { |user_id, format| user_id}
373
- end
374
- get '/id/5.json'
375
- assert_equal '5', body
376
- end
377
-
378
- should "allow .'s in param values" do
379
- #skip
380
- mock_app do
381
- get('/id/:email', :provides => [:json]) { |email, format| [email, format] * '/' }
382
- end
383
- get '/id/foo@bar.com.json'
384
- assert_equal 'foo@bar.com/json', body
385
- end
386
-
387
- should "set correct content_type for Accept not equal to */* even if */* also provided" do
388
- mock_app do
389
- get("/foo", :provides => [:html, :js, :xml]) { content_type.to_s }
390
- end
391
-
392
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/javascript, */*;q=0.5' }
393
- assert_equal 'js', body
394
- end
395
-
396
- should "return the first content type in provides if accept header is empty" do
397
- mock_app do
398
- get(:a, :provides => [:js]){ content_type.to_s }
399
- end
400
-
401
- get "/a", {}, {}
402
- assert_equal "js", body
403
- end
404
-
405
- should "not default to HTML if HTML is not provided and no type is given" do
406
- mock_app do
407
- get(:a, :provides => [:js]){ content_type }
408
- end
409
-
410
- get "/a", {}, {}
411
- assert_equal "application/javascript;charset=utf-8", content_type
412
- end
413
-
414
- should "not match routes if url_format and http_accept is provided but not included" do
415
- mock_app do
416
- get(:a, :provides => [:js, :html]){ content_type }
417
- end
418
-
419
- get "/a.xml", {}, {"HTTP_ACCEPT" => "text/html"}
420
- assert_equal 404, status
421
- end
422
-
423
- should "generate routes for format simple" do
424
- mock_app do
425
- get(:foo, :provides => [:html, :rss]) { render :haml, "Test" }
426
- end
427
- get "/foo"
428
- assert_equal "Test\n", body
429
- get "/foo.rss"
430
- assert_equal "Test\n", body
431
- end
432
-
433
- should "should inject the controller name into the request" do
434
- mock_app do
435
- controller :posts do
436
- get(:index) { request.controller }
437
- controller :mini do
438
- get(:index) { request.controller }
439
- end
440
- end
441
- end
442
- get "/posts"
443
- assert_equal "posts", body
444
- get "/mini"
445
- assert_equal "mini", body
446
- end
447
-
448
- should "should inject the action name into the request" do
449
- mock_app do
450
- controller :posts do
451
- get('/omnomnom(/:id)?') { request.action.inspect }
452
- controller :mini do
453
- get([:a, :b, :c]) { request.action.inspect }
454
- end
455
- end
456
- end
457
- get "/posts/omnomnom"
458
- assert_equal "\"/omnomnom(/:id)?\"", body
459
- get "/mini/a/b/c"
460
- assert_equal ":a", body
461
- end
462
-
463
- should "support not_found" do
464
- mock_app do
465
- not_found { 'whatever' }
466
-
467
- get :index, :map => "/" do
468
- 'index'
469
- end
470
- end
471
- get '/wrong'
472
- assert_equal 404, status
473
- assert_equal 'whatever', body
474
- get '/'
475
- assert_equal 'index', body
476
- assert_equal 200, status
477
- end
478
-
479
- should "should inject the route into the request" do
480
- mock_app do
481
- controller :posts do
482
- get(:index) { request.route_obj.name.to_s }
483
- end
484
- end
485
- get "/posts"
486
- assert_equal "posts index", body
487
- end
488
-
489
- should "preserve the format if you set it manually" do
490
- mock_app do
491
- before do
492
- params[:format] = "json"
493
- end
494
-
495
- get "test", :provides => [:html, :json] do
496
- content_type.inspect
497
- end
498
- end
499
- get "/test"
500
- assert_equal ":json", body
501
- get "/test.html"
502
- assert_equal ":json", body
503
- get "/test.php"
504
- assert_equal ":json", body
505
- end
506
-
507
- should "correctly accept '.' in the route" do
508
- mock_app do
509
- get "test.php", :provides => [:html, :json] do
510
- content_type.inspect
511
- end
512
- end
513
- get "/test.php"
514
- assert_equal ":html", body
515
- get "/test.php.json"
516
- assert_equal ":json", body
517
- end
518
-
519
- should "correctly accept priority of format" do
520
- mock_app do
521
- get "test.php", :provides => [:html, :json, :xml] do
522
- content_type.inspect
523
- end
524
- end
525
-
526
- get "/test.php"
527
- assert_equal ":html", body
528
- get "/test.php", {}, { 'HTTP_ACCEPT' => 'application/xml' }
529
- assert_equal ":xml", body
530
- get "/test.php?format=json", { 'HTTP_ACCEPT' => 'application/xml' }
531
- assert_equal ":json", body
532
- get "/test.php.json?format=html", { 'HTTP_ACCEPT' => 'application/xml' }
533
- assert_equal ":json", body
534
- end
535
-
536
- should "generate routes for format with controller" do
537
- mock_app do
538
- controller :posts do
539
- get(:index, :provides => [:html, :rss, :atom, :js]) { render :haml, "Index.#{content_type}" }
540
- get(:show, :with => :id, :provides => [:html, :rss, :atom]) { render :haml, "Show.#{content_type}" }
541
- end
542
- end
543
- get "/posts"
544
- assert_equal "Index.html\n", body
545
- get "/posts.rss"
546
- assert_equal "Index.rss\n", body
547
- get "/posts.atom"
548
- assert_equal "Index.atom\n", body
549
- get "/posts.js"
550
- assert_equal "Index.js\n", body
551
- get "/posts/show/5"
552
- assert_equal "Show.html\n", body
553
- get "/posts/show/5.rss"
554
- assert_equal "Show.rss\n", body
555
- get "/posts/show/10.atom"
556
- assert_equal "Show.atom\n", body
557
- end
558
-
559
- should 'map routes' do
560
- mock_app do
561
- get(:bar){ "bar" }
562
- end
563
- get "/bar"
564
- assert_equal "bar", body
565
- assert_equal "/bar", @app.url(:bar)
566
- end
567
-
568
- should 'remove index from path' do
569
- mock_app do
570
- get(:index){ "index" }
571
- get("/accounts/index"){ "accounts" }
572
- end
573
- get "/"
574
- assert_equal "index", body
575
- assert_equal "/", @app.url(:index)
576
- get "/accounts/index"
577
- assert_equal "accounts", body
578
- end
579
-
580
- should 'remove index from path with params' do
581
- mock_app do
582
- get(:index, :with => :name){ "index with #{params[:name]}" }
583
- end
584
- get "/bobby"
585
- assert_equal "index with bobby", body
586
- assert_equal "/john", @app.url(:index, :name => "john")
587
- end
588
-
589
- should 'parse named params' do
590
- mock_app do
591
- get(:print, :with => :id){ "Im #{params[:id]}" }
592
- end
593
- get "/print/9"
594
- assert_equal "Im 9", body
595
- assert_equal "/print/9", @app.url(:print, :id => 9)
596
- end
597
-
598
- should '405 on wrong request_method' do
599
- mock_app do
600
- post('/bar'){ "bar" }
601
- end
602
- get "/bar"
603
- assert_equal 405, status
604
- end
605
-
606
- should "set Allow header when occur 405" do
607
- mock_app do
608
- post("/bar"){}
609
- put("/bar"){}
610
- end
611
- get "/bar"
612
- assert_equal "POST, PUT", response.headers['Allow']
613
- end
614
-
615
- should 'respond to' do
616
- mock_app do
617
- get(:a, :provides => :js){ "js" }
618
- get(:b, :provides => :any){ "any" }
619
- get(:c, :provides => [:js, :json]){ "js,json" }
620
- get(:d, :provides => [:html, :js]){ "html,js"}
621
- end
622
- get "/a"
623
- assert_equal 200, status
624
- assert_equal "js", body
625
- get "/a.js"
626
- assert_equal "js", body
627
- get "/b"
628
- assert_equal "any", body
629
- # TODO randomly fails in minitest :(
630
- # assert_raises(RuntimeError) { get "/b.foo" }
631
- get "/c"
632
- assert_equal 200, status
633
- assert_equal "js,json", body
634
- get "/c.js"
635
- assert_equal "js,json", body
636
- get "/c.json"
637
- assert_equal "js,json", body
638
- get "/d"
639
- assert_equal "html,js", body
640
- get "/d.js"
641
- assert_equal "html,js", body
642
- end
643
-
644
- should 'respond_to and set content_type' do
645
- Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
646
- mock_app do
647
- get :a, :provides => :any do
648
- case content_type
649
- when :js then "js"
650
- when :json then "json"
651
- when :foo then "foo"
652
- when :html then "html"
653
- end
654
- end
655
- end
656
- get "/a.js"
657
- assert_equal "js", body
658
- assert_equal 'application/javascript;charset=utf-8', response["Content-Type"]
659
- get "/a.json"
660
- assert_equal "json", body
661
- assert_equal 'application/json', response["Content-Type"]
662
- get "/a.foo"
663
- assert_equal "foo", body
664
- assert_equal 'application/foo;charset=utf-8', response["Content-Type"]
665
- get "/a"
666
- assert_equal "html", body
667
- assert_equal 'text/html;charset=utf-8', response["Content-Type"]
668
- end
669
-
670
- should 'use controllers' do
671
- mock_app do
672
- controller "/admin" do
673
- get("/"){ "index" }
674
- get("/show/:id"){ "show #{params[:id]}" }
675
- end
676
- end
677
- get "/admin"
678
- assert_equal "index", body
679
- get "/admin/show/1"
680
- assert_equal "show 1", body
681
- end
682
-
683
- should 'use named controllers' do
684
- mock_app do
685
- controller :admin do
686
- get(:index, :with => :id){ params[:id] }
687
- get(:show, :with => :id){ "show #{params[:id]}" }
688
- end
689
- controllers :foo, :bar do
690
- get(:index){ "foo_bar_index" }
691
- end
692
- end
693
- get "/admin/1"
694
- assert_equal "1", body
695
- get "/admin/show/1"
696
- assert_equal "show 1", body
697
- assert_equal "/admin/1", @app.url(:admin, :index, :id => 1)
698
- assert_equal "/admin/show/1", @app.url(:admin, :show, :id => 1)
699
- get "/foo/bar"
700
- assert_equal "foo_bar_index", body
701
- end
702
-
703
- should 'use map and with' do
704
- mock_app do
705
- get :index, :map => '/bugs', :with => :id do
706
- params[:id]
707
- end
708
- end
709
- get '/bugs/4'
710
- assert_equal '4', body
711
- assert_equal "/bugs/4", @app.url(:index, :id => 4)
712
- end
713
-
714
- should "ignore trailing delimiters within a named controller" do
715
- mock_app do
716
- controller :posts do
717
- get(:index, :provides => [:html, :js]){ "index" }
718
- get(:new) { "new" }
719
- get(:show, :with => :id){ "show #{params[:id]}" }
720
- end
721
- end
722
- get "/posts"
723
- assert_equal "index", body
724
- get "/posts/"
725
- assert_equal "index", body
726
- get "/posts.js"
727
- assert_equal "index", body
728
- get "/posts.js/"
729
- assert_equal "index", body
730
- get "/posts/new"
731
- assert_equal "new", body
732
- get "/posts/new/"
733
- assert_equal "new", body
734
- end
735
-
736
- should "ignore trailing delimiters within a named controller for unnamed actions" do
737
- mock_app do
738
- controller :accounts do
739
- get("/") { "account_index" }
740
- get("/new") { "new" }
741
- end
742
- controller :votes do
743
- get("/") { "vote_index" }
744
- end
745
- end
746
- get "/accounts"
747
- assert_equal "account_index", body
748
- get "/accounts/"
749
- assert_equal "account_index", body
750
- get "/accounts/new"
751
- assert_equal "new", body
752
- get "/accounts/new/"
753
- assert_equal "new", body
754
- get "/votes"
755
- assert_equal "vote_index", body
756
- get "/votes/"
757
- assert_equal "vote_index", body
758
- end
759
-
760
- should 'use named controllers with array routes' do
761
- mock_app do
762
- controller :admin do
763
- get(:index){ "index" }
764
- get(:show, :with => :id){ "show #{params[:id]}" }
765
- end
766
- controllers :foo, :bar do
767
- get(:index){ "foo_bar_index" }
768
- end
769
- end
770
- get "/admin"
771
- assert_equal "index", body
772
- get "/admin/show/1"
773
- assert_equal "show 1", body
774
- assert_equal "/admin", @app.url(:admin, :index)
775
- assert_equal "/admin/show/1", @app.url(:admin, :show, :id => 1)
776
- get "/foo/bar"
777
- assert_equal "foo_bar_index", body
778
- end
779
-
780
- should "support a reindex action and remove index inside controller" do
781
- mock_app do
782
- controller :posts do
783
- get(:index){ "index" }
784
- get(:reindex){ "reindex" }
785
- end
786
- end
787
- get "/posts"
788
- assert_equal "index", body
789
- get "/posts/reindex"
790
- assert_equal "/posts/reindex", @app.url(:posts, :reindex)
791
- assert_equal "reindex", body
792
- end
793
-
794
- should 'use uri_root' do
795
- mock_app do
796
- get(:foo){ "foo" }
797
- end
798
- @app.uri_root = '/'
799
- assert_equal "/foo", @app.url(:foo)
800
- @app.uri_root = '/testing'
801
- assert_equal "/testing/foo", @app.url(:foo)
802
- @app.uri_root = '/testing/'
803
- assert_equal "/testing/foo", @app.url(:foo)
804
- @app.uri_root = 'testing/bar///'
805
- assert_equal "/testing/bar/foo", @app.url(:foo)
806
- end
807
-
808
- should 'use uri_root with controllers' do
809
- mock_app do
810
- controller :foo do
811
- get(:bar){ "bar" }
812
- end
813
- end
814
- @app.uri_root = '/testing'
815
- assert_equal "/testing/foo/bar", @app.url(:foo, :bar)
816
- end
817
-
818
- should 'use RACK_BASE_URI' do
819
- mock_app do
820
- get(:foo){ "foo" }
821
- end
822
- # Wish there was a side-effect free way to test this...
823
- ENV['RACK_BASE_URI'] = '/'
824
- assert_equal "/foo", @app.url(:foo)
825
- ENV['RACK_BASE_URI'] = '/testing'
826
- assert_equal "/testing/foo", @app.url(:foo)
827
- ENV['RACK_BASE_URI'] = nil
828
- end
829
-
830
- it 'should use uri_root and RACK_BASE_URI' do
831
- mock_app do
832
- controller :foo do
833
- get(:bar){ "bar" }
834
- end
835
- end
836
- ENV['RACK_BASE_URI'] = '/base'
837
- @app.uri_root = 'testing'
838
- assert_equal '/base/testing/foo/bar', @app.url(:foo, :bar)
839
- ENV['RACK_BASE_URI'] = nil
840
- end
841
-
842
- should 'reset routes' do
843
- mock_app do
844
- get("/"){ "foo" }
845
- reset_router!
846
- end
847
- get "/"
848
- assert_equal 404, status
849
- end
850
-
851
- should "match params and format" do
852
- app = mock_app do
853
- get '/:id', :provides => [:json, :html] do |id, _|
854
- id
855
- end
856
-
857
- get 'format/:id', :provides => [:json, :html] do |id, format|
858
- format
859
- end
860
- end
861
-
862
- get '/123.html'
863
- assert_equal '123', body
864
-
865
- get 'format/123.html'
866
- assert_equal 'html', body
867
- end
868
-
869
-
870
- should 'respect priorities' do
871
- route_order = []
872
- mock_app do
873
- get(:index, :priority => :normal) { route_order << :normal; pass }
874
- get(:index, :priority => :low) { route_order << :low; "hello" }
875
- get(:index, :priority => :high) { route_order << :high; pass }
876
- end
877
- get '/'
878
- assert_equal [:high, :normal, :low], route_order
879
- assert_equal "hello", body
880
- end
881
-
882
- should 'catch all after controllers' do
883
- mock_app do
884
- get(:index, :with => :slug, :priority => :low) { "catch all" }
885
- controllers :contact do
886
- get(:index) { "contact"}
887
- end
888
- end
889
- get "/contact"
890
- assert_equal "contact", body
891
- get "/foo"
892
- assert_equal "catch all", body
893
- end
894
-
895
- should 'allow optionals' do
896
- mock_app do
897
- get(:show, :map => "/stories/:type(/:category)?") do
898
- "#{params[:type]}/#{params[:category]}"
899
- end
900
- end
901
- get "/stories/foo"
902
- assert_equal "foo/", body
903
- get "/stories/foo/bar"
904
- assert_equal "foo/bar", body
905
- end
906
-
907
- should 'apply maps' do
908
- mock_app do
909
- controllers :admin do
910
- get(:index, :map => "/"){ "index" }
911
- get(:show, :with => :id, :map => "/show"){ "show #{params[:id]}" }
912
- get(:edit, :map => "/edit/:id/product"){ "edit #{params[:id]}" }
913
- get(:wacky, :map => "/wacky-:id-:product_id"){ "wacky #{params[:id]}-#{params[:product_id]}" }
914
- end
915
- end
916
- get "/"
917
- assert_equal "index", body
918
- get @app.url(:admin, :index)
919
- assert_equal "index", body
920
- get "/show/1"
921
- assert_equal "show 1", body
922
- get "/edit/1/product"
923
- assert_equal "edit 1", body
924
- get "/wacky-1-2"
925
- assert_equal "wacky 1-2", body
926
- end
927
-
928
- should 'apply maps when given path is kind of hash' do
929
- mock_app do
930
- controllers :admin do
931
- get(:foobar, "/foo/bar"){ "foobar" }
932
- end
933
- end
934
- get "/foo/bar"
935
- assert_equal "foobar", body
936
- end
937
-
938
- should "apply parent to route" do
939
- mock_app do
940
- controllers :project do
941
- get(:index, :parent => :user) { "index #{params[:user_id]}" }
942
- get(:index, :parent => [:user, :section]) { "index #{params[:user_id]} #{params[:section_id]}" }
943
- get(:edit, :with => :id, :parent => :user) { "edit #{params[:id]} #{params[:user_id]}"}
944
- get(:show, :with => :id, :parent => [:user, :product]) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}"}
945
- end
946
- end
947
- get "/user/1/project"
948
- assert_equal "index 1", body
949
- get "/user/1/section/3/project"
950
- assert_equal "index 1 3", body
951
- get "/user/1/project/edit/2"
952
- assert_equal "edit 2 1", body
953
- get "/user/1/product/2/project/show/3"
954
- assert_equal "show 3 1 2", body
955
- end
956
-
957
- should "respect parent precedence: controllers parents go before route parents" do
958
- mock_app do
959
- controllers :project do
960
- get(:index, :parent => :user) { "index #{params[:user_id]}" }
961
- end
962
-
963
- controllers :bar, :parent => :foo do
964
- get(:index) { "index on foo #{params[:foo_id]} @ bar" }
965
- get(:index, :parent => :baz) { "index on foo #{params[:foo_id]} @ baz #{params[:baz_id]} @ bar" }
966
- end
967
- end
968
-
969
- get "/user/1/project"
970
- assert_equal "index 1", body
971
- get "/foo/1/bar"
972
- assert_equal "index on foo 1 @ bar", body
973
- get "/foo/1/baz/2/bar"
974
- assert_equal "index on foo 1 @ baz 2 @ bar", body
975
- end
976
-
977
- should "keep a reference to the parent on the route" do
978
- mock_app do
979
- controllers :project do
980
- get(:index, :parent => :user) { "index #{params[:user_id]}" }
981
- get(:index, :parent => [:user, :section]) { "index #{params[:user_id]} #{params[:section_id]}" }
982
- get(:edit, :with => :id, :parent => :user) { "edit #{params[:id]} #{params[:user_id]}"}
983
- get(:show, :with => :id, :parent => [:user, :product]) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}"}
984
- end
985
-
986
- controllers :bar, :parent => :foo do
987
- get(:index) { "index on foo/bar" }
988
- get(:index, :parent => :baz) { "index on foo/baz/bar" }
989
- end
990
- end
991
-
992
- # get "/user/1/project"
993
- assert_equal :user, @app.routes[0].parent
994
- # get "/user/1/section/3/project"
995
- assert_equal [:user, :section], @app.routes[2].parent
996
- # get "/user/1/project/edit/2"
997
- assert_equal :user, @app.routes[4].parent
998
- # get "/user/1/product/2/project/show/3"
999
- assert_equal [:user, :product], @app.routes[6].parent
1000
- # get "/foo/1/bar"
1001
- assert_equal :foo, @app.routes[8].parent
1002
- # get "/foo/1/baz/2/bar"
1003
- assert_equal [:foo, :baz], @app.routes[10].parent
1004
- end
1005
-
1006
- should "apply parent to controller" do
1007
- mock_app do
1008
- controller :project, :parent => :user do
1009
- get(:index) { "index #{params[:user_id]}"}
1010
- get(:edit, :with => :id, :parent => :user) { "edit #{params[:id]} #{params[:user_id]}"}
1011
- get(:show, :with => :id, :parent => :product) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]}"}
1012
- end
1013
- end
1014
-
1015
- user_project_url = "/user/1/project"
1016
- get user_project_url
1017
- assert_equal "index 1", body
1018
- assert_equal user_project_url, @app.url(:project, :index, :user_id => 1)
1019
-
1020
- user_project_edit_url = "/user/1/project/edit/2"
1021
- get user_project_edit_url
1022
- assert_equal "edit 2 1", body
1023
- assert_equal user_project_edit_url, @app.url(:project, :edit, :user_id => 1, :id => 2)
1024
-
1025
- user_product_project_url = "/user/1/product/2/project/show/3"
1026
- get user_product_project_url
1027
- assert_equal "show 3 1 2", body
1028
- assert_equal user_product_project_url, @app.url(:project, :show, :user_id => 1, :product_id => 2, :id => 3)
1029
- end
1030
-
1031
- should "apply parent with shallowing to controller" do
1032
- mock_app do
1033
- controller :project do
1034
- parent :user
1035
- parent :shop, :optional => true
1036
- get(:index) { "index #{params[:user_id]} #{params[:shop_id]}" }
1037
- get(:edit, :with => :id) { "edit #{params[:id]} #{params[:user_id]} #{params[:shop_id]}" }
1038
- get(:show, :with => :id, :parent => :product) { "show #{params[:id]} #{params[:user_id]} #{params[:product_id]} #{params[:shop_id]}" }
1039
- end
1040
- end
1041
-
1042
- assert_equal "/user/1/project", @app.url(:project, :index, :user_id => 1, :shop_id => nil)
1043
- assert_equal "/user/1/shop/23/project", @app.url(:project, :index, :user_id => 1, :shop_id => 23)
1044
-
1045
- user_project_url = "/user/1/project"
1046
- get user_project_url
1047
- assert_equal "index 1 ", body
1048
- assert_equal user_project_url, @app.url(:project, :index, :user_id => 1)
1049
-
1050
- user_project_edit_url = "/user/1/project/edit/2"
1051
- get user_project_edit_url
1052
- assert_equal "edit 2 1 ", body
1053
- assert_equal user_project_edit_url, @app.url(:project, :edit, :user_id => 1, :id => 2)
1054
-
1055
- user_product_project_url = "/user/1/product/2/project/show/3"
1056
- get user_product_project_url
1057
- assert_equal "show 3 1 2 ", body
1058
- assert_equal user_product_project_url, @app.url(:project, :show, :user_id => 1, :product_id => 2, :id => 3)
1059
-
1060
- user_project_url = "/user/1/shop/1/project"
1061
- get user_project_url
1062
- assert_equal "index 1 1", body
1063
- assert_equal user_project_url, @app.url(:project, :index, :user_id => 1, :shop_id => 1)
1064
-
1065
- user_project_edit_url = "/user/1/shop/1/project/edit/2"
1066
- get user_project_edit_url
1067
- assert_equal "edit 2 1 1", body
1068
- assert_equal user_project_edit_url, @app.url(:project, :edit, :user_id => 1, :id => 2, :shop_id => 1)
1069
-
1070
- user_product_project_url = "/user/1/shop/1/product/2/project/show/3"
1071
- get user_product_project_url
1072
- assert_equal "show 3 1 2 1", body
1073
- assert_equal user_product_project_url, @app.url(:project, :show, :user_id => 1, :product_id => 2, :id => 3, :shop_id => 1)
1074
- end
1075
-
1076
- should "respect map in parents with shallowing" do
1077
- mock_app do
1078
- controller :project do
1079
- parent :shop, :map => "/foo/bar"
1080
- get(:index) { "index #{params[:shop_id]}" }
1081
- end
1082
- end
1083
-
1084
- shop_project_url = "/foo/bar/1/project"
1085
- get shop_project_url
1086
- assert_equal "index 1", body
1087
- assert_equal shop_project_url, @app.url(:project, :index, :shop_id => 1)
1088
- end
1089
-
1090
- should "use default values" do
1091
- mock_app do
1092
- controller :lang => :it do
1093
- get(:index, :map => "/:lang") { "lang is #{params[:lang]}" }
1094
- end
1095
- # This is only for be sure that default values
1096
- # work only for the given controller
1097
- get(:foo, :map => "/foo") {}
1098
- end
1099
- assert_equal "/it", @app.url(:index)
1100
- assert_equal "/foo", @app.url(:foo)
1101
- get "/en"
1102
- assert_equal "lang is en", body
1103
- end
1104
-
1105
- should "transitions to the next matching route on pass" do
1106
- mock_app do
1107
- get '/:foo' do
1108
- pass
1109
- 'Hello Foo'
1110
- end
1111
- get '/:bar' do
1112
- 'Hello World'
1113
- end
1114
- end
1115
-
1116
- get '/za'
1117
- assert_equal 'Hello World', body
1118
- end
1119
-
1120
- should "filters by accept header" do
1121
- mock_app do
1122
- get '/foo', :provides => [:xml, :js] do
1123
- request.env['HTTP_ACCEPT']
1124
- end
1125
- end
1126
-
1127
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
1128
- assert ok?
1129
- assert_equal 'application/xml', body
1130
- assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
1131
-
1132
- get '/foo.xml'
1133
- assert ok?
1134
- assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
1135
-
1136
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/javascript' }
1137
- assert ok?
1138
- assert_equal 'application/javascript', body
1139
- assert_equal 'application/javascript;charset=utf-8', response.headers['Content-Type']
1140
-
1141
- get '/foo.js'
1142
- assert ok?
1143
- assert_equal 'application/javascript;charset=utf-8', response.headers['Content-Type']
1144
-
1145
- get '/foo', {}, { "HTTP_ACCEPT" => 'text/html' }
1146
- assert_equal 406, status
1147
- end
1148
-
1149
- should "does not allow global provides" do
1150
- mock_app do
1151
- provides :xml
1152
-
1153
- get("/foo"){ "Foo in #{content_type.inspect}" }
1154
- get("/bar"){ "Bar in #{content_type.inspect}" }
1155
- end
1156
-
1157
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
1158
- assert_equal 'Foo in :xml', body
1159
- get '/foo'
1160
- assert_equal 'Foo in :xml', body
1161
-
1162
- get '/bar', {}, { 'HTTP_ACCEPT' => 'application/xml' }
1163
- assert_equal 'Bar in nil', body
1164
- end
1165
-
1166
- should "does not allow global provides in controller" do
1167
- mock_app do
1168
- controller :base do
1169
- provides :xml
1170
-
1171
- get(:foo, "/foo"){ "Foo in #{content_type.inspect}" }
1172
- get(:bar, "/bar"){ "Bar in #{content_type.inspect}" }
1173
- end
1174
- end
1175
-
1176
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
1177
- assert_equal 'Foo in :xml', body
1178
- get '/foo'
1179
- assert_equal 'Foo in :xml', body
1180
-
1181
- get '/bar', {}, { 'HTTP_ACCEPT' => 'application/xml' }
1182
- assert_equal 'Bar in nil', body
1183
- end
1184
-
1185
- should "map non named routes in controllers" do
1186
- mock_app do
1187
- controller :base do
1188
- get("/foo") { "ok" }
1189
- get("/bar") { "ok" }
1190
- end
1191
- end
1192
-
1193
- get "/base/foo"
1194
- assert ok?
1195
- get "/base/bar"
1196
- assert ok?
1197
- end
1198
-
1199
- should "set content_type to :html for both empty Accept as well as Accept text/html" do
1200
- mock_app do
1201
- provides :html
1202
-
1203
- get("/foo"){ content_type.to_s }
1204
- end
1205
-
1206
- get '/foo', {}, {}
1207
- assert_equal 'html', body
1208
-
1209
- get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html' }
1210
- assert_equal 'html', body
1211
- end
1212
-
1213
- should "set content_type to :html if Accept */*" do
1214
- mock_app do
1215
- get("/foo", :provides => [:html, :js]) { content_type.to_s }
1216
- end
1217
- get '/foo', {}, {}
1218
- assert_equal 'html', body
1219
-
1220
- get '/foo', {}, { 'HTTP_ACCEPT' => '*/*;q=0.5' }
1221
- assert_equal 'html', body
1222
- end
1223
-
1224
- should "set content_type to :js if Accept includes both application/javascript and */*;q=0.5" do
1225
- mock_app do
1226
- get("/foo", :provides => [:html, :js]) { content_type.to_s }
1227
- end
1228
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/javascript, */*;q=0.5' }
1229
- assert_equal 'js', body
1230
- end
1231
-
1232
- should "set content_type to :html if Accept */* and provides of :any" do
1233
- mock_app do
1234
- get("/foo", :provides => :any) { content_type.to_s }
1235
- end
1236
-
1237
- get '/foo', {}, { 'HTTP_ACCEPT' => '*/*' }
1238
- assert_equal 'html', body
1239
- end
1240
-
1241
- should "set content_type to :js if Accept includes both application/javascript, */*;q=0.5 and provides of :any" do
1242
- mock_app do
1243
- get("/foo", :provides => :any) { content_type.to_s }
1244
- end
1245
-
1246
- get '/foo', {}, { 'HTTP_ACCEPT' => 'application/javascript, */*;q=0.5' }
1247
- assert_equal 'js', body
1248
- end
1249
-
1250
- should 'allows custom route-conditions to be set via route options and halt' do
1251
- protector = Module.new do
1252
- def protect(*args)
1253
- condition {
1254
- unless authorize(params["user"], params["password"])
1255
- halt 403, "go away"
1256
- end
1257
- }
1258
- end
1259
- end
1260
-
1261
- mock_app do
1262
- register protector
1263
-
1264
- helpers do
1265
- def authorize(username, password)
1266
- username == "foo" && password == "bar"
1267
- end
1268
- end
1269
-
1270
- get "/", :protect => true do
1271
- "hey"
1272
- end
1273
- end
1274
-
1275
- get "/"
1276
- assert forbidden?
1277
- assert_equal "go away", body
1278
-
1279
- get "/", :user => "foo", :password => "bar"
1280
- assert ok?
1281
- assert_equal "hey", body
1282
- end
1283
-
1284
- should 'allows custom route-conditions to be set via route options using two routes' do
1285
- protector = Module.new do
1286
- def protect(*args)
1287
- condition { authorize(params["user"], params["password"]) }
1288
- end
1289
- end
1290
-
1291
- mock_app do
1292
- register protector
1293
-
1294
- helpers do
1295
- def authorize(username, password)
1296
- username == "foo" && password == "bar"
1297
- end
1298
- end
1299
-
1300
- get "/", :protect => true do
1301
- "hey"
1302
- end
1303
-
1304
- get "/" do
1305
- "go away"
1306
- end
1307
- end
1308
-
1309
- get "/"
1310
- assert_equal "go away", body
1311
-
1312
- get "/", :user => "foo", :password => "bar"
1313
- assert ok?
1314
- assert_equal "hey", body
1315
- end
1316
-
1317
- should "allow concise routing" do
1318
- mock_app do
1319
- get :index, ":id" do
1320
- params[:id]
1321
- end
1322
-
1323
- get :map, "route/:id" do
1324
- params[:id]
1325
- end
1326
- end
1327
-
1328
- get "/123"
1329
- assert_equal "123", body
1330
-
1331
- get "/route/123"
1332
- assert_equal "123", body
1333
- end
1334
-
1335
- should "support halting with 404 and message" do
1336
- mock_app do
1337
- controller do
1338
- get :index do
1339
- halt 404, "not found"
1340
- end
1341
- end
1342
- end
1343
-
1344
- get "/"
1345
- assert_equal 404, status
1346
- assert_equal "not found", body
1347
- end
1348
-
1349
- should "allow passing & halting in before filters" do
1350
- mock_app do
1351
- controller do
1352
- before { env['QUERY_STRING'] == 'secret' or pass }
1353
- get :index do
1354
- "secret index"
1355
- end
1356
- end
1357
-
1358
- controller do
1359
- before { env['QUERY_STRING'] == 'halt' and halt 401, 'go away!' }
1360
- get :index do
1361
- "index"
1362
- end
1363
- end
1364
- end
1365
-
1366
- get "/?secret"
1367
- assert_equal "secret index", body
1368
-
1369
- get "/?halt"
1370
- assert_equal "go away!", body
1371
- assert_equal 401, status
1372
-
1373
- get "/"
1374
- assert_equal "index", body
1375
- end
1376
-
1377
- should 'scope filters in the given controller' do
1378
- mock_app do
1379
- before { @global = 'global' }
1380
- after { @global = nil }
1381
-
1382
- controller :foo do
1383
- before { @foo = :foo }
1384
- after { @foo = nil }
1385
- get("/") { [@foo, @bar, @global].compact.join(" ") }
1386
- end
1387
-
1388
- get("/") { [@foo, @bar, @global].compact.join(" ") }
1389
-
1390
- controller :bar do
1391
- before { @bar = :bar }
1392
- after { @bar = nil }
1393
- get("/") { [@foo, @bar, @global].compact.join(" ") }
1394
- end
1395
- end
1396
-
1397
- get "/bar"
1398
- assert_equal "bar global", body
1399
-
1400
- get "/foo"
1401
- assert_equal "foo global", body
1402
-
1403
- get "/"
1404
- assert_equal "global", body
1405
- end
1406
-
1407
- should 'works with optionals params' do
1408
- mock_app do
1409
- get("/foo(/:bar)?") { params[:bar] }
1410
- end
1411
-
1412
- get "/foo/bar"
1413
- assert_equal "bar", body
1414
-
1415
- get "/foo"
1416
- assert_equal "", body
1417
- end
1418
-
1419
- should 'work with multiple dashed params' do
1420
- mock_app do
1421
- get "/route/:foo/:bar/:baz", :provides => :html do
1422
- "#{params[:foo]};#{params[:bar]};#{params[:baz]}"
1423
- end
1424
- end
1425
-
1426
- get "/route/foo/bar/baz"
1427
- assert_equal 'foo;bar;baz', body
1428
-
1429
- get "/route/foo/bar-whatever/baz"
1430
- assert_equal 'foo;bar-whatever;baz', body
1431
- end
1432
-
1433
- should 'work with arbitrary params' do
1434
- mock_app do
1435
- get(:testing) { params[:foo] }
1436
- end
1437
-
1438
- url = @app.url(:testing, :foo => 'bar')
1439
- assert_equal "/testing?foo=bar", url
1440
- get url
1441
- assert_equal "bar", body
1442
- end
1443
-
1444
- should 'ignore nil params' do
1445
- mock_app do
1446
- get(:testing, :provides => [:html, :json]) do
1447
- end
1448
- end
1449
- assert_equal '/testing.html', @app.url(:testing, :format => :html)
1450
- assert_equal '/testing', @app.url(:testing, :format => nil)
1451
- end
1452
-
1453
- should 'be able to access params in a before filter' do
1454
- username_from_before_filter = nil
1455
-
1456
- mock_app do
1457
- before do
1458
- username_from_before_filter = params[:username]
1459
- end
1460
-
1461
- get :users, :with => :username do
1462
- end
1463
- end
1464
- get '/users/josh'
1465
- assert_equal 'josh', username_from_before_filter
1466
- end
1467
-
1468
- should "be able to access params normally when a before filter is specified" do
1469
- mock_app do
1470
- before { }
1471
- get :index do
1472
- params.inspect
1473
- end
1474
- end
1475
- get '/?test=what'
1476
- assert_equal '{"test"=>"what"}', body
1477
- end
1478
-
1479
- should 'work with controller and arbitrary params' do
1480
- mock_app do
1481
- get(:testing) { params[:foo] }
1482
- controller :test1 do
1483
- get(:url1) { params[:foo] }
1484
- get(:url2, :provides => [:html, :json]) { params[:foo] }
1485
- end
1486
- end
1487
-
1488
- url = @app.url(:test1, :url1, :foo => 'bar1')
1489
- assert_equal "/test1/url1?foo=bar1", url
1490
- get url
1491
- assert_equal "bar1", body
1492
-
1493
- url = @app.url(:test1, :url2, :foo => 'bar2')
1494
- assert_equal "/test1/url2?foo=bar2", url
1495
- get url
1496
- assert_equal "bar2", body
1497
- end
1498
-
1499
- should "parse two routes with the same path but different http verbs" do
1500
- mock_app do
1501
- get(:index) { "This is the get index" }
1502
- post(:index) { "This is the post index" }
1503
- end
1504
- get "/"
1505
- assert_equal "This is the get index", body
1506
- post "/"
1507
- assert_equal "This is the post index", body
1508
- end
1509
-
1510
- should "use optionals params" do
1511
- mock_app do
1512
- get(:index, :map => "/:foo(/:bar)?") { "#{params[:foo]}-#{params[:bar]}" }
1513
- end
1514
- get "/foo"
1515
- assert_equal "foo-", body
1516
- get "/foo/bar"
1517
- assert_equal "foo-bar", body
1518
- end
1519
-
1520
- should "parse two routes with the same path but different http verbs and provides" do
1521
- mock_app do
1522
- get(:index, :provides => [:html, :json]) { "This is the get index.#{content_type}" }
1523
- post(:index, :provides => [:html, :json]) { "This is the post index.#{content_type}" }
1524
- end
1525
- get "/"
1526
- assert_equal "This is the get index.html", body
1527
- post "/"
1528
- assert_equal "This is the post index.html", body
1529
- get "/.json"
1530
- assert_equal "This is the get index.json", body
1531
- get "/.js"
1532
- assert_equal 404, status
1533
- post "/.json"
1534
- assert_equal "This is the post index.json", body
1535
- post "/.js"
1536
- assert_equal 404, status
1537
- end
1538
-
1539
- should "allow controller level mapping" do
1540
- mock_app do
1541
- controller :map => "controller-:id" do
1542
- get(:url3) { "#{params[:id]}" }
1543
- get(:url4, :map => 'test-:id2') { "#{params[:id]}, #{params[:id2]}" }
1544
- end
1545
- end
1546
-
1547
- url = @app.url(:url3, :id => 1)
1548
- assert_equal "/controller-1/url3", url
1549
- get url
1550
- assert_equal "1", body
1551
-
1552
- url = @app.url(:url4, 1, 2)
1553
- assert_equal "/controller-1/test-2", url
1554
- get url
1555
- assert_equal "1, 2", body
1556
- end
1557
-
1558
- should "replace name of named controller with mapping path" do
1559
- mock_app do
1560
- controller :ugly, :map => "/pretty/:id" do
1561
- get(:url3) { "#{params[:id]}" }
1562
- get(:url4, :map => 'test-:id2') { "#{params[:id]}, #{params[:id2]}" }
1563
- end
1564
- controller :voldemort, :map => "" do
1565
- get(:url5) { "okay" }
1566
- end
1567
- end
1568
-
1569
- url = @app.url(:ugly, :url3, :id => 1)
1570
- assert_equal "/pretty/1/url3", url
1571
- get url
1572
- assert_equal "1", body
1573
-
1574
- url = @app.url(:ugly, :url4, 3, 5)
1575
- assert_equal "/pretty/3/test-5", url
1576
- get url
1577
- assert_equal "3, 5", body
1578
-
1579
- url = @app.url(:voldemort, :url5)
1580
- assert_equal "/url5", url
1581
- get url
1582
- assert_equal 'okay', body
1583
- end
1584
-
1585
- should 'use absolute and relative maps' do
1586
- mock_app do
1587
- controller :one do
1588
- parent :three
1589
- get :index, :map => 'one' do; end
1590
- get :index2, :map => '/one' do; end
1591
- end
1592
-
1593
- controller :two, :map => 'two' do
1594
- parent :three
1595
- get :index, :map => 'two' do; end
1596
- get :index2, :map => '/two', :with => :id do; end
1597
- end
1598
- end
1599
- assert_equal "/three/three_id/one", @app.url(:one, :index, 'three_id')
1600
- assert_equal "/one", @app.url(:one, :index2)
1601
- assert_equal "/two/three/three_id/two", @app.url(:two, :index, 'three_id')
1602
- assert_equal "/two/four_id", @app.url(:two, :index2, 'four_id')
1603
- end
1604
-
1605
- should "work with params and parent options" do
1606
- mock_app do
1607
- controller :test2, :parent => :parent1, :parent1_id => 1 do
1608
- get(:url3) { params[:foo] }
1609
- get(:url4, :with => :with1) { params[:foo] }
1610
- get(:url5, :with => :with2, :provides => [:html]) { params[:foo] }
1611
- end
1612
- end
1613
-
1614
- url = @app.url(:test2, :url3, :foo => 'bar3')
1615
- assert_equal "/parent1/1/test2/url3?foo=bar3", url
1616
- get url
1617
- assert_equal "bar3", body
1618
-
1619
- url = @app.url(:test2, :url4, :with1 => 'awith1', :foo => 'bar4')
1620
- assert_equal "/parent1/1/test2/url4/awith1?foo=bar4", url
1621
- get url
1622
- assert_equal "bar4", body
1623
-
1624
- url = @app.url(:test2, :url5, :with2 => 'awith1', :foo => 'bar5')
1625
- assert_equal "/parent1/1/test2/url5/awith1?foo=bar5", url
1626
- get url
1627
- assert_equal "bar5", body
1628
- end
1629
-
1630
- should "parse params without explicit provides for every matching route" do
1631
- mock_app do
1632
- get(:index, :map => "/foos/:bar") { "get bar = #{params[:bar]}" }
1633
- post :create, :map => "/foos/:bar", :provides => [:html, :js] do
1634
- "post bar = #{params[:bar]}"
1635
- end
1636
- end
1637
-
1638
- get "/foos/hello"
1639
- assert_equal "get bar = hello", body
1640
- post "/foos/hello"
1641
- assert_equal "post bar = hello", body
1642
- post "/foos/hello.js"
1643
- assert_equal "post bar = hello", body
1644
- end
1645
-
1646
- should "properly route to first foo with two similar routes" do
1647
- mock_app do
1648
- controllers do
1649
- get('/foo/') { "this is foo" }
1650
- get(:show, :map => "/foo/:bar/:id") { "/foo/#{params[:bar]}/#{params[:id]}" }
1651
- end
1652
- end
1653
- get "/foo"
1654
- assert_equal "this is foo", body
1655
- get "/foo/"
1656
- assert_equal "this is foo", body
1657
- get '/foo/5/10'
1658
- assert_equal "/foo/5/10", body
1659
- end
1660
-
1661
- should "index routes should be optional when nested" do
1662
- mock_app do
1663
- controller '/users', :provides => [:json] do
1664
- get '/' do
1665
- "foo"
1666
- end
1667
- end
1668
- end
1669
- get "/users.json"
1670
- assert_equal "foo", body
1671
- end
1672
-
1673
- should "use provides as conditional" do
1674
- mock_app do
1675
- provides :json
1676
- get "/" do
1677
- "foo"
1678
- end
1679
- end
1680
- get "/.json"
1681
- assert_equal "foo", body
1682
- end
1683
-
1684
- should_eventually "reset provides for routes that didn't use it" do
1685
- mock_app do
1686
- get('/foo', :provides => :js){}
1687
- get('/bar'){}
1688
- end
1689
- get '/foo'
1690
- assert ok?
1691
- get '/foo.js'
1692
- assert ok?
1693
- get '/bar'
1694
- assert ok?
1695
- get '/bar.js'
1696
- assert_equal 404, status
1697
- end
1698
-
1699
- should "pass controller conditions to each route" do
1700
- counter = 0
1701
-
1702
- mock_app do
1703
- self.class.send(:define_method, :increment!) do |*args|
1704
- condition { counter += 1 }
1705
- end
1706
-
1707
- controller :posts, :conditions => {:increment! => true} do
1708
- get("/foo") { "foo" }
1709
- get("/bar") { "bar" }
1710
- end
1711
-
1712
- end
1713
-
1714
- get "/posts/foo"
1715
- get "/posts/bar"
1716
- assert_equal 2, counter
1717
- end
1718
-
1719
- should "allow controller conditions to be overridden" do
1720
- counter = 0
1721
-
1722
- mock_app do
1723
- self.class.send(:define_method, :increment!) do |increment|
1724
- condition { counter += 1 } if increment
1725
- end
1726
-
1727
- controller :posts, :conditions => {:increment! => true} do
1728
- get("/foo") { "foo" }
1729
- get("/bar", :increment! => false) { "bar" }
1730
- end
1731
-
1732
- end
1733
-
1734
- get "/posts/foo"
1735
- get "/posts/bar"
1736
- assert_equal 1, counter
1737
- end
1738
-
1739
- should "parse params with class level provides" do
1740
- mock_app do
1741
- controllers :posts, :provides => [:html, :js] do
1742
- post(:create, :map => "/foo/:bar/:baz/:id") {
1743
- "POST CREATE #{params[:bar]} - #{params[:baz]} - #{params[:id]}"
1744
- }
1745
- end
1746
- controllers :topics, :provides => [:js, :html] do
1747
- get(:show, :map => "/foo/:bar/:baz/:id") { render "topics/show" }
1748
- post(:create, :map => "/foo/:bar/:baz") { "TOPICS CREATE #{params[:bar]} - #{params[:baz]}" }
1749
- end
1750
- end
1751
- post "/foo/bar/baz.js"
1752
- assert_equal "TOPICS CREATE bar - baz", body, "should parse params with explicit .js"
1753
- post @app.url(:topics, :create, :format => :js, :bar => 'bar', :baz => 'baz')
1754
- assert_equal "TOPICS CREATE bar - baz", body, "should parse params from generated url"
1755
- post "/foo/bar/baz/5.js"
1756
- assert_equal "POST CREATE bar - baz - 5", body
1757
- post @app.url(:posts, :create, :format => :js, :bar => 'bar', :baz => 'baz', :id => 5)
1758
- assert_equal "POST CREATE bar - baz - 5", body
1759
- end
1760
-
1761
- should "parse params properly with inline provides" do
1762
- mock_app do
1763
- controllers :posts do
1764
- post(:create, :map => "/foo/:bar/:baz/:id", :provides => [:html, :js]) {
1765
- "POST CREATE #{params[:bar]} - #{params[:baz]} - #{params[:id]}"
1766
- }
1767
- end
1768
- controllers :topics do
1769
- get(:show, :map => "/foo/:bar/:baz/:id", :provides => [:html, :js]) { render "topics/show" }
1770
- post(:create, :map => "/foo/:bar/:baz", :provides => [:html, :js]) { "TOPICS CREATE #{params[:bar]} - #{params[:baz]}" }
1771
- end
1772
- end
1773
- post @app.url(:topics, :create, :format => :js, :bar => 'bar', :baz => 'baz')
1774
- assert_equal "TOPICS CREATE bar - baz", body, "should properly post to topics create action"
1775
- post @app.url(:posts, :create, :format => :js, :bar => 'bar', :baz => 'baz', :id => 5)
1776
- assert_equal "POST CREATE bar - baz - 5", body, "should properly post to create action"
1777
- end
1778
-
1779
- should "have overideable format" do
1780
- ::Rack::Mime::MIME_TYPES[".other"] = "text/html"
1781
- mock_app do
1782
- before do
1783
- params[:format] ||= :other
1784
- end
1785
- get("/format_test", :provides => [:html, :other]){ content_type.to_s }
1786
- end
1787
- get "/format_test"
1788
- assert_equal "other", body
1789
- ::Rack::Mime::MIME_TYPES.delete('.other')
1790
- end
1791
-
1792
- should 'invokes handlers registered with ::error when raised' do
1793
- mock_app do
1794
- set :raise_errors, false
1795
- error(FooError) { 'Foo!' }
1796
- get '/' do
1797
- raise FooError
1798
- end
1799
- end
1800
- get '/'
1801
- assert_equal 500, status
1802
- assert_equal 'Foo!', body
1803
- end
1804
-
1805
- should 'have MethodOverride middleware' do
1806
- mock_app do
1807
- put('/') { 'okay' }
1808
- end
1809
- assert @app.method_override?
1810
- post '/', {'_method'=>'PUT'}, {}
1811
- assert_equal 200, status
1812
- assert_equal 'okay', body
1813
- end
1814
-
1815
- should 'return value from params' do
1816
- mock_app do
1817
- get("/foo/:bar"){ raise "'bar' should be a string" unless params[:bar].kind_of? String}
1818
- end
1819
- get "/foo/50"
1820
- assert ok?
1821
- end
1822
-
1823
- should 'have MethodOverride middleware with more options' do
1824
- mock_app do
1825
- put('/hi', :provides => [:json]) { 'hi' }
1826
- end
1827
- post '/hi', {'_method'=>'PUT'}
1828
- assert_equal 200, status
1829
- assert_equal 'hi', body
1830
- post '/hi.json', {'_method'=>'PUT'}
1831
- assert_equal 200, status
1832
- assert_equal 'hi', body
1833
- post '/hi.json'
1834
- assert_equal 405, status
1835
- end
1836
-
1837
- should 'parse nested params' do
1838
- mock_app do
1839
- get(:index) { "%s %s" % [params[:account][:name], params[:account][:surname]] }
1840
- end
1841
- get "/?account[name]=foo&account[surname]=bar"
1842
- assert_equal 'foo bar', body
1843
- get @app.url(:index, "account[name]" => "foo", "account[surname]" => "bar")
1844
- assert_equal 'foo bar', body
1845
- end
1846
-
1847
- should 'render sinatra NotFound page' do
1848
- mock_app { set :environment, :development }
1849
- get "/"
1850
- assert_equal 404, status
1851
- assert_match %r{(Sinatra doesn&rsquo;t know this ditty.|<h1>Not Found</h1>)}, body
1852
- end
1853
-
1854
- should 'render a custom NotFound page' do
1855
- mock_app do
1856
- error(Sinatra::NotFound) { "not found" }
1857
- end
1858
- get "/"
1859
- assert_equal 404, status
1860
- assert_match(/not found/, body)
1861
- end
1862
-
1863
- should 'render a custom 404 page using not_found' do
1864
- mock_app do
1865
- not_found { "custom 404 not found" }
1866
- end
1867
- get "/"
1868
- assert_equal 404, status
1869
- assert_equal "custom 404 not found", body
1870
- end
1871
-
1872
- should 'render a custom error page using error method' do
1873
- skip
1874
- mock_app do
1875
- error(404) { "custom 404 error" }
1876
- end
1877
- get "/"
1878
- assert_equal 404, status
1879
- assert_equal "custom 404 error", body
1880
- end
1881
-
1882
- should 'render a custom 403 page' do
1883
- mock_app do
1884
- error(403) { "custom 403 not found" }
1885
- get("/") { status 403 }
1886
- end
1887
- get "/"
1888
- assert_equal 403, status
1889
- assert_equal "custom 403 not found", body
1890
- end
1891
-
1892
- should 'recognize paths' do
1893
- mock_app do
1894
- controller :foo do
1895
- get(:bar, :map => "/my/:id/custom-route") { }
1896
- end
1897
- get(:simple, :map => "/simple/:id") { }
1898
- get(:with_format, :with => :id, :provides => :js) { }
1899
- end
1900
- assert_equal [:"foo bar", { :id => "fantastic" }.with_indifferent_access], @app.recognize_path(@app.url(:foo, :bar, :id => :fantastic))
1901
- assert_equal [:"foo bar", { :id => "18" }.with_indifferent_access], @app.recognize_path(@app.url(:foo, :bar, :id => 18))
1902
- assert_equal [:simple, { :id => "bar" }.with_indifferent_access], @app.recognize_path(@app.url(:simple, :id => "bar"))
1903
- assert_equal [:simple, { :id => "true" }.with_indifferent_access], @app.recognize_path(@app.url(:simple, :id => true))
1904
- assert_equal [:simple, { :id => "9" }.with_indifferent_access], @app.recognize_path(@app.url(:simple, :id => 9))
1905
- assert_equal [:with_format, { :id => "bar", :format => "js" }.with_indifferent_access], @app.recognize_path(@app.url(:with_format, :id => "bar", :format => :js))
1906
- assert_equal [:with_format, { :id => "true", :format => "js" }.with_indifferent_access], @app.recognize_path(@app.url(:with_format, :id => true, :format => "js"))
1907
- assert_equal [:with_format, { :id => "9", :format => "js" }.with_indifferent_access], @app.recognize_path(@app.url(:with_format, :id => 9, :format => :js))
1908
- end
1909
-
1910
- should 'have current_path' do
1911
- mock_app do
1912
- controller :foo do
1913
- get(:index) { current_path }
1914
- get :bar, :map => "/paginate/:page" do
1915
- current_path
1916
- end
1917
- get(:after) { current_path }
1918
- end
1919
- end
1920
- get "/paginate/10"
1921
- assert_equal "/paginate/10", body
1922
- get "/foo/after"
1923
- assert_equal "/foo/after", body
1924
- get "/foo"
1925
- assert_equal "/foo", body
1926
- end
1927
-
1928
- should 'accept :map and :parent' do
1929
- mock_app do
1930
- controller :posts do
1931
- get :show, :parent => :users, :map => "posts/:id" do
1932
- "#{params[:user_id]}-#{params[:id]}"
1933
- end
1934
- end
1935
- end
1936
- get '/users/123/posts/321'
1937
- assert_equal "123-321", body
1938
- end
1939
-
1940
- should 'change params in current_path' do
1941
- mock_app do
1942
- get :index, :map => "/paginate/:page" do
1943
- current_path(:page => 66)
1944
- end
1945
- end
1946
- get @app.url(:index, :page => 10)
1947
- assert_equal "/paginate/66", body
1948
- end
1949
-
1950
- should 'not route get :users, :with => :id to /users//' do
1951
- mock_app do
1952
- get(:users, :with => :id) { 'boo' }
1953
- end
1954
- get '/users//'
1955
- assert_equal 404, status
1956
- end
1957
- describe "Padrino::ParamsProtection" do
1958
- before do
1959
- @teri = { 'name' => 'Teri Bauer', 'position' => 'baby' }
1960
- @kim = { 'name' => 'Kim Bauer', 'position' => 'daughter', 'child' => @teri }
1961
- @jack = { 'name' => 'Jack Bauer', 'position' => 'terrorist', 'child' => @kim }
1962
- @family = { 'name' => 'Bauer', 'persons' => { 1 => @teri, 2 => @kim, 3 => @jack } }
1963
- end
1964
-
1965
- it 'should drop all parameters except allowed ones' do
1966
- result = nil
1967
- mock_app do
1968
- post :basic, :params => [ :name ] do
1969
- result = params
1970
- ''
1971
- end
1972
- end
1973
- post '/basic?' + @jack.to_query
1974
- assert_equal({ 'name' => @jack['name'] }, result)
1975
- end
1976
-
1977
- it 'should preserve original params' do
1978
- result = nil
1979
- mock_app do
1980
- post :basic, :params => [ :name ] do
1981
- result = original_params
1982
- ''
1983
- end
1984
- end
1985
- post '/basic?' + @jack.to_query
1986
- assert_equal(@jack, result)
1987
- end
1988
-
1989
- it 'should work with recursive data' do
1990
- result = nil
1991
- mock_app do
1992
- post :basic, :params => [ :name, :child => [ :name, :child => [ :name ] ] ] do
1993
- result = [params, original_params]
1994
- ''
1995
- end
1996
- end
1997
- post '/basic?' + @jack.to_query
1998
- assert_equal(
1999
- [
2000
- { 'name' => @jack['name'], 'child' => { 'name' => @kim['name'], 'child' => { 'name' => @teri['name'] } } },
2001
- @jack
2002
- ],
2003
- result
2004
- )
2005
- end
2006
-
2007
- it 'should be able to process the data' do
2008
- result = nil
2009
- mock_app do
2010
- post :basic, :params => [ :name, :position => proc{ |v| 'anti-'+v } ] do
2011
- result = params
2012
- ''
2013
- end
2014
- end
2015
- post '/basic?' + @jack.to_query
2016
- assert_equal({ 'name' => @jack['name'], 'position' => 'anti-terrorist' }, result)
2017
- end
2018
-
2019
- it 'should pass :with parameters' do
2020
- result = nil
2021
- mock_app do
2022
- post :basic, :with => [:id, :tag], :params => [ :name ] do
2023
- result = params
2024
- ''
2025
- end
2026
- end
2027
- post '/basic/24/42?' + @jack.to_query
2028
- assert_equal({ 'name' => @jack['name'], 'id' => '24', 'tag' => '42' }, result)
2029
- end
2030
-
2031
- it 'should understand true or false values' do
2032
- result = nil
2033
- mock_app do
2034
- get :hide, :with => [ :id ], :params => false do
2035
- result = params
2036
- ''
2037
- end
2038
- get :show, :with => [ :id ], :params => true do
2039
- result = params
2040
- ''
2041
- end
2042
- end
2043
- get '/hide/1?' + @jack.to_query
2044
- assert_equal({"id"=>"1"}, result)
2045
- get '/show/1?' + @jack.to_query
2046
- assert_equal({"id"=>"1"}.merge(@jack), result)
2047
- end
2048
-
2049
- it 'should be configurable with controller options' do
2050
- result = nil
2051
- mock_app do
2052
- controller :persons, :params => [ :name ] do
2053
- post :create, :params => [ :name, :position ] do
2054
- result = params
2055
- ''
2056
- end
2057
- post :update, :with => [ :id ] do
2058
- result = params
2059
- ''
2060
- end
2061
- post :delete, :params => true do
2062
- result = params
2063
- ''
2064
- end
2065
- post :destroy, :with => [ :id ], :params => false do
2066
- result = params
2067
- ''
2068
- end
2069
- end
2070
- controller :noparam, :params => false do
2071
- get :index do
2072
- result = params
2073
- ''
2074
- end
2075
- end
2076
- end
2077
- post '/persons/create?' + @jack.to_query
2078
- assert_equal({ 'name' => @jack['name'], 'position' => 'terrorist' }, result)
2079
- post '/persons/update/1?name=Chloe+O\'Brian&position=hacker'
2080
- assert_equal({ 'id' => '1', 'name' => 'Chloe O\'Brian' }, result)
2081
- post '/persons/delete?' + @jack.to_query
2082
- assert_equal(@jack, result)
2083
- post '/persons/destroy/1?' + @jack.to_query
2084
- assert_equal({"id"=>"1"}, result)
2085
- get '/noparam?a=1;b=2'
2086
- assert_equal({}, result)
2087
- end
2088
-
2089
- it 'should successfully filter hashes' do
2090
- result = nil
2091
- mock_app do
2092
- post :family, :params => [ :persons => [ :name ] ] do
2093
- result = params
2094
- ''
2095
- end
2096
- end
2097
- post '/family?' + @family.to_query
2098
- assert_equal({"persons" => {"3" => {"name" => @jack["name"]}, "2" => {"name" => @kim["name"]}, "1" => {"name" => @teri["name"]}}}, result)
2099
- end
2100
-
2101
- it 'should pass arrays' do
2102
- result = nil
2103
- mock_app do
2104
- post :family, :params => [ :names => [] ] do
2105
- result = params
2106
- ''
2107
- end
2108
- end
2109
- post '/family?names[]=Jack&names[]=Kim&names[]=Teri'
2110
- assert_equal({"names" => %w[Jack Kim Teri]}, result)
2111
- end
2112
- end
2113
- end