usher 0.5.7 → 0.5.8

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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 5
3
- :patch: 7
2
+ :patch: 8
4
3
  :major: 0
4
+ :minor: 5
@@ -12,7 +12,7 @@ class Usher
12
12
  end
13
13
 
14
14
  def reset!
15
- @usher ||= Usher.new(:generator => Usher::Util::Generators::URL.new)
15
+ @usher ||= Usher.new(:generator => Usher::Util::Generators::URL.new, :request_methods => [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains])
16
16
  @module ||= Module.new
17
17
  @module.instance_methods.each do |selector|
18
18
  @module.class_eval { remove_method selector }
@@ -72,7 +72,7 @@ class Usher
72
72
  end
73
73
 
74
74
  def reset!
75
- @router = Usher.new(:generator => Usher::Util::Generators::URL.new)
75
+ @router = Usher.new(:generator => Usher::Util::Generators::URL.new, :request_methods => [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains])
76
76
  @configuration_files = []
77
77
  @module ||= Module.new
78
78
  @controller_route_added = false
data/lib/usher/node.rb CHANGED
@@ -173,7 +173,7 @@ class Usher
173
173
 
174
174
  def set_path_with_destination(path, destination = path)
175
175
  node = path.parts.inject(self){ |node, key| process_path_part(node, key) }
176
- node = process_request_parts(node, request_methods_for_path(path))
176
+ node = process_request_parts(node, request_methods_for_path(path)) if request_methods
177
177
 
178
178
  while node.request_method_type
179
179
  node = (node.request[nil] ||= Node.new(node, Route::RequestMethod.new(node.request_method_type, nil)))
@@ -2,7 +2,7 @@ class Usher
2
2
  class Route
3
3
  class Variable
4
4
  attr_reader :type, :name, :validator, :regex_matcher
5
- attr_accessor :look_ahead, :default_value
5
+ attr_accessor :look_ahead, :default_value, :look_ahead_priority
6
6
 
7
7
  def initialize(name, regex_matcher = nil, validator = nil)
8
8
  @name = name.to_s.to_sym
@@ -0,0 +1,21 @@
1
+ class Usher
2
+ module Util
3
+ class Graph
4
+
5
+ attr_reader :router
6
+
7
+ def initialize(router)
8
+ @router = router
9
+ end
10
+
11
+ def draw(output)
12
+ File.open(output, 'w') do |f|
13
+ f.puts "digraph {"
14
+ f.puts %|node[label="node"]|
15
+ f.puts "}"
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -44,9 +44,19 @@ class Usher
44
44
  part.default_value = default_values[part.name] if part.is_a?(Usher::Route::Variable) && default_values && default_values[part.name]
45
45
  case part
46
46
  when Usher::Route::Variable::Glob
47
- part.look_ahead = path[index + 1, path.size].find{|p| !p.is_a?(Usher::Route::Variable) && !router.delimiter_chars.include?(p[0])} || nil
47
+ if part.look_ahead && !part.look_ahead_priority
48
+ part.look_ahead = nil
49
+ part.look_ahead_priority = true
50
+ else
51
+ part.look_ahead = path[index + 1, path.size].find{|p| !p.is_a?(Usher::Route::Variable) && !router.delimiter_chars.include?(p[0])} || nil
52
+ end
48
53
  when Usher::Route::Variable
49
- part.look_ahead = path[index + 1, path.size].find{|p| router.delimiter_chars.include?(p[0])} || router.delimiters.first
54
+ if part.look_ahead && !part.look_ahead_priority
55
+ part.look_ahead = nil
56
+ part.look_ahead_priority = true
57
+ else
58
+ part.look_ahead = path[index + 1, path.size].find{|p| router.delimiter_chars.include?(p[0])} || router.delimiters.first
59
+ end
50
60
  end
51
61
  end
52
62
  end
data/lib/usher/util.rb CHANGED
@@ -2,5 +2,6 @@ class Usher
2
2
  module Util
3
3
  autoload :Generators, File.join(File.dirname(__FILE__), 'util', 'generate')
4
4
  autoload :Parser, File.join(File.dirname(__FILE__), 'util', 'parser')
5
+ autoload :Graph, File.join(File.dirname(__FILE__), 'util', 'graph')
5
6
  end
6
7
  end
data/lib/usher.rb CHANGED
@@ -53,7 +53,7 @@ class Usher
53
53
  self.generator = options && options.delete(:generator)
54
54
  self.delimiters = options && options.delete(:delimiters) || ['/', '.']
55
55
  self.valid_regex = options && options.delete(:valid_regex) || '[0-9A-Za-z\$\-_\+!\*\',]+'
56
- self.request_methods = options && options.delete(:request_methods) || [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains]
56
+ self.request_methods = options && options.delete(:request_methods)
57
57
  reset!
58
58
  end
59
59
 
@@ -1,8 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
2
  require "usher"
3
3
 
4
- route_set = Usher.new
5
-
6
4
  def build_request(opts)
7
5
  request = mock "Request"
8
6
  opts.each do |k,v|
@@ -14,228 +12,249 @@ end
14
12
  describe "Usher route recognition" do
15
13
 
16
14
  before(:each) do
17
- route_set.reset!
15
+ @route_set = Usher.new(:request_methods => [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains])
18
16
  end
19
17
 
20
18
  describe 'request conditions' do
21
19
 
22
20
  it "should recognize a specific domain name" do
23
- target_route = route_set.add_route('/sample', :controller => 'sample', :action => 'action', :conditions => {:protocol => 'http'})
24
- route_set.add_route('/sample', :controller => 'sample', :action => 'action2', :conditions => {:protocol => 'https'})
25
- route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http'})).path.route.should == target_route
21
+ target_route = @route_set.add_route('/sample', :controller => 'sample', :action => 'action', :conditions => {:protocol => 'http'})
22
+ @route_set.add_route('/sample', :controller => 'sample', :action => 'action2', :conditions => {:protocol => 'https'})
23
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http'})).path.route.should == target_route
26
24
  end
27
25
 
28
26
  it "should recognize a regex domain name" do
29
- target_route = route_set.add_route('/sample', :controller => 'sample', :action => 'action', :conditions => {:domain => /^admin.*$/})
30
- route_set.add_route('/sample', :controller => 'sample', :action => 'action2', :conditions => {:domain => 'www.host.com'})
31
- route_set.recognize(build_request({:method => 'get', :path => '/sample', :domain => 'admin.host.com'})).path.route.should == target_route
27
+ target_route = @route_set.add_route('/sample', :controller => 'sample', :action => 'action', :conditions => {:domain => /^admin.*$/})
28
+ @route_set.add_route('/sample', :controller => 'sample', :action => 'action2', :conditions => {:domain => 'www.host.com'})
29
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample', :domain => 'admin.host.com'})).path.route.should == target_route
32
30
  end
33
31
 
34
32
  it "should recognize a specific route when several http-style restrictions are used" do
35
- target_route_http_admin_generic = route_set.add_route('/sample', :conditions => {:domain => 'admin.spec.com'})
36
- target_route_http_admin = route_set.add_route('/sample', :conditions => {:protocol => 'http', :domain => 'admin.spec.com'})
37
- target_route_http_www = route_set.add_route('/sample', :conditions => {:protocol => 'http', :domain => 'www.spec.com'})
38
- target_route_https_msie = route_set.add_route('/sample', :conditions => {:protocol => 'https', :domain => 'admin.spec.com', :user_agent => 'MSIE 6.0'})
39
- target_route_https_admin = route_set.add_route('/sample', :conditions => {:protocol => 'https', :domain => 'admin.spec.com'})
40
- route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_http_admin
41
- route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http', :domain => 'www.spec.com', :user_agent => nil})).path.route.should == target_route_http_www
42
- route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'https', :domain => 'admin.spec.com', :user_agent => 'MSIE 6.0'})).path.route.should == target_route_https_msie
43
- route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'https', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_https_admin
44
- route_set.recognize(build_request({:method => 'put', :path => '/sample', :protocol => 'wacky', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_http_admin_generic
33
+ target_route_http_admin_generic = @route_set.add_route('/sample', :conditions => {:domain => 'admin.spec.com'})
34
+ target_route_http_admin = @route_set.add_route('/sample', :conditions => {:protocol => 'http', :domain => 'admin.spec.com'})
35
+ target_route_http_www = @route_set.add_route('/sample', :conditions => {:protocol => 'http', :domain => 'www.spec.com'})
36
+ target_route_https_msie = @route_set.add_route('/sample', :conditions => {:protocol => 'https', :domain => 'admin.spec.com', :user_agent => 'MSIE 6.0'})
37
+ target_route_https_admin = @route_set.add_route('/sample', :conditions => {:protocol => 'https', :domain => 'admin.spec.com'})
38
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_http_admin
39
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http', :domain => 'www.spec.com', :user_agent => nil})).path.route.should == target_route_http_www
40
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'https', :domain => 'admin.spec.com', :user_agent => 'MSIE 6.0'})).path.route.should == target_route_https_msie
41
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'https', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_https_admin
42
+ @route_set.recognize(build_request({:method => 'put', :path => '/sample', :protocol => 'wacky', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_http_admin_generic
45
43
 
46
44
  end
47
45
 
48
46
  it "should correctly fix that tree if conditionals are used later" do
49
- noop_route = route_set.add_route('/noop', :controller => 'products', :action => 'noop')
50
- product_show_route = route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:method => 'get'})
51
- route_set.recognize(build_request({:method => 'get', :path => '/noop', :domain => 'admin.host.com'})).path.route.should == noop_route
52
- route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com'})).path.route.should == product_show_route
47
+ noop_route = @route_set.add_route('/noop', :controller => 'products', :action => 'noop')
48
+ product_show_route = @route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:method => 'get'})
49
+ @route_set.recognize(build_request({:method => 'get', :path => '/noop', :domain => 'admin.host.com'})).path.route.should == noop_route
50
+ @route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com'})).path.route.should == product_show_route
53
51
  end
54
52
 
55
53
  it "should use conditionals that are boolean" do
56
54
  # hijacking user_agent
57
- insecure_product_show_route = route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:user_agent => false, :method => 'get'})
58
- secure_product_show_route = route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:user_agent => true, :method => 'get'})
55
+ insecure_product_show_route = @route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:user_agent => false, :method => 'get'})
56
+ secure_product_show_route = @route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:user_agent => true, :method => 'get'})
59
57
 
60
- secure_product_show_route.should == route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com', :user_agent => true})).path.route
61
- insecure_product_show_route.should == route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com', :user_agent => false})).path.route
58
+ secure_product_show_route.should == @route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com', :user_agent => true})).path.route
59
+ insecure_product_show_route.should == @route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com', :user_agent => false})).path.route
62
60
  end
63
61
 
64
62
  it "should use conditionals that are arrays" do
65
63
  # hijacking user_agent
66
- www_product_show_route = route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:subdomains => ['www'], :method => 'get'})
67
- admin_product_show_route = route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:subdomains => ['admin'], :method => 'get'})
64
+ www_product_show_route = @route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:subdomains => ['www'], :method => 'get'})
65
+ admin_product_show_route = @route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:subdomains => ['admin'], :method => 'get'})
68
66
 
69
- admin_product_show_route.should == route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :subdomains => ['admin'], :user_agent => true})).path.route
70
- www_product_show_route.should == route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :subdomains => ['www'], :user_agent => false})).path.route
67
+ admin_product_show_route.should == @route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :subdomains => ['admin'], :user_agent => true})).path.route
68
+ www_product_show_route.should == @route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :subdomains => ['www'], :user_agent => false})).path.route
71
69
  end
72
70
  end
73
71
 
74
72
  it "should recognize a format-style variable" do
75
- target_route = route_set.add_route('/sample.:format', :controller => 'sample', :action => 'action')
76
- route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:format , 'html']], nil, "/sample.html")
73
+ target_route = @route_set.add_route('/sample.:format', :controller => 'sample', :action => 'action')
74
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:format , 'html']], nil, "/sample.html")
77
75
  end
78
76
 
79
77
  it "should recognize a glob-style variable" do
80
- target_route = route_set.add_route('/sample/*format', :controller => 'sample', :action => 'action')
81
- route_set.recognize(build_request({:method => 'get', :path => '/sample/html/json/apple'})).params.should == [[:format, ['html', 'json', 'apple']]]
78
+ target_route = @route_set.add_route('/sample/*format', :controller => 'sample', :action => 'action')
79
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample/html/json/apple'})).params.should == [[:format, ['html', 'json', 'apple']]]
82
80
  end
83
81
 
84
82
  it "should recgonize only a glob-style variable" do
85
- target_route = route_set.add_route('/*format')
86
- response = route_set.recognize(build_request({:method => 'get', :path => '/sample/html/json/apple'}))
83
+ target_route = @route_set.add_route('/*format')
84
+ response = @route_set.recognize(build_request({:method => 'get', :path => '/sample/html/json/apple'}))
87
85
  response.params.should == [[:format, ['sample', 'html', 'json', 'apple']]]
88
86
  response.path.route.should == target_route
89
87
  end
90
88
 
91
89
  it "should recgonize a regex static part" do
92
- target_route = route_set.add_route('/test/part/{one|two}')
93
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one'})).path.route.should == target_route
94
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/two'})).path.route.should == target_route
95
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/three'})).should == nil
90
+ target_route = @route_set.add_route('/test/part/{one|two}')
91
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one'})).path.route.should == target_route
92
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/two'})).path.route.should == target_route
93
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/three'})).should == nil
96
94
  end
97
95
 
98
96
  it "shouldn't accept a nil variable" do
99
- target_route = route_set.add_route('/:one')
100
- route_set.recognize(build_request({:method => 'get', :path => '/one'})).path.route.should == target_route
101
- route_set.recognize(build_request({:method => 'get', :path => '/'})).should == nil
97
+ target_route = @route_set.add_route('/:one')
98
+ @route_set.recognize(build_request({:method => 'get', :path => '/one'})).path.route.should == target_route
99
+ @route_set.recognize(build_request({:method => 'get', :path => '/'})).should == nil
102
100
  end
103
101
 
104
102
  it "should recgonize a regex static part containing {}'s" do
105
- target_route = route_set.add_route('/test/part/{^o{2,3}$}')
106
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/oo'})).path.route.should == target_route
107
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/ooo'})).path.route.should == target_route
108
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/oooo'})).should == nil
103
+ target_route = @route_set.add_route('/test/part/{^o{2,3}$}')
104
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/oo'})).path.route.should == target_route
105
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/ooo'})).path.route.should == target_route
106
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/oooo'})).should == nil
109
107
  end
110
108
 
111
109
  it "should recgonize a regex single variable" do
112
- target_route = route_set.add_route('/test/part/{:test,hello|again}')
113
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello'})).path.route.should == target_route
114
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello'})).params.should == [[:test, 'hello']]
115
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/again'})).path.route.should == target_route
116
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/again'})).params.should == [[:test, 'again']]
117
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/world'})).should == nil
110
+ target_route = @route_set.add_route('/test/part/{:test,hello|again}')
111
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello'})).path.route.should == target_route
112
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello'})).params.should == [[:test, 'hello']]
113
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/again'})).path.route.should == target_route
114
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/again'})).params.should == [[:test, 'again']]
115
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/world'})).should == nil
118
116
  end
119
117
 
120
118
  it "should recgonize a regex glob variable" do
121
- target_route = route_set.add_route('/test/part/{*test,^(hello|again|\d+)$}')
122
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again'})).path.route.should == target_route
123
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again'})).params.should == [[:test, ['hello', 'again', '123', 'hello', 'again']]]
124
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/agaim/123/hello/again'})).should == nil
119
+ target_route = @route_set.add_route('/test/part/{*test,^(hello|again|\d+)$}')
120
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again'})).path.route.should == target_route
121
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again'})).params.should == [[:test, ['hello', 'again', '123', 'hello', 'again']]]
122
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/agaim/123/hello/again'})).should == nil
125
123
  end
126
124
 
127
125
  it "should recgonize a regex glob variable terminated by a static part" do
128
- target_route = route_set.add_route('/test/part/{*test,^(hello|again|\d+)$}/onemore')
129
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).path.route.should == target_route
130
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).params.should == [[:test, ['hello', 'again', '123', 'hello', 'again']]]
126
+ target_route = @route_set.add_route('/test/part/{*test,^(hello|again|\d+)$}/onemore')
127
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).path.route.should == target_route
128
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).params.should == [[:test, ['hello', 'again', '123', 'hello', 'again']]]
131
129
  end
132
130
 
133
131
  it "should recgonize a regex glob variable terminated by a single regex variable" do
134
- target_route = route_set.add_route('/test/part/{*test,^(hello|again|\d+)$}/{:party,onemore}')
135
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).path.route.should == target_route
136
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).params.should == [[:test, ['hello', 'again', '123', 'hello', 'again']], [:party, 'onemore']]
132
+ target_route = @route_set.add_route('/test/part/{*test,^(hello|again|\d+)$}/{:party,onemore}')
133
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).path.route.should == target_route
134
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/hello/again/123/hello/again/onemore'})).params.should == [[:test, ['hello', 'again', '123', 'hello', 'again']], [:party, 'onemore']]
137
135
  end
138
136
 
139
137
  it "should recgonize a greedy regex single variable" do
140
- target_route = route_set.add_route('/test/part/{!test,one/more/time}')
141
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time'})).path.route.should == target_route
142
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time'})).params.should == [[:test, 'one/more/time']]
138
+ target_route = @route_set.add_route('/test/part/{!test,one/more/time}')
139
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time'})).path.route.should == target_route
140
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time'})).params.should == [[:test, 'one/more/time']]
143
141
  end
144
142
 
145
143
  it "should recgonize a greedy regex that matches across / and not" do
146
- target_route = route_set.add_route('/test/part/{!test,one/more|one}')
147
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more'})).path.route.should == target_route
148
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more'})).params.should == [[:test, 'one/more']]
149
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one'})).path.route.should == target_route
150
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one'})).params.should == [[:test, 'one']]
144
+ target_route = @route_set.add_route('/test/part/{!test,one/more|one}')
145
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more'})).path.route.should == target_route
146
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more'})).params.should == [[:test, 'one/more']]
147
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one'})).path.route.should == target_route
148
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one'})).params.should == [[:test, 'one']]
151
149
  end
152
150
 
153
151
  it "should recgonize a greedy regex single variable with static parts after" do
154
- target_route = route_set.add_route('/test/part/{!test,one/more/time}/help')
155
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time/help'})).path.route.should == target_route
156
- route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time/help'})).params.should == [[:test, 'one/more/time']]
152
+ target_route = @route_set.add_route('/test/part/{!test,one/more/time}/help')
153
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time/help'})).path.route.should == target_route
154
+ @route_set.recognize(build_request({:method => 'get', :path => '/test/part/one/more/time/help'})).params.should == [[:test, 'one/more/time']]
157
155
  end
158
156
 
159
157
  it "should recgonize two glob-style variables separated by a static part" do
160
- target_route = route_set.add_route('/*format/innovate/*onemore')
161
- response = route_set.recognize(build_request({:method => 'get', :path => '/sample/html/innovate/apple'}))
158
+ target_route = @route_set.add_route('/*format/innovate/*onemore')
159
+ response = @route_set.recognize(build_request({:method => 'get', :path => '/sample/html/innovate/apple'}))
162
160
  response.params.should == [[:format, ['sample', 'html']], [:onemore, ['apple']]]
163
161
  response.path.route.should == target_route
164
162
  end
165
163
 
166
164
  it "should recgonize only a glob-style variable with a condition" do
167
- target_route = route_set.add_route('/*format', :conditions => {:domain => 'test-domain'})
168
- response = route_set.recognize(build_request({:method => 'get', :path => '/sample/html/json/apple', :domain => 'test-domain'}))
165
+ target_route = @route_set.add_route('/*format', :conditions => {:domain => 'test-domain'})
166
+ response = @route_set.recognize(build_request({:method => 'get', :path => '/sample/html/json/apple', :domain => 'test-domain'}))
169
167
  response.params.should == [[:format, ['sample', 'html', 'json', 'apple']]]
170
168
  response.path.route.should == target_route
171
169
  end
172
170
 
173
171
  it "should recognize a format-style literal" do
174
- target_route = route_set.add_route('/:action.html', :controller => 'sample', :action => 'action')
175
- route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:action , 'sample']], nil, "/sample.html")
172
+ target_route = @route_set.add_route('/:action.html', :controller => 'sample', :action => 'action')
173
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:action , 'sample']], nil, "/sample.html")
176
174
  end
177
175
 
178
176
  it "should recognize a format-style variable along side another variable" do
179
- target_route = route_set.add_route('/:action.:format', :controller => 'sample', :action => 'action')
180
- route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:action , 'sample'], [:format, 'html']], nil, '/sample.html')
177
+ target_route = @route_set.add_route('/:action.:format', :controller => 'sample', :action => 'action')
178
+ @route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:action , 'sample'], [:format, 'html']], nil, '/sample.html')
181
179
  end
182
180
 
183
181
  it "should use a requirement (proc) on incoming variables" do
184
- route_set.add_route('/:controller/:action/:id', :id => proc{|v| Integer(v)})
185
- proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com'}))}.should_not raise_error(Usher::ValidationException)
186
- proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/123asd', :domain => 'admin.host.com'}))}.should raise_error(Usher::ValidationException)
182
+ @route_set.add_route('/:controller/:action/:id', :id => proc{|v| Integer(v)})
183
+ proc {@route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com'}))}.should_not raise_error(Usher::ValidationException)
184
+ proc {@route_set.recognize(build_request({:method => 'get', :path => '/products/show/123asd', :domain => 'admin.host.com'}))}.should raise_error(Usher::ValidationException)
187
185
  end
188
186
 
189
187
  it "shouldn't care about mildly weird characters in the URL" do
190
- route = route_set.add_route('/!asd,qwe/hjk$qwe/:id')
191
- route_set.recognize(build_request({:method => 'get', :path => '/!asd,qwe/hjk$qwe/09AZaz$-_+!*\'', :domain => 'admin.host.com'})).params.rassoc('09AZaz$-_+!*\'').first.should == :id
188
+ route = @route_set.add_route('/!asd,qwe/hjk$qwe/:id')
189
+ @route_set.recognize(build_request({:method => 'get', :path => '/!asd,qwe/hjk$qwe/09AZaz$-_+!*\'', :domain => 'admin.host.com'})).params.rassoc('09AZaz$-_+!*\'').first.should == :id
192
190
  end
193
191
 
194
192
  it "shouldn't care about non-primary delimiters in the path" do
195
- route = route_set.add_route('/testing/:id/testing2/:id2/:id3')
196
- route_set.recognize(build_request({:method => 'get', :path => '/testing/asd.qwe/testing2/poi.zxc/oiu.asd'})).params.should == [[:id, 'asd.qwe'], [:id2, 'poi.zxc'], [:id3, 'oiu.asd']]
193
+ route = @route_set.add_route('/testing/:id/testing2/:id2/:id3')
194
+ @route_set.recognize(build_request({:method => 'get', :path => '/testing/asd.qwe/testing2/poi.zxc/oiu.asd'})).params.should == [[:id, 'asd.qwe'], [:id2, 'poi.zxc'], [:id3, 'oiu.asd']]
195
+ end
196
+
197
+ it "should pick the path when there are mutliple conflicting delimiters" do
198
+ @route_set.add_route('/:id1(.:format)')
199
+ @route_set.add_route('/:id1/one(.:format)')
200
+ @route_set.add_route('/:id1/one/:id2(.:format)')
201
+
202
+ @route_set.recognize(build_request({:path => '/id1'})).params.should == [[:id1, 'id1']]
203
+ @route_set.recognize(build_request({:path => '/id1.html'})).params.should == [[:id1, 'id1'], [:format, 'html']]
204
+ @route_set.recognize(build_request({:path => '/id1/one'})).params.should == [[:id1, 'id1']]
205
+ @route_set.recognize(build_request({:path => '/id1/one.html'})).params.should == [[:id1, 'id1'], [:format, 'html']]
206
+ @route_set.recognize(build_request({:path => '/id1/one/id2'})).params.should == [[:id1, 'id1'], [:id2, 'id2']]
207
+ @route_set.recognize(build_request({:path => '/id1/one/id2.html'})).params.should == [[:id1, 'id1'], [:id2, 'id2'], [:format, 'html']]
197
208
  end
198
209
 
199
210
  it "should recognize a path with an optional compontnet" do
200
- route_set.add_route("/:name(/:surname)", :conditions => {:method => 'get'})
201
- result = route_set.recognize(build_request({:method => 'get', :path => '/homer'}))
211
+ @route_set.add_route("/:name(/:surname)", :conditions => {:method => 'get'})
212
+ result = @route_set.recognize(build_request({:method => 'get', :path => '/homer'}))
202
213
  result.params.should == [[:name, "homer"]]
203
- result = route_set.recognize(build_request({:method => 'get', :path => "/homer/simpson"}))
214
+ result = @route_set.recognize(build_request({:method => 'get', :path => "/homer/simpson"}))
204
215
  result.params.should == [[:name, "homer"],[:surname, "simpson"]]
205
216
  end
206
217
 
207
218
  it "should should raise if malformed variables are used" do
208
- route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:method => 'get'})
209
- proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/qweasd', :domain => 'admin.host.com'}))}.should raise_error
219
+ @route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:method => 'get'})
220
+ proc {@route_set.recognize(build_request({:method => 'get', :path => '/products/show/qweasd', :domain => 'admin.host.com'}))}.should raise_error
210
221
  end
211
222
 
212
223
  it "should recognize multiple optional parts" do
213
- target_route = route_set.add_route('/test(/this)(/too)')
214
- route_set.recognize_path('/test').path.route.should == target_route
215
- route_set.recognize_path('/test/this').path.route.should == target_route
216
- route_set.recognize_path('/test/too').path.route.should == target_route
217
- route_set.recognize_path('/test/this/too').path.route.should == target_route
224
+ target_route = @route_set.add_route('/test(/this)(/too)')
225
+ @route_set.recognize_path('/test').path.route.should == target_route
226
+ @route_set.recognize_path('/test/this').path.route.should == target_route
227
+ @route_set.recognize_path('/test/too').path.route.should == target_route
228
+ @route_set.recognize_path('/test/this/too').path.route.should == target_route
218
229
  end
219
230
 
231
+ it "should match between two routes where one is more specific from request conditions" do
232
+ route_with_post = @route_set.add_route("/foo", :conditions => {:method => 'post'})
233
+ route = @route_set.add_route("/foo")
234
+
235
+ @route_set.recognize(build_request({:method => 'post', :path => '/foo'})).path.route.should == route_with_post
236
+ @route_set.recognize(build_request({:method => 'get', :path => '/foo'})).path.route.should == route
237
+ end
238
+
220
239
  describe "partial recognition" do
221
240
  it "should partially match a route" do
222
- route = route_set.add_route("/foo")
241
+ route = @route_set.add_route("/foo")
223
242
  route.match_partially!
224
- route_set.recognize(build_request(:method => "get", :path => "/foo/bar")).should == Usher::Node::Response.new(route.paths.first, [], "/bar", '/foo')
243
+ @route_set.recognize(build_request(:method => "get", :path => "/foo/bar")).should == Usher::Node::Response.new(route.paths.first, [], "/bar", '/foo')
225
244
  end
226
245
 
227
246
  it "should partially match a route and use request conditions" do
228
- route = route_set.add_route("/foo", :conditions => {:method => 'get'})
247
+ route = @route_set.add_route("/foo", :conditions => {:method => 'get'})
229
248
  route.match_partially!
230
249
 
231
- route_set.recognize(build_request({:method => 'get', :path => '/foo/bar'})).path.route.should == route
232
- route_set.recognize(build_request({:method => 'post', :path => '/foo/bar'})).should.nil?
250
+ @route_set.recognize(build_request({:method => 'get', :path => '/foo/bar'})).path.route.should == route
251
+ @route_set.recognize(build_request({:method => 'post', :path => '/foo/bar'})).should.nil?
233
252
  end
234
253
 
235
254
  it "should not match partially when a route is not set as partially matched" do
236
- route = route_set.add_route("/foo", :foo => :bar)
237
- route_set.recognize(build_request(:path => "/foo")).path.route.should == route
238
- route_set.recognize(build_request(:path => "/foo/bar")).should be_nil
255
+ route = @route_set.add_route("/foo", :foo => :bar)
256
+ @route_set.recognize(build_request(:path => "/foo")).path.route.should == route
257
+ @route_set.recognize(build_request(:path => "/foo/bar")).should be_nil
239
258
  end
240
259
 
241
260
 
@@ -243,28 +262,28 @@ describe "Usher route recognition" do
243
262
 
244
263
  describe "dup safety" do
245
264
  before do
246
- route_set.add_route("/foo", :foo => "foo")
247
- @r2 = route_set.dup
265
+ @route_set.add_route("/foo", :foo => "foo")
266
+ @r2 = @route_set.dup
248
267
  end
249
268
 
250
269
  it "should provide a different object" do
251
- route_set.should_not eql(@r2)
270
+ @route_set.should_not eql(@r2)
252
271
  end
253
272
 
254
273
  it "should recognize the originals routes in the dup" do
255
- route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
274
+ @route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
256
275
  @r2.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
257
276
  end
258
277
 
259
278
  it "should not add routes added to the dup to the original" do
260
279
  @r2.add_route("/bar", :bar => "bar")
261
280
  @r2.recognize( build_request(:path => "/bar")).path.route.destination.should == {:bar => "bar"}
262
- route_set.recognize( build_request(:path => "/bar")).should == nil
281
+ @route_set.recognize( build_request(:path => "/bar")).should == nil
263
282
  end
264
283
 
265
284
  it "should not delete routes added to the dup to the original" do
266
285
  @r2.delete_route("/foo")
267
- route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo => "foo"}
286
+ @route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo => "foo"}
268
287
  @r2.recognize( build_request(:path => "/foo")).should == nil
269
288
  end
270
289
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-25 00:00:00 -04:00
12
+ date: 2009-10-09 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - lib/usher/splitter.rb
60
60
  - lib/usher/util.rb
61
61
  - lib/usher/util/generate.rb
62
+ - lib/usher/util/graph.rb
62
63
  - lib/usher/util/parser.rb
63
64
  - lib/usher/util/rack-mixins.rb
64
65
  - rails/init.rb