joshbuddy-usher 0.5.4 → 0.5.6

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.
@@ -2,37 +2,37 @@ require 'lib/usher'
2
2
  require 'rack'
3
3
 
4
4
  describe "Usher URL generation" do
5
-
5
+
6
6
  before(:each) do
7
7
  @route_set = Usher.new(:generator => Usher::Util::Generators::URL.new)
8
8
  @route_set.reset!
9
9
  end
10
-
10
+
11
11
  it "should generate a simple URL" do
12
12
  @route_set.add_named_route(:sample, '/sample', :controller => 'sample', :action => 'action')
13
13
  @route_set.generator.generate(:sample, {}).should == '/sample'
14
14
  end
15
-
15
+
16
16
  it "should generate a simple URL with a single variable" do
17
17
  @route_set.add_named_route(:sample, '/sample/:action', :controller => 'sample')
18
18
  @route_set.generator.generate(:sample, {:action => 'action'}).should == '/sample/action'
19
19
  end
20
-
20
+
21
21
  it "should generate a simple URL with a single variable (and escape)" do
22
22
  @route_set.add_named_route(:sample, '/sample/:action', :controller => 'sample')
23
23
  @route_set.generator.generate(:sample, {:action => 'action time'}).should == '/sample/action%20time'
24
24
  end
25
-
25
+
26
26
  it "should generate a simple URL with a single variable (thats not a string)" do
27
27
  @route_set.add_named_route(:sample, '/sample/:action/:id', :controller => 'sample')
28
28
  @route_set.generator.generate(:sample, {:action => 'action', :id => 123}).should == '/sample/action/123'
29
29
  end
30
-
30
+
31
31
  it "should generate a simple URL with a glob variable" do
32
32
  @route_set.add_named_route(:sample, '/sample/*action', :controller => 'sample')
33
33
  @route_set.generator.generate(:sample, {:action => ['foo', 'baz']}).should == '/sample/foo/baz'
34
34
  end
35
-
35
+
36
36
  it "should generate a mutliple vairable URL from a hash" do
37
37
  @route_set.add_named_route(:sample, '/sample/:first/:second', :controller => 'sample')
38
38
  @route_set.generator.generate(:sample, {:first => 'zoo', :second => 'maz'}).should == '/sample/zoo/maz'
@@ -125,7 +125,7 @@ describe "Usher URL generation" do
125
125
  @route_set.add_named_route(:name, '/:one/:two/:three', {:default_values => {:one => 'one', :two => 'two', :three => 'three'}})
126
126
  @route_set.generator.generate(:name).should == '/one/two/three'
127
127
  end
128
-
128
+
129
129
  it "should generate a route using defaults and optionals using the last parameter" do
130
130
  @route_set.add_named_route(:opts_with_defaults, '/:one(/:two(/:three))', {:default_values => {:one => '1', :two => '2', :three => '3'}})
131
131
  @route_set.generator.generate(:opts_with_defaults, {:three => 'three'}).should == '/1/2/three'
@@ -135,61 +135,114 @@ describe "Usher URL generation" do
135
135
  @route_set.add_named_route(:optionals, '/:controller(/:action(/:id))(.:format)')
136
136
  @route_set.generator.generate(:optionals, {:controller => "foo", :action => "bar"}).should == '/foo/bar'
137
137
  end
138
-
138
+
139
139
  describe "nested generation" do
140
140
  before do
141
141
  @route_set2 = Usher.new(:generator => Usher::Util::Generators::URL.new)
142
142
  @route_set3 = Usher.new(:generator => Usher::Util::Generators::URL.new)
143
143
  @route_set4 = Usher.new(:generator => Usher::Util::Generators::URL.new)
144
-
144
+
145
145
  @route_set.add_named_route(:simple, "/mount_point").match_partially!.to(@route_set2)
146
146
  @route_set.add_route("/third/:foo", :default_values => {:foo => "foo"}).match_partially!.to(@route_set3)
147
147
  @route_set.add_route("/fourth/:bar").match_partially!.to(@route_set4)
148
-
148
+
149
149
  @route_set2.add_named_route(:nested_simple, "/nested/simple", :controller => "nested", :action => "simple")
150
150
  @route_set2.add_named_route(:nested_complex, "/another_nested(/:complex)", :controller => "nested", :action => "complex")
151
-
151
+
152
152
  @route_set3.add_named_route(:nested_simple, "/nested/simple", :controller => "nested", :action => "simple")
153
153
  @route_set3.add_named_route(:nested_complex, "/another_nested(/:complex)", :controller => "nested", :action => "complex")
154
-
154
+
155
155
  @route_set4.add_named_route(:nested_simple, "/nested/simple", :controller => "nested", :action => "simple")
156
156
  end
157
-
157
+
158
158
  it "should generate a route for the simple nested route" do
159
159
  @route_set2.generator.generate(:nested_simple).should == "/mount_point/nested/simple"
160
160
  end
161
-
161
+
162
162
  it "should generate a simple route without optional segments" do
163
163
  @route_set2.generator.generate(:nested_complex).should == "/mount_point/another_nested"
164
164
  end
165
-
165
+
166
166
  it "should generate a route with optional segements" do
167
167
  @route_set2.generator.generate(:nested_complex, :complex => "foo").should == "/mount_point/another_nested/foo"
168
168
  end
169
-
169
+
170
170
  it "should genearte a route with the specified value for the parent route" do
171
171
  @route_set3.generator.generate(:nested_simple, :foo => "bar").should == "/third/bar/nested/simple"
172
172
  end
173
-
173
+
174
174
  it "should generate a route with the default value from the parent route" do
175
175
  @route_set3.generator.generate(:nested_simple).should == "/third/foo/nested/simple"
176
176
  end
177
-
177
+
178
178
  it "should generate a route with an optional segement in the parent and child" do
179
179
  @route_set3.generator.generate(:nested_complex, :complex => "complex").should == "/third/foo/another_nested/complex"
180
180
  end
181
-
181
+
182
182
  it "should generate a route without the optional value from the child" do
183
183
  @route_set3.generator.generate(:nested_complex).should == "/third/foo/another_nested"
184
184
  end
185
-
185
+
186
186
  it "should raise an exception when trying to generate a route where the parent variable is not defined and does not have a default value" do
187
187
  lambda do
188
188
  @route_set4.generator.generate(:nested_simple)
189
189
  end.should raise_error(Usher::MissingParameterException)
190
190
  end
191
-
192
-
193
-
194
191
  end
195
- end
192
+
193
+ describe "dupped generation" do
194
+ before(:each) do
195
+ @r1 = Usher.new(:generator => Usher::Util::Generators::URL.new)
196
+ @r2 = Usher.new(:generator => Usher::Util::Generators::URL.new)
197
+ @r3 = Usher.new(:generator => Usher::Util::Generators::URL.new)
198
+
199
+ @r1.add_route("/r1", :router => "r1").name(:route)
200
+ @r2.add_route("/r2", :router => "r2").name(:route)
201
+ @r3.add_route("/r3", :router => "r3").name(:route)
202
+ end
203
+
204
+ it "should generate dupped routes" do
205
+ @r1.generator.generate(:route).should == "/r1"
206
+ r1 = @r1.dup
207
+ r1.generator.generate(:route).should == "/r1"
208
+ end
209
+
210
+ it "should not generate new routes added to a dup on the original" do
211
+ r1 = @r1.dup
212
+ r1.add_route("/new_r1", :router => "r4").name(:new_route)
213
+ lambda do
214
+ @r1.generator.generate(:new_route).should be_nil
215
+ end
216
+ end
217
+
218
+ it "should generate new routes added to a dup" do
219
+ r1 = @r1.dup
220
+ r1.add_route("/new_r1", :router => "r4").name(:new_route)
221
+ r1.generator.generate(:new_route).should == "/new_r1"
222
+ end
223
+
224
+ it "should generate a route for a nested usher" do
225
+ @r1.add_route("/mounted").match_partially!.to(@r2)
226
+ @r2.generator.generate(:route).should == "/mounted/r2"
227
+ end
228
+
229
+ it "should generate a route for a dupped nested usher" do
230
+ r3 = @r3.dup
231
+ @r1.add_route("/mounted").match_partially!.to(r3)
232
+ r3.generator.generate(:route).should == "/mounted/r3"
233
+ end
234
+
235
+ it "should generate a route for 2 differently mounted dupped ushers" do
236
+ r21 = @r2.dup
237
+ r22 = @r2.dup
238
+
239
+ @r1.add_route("/mounted").match_partially!.to(r21)
240
+ @r1.add_route("/other_mount").match_partially!.to(r22)
241
+
242
+ r21.generator.generate(:route).should == "/mounted/r2"
243
+ r22.generator.generate(:route).should == "/other_mount/r2"
244
+ @r2.generator.generate(:route).should == "/r2"
245
+ end
246
+ end
247
+
248
+ end
@@ -55,90 +55,96 @@ describe "Usher (for rack) route dispatching" do
55
55
  response = route_set.call_with_mock_request("/not-existing-url")
56
56
  response.status.should eql(404)
57
57
  end
58
-
58
+
59
59
  describe "mounted rack instances" do
60
60
  before do
61
61
  @bad_app = mock("bad_app")
62
-
62
+
63
63
  @usher2 = Usher::Interface.for(:rack)
64
64
  @usher2.add("/good" ).to(@app)
65
65
  @usher2.add("/bad" ).match_partially!.to(@bad_app)
66
66
  @usher2.add("/some(/:foo)").to(@app)
67
-
67
+
68
68
  route_set.add("/foo/:bar", :default_values => {:foo => "foo"}).match_partially!.to(@usher2)
69
69
  route_set.add("/foo", :default_values => {:controller => :foo}).to(@app)
70
70
  end
71
-
71
+
72
72
  it "should match the route without nesting" do
73
73
  @app.should_receive(:call).once.with{ |e| e['usher.params'].should == {:controller => :foo}}
74
74
  route_set.call(Rack::MockRequest.env_for("/foo"))
75
75
  end
76
-
76
+
77
77
  it "should route through the first route, and the second to the app" do
78
78
  @app.should_receive(:call).once.with{|e| e['usher.params'].should == {:bar => "bar", :foo => "foo"}}
79
79
  result = route_set.call(Rack::MockRequest.env_for("/foo/bar/good"))
80
80
  end
81
-
81
+
82
82
  it "should go through to the bad app" do
83
83
  @bad_app.should_receive(:call).once.with{|e| e['usher.params'].should == {:bar => "some_bar", :foo => "foo"}}
84
84
  result = route_set.call(Rack::MockRequest.env_for("/foo/some_bar/bad"))
85
85
  end
86
-
86
+
87
87
  it "should match optional routes paramters" do
88
88
  @app.should_receive(:call).once.with{|e| e['usher.params'].should == {:bar => "bar", :foo => "a_different_foo"}}
89
89
  route_set.call(Rack::MockRequest.env_for("/foo/bar/some/a_different_foo"))
90
90
  end
91
-
91
+
92
92
  describe "SCRIPT_NAME & PATH_INFO" do
93
- it "should update the script name for a fully consumed route" do
93
+ it "shouldn't update the script name for a fully consumed route" do
94
94
  @app.should_receive(:call).once.with do |e|
95
- e['SCRIPT_NAME'].should == "/foo"
96
- e['PATH_INFO'].should == ""
95
+ e['SCRIPT_NAME'].should == ""
96
+ e['PATH_INFO'].should == "/foo"
97
97
  end
98
98
  route_set.call(Rack::MockRequest.env_for("/foo"))
99
99
  end
100
-
100
+
101
101
  it "should update the script name and path info for a partially consumed route" do
102
102
  @app.should_receive(:call).once.with do |e|
103
103
  e['SCRIPT_NAME'].should == "/partial"
104
104
  e['PATH_INFO'].should == "/bar/baz"
105
105
  end
106
-
106
+
107
107
  route_set.add("/partial").match_partially!.to(@app)
108
108
  route_set.call(Rack::MockRequest.env_for("/partial/bar/baz"))
109
109
  end
110
-
110
+
111
111
  it "should consume the path through a mounted usher" do
112
112
  @bad_app.should_receive(:call).once.with do |e|
113
113
  e['SCRIPT_NAME'].should == "/foo/bar/bad"
114
114
  e['PATH_INFO'].should == "/leftovers"
115
115
  end
116
-
116
+
117
117
  route_set.call(Rack::MockRequest.env_for("/foo/bar/bad/leftovers"))
118
118
  end
119
-
119
+
120
+ it "should not modify SCRIPT_NAME in place since thin freezes it" do
121
+ @app.should_receive(:call).once
122
+ env = Rack::MockRequest.env_for("/foo/bar/good")
123
+ env["SCRIPT_NAME"] = "".freeze
124
+ route_set.call(env)
125
+ end
120
126
  end
121
-
127
+
122
128
  describe "dupping" do
123
- before do
129
+ before do
124
130
  @app = mock("app")
125
131
  @u1 = Usher::Interface.for(:rack)
126
132
  @u2 = Usher::Interface.for(:rack)
127
-
133
+
128
134
  @u1.add("/one", :default_values => {:one => :one}).to(@app)
129
135
  @u1.add("/mount").match_partially!.to(@u2)
130
-
136
+
131
137
  @u2.add("/app", :default_values => {:foo => :bar}).to(@app)
132
-
138
+
133
139
  end
134
-
140
+
135
141
  it "should allow me to dup the router" do
136
142
  @app.should_receive(:call).twice.with{|e| e['usher.params'].should == {:one => :one}}
137
143
  @u1.call(Rack::MockRequest.env_for("/one"))
138
144
  u1_dash = @u1.dup
139
145
  u1_dash.call(Rack::MockRequest.env_for("/one"))
140
146
  end
141
-
147
+
142
148
  it "should allow me to dup the router and add a new route without polluting the original" do
143
149
  @app.should_receive(:call).with{|e| e['usher.params'].should == {:foo => :bar}}
144
150
  u1_dash = @u1.dup
@@ -147,27 +153,53 @@ describe "Usher (for rack) route dispatching" do
147
153
  @app.should_not_receive(:call)
148
154
  @u1.call(Rack::MockRequest.env_for("/foo"))
149
155
  end
150
-
156
+
151
157
  it "should allow me to dup the router and nested routers should remain intact" do
152
158
  @app.should_receive(:call).with{|e| e['usher.params'].should == {:foo => :bar}}
153
159
  u1_dash = @u1.dup
154
160
  u1_dash.call(Rack::MockRequest.env_for("/mount/app"))
155
161
  end
156
-
162
+
157
163
  it "should allow me to dup the router and add more routes" do
158
164
  @app.should_receive(:call).with{|e| e['usher.params'].should == {:another => :bar}}
159
-
165
+
160
166
  u3 = Usher::Interface.for(:rack)
161
167
  u1_dash = @u1.dup
162
-
168
+
163
169
  u3.add("/another_bar", :default_values => {:another => :bar}).to(@app)
164
170
  u1_dash.add("/some/mount").match_partially!.to(u3)
165
-
171
+
166
172
  u1_dash.call(Rack::MockRequest.env_for("/some/mount/another_bar"))
167
-
173
+
168
174
  @app.should_not_receive(:call)
169
175
  @u1.call(Rack::MockRequest.env_for("/some/mount/another_bar"))
170
176
  end
171
177
  end
172
178
  end
179
+
180
+ describe "use as middlware" do
181
+ it "should allow me to set a default application to use" do
182
+ @app.should_receive(:call).with{|e| e['usher.params'].should == {:middle => :ware}}
183
+
184
+ u = Usher::Interface::RackInterface.new(@app)
185
+ u.add("/foo", :default_values => {:middle => :ware}).name(:foo)
186
+
187
+ u.call(Rack::MockRequest.env_for("/foo"))
188
+ end
189
+
190
+ it "should use the default application when no routes match" do
191
+ env = Rack::MockRequest.env_for("/not_a_route")
192
+ @app.should_receive(:call).with(env)
193
+ u = Usher::Interface::RackInterface.new(@app)
194
+ u.call(env)
195
+ end
196
+
197
+ it "should allow me to set the application after initialization" do
198
+ @app.should_receive(:call).with{|e| e['usher.params'].should == {:after => :stuff}}
199
+ u = Usher::Interface.for(:rack)
200
+ u.app = @app
201
+ u.add("/foo", :default_values => {:after => :stuff})
202
+ u.call(Rack::MockRequest.env_for("/foo"))
203
+ end
204
+ end
173
205
  end
@@ -0,0 +1,41 @@
1
+ require 'lib/usher'
2
+ require 'rack'
3
+
4
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
5
+ route_set = Usher::Interface.for(:rack)
6
+ route_set.extend(CallWithMockRequestMixin)
7
+
8
+ describe "Usher (for rack) route generation" do
9
+ before(:each) do
10
+ route_set.reset!
11
+ @app = MockApp.new("Hello World!")
12
+ route_set.add("/fixed").name(:fixed)
13
+ route_set.add("/simple/:simple_var")
14
+ route_set.add("/named/simple/:named_simple_var").name(:simple)
15
+ route_set.add("/optional(/:optional_var)")
16
+ route_set.add("/named/optional(/:named_optional_var)").name(:optional)
17
+ end
18
+
19
+ describe "named routes" do
20
+ it "should generate a fixed path" do
21
+ route_set.generate(:fixed).should == "/fixed"
22
+ end
23
+
24
+ it "should generate a basic path route" do
25
+ route_set.generate(nil, :simple_var => "simple_var").should == "/simple/simple_var"
26
+ end
27
+
28
+ it "should generate a named path route" do
29
+ route_set.generate(:simple, :named_simple_var => "the_var").should == "/named/simple/the_var"
30
+ end
31
+
32
+ it "should generate a route with options" do
33
+ route_set.generate(nil, :optional_var => "var").should == "/optional/var"
34
+ end
35
+
36
+ it "should generate a named route with options" do
37
+ route_set.generate(:optional).should == "/named/optional"
38
+ route_set.generate(:optional, :named_optional_var => "the_var").should == "/named/optional/the_var"
39
+ end
40
+ end
41
+ end
@@ -195,6 +195,14 @@ describe "Usher route recognition" do
195
195
  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']]
196
196
  end
197
197
 
198
+ it "should recognize a path with an optional compontnet" do
199
+ route_set.add_route("/:name(/:surname)", :conditions => {:method => 'get'})
200
+ result = route_set.recognize(build_request({:method => 'get', :path => '/homer'}))
201
+ result.params.should == [[:name, "homer"]]
202
+ result = route_set.recognize(build_request({:method => 'get', :path => "/homer/simpson"}))
203
+ result.params.should == [[:name, "homer"],[:surname, "simpson"]]
204
+ end
205
+
198
206
  it "should should raise if malformed variables are used" do
199
207
  route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:method => 'get'})
200
208
  proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/qweasd', :domain => 'admin.host.com'}))}.should raise_error
@@ -215,6 +223,13 @@ describe "Usher route recognition" do
215
223
  route_set.recognize(build_request({:method => 'post', :path => '/foo/bar'})).should.nil?
216
224
  end
217
225
 
226
+ it "should not match partially when a route is not set as partially matched" do
227
+ route = route_set.add_route("/foo", :foo => :bar)
228
+ route_set.recognize(build_request(:path => "/foo")).path.route.should == route
229
+ route_set.recognize(build_request(:path => "/foo/bar")).should be_nil
230
+ end
231
+
232
+
218
233
  end
219
234
 
220
235
  describe "dup safety" do
@@ -228,8 +243,8 @@ describe "Usher route recognition" do
228
243
  end
229
244
 
230
245
  it "should recognize the originals routes in the dup" do
231
- route_set.recognize(build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
232
- @r2.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
246
+ route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
247
+ @r2.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"}
233
248
  end
234
249
 
235
250
  it "should not add routes added to the dup to the original" do
@@ -240,8 +255,8 @@ describe "Usher route recognition" do
240
255
 
241
256
  it "should not delete routes added to the dup to the original" do
242
257
  @r2.delete_route("/foo")
243
- route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo => "foo"}
244
- @r2.recognize( build_request(:path => "/foo")).should == nil
258
+ route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo => "foo"}
259
+ @r2.recognize( build_request(:path => "/foo")).should == nil
245
260
  end
246
261
 
247
262
 
@@ -263,4 +278,4 @@ describe "Usher route recognition" do
263
278
  end
264
279
 
265
280
  end
266
- end
281
+ end