http_router 0.10.2 → 0.11.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.
Files changed (42) hide show
  1. data/.gitignore +2 -1
  2. data/Rakefile +7 -5
  3. data/benchmarks/gen2.rb +1 -1
  4. data/benchmarks/rack_mount.rb +8 -14
  5. data/examples/rack_mapper.ru +12 -13
  6. data/examples/variable_with_regex.ru +1 -1
  7. data/http_router.gemspec +1 -1
  8. data/lib/http_router.rb +159 -62
  9. data/lib/http_router/generation_helper.rb +29 -0
  10. data/lib/http_router/generator.rb +150 -0
  11. data/lib/http_router/node.rb +27 -17
  12. data/lib/http_router/node/abstract_request_node.rb +31 -0
  13. data/lib/http_router/node/host.rb +9 -0
  14. data/lib/http_router/node/lookup.rb +8 -10
  15. data/lib/http_router/node/path.rb +23 -38
  16. data/lib/http_router/node/request_method.rb +16 -0
  17. data/lib/http_router/node/root.rb +104 -10
  18. data/lib/http_router/node/scheme.rb +9 -0
  19. data/lib/http_router/node/user_agent.rb +9 -0
  20. data/lib/http_router/regex_route_generation.rb +10 -0
  21. data/lib/http_router/request.rb +7 -17
  22. data/lib/http_router/response.rb +4 -0
  23. data/lib/http_router/route.rb +16 -277
  24. data/lib/http_router/route_helper.rb +126 -0
  25. data/lib/http_router/util.rb +1 -37
  26. data/lib/http_router/version.rb +1 -1
  27. data/test/common/generate.txt +1 -1
  28. data/test/generation.rb +15 -11
  29. data/test/generic.rb +2 -3
  30. data/test/helper.rb +15 -10
  31. data/test/rack/test_route.rb +0 -5
  32. data/test/test_misc.rb +50 -40
  33. data/test/test_mounting.rb +27 -26
  34. data/test/test_recognition.rb +1 -76
  35. metadata +104 -161
  36. data/.rspec +0 -1
  37. data/lib/http_router/node/arbitrary.rb +0 -30
  38. data/lib/http_router/node/request.rb +0 -52
  39. data/lib/http_router/rack.rb +0 -19
  40. data/lib/http_router/rack/builder.rb +0 -61
  41. data/lib/http_router/rack/url_map.rb +0 -16
  42. data/lib/http_router/regex_route.rb +0 -39
@@ -1,41 +1,5 @@
1
1
  class HttpRouter
2
2
  module Util
3
- def self.add_path_generation(target, route, path, path_validation_regex = nil)
4
- regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
5
- regex, code = '', ''
6
- dynamic = false
7
- regex_parts.each_with_index do |part, index|
8
- case part[0]
9
- when ?:, ?*
10
- if index != 0 && regex_parts[index - 1][-1] == ?\\
11
- regex << Regexp.quote(part) unless path_validation_regex
12
- code << part
13
- dynamic = true
14
- else
15
- regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s unless path_validation_regex
16
- code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
17
- dynamic = true
18
- end
19
- else
20
- regex << Regexp.quote(part) unless path_validation_regex
21
- code << part
22
- end
23
- end
24
- path_validation_regex ||= Regexp.new("^#{regex}$") if dynamic
25
- if path_validation_regex
26
- target.instance_eval <<-EOT, __FILE__, __LINE__ + 1
27
- def raw_url(args, options)
28
- url = \"#{code}\"
29
- #{path_validation_regex.inspect}.match(url) ? url : nil
30
- end
31
- EOT
32
- else
33
- target.instance_eval <<-EOT, __FILE__, __LINE__ + 1
34
- def raw_url(args, options)
35
- \"#{code}\"
36
- end
37
- EOT
38
- end
39
- end
3
+
40
4
  end
41
5
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class HttpRouter #:nodoc
3
- VERSION = '0.10.2'
3
+ VERSION = '0.11.0'
4
4
  end
@@ -1,6 +1,6 @@
1
1
  {"a": "/:var"}
2
2
  ["/test", "a", {"var":"test"}]
3
- # ["/test", "a", ["test"]]
3
+ ["/test", "a", ["test"]]
4
4
 
5
5
  {"a": "/"}
6
6
  {"b": "/test"}
data/test/generation.rb CHANGED
@@ -2,18 +2,22 @@ require 'json'
2
2
  require "#{File.dirname(__FILE__)}/generic"
3
3
  class GenerationTest < AbstractTest
4
4
  def run_tests
5
- @tests.map(&:case).each do |(expected_result, name, args)|
6
- args = [args] unless args.is_a?(Array)
7
- args.compact!
8
- args.map!{|a| a.is_a?(Hash) ? Hash[a.map{|k,v| [k.to_sym, v]}] : a }
9
- result = begin
10
- @router.url(name.to_sym, *args)
11
- rescue HttpRouter::InvalidRouteException
12
- nil
13
- rescue HttpRouter::MissingParameterException
14
- nil
5
+ @tests.map(&:case).each do |(expected_result, name, oargs)|
6
+ oargs = [oargs] unless oargs.is_a?(Array)
7
+ oargs.compact!
8
+ oargs.map!{|a| a.is_a?(Hash) ? Hash[a.map{|k,v| [k.to_sym, v]}] : a }
9
+ [[:path, ''], [:url_ns, '://localhost'], [:url, 'http://localhost']].each do |(meth, prefix)|
10
+ args = oargs.map{|o| o.dup rescue o}
11
+ result = begin
12
+ path = @router.send(meth, name.to_sym, *args.dup)
13
+ path
14
+ rescue HttpRouter::InvalidRouteException
15
+ nil
16
+ rescue HttpRouter::MissingParameterException
17
+ nil
18
+ end
19
+ error("Result #{result.inspect} did not match expectation #{expected_result.inspect}") unless result == (expected_result ? prefix + expected_result : expected_result)
15
20
  end
16
- error("Result #{result.inspect} did not match expectation #{expected_result.inspect}") unless result == expected_result
17
21
  end
18
22
  print '.'
19
23
  end
data/test/generic.rb CHANGED
@@ -83,11 +83,11 @@ class AbstractTest
83
83
  @routes.case.each do |route_definition|
84
84
  error("Too many keys! #{route_definition.keys.inspect}") unless route_definition.keys.size == 1
85
85
  route_name, route_properties = route_definition.keys.first, route_definition.values.first
86
+ opts = {:name => route_name.to_sym}
86
87
  route = case route_properties
87
88
  when String
88
- @router.add(route_properties)
89
+ @router.add(route_properties, opts)
89
90
  when Hash
90
- opts = {}
91
91
  route_path = interpret_val(route_properties.delete("path"))
92
92
  if route_properties.key?("conditions")
93
93
  opts[:conditions] = Hash[route_properties.delete("conditions").map{|k, v| [k.to_sym, interpret_val(v)]}]
@@ -102,7 +102,6 @@ class AbstractTest
102
102
  else
103
103
  error("Route isn't a String or hash")
104
104
  end
105
- route.name(route_name.to_sym)
106
105
  route.to{|env| [200, {"env-to-test" => env.dup}, [route_name]]}
107
106
  end
108
107
  run_tests
data/test/helper.rb CHANGED
@@ -2,18 +2,20 @@
2
2
  require 'minitest/autorun'
3
3
  require 'phocus'
4
4
 
5
- class HttpRouter::Route
6
- def default_destination
7
- to{|env| Rack::Response.new("Routing to #{to_s}").finish}
5
+ class HttpRouter
6
+ module RouteHelper
7
+ def default_destination
8
+ to proc {|env| ::Rack::Response.new("Routing to #{object_id}").finish}
9
+ self
10
+ end
8
11
  end
9
12
  end
10
13
 
11
14
  class MiniTest::Unit::TestCase
12
-
13
15
  def router(opts = nil, &blk)
14
16
  @router ||= HttpRouter.new(opts, &blk)
15
17
  if blk
16
- @router.routes.each { |route| route.default_destination if route.dest.nil? }
18
+ @router.routes.each { |route| route.dest ||= proc {|env| Rack::Response.new("Routing to #{route.object_id}").finish} }
17
19
  @router.routes.size > 1 ? @router.routes : @router.routes.first
18
20
  else
19
21
  @router
@@ -44,15 +46,18 @@ class MiniTest::Unit::TestCase
44
46
  end
45
47
 
46
48
  def assert_route(route, request, params = nil, &blk)
47
- if route.is_a?(String)
49
+ route = case route
50
+ when String
48
51
  router.reset!
49
- route = router.add(route)
52
+ router.add(route)
53
+ else
54
+ route
50
55
  end
51
- dest = "Routing to #{route.to_s}"
52
- route.to{|env| Rack::Response.new(dest).finish} if route && route.dest.nil?
56
+ route.default_destination if route && route.dest.nil?
53
57
  request = Rack::MockRequest.env_for(request) if request.is_a?(String)
54
58
  response = @router.call(request)
55
59
  if route
60
+ dest = "Routing to #{route.object_id}"
56
61
  assert_equal [dest], response.last.body
57
62
  if params
58
63
  raise "Params was nil, but you expected params" if request['router.params'].nil?
@@ -72,6 +77,6 @@ class MiniTest::Unit::TestCase
72
77
  route = router.add(route)
73
78
  end
74
79
  route.to{|env| Rack::Response.new("Routing to #{route.to_s}").finish} if route && route.respond_to?(:to) && !route.dest
75
- assert_equal path.gsub('[','%5B').gsub(']','%5D'), router.url(route, *args)
80
+ assert_equal path.gsub('[','%5B').gsub(']','%5D'), router.path(route, *args)
76
81
  end
77
82
  end
@@ -26,11 +26,6 @@ class TestRouteExtensions < MiniTest::Unit::TestCase
26
26
  assert_equal __FILE__, body.path
27
27
  end
28
28
 
29
- def test_chainable
30
- router.get("/index.html").redirect("/").name(:root)
31
- assert_equal "/index.html", router.url(:root)
32
- end
33
-
34
29
  def test_custom_status
35
30
  router.get("/index.html").redirect("/", 303)
36
31
  response = router.call(Rack::MockRequest.env_for("/index.html"))
data/test/test_misc.rb CHANGED
@@ -1,36 +1,41 @@
1
1
  class TestMisc < MiniTest::Unit::TestCase
2
+
2
3
  def test_cloning
3
- r1 = HttpRouter.new { add('/test').name(:test_route).to(:test) }
4
+ r1 = HttpRouter.new { add('/test', :name => :test_route).to(:test) }
4
5
  r2 = r1.clone
5
6
 
6
- r2.add('/test2').name(:test).to(:test2)
7
+ r2.add('/test2', :name => :test).to(:test2)
7
8
  assert_equal 2, r2.routes.size
8
9
 
9
- assert_equal nil, r1.recognize(Rack::Request.new(Rack::MockRequest.env_for('/test2')))
10
- assert r2.recognize(Rack::MockRequest.env_for('/test2'))
11
- assert_equal r1.routes.first, r1.named_routes[:test_route].first
12
- assert_equal r2.routes.first, r2.named_routes[:test_route].first
10
+ matches, other_methods = r1.recognize(Rack::Request.new(Rack::MockRequest.env_for('/test2')))
11
+ assert_equal nil, matches
12
+ assert r2.recognize(Rack::MockRequest.env_for('/test2')).first
13
+ assert_equal r1.routes.size, 1
14
+ assert_equal r2.routes.size, 2
13
15
 
14
- r1.add('/another').name(:test).to(:test2)
16
+ r1.add('/another', :name => :test).to(:test2)
15
17
 
16
18
  assert_equal r1.routes.size, r2.routes.size
17
- assert_equal '/another', r1.url(:test)
18
- assert_equal '/test2', r2.url(:test)
19
+ assert_equal '/another', r1.path(:test)
20
+ assert_equal '/test2', r2.path(:test)
19
21
  assert_equal :test, r1.routes.first.dest
20
22
  assert_equal :test, r2.routes.first.dest
21
23
  end
22
24
 
23
25
  def test_reseting
24
- r = HttpRouter.new { add('/hi').to(:test) }
25
- assert r.recognize(Rack::MockRequest.env_for('/hi'))
26
- r.reset!
27
- assert !r.recognize(Rack::MockRequest.env_for('/hi'))
26
+ router = HttpRouter.new
27
+ r = router.add('/hi').to(:test)
28
+ matches, other_methods = router.recognize(Rack::MockRequest.env_for('/hi'))
29
+ assert_equal r, matches.first.route
30
+ router.reset!
31
+ assert_equal nil, router.recognize(Rack::MockRequest.env_for('/hi')).first
28
32
  end
29
33
 
30
34
  def test_redirect_trailing_slash
31
35
  r = HttpRouter.new(:redirect_trailing_slash => true) { add('/hi').to(:test) }
32
- response = r.recognize(Rack::MockRequest.env_for('/hi/'))
33
- assert_equal nil, response
36
+ response = r.call(Rack::MockRequest.env_for('/hi/'))
37
+ assert_equal 302, response.first
38
+ assert_equal '/hi', response[1]['Location']
34
39
  end
35
40
 
36
41
  def test_multi_recognize
@@ -41,41 +46,45 @@ class TestMisc < MiniTest::Unit::TestCase
41
46
  add('/:var1/there')
42
47
  }
43
48
  response = router.recognize(Rack::MockRequest.env_for('/hi/there'))
44
- assert_equal [r1, r2, r3, r4], response.map{|resp| resp.path.route}
49
+ assert_equal [r1, r2, r3, r4], response.first.map{|resp| resp.path.route}
45
50
  response = router.recognize(Rack::MockRequest.env_for('/hi/var'))
46
- assert_equal [r2, r3], response.map{|resp| resp.path.route}
51
+ assert_equal [r2, r3], response.first.map{|resp| resp.path.route}
47
52
  response = router.recognize(Rack::MockRequest.env_for('/you/there'))
48
- assert_equal [r2, r4], response.map{|resp| resp.path.route}
53
+ assert_equal [r2, r4], response.first.map{|resp| resp.path.route}
49
54
  end
50
55
 
51
56
  def test_multi_name_gen
52
- r = HttpRouter.new
53
- r.add('/').name(:index).default_destination
54
- r.add('/:name').name(:index).default_destination
55
- r.add('/:name/:category').name(:index).default_destination
56
- assert_equal '/', r.url(:index)
57
- assert_equal '/name', r.url(:index, 'name')
58
- assert_equal '/name/category', r.url(:index, 'name', 'category')
57
+ r = router
58
+ r.add('/', :name => :index).default_destination
59
+ r.add('/:name', :name => :index).default_destination
60
+ r.add('/:name/:category', :name => :index).default_destination
61
+ assert_equal '/', r.path(:index)
62
+ assert_equal '/name', r.path(:index, 'name')
63
+ assert_equal '/name/category', r.path(:index, 'name', 'category')
59
64
  end
60
65
 
61
- def test_regex_generation
66
+ def test_yielding_from_recognize
62
67
  r = HttpRouter.new
63
- r.add(%r|/test/.*|, :path_for_generation => '/test/:variable').name(:route).default_destination
64
- assert_equal '/test/var', r.url(:route, "var")
68
+ r1 = r.add('/:name').default_destination
69
+ r2 = r.add('/:name').default_destination
70
+ r3 = r.add('/:name').default_destination
71
+ matches = []
72
+ r.recognize(Rack::MockRequest.env_for('/test')) { |r| matches << r.route }
73
+ assert_equal [r1, r2, r3], matches
65
74
  end
66
75
 
67
- def test_too_many_params
68
- r = HttpRouter.new
69
- r.add(%r|/test/.*|, :path_for_generation => '/test/:variable').name(:route).default_destination
70
- assert_equal '/test/var', r.url(:route, "var")
71
- assert_equal '/test/var', r.url(:route, :variable => "var")
72
- assert_raises(HttpRouter::InvalidRouteException) { r.url(:route) }
76
+ def test_regex_generation
77
+ r = router
78
+ r.add(%r|/test/.*|, :path_for_generation => '/test/:variable', :name => :route).default_destination
79
+ assert_equal '/test/var', r.path(:route, "var")
73
80
  end
74
81
 
75
- def test_too_many_args
76
- r = HttpRouter.new
77
- r.add('/').name(:route).default_destination
78
- assert_raises(HttpRouter::TooManyParametersException) { r.url(:route, "hi") }
82
+ def test_too_many_params
83
+ r = router
84
+ r.add(%r|/test/.*|, :path_for_generation => '/test/:variable', :name => :route).default_destination
85
+ assert_equal '/test/var', r.path(:route, "var")
86
+ assert_equal '/test/var', r.path(:route, :variable => "var")
87
+ assert_raises(HttpRouter::InvalidRouteException) { r.path(:route) }
79
88
  end
80
89
 
81
90
  def test_public_interface
@@ -91,6 +100,7 @@ class TestMisc < MiniTest::Unit::TestCase
91
100
  assert methods.include?(:process_destination_path)
92
101
  assert methods.include?(:rewrite_partial_path_info)
93
102
  assert methods.include?(:rewrite_path_info)
103
+ assert methods.include?(:extend_route)
94
104
  end
95
105
 
96
106
  def test_to_s_and_inspect
@@ -98,8 +108,8 @@ class TestMisc < MiniTest::Unit::TestCase
98
108
  router.add('/').to(:test)
99
109
  router.add('/test').to(:test2)
100
110
  router.post('/test').to(:test3)
101
- assert router.to_s.match(/^#<HttpRouter:0x[0-9a-f]+ number of routes \(3\) ignore_trailing_slash\? \(true\) redirect_trailing_slash\? \(false\) known_methods \(POST\)>$/)
102
- assert router.inspect.match(/^#<HttpRouter:0x[0-9a-f]+ number of routes \(3\) ignore_trailing_slash\? \(true\) redirect_trailing_slash\? \(false\) known_methods \(POST\)>/)
111
+ assert router.to_s.match(/^#<HttpRouter:0x[0-9a-f-]+ number of routes \(3\) ignore_trailing_slash\? \(true\) redirect_trailing_slash\? \(false\)>$/)
112
+ assert router.inspect.match(/^#<HttpRouter:0x[0-9a-f-]+ number of routes \(3\) ignore_trailing_slash\? \(true\) redirect_trailing_slash\? \(false\)>/)
103
113
  assert router.inspect.match(/Path: "\/test" for route unnamed route to :test3/)
104
114
  end
105
115
  end
@@ -2,67 +2,68 @@ class TestMounting < MiniTest::Unit::TestCase
2
2
  def setup
3
3
  @r1 = HttpRouter.new
4
4
  @r2 = HttpRouter.new
5
- @r2.add("/bar").name(:test).to{|env| [200, {}, []]}
5
+ @r2.add("/bar", :name => :test).to{|env| [200, {}, []]}
6
6
  end
7
7
 
8
8
  def test_url_mount_for_child_route
9
9
  route = @r1.add("/foo").to(@r2)
10
10
  assert_equal "/foo", @r2.url_mount.url
11
- assert_equal "/foo/bar", @r2.url(:test)
11
+ assert_equal "/foo/bar", @r2.path(:test)
12
12
  end
13
13
 
14
14
  def test_default_values
15
- route = @r1.add("/foo/:bar").default(:bar => "baz").to(@r2)
16
- assert_equal "/foo/baz/bar", @r2.url(:test)
17
- assert_equal "/foo/haha/bar", @r2.url(:test, :bar => "haha")
15
+ route = @r1.add("/foo/:bar", :default_values => {:bar => "baz"}).to(@r2)
16
+ assert_equal "/foo/baz/bar", @r2.path(:test)
17
+ assert_equal "/foo/haha/bar", @r2.path(:test, :bar => "haha")
18
18
  end
19
19
 
20
20
  def test_multiple_values
21
- @r1.add("/foo/:bar/:baz").default(:bar => "bar").to(@r2)
22
- assert_equal "/foo/bar/baz/bar", @r2.url(:test, :baz => "baz")
21
+ @r1.add("/foo/:bar/:baz", :default_values => {:bar => "bar"}).to(@r2)
22
+ assert_equal "/foo/bar/baz/bar", @r2.path(:test, :baz => "baz")
23
23
  end
24
24
 
25
25
  def test_bubble_params
26
- route = @r1.add("/foo/:bar").default(:bar => "baz").to(@r2)
27
- assert_equal "/foo/baz/bar?bang=ers", @r2.url(:test, :bang => "ers")
28
- assert_equal "/foo/haha/bar?bang=ers", @r2.url(:test, :bar => "haha", :bang => "ers")
26
+ route = @r1.add("/foo/:bar", :default_values => {:bar => 'baz'})
27
+ route.to(@r2)
28
+ assert_equal "/foo/baz/bar?bang=ers", @r2.path(:test, :bang => "ers")
29
+ assert_equal "/foo/haha/bar?bang=ers", @r2.path(:test, :bar => "haha", :bang => "ers")
29
30
  end
30
31
 
31
32
  def test_path_with_optional
32
33
  @r1.add("/foo(/:bar)").to(@r2)
33
- @r2.add("/hey(/:there)").name(:test2).to{|env| [200, {}, []]}
34
- assert_equal "/foo/hey", @r2.url(:test2)
35
- assert_equal "/foo/bar/hey", @r2.url(:test2, :bar => "bar")
36
- assert_equal "/foo/bar/hey/there", @r2.url(:test2, :bar => "bar", :there => "there")
34
+ @r2.add("/hey(/:there)", :name => :test2).to{|env| [200, {}, []]}
35
+ assert_equal "/foo/hey", @r2.path(:test2)
36
+ assert_equal "/foo/bar/hey", @r2.path(:test2, :bar => "bar")
37
+ assert_equal "/foo/bar/hey/there", @r2.path(:test2, :bar => "bar", :there => "there")
37
38
  end
38
39
 
39
40
  def test_nest3
40
41
  @r3 = HttpRouter.new
41
- @r1.add("/foo(/:bar)").default(:bar => "barry").to(@r2)
42
- @r2.add("/hi").name(:hi).to{|env| [200, {}, []]}
42
+ @r1.add("/foo(/:bar)", :default_values => {:bar => 'barry'}).to(@r2)
43
+ @r2.add("/hi", :name => :hi).to{|env| [200, {}, []]}
43
44
  @r2.add("/mounted").to(@r3)
44
- @r3.add("/endpoint").name(:endpoint).to{|env| [200, {}, []]}
45
+ @r3.add("/endpoint", :name => :endpoint).to{|env| [200, {}, []]}
45
46
 
46
- assert_equal "/foo/barry/hi", @r2.url(:hi)
47
- assert_equal "/foo/barry/mounted/endpoint", @r3.url(:endpoint)
48
- assert_equal "/foo/flower/mounted/endpoint", @r3.url(:endpoint, :bar => "flower")
47
+ assert_equal "/foo/barry/hi", @r2.path(:hi)
48
+ assert_equal "/foo/barry/mounted/endpoint", @r3.path(:endpoint)
49
+ assert_equal "/foo/flower/mounted/endpoint", @r3.path(:endpoint, :bar => "flower")
49
50
  end
50
51
 
51
52
  def test_with_default_host
52
- @r1.add("/mounted").default(:host => "example.com").to(@r2)
53
- assert_equal "http://example.com/mounted/bar", @r2.url(:test)
53
+ @r1.add("/mounted", :default_values => {:host => "example.com"}).to(@r2)
54
+ assert_equal "http://example.com/mounted/bar", @r2.path(:test)
54
55
  end
55
56
 
56
57
  def test_with_host
57
58
  @r1.add("/mounted").to(@r2)
58
- assert_equal "/mounted/bar", @r2.url(:test)
59
- assert_equal "http://example.com/mounted/bar", @r2.url(:test, :host => "example.com")
59
+ assert_equal "/mounted/bar", @r2.path(:test)
60
+ assert_equal "http://example.com/mounted/bar", @r2.path(:test, :host => "example.com")
60
61
  end
61
62
 
62
63
  def test_with_scheme
63
64
  @r1.add("/mounted").to(@r2)
64
- assert_equal "/mounted/bar", @r2.url(:test)
65
- assert_equal "https://example.com/mounted/bar", @r2.url(:test, :scheme => "https", :host => "example.com")
65
+ assert_equal "/mounted/bar", @r2.path(:test)
66
+ assert_equal "https://example.com/mounted/bar", @r2.path(:test, :scheme => "https", :host => "example.com")
66
67
  end
67
68
 
68
69
  def test_clone
@@ -8,71 +8,6 @@ class TestRecognition < MiniTest::Unit::TestCase
8
8
  EOT
9
9
  end
10
10
 
11
- def test_match
12
- hello, love80, love8080 = router {
13
- add('test').arbitrary(Proc.new{|req, params| req.rack.host == 'hellodooly' })
14
- add("test").arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 80}
15
- add("test").arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 8080}
16
- }
17
- assert_route love8080, 'http://lovelove:8080/test'
18
- end
19
-
20
- def test_less_specific_node
21
- hello, love80, love8080, general = router {
22
- add("/test").arbitrary(Proc.new{|req, params| req.rack.host == 'hellodooly' })
23
- add("/test").arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 80}
24
- add("/test").arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 8080}
25
- add("/test")
26
- }
27
- assert_route general, 'http://lovelove:8081/test'
28
- assert_route hello, 'http://hellodooly:8081/test'
29
- assert_route love80, 'http://lovelove:80/test'
30
- assert_route love8080, 'http://lovelove:8080/test'
31
- end
32
-
33
- def test_match_request
34
- love80, love8080 = router {
35
- add("/test").get.arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 80}
36
- add("/test").get.arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 8080}
37
- }
38
- assert_route love80, 'http://lovelove:80/test'
39
- assert_route love8080, 'http://lovelove:8080/test'
40
- end
41
-
42
- def test_less_specific_with_request
43
- love80, love8080, general = router {
44
- add("test").post.arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 80}
45
- add("test").post.arbitrary(Proc.new{|req, params| req.rack.host == 'lovelove' }).arbitrary{|req, params| req.rack.port == 8080}
46
- add("test").post
47
- }
48
- assert_route love8080, Rack::MockRequest.env_for('http://lovelove:8080/test', :method => :post)
49
- assert_route love80, Rack::MockRequest.env_for('http://lovelove:80/test', :method => :post)
50
- assert_route general, Rack::MockRequest.env_for('/test', :method => :post)
51
- end
52
-
53
- def test_pass_params
54
- r = router {
55
- add(":test").get.arbitrary(Proc.new{|req, params, dest| params[:test] == 'test' })
56
- }
57
- assert_route r, '/test', {:test => 'test'}
58
- end
59
-
60
- def test_continue
61
- no, yes = router {
62
- add('test').arbitrary_with_continue{|req, p| req.continue[false]}
63
- add('test').arbitrary_with_continue{|req, p| req.continue[true]}
64
- }
65
- assert_route yes, '/test'
66
- end
67
-
68
- def test_passing
69
- passed, working = router {
70
- add('/').to { |env| throw :pass; [200, {}, ['pass']] }
71
- add('/').to { |env| [200, {}, ['working']] }
72
- }
73
- assert_body 'working', router.call(Rack::MockRequest.env_for('/'))
74
- end
75
-
76
11
  def test_non_path_matching
77
12
  passed, working = router {
78
13
  add(:conditions => {:user_agent => /MSIE/}).to { |env| [200, {}, ['IE']] }
@@ -90,18 +25,8 @@ class TestRecognition < MiniTest::Unit::TestCase
90
25
  assert_body 'working', router.call(Rack::MockRequest.env_for('/'))
91
26
  end
92
27
 
93
- def test_request_mutation
94
- got_this_far = false
95
- non_matching, matching = router {
96
- add("/test/:var/:var2/*glob").matching(:var2 => /123/, :glob => /[a-z]+/).get.arbitrary{|env, params| got_this_far = true; false}
97
- add("/test/:var/:var2/*glob").matching(:var2 => /123/, :glob => /[a-z]+/).get
98
- }
99
- assert_route matching, '/test/123/123/asd/aasd/zxcqwe/asdzxc', {:var => '123', :var2 => '123', :glob => %w{asd aasd zxcqwe asdzxc}}
100
- assert got_this_far, "matching should have gotten this far"
101
- end
102
-
103
28
  def test_compiling_uncompiling
104
- @router = HttpRouter.new
29
+ @router = router
105
30
  root = @router.add('/').default_destination
106
31
  assert_route root, '/'
107
32
  test = @router.add('/test').default_destination