http_router 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +6 -5
- data/benchmarks/rack_mount.rb +16 -45
- data/benchmarks/rec2.rb +8 -8
- data/http_router.gemspec +5 -4
- data/lib/http_router/interface/sinatra.rb +7 -7
- data/lib/http_router/node.rb +106 -105
- data/lib/http_router/optional_compiler.rb +14 -5
- data/lib/http_router/path.rb +18 -28
- data/lib/http_router/root.rb +17 -29
- data/lib/http_router/route.rb +47 -16
- data/lib/http_router/static.rb +5 -0
- data/lib/http_router/variable.rb +2 -5
- data/lib/http_router/version.rb +1 -1
- data/lib/http_router.rb +38 -65
- data/test/helper.rb +74 -0
- data/test/rack/test_dispatch.rb +120 -0
- data/test/rack/test_route.rb +44 -0
- data/test/rack/test_urlmap.rb +12 -0
- data/{spec → test}/sinatra/recognize_spec.rb +0 -0
- data/test/sinatra/test_recognize.rb +150 -0
- data/test/test_arbitrary.rb +50 -0
- data/test/test_generate.rb +93 -0
- data/test/test_greedy.rb +24 -0
- data/test/test_interstitial.rb +47 -0
- data/test/test_misc.rb +30 -0
- data/test/test_mounting.rb +89 -0
- data/test/test_recognize.rb +56 -0
- data/test/test_request.rb +85 -0
- data/test/test_trailing_slash.rb +28 -0
- data/test/test_variable.rb +108 -0
- metadata +41 -32
- data/lib/http_router/response.rb +0 -46
- data/spec/generate_spec.rb +0 -234
- data/spec/misc_spec.rb +0 -65
- data/spec/mounting_spec.rb +0 -5
- data/spec/rack/dispatch_spec.rb +0 -119
- data/spec/rack/generate_spec.rb +0 -29
- data/spec/rack/middleware_spec.rb +0 -22
- data/spec/rack/route_spec.rb +0 -72
- data/spec/rack/urlmap_spec.rb +0 -13
- data/spec/recognize_spec.rb +0 -497
- data/spec/spec_helper.rb +0 -25
data/spec/recognize_spec.rb
DELETED
@@ -1,497 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
describe "HttpRouter#recognize" do
|
3
|
-
before(:each) do
|
4
|
-
@router = HttpRouter.new
|
5
|
-
end
|
6
|
-
|
7
|
-
context("with static paths") do
|
8
|
-
['/', '/test', '/test/time', '/one/more/what', '/test.html', '/.html'].each do |path|
|
9
|
-
it "should recognize #{path.inspect}" do
|
10
|
-
route = @router.add(path).to(path)
|
11
|
-
@router.recognize(Rack::MockRequest.env_for(path)).route.should == route
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context("with optional parts") do
|
16
|
-
it "work either way" do
|
17
|
-
route = @router.add("/test(/optional)").to(:test)
|
18
|
-
@router.recognize(Rack::MockRequest.env_for('/test')).route.should == route
|
19
|
-
@router.recognize(Rack::MockRequest.env_for('/test/optional')).route.should == route
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context("with escaping") do
|
24
|
-
it "should recognize ()" do
|
25
|
-
route = @router.add('/test\(:variable\)').to(:test)
|
26
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test(hello)'))
|
27
|
-
response.route.should == route
|
28
|
-
response.params.first.should == 'hello'
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should recognize :" do
|
32
|
-
route = @router.add('/test\:variable').to(:test)
|
33
|
-
@router.recognize(Rack::MockRequest.env_for('/test:variable')).dest.should == :test
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should recognize *" do
|
37
|
-
route = @router.add('/test\*variable').to(:test)
|
38
|
-
@router.recognize(Rack::MockRequest.env_for('/test*variable')).dest.should == :test
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context("with partial matching") do
|
43
|
-
before(:each) do
|
44
|
-
route = @router.add("/test*").to{|env| []}
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should match partially" do
|
48
|
-
route = @router.add("/test*").to{|env| env['PATH_INFO'].should == '/optional'; [200, {}, []]}
|
49
|
-
response = @router.call(Rack::MockRequest.env_for('/test/optional'))
|
50
|
-
response[0].should == 200
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should match partially and use / for rest if the route matched completely" do
|
54
|
-
route = @router.add("/test*").to{|env| env['PATH_INFO'].should == '/'; [200, {}, []]}
|
55
|
-
response = @router.call(Rack::MockRequest.env_for('/test'))
|
56
|
-
response[0].should == 200
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
context("with multiple partial matching") do
|
62
|
-
it "should match partially" do
|
63
|
-
@router.add("/test").partial.to{|env| [200, {}, ['/test',env['PATH_INFO']]]}
|
64
|
-
@router.add("/").partial.to{|env| [200, {}, ['/',env['PATH_INFO']]]}
|
65
|
-
@router.call(Rack::MockRequest.env_for('/test/optional')).last.should == ['/test', '/optional']
|
66
|
-
@router.call(Rack::MockRequest.env_for('/testing/optional')).last.should == ['/', '/testing/optional']
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
|
71
|
-
context("with proc acceptance") do
|
72
|
-
it "should match" do
|
73
|
-
@router.add("/test").arbitrary(Proc.new{|req, params, dest| req.host == 'hellodooly' }).to(:test1)
|
74
|
-
@router.add("/test").arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 80}.to(:test2)
|
75
|
-
@router.add("/test").arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 8080}.to(:test3)
|
76
|
-
response = @router.recognize(Rack::MockRequest.env_for('http://lovelove:8080/test'))
|
77
|
-
response.dest.should == :test3
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should still use an existing less specific node if possible" do
|
81
|
-
@router.add("/test").to(:test4)
|
82
|
-
@router.add("/test").arbitrary(Proc.new{|req, params, dest| req.host == 'hellodooly' }).to(:test1)
|
83
|
-
@router.add("/test").arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 80}.to(:test2)
|
84
|
-
@router.add("/test").arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 8080}.to(:test3)
|
85
|
-
response = @router.recognize(Rack::MockRequest.env_for('http://lovelove:8081/test'))
|
86
|
-
response.dest.should == :test4
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should match with request conditions" do
|
90
|
-
@router.add("/test").get.arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 80}.to(:test1)
|
91
|
-
@router.add("/test").get.arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 8080}.to(:test2)
|
92
|
-
response = @router.recognize(Rack::MockRequest.env_for('http://lovelove:8080/test'))
|
93
|
-
response.dest.should == :test2
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should still use an existing less specific node if possible with request conditions" do
|
97
|
-
@router.add("/test").get.arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 80}.to(:test1)
|
98
|
-
@router.add("/test").get.arbitrary(Proc.new{|req, params, dest| req.host == 'lovelove' }).arbitrary{|req, params, dest| req.port == 8080}.to(:test2)
|
99
|
-
@router.add("/test").get.to(:test3)
|
100
|
-
response = @router.recognize(Rack::MockRequest.env_for('http://lovelove:8081/test'))
|
101
|
-
response.dest.should == :test3
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should pass params and dest" do
|
105
|
-
@router.add("/:test").get.arbitrary(Proc.new{|req, params, dest| params[:test] == 'test' and dest == :test1 }).to(:test1)
|
106
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test'))
|
107
|
-
response.dest.should == :test1
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
context("with trailing slashes") do
|
112
|
-
it "should ignore" do
|
113
|
-
route = @router.add("/test").to(:test)
|
114
|
-
@router.recognize(Rack::MockRequest.env_for('/test/')).route.should == route
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should not recognize when used with the /? syntax and ignore_trailing_slash disabled" do
|
118
|
-
@router = HttpRouter.new(:ignore_trailing_slash => false)
|
119
|
-
route = @router.add("/test/?").to(:test)
|
120
|
-
@router.recognize(Rack::MockRequest.env_for('/test/')).route.should == route
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should recognize when used with the /? syntax and ignore_trailing_slash enabled" do
|
124
|
-
@router = HttpRouter.new(:ignore_trailing_slash => false)
|
125
|
-
route = @router.add("/test").to(:test)
|
126
|
-
@router.recognize(Rack::MockRequest.env_for('/test/')).should be_nil
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should not capture normally" do
|
130
|
-
route = @router.add("/:test").to(:test)
|
131
|
-
@router.recognize(Rack::MockRequest.env_for('/test/')).params.first.should == 'test'
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should recognize trailing slashes when there are other more specific routes near by" do
|
135
|
-
@router = HttpRouter.new
|
136
|
-
route = @router.add("/foo").to(:foo)
|
137
|
-
route = @router.add("/foo/:bar/:id").to(:foo_bar)
|
138
|
-
@router.recognize(Rack::MockRequest.env_for('/foo')).dest.should == :foo
|
139
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/')).dest.should == :foo
|
140
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/5/10')).dest.should == :foo_bar
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
context "with missing leading /" do
|
146
|
-
it "should recognize" do
|
147
|
-
@router.add("foo").to(:test1)
|
148
|
-
@router.add("foo.html").to(:test2)
|
149
|
-
@router.recognize(Rack::MockRequest.env_for('/foo')).dest.should == :test1
|
150
|
-
@router.recognize(Rack::MockRequest.env_for('/foo.html')).dest.should == :test2
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
context "with request methods" do
|
155
|
-
it "should recognize" do
|
156
|
-
route = @router.post("/test").to(:test)
|
157
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'POST')).route.should == route
|
158
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).status.should == 405
|
159
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).headers['Allow'].should == "POST"
|
160
|
-
end
|
161
|
-
|
162
|
-
it "should recognize with optional parts and correctly return a 405 when the method isn't found" do
|
163
|
-
@router.get("/test(.:format)").to(:get)
|
164
|
-
@router.post("/test(.:format)").to(:post)
|
165
|
-
@router.delete("/test(.:format)").to(:delete)
|
166
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'POST')).destination.should == :post
|
167
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).destination.should == :get
|
168
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'DELETE')).destination.should == :delete
|
169
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'PUT')).status.should == 405
|
170
|
-
methods = @router.recognize(Rack::MockRequest.env_for('/test', :method => 'PUT')).headers['Allow'].split(/,\s+/)
|
171
|
-
methods.should include('DELETE')
|
172
|
-
methods.should include('GET')
|
173
|
-
methods.should include('POST')
|
174
|
-
methods.size.should == 3
|
175
|
-
@router.recognize(Rack::MockRequest.env_for('/test.html', :method => 'POST')).destination.should == :post
|
176
|
-
@router.recognize(Rack::MockRequest.env_for('/test.html', :method => 'GET')).destination.should == :get
|
177
|
-
@router.recognize(Rack::MockRequest.env_for('/test.html', :method => 'DELETE')).destination.should == :delete
|
178
|
-
@router.recognize(Rack::MockRequest.env_for('/test.html', :method => 'PUT')).status.should == 405
|
179
|
-
end
|
180
|
-
|
181
|
-
it "should recognize deeply" do
|
182
|
-
@router.post("/test").to(:test_post)
|
183
|
-
@router.post("/test/post").to(:test_post_post)
|
184
|
-
@router.get("/test").to(:test_get)
|
185
|
-
@router.get("/test/post").to(:test_post_get)
|
186
|
-
@router.add("/test/post").to(:test_post_catchall)
|
187
|
-
@router.add("/test").to(:test_catchall)
|
188
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'POST')).dest.should == :test_post
|
189
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'GET')).dest.should == :test_get
|
190
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'PUT')).dest.should == :test_catchall
|
191
|
-
@router.recognize(Rack::MockRequest.env_for('/test/post', :method => 'POST')).dest.should == :test_post_post
|
192
|
-
@router.recognize(Rack::MockRequest.env_for('/test/post', :method => 'GET')).dest.should == :test_post_get
|
193
|
-
@router.recognize(Rack::MockRequest.env_for('/test/post', :method => 'PUT')).dest.should == :test_post_catchall
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should move an endpoint to the non-specific request method when a more specific route gets added" do
|
197
|
-
@router.add("/test").name(:test_catchall).to(:test1)
|
198
|
-
@router.post("/test").name(:test_post).to(:test2)
|
199
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'POST')).route.named.should == :test_post
|
200
|
-
@router.recognize(Rack::MockRequest.env_for('/test', :method => 'PUT')).route.named.should == :test_catchall
|
201
|
-
end
|
202
|
-
|
203
|
-
it "should try multiple request method restrictions in both orders" do
|
204
|
-
@router.add("/test").host('host2').to(:host2)
|
205
|
-
@router.add("/test").post.to(:post)
|
206
|
-
@router.add("/test").host('host2').get.to(:host2_get)
|
207
|
-
@router.add("/test").post.host('host2').to(:host2_post)
|
208
|
-
|
209
|
-
@router.recognize(Rack::MockRequest.env_for('http://host2/test', :method => 'PUT')).dest.should == :host2
|
210
|
-
@router.recognize(Rack::MockRequest.env_for('http://host1/test', :method => 'POST')).dest.should == :post
|
211
|
-
@router.recognize(Rack::MockRequest.env_for('http://host2/test', :method => 'GET')).dest.should == :host2_get
|
212
|
-
@router.recognize(Rack::MockRequest.env_for('http://host2/test', :method => 'POST')).dest.should == :host2_post
|
213
|
-
end
|
214
|
-
|
215
|
-
it "should be able to use regexp in request method conditions" do
|
216
|
-
@router.get("/test").host(/host1/).to(:with_regexp)
|
217
|
-
@router.get("/test").to(:without_regexp)
|
218
|
-
@router.recognize(Rack::MockRequest.env_for('http://host2/test')).dest.should == :without_regexp
|
219
|
-
@router.recognize(Rack::MockRequest.env_for('http://host2.host1.com/test')).dest.should == :with_regexp
|
220
|
-
end
|
221
|
-
|
222
|
-
it "should try both specific and non-specifc routes" do
|
223
|
-
@router.post("/test").host('host1').to(:post_host1)
|
224
|
-
@router.add("/test").host('host2').to(:any_post2)
|
225
|
-
@router.recognize(Rack::MockRequest.env_for('http://host2/test', :method => 'POST')).dest.should == :any_post2
|
226
|
-
end
|
227
|
-
|
228
|
-
it "should match on scheme" do
|
229
|
-
@router.get("/test").scheme('http').to(:http)
|
230
|
-
@router.get("/test").scheme('https').to(:https)
|
231
|
-
@router.recognize(Rack::MockRequest.env_for('http://example.org/test')).dest.should == :http
|
232
|
-
@router.recognize(Rack::MockRequest.env_for('https://example.org/test')).dest.should == :https
|
233
|
-
@router.recognize(Rack::MockRequest.env_for('https://example.org/test', :method => 'POST')).matched?.should be_false
|
234
|
-
end
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
context("with dynamic paths") do
|
239
|
-
it "should recognize /foo/:id and /foo" do
|
240
|
-
@router.add("/foo/:id").to(:test2)
|
241
|
-
@router.add("/foo").to(:test1)
|
242
|
-
@router.recognize(Rack::MockRequest.env_for('/foo')).dest.should == :test1
|
243
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id')).dest.should == :test2
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should recognize /foo/: and map it to $1" do
|
247
|
-
@router.add("/foo/:").to(:test2)
|
248
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id')).dest.should == :test2
|
249
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id')).params_as_hash[:$1].should == 'id'
|
250
|
-
end
|
251
|
-
|
252
|
-
it "should use a static part as a variable if no further match is available" do
|
253
|
-
@router.add("/foo/foo").to(:test1)
|
254
|
-
@router.add("/:foo/foo2").to(:test2)
|
255
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/foo')).dest.should == :test1
|
256
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/foo2')).dest.should == :test2
|
257
|
-
end
|
258
|
-
|
259
|
-
it "should recognize /foo/:/: and map it to $1 and $2" do
|
260
|
-
@router.add("/foo/:/:").to(:test2)
|
261
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id/what')).dest.should == :test2
|
262
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id/what')).params_as_hash[:$1].should == 'id'
|
263
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id/what')).params_as_hash[:$2].should == 'what'
|
264
|
-
end
|
265
|
-
|
266
|
-
it "should recognize '/:variable'" do
|
267
|
-
route = @router.add('/:variable').to(:test)
|
268
|
-
response = @router.recognize(Rack::MockRequest.env_for('/%E6%AE%BA%E3%81%99'))
|
269
|
-
response.route.should == route
|
270
|
-
response.params.should == ["\346\256\272\343\201\231"]
|
271
|
-
response.params_as_hash[:variable].should == "\346\256\272\343\201\231"
|
272
|
-
end
|
273
|
-
|
274
|
-
it "should recognize '/:variable' and URI unescape variables" do
|
275
|
-
route = @router.add('/:variable').to(:test)
|
276
|
-
response = @router.recognize(Rack::MockRequest.env_for('/value'))
|
277
|
-
response.route.should == route
|
278
|
-
response.params.should == ['value']
|
279
|
-
response.params_as_hash[:variable].should == 'value'
|
280
|
-
end
|
281
|
-
|
282
|
-
it "should recognize using match_path" do
|
283
|
-
route = @router.add('/:test').match_path(%r{/(test123|\d+)}).to(:test)
|
284
|
-
@router.recognize(Rack::MockRequest.env_for('/test123')).params_as_hash[:test].should == 'test123'
|
285
|
-
@router.recognize(Rack::MockRequest.env_for('/123')).params_as_hash[:test].should == '123'
|
286
|
-
@router.recognize(Rack::MockRequest.env_for('/test321')).should be_nil
|
287
|
-
@router.recognize(Rack::MockRequest.env_for('/test123andmore')).should be_nil
|
288
|
-
@router.recognize(Rack::MockRequest.env_for('/lesstest123')).should be_nil
|
289
|
-
end
|
290
|
-
|
291
|
-
it "should recognize '/test.:format'" do
|
292
|
-
route = @router.add('/test.:format').to(:test)
|
293
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test.html'))
|
294
|
-
response.route.should == route
|
295
|
-
response.params_as_hash[:format].should == 'html'
|
296
|
-
@router.recognize(Rack::MockRequest.env_for('/test')).should be_nil
|
297
|
-
end
|
298
|
-
|
299
|
-
it "should recognize '/test(.:format)'" do
|
300
|
-
route = @router.add('/test(.:format)').to(:test)
|
301
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test.html'))
|
302
|
-
response.route.should == route
|
303
|
-
response.params_as_hash[:format].should == 'html'
|
304
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test'))
|
305
|
-
response.route.should == route
|
306
|
-
response.params_as_hash[:format].should be_nil
|
307
|
-
end
|
308
|
-
|
309
|
-
it "should recognize '/(.:format)'" do
|
310
|
-
route = @router.add('/(.:format)').to(:test)
|
311
|
-
response = @router.recognize(Rack::MockRequest.env_for('/.html'))
|
312
|
-
response.route.should == route
|
313
|
-
response.params_as_hash[:format].should == 'html'
|
314
|
-
response = @router.recognize(Rack::MockRequest.env_for('/'))
|
315
|
-
response.route.should == route
|
316
|
-
response.params_as_hash[:format].should be_nil
|
317
|
-
end
|
318
|
-
|
319
|
-
it "should recognize '/:test.:format'" do
|
320
|
-
route = @router.add('/:test.:format').to(:test)
|
321
|
-
response = @router.recognize(Rack::MockRequest.env_for('/hey.html'))
|
322
|
-
response.route.should == route
|
323
|
-
response.params_as_hash[:format].should == 'html'
|
324
|
-
response.params_as_hash[:test].should == 'hey'
|
325
|
-
end
|
326
|
-
|
327
|
-
it "should recognize '/:test(.:format)'" do
|
328
|
-
route = @router.add('/:test(.:format)').to(:test)
|
329
|
-
response = @router.recognize(Rack::MockRequest.env_for('/hey.html'))
|
330
|
-
response.route.should == route
|
331
|
-
response.params_as_hash[:format].should == 'html'
|
332
|
-
response.params_as_hash[:test].should == 'hey'
|
333
|
-
response = @router.recognize(Rack::MockRequest.env_for('/hey'))
|
334
|
-
response.route.should == route
|
335
|
-
response.params_as_hash[:format].should be_nil
|
336
|
-
response.params_as_hash[:test].should == 'hey'
|
337
|
-
end
|
338
|
-
|
339
|
-
context "with globs" do
|
340
|
-
it "should recognize" do
|
341
|
-
route = @router.add('/test/*variable').to(:test)
|
342
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test/one/two/three'))
|
343
|
-
response.route.should == route
|
344
|
-
response.params.should == [['one', 'two', 'three']]
|
345
|
-
end
|
346
|
-
|
347
|
-
it "should recognize" do
|
348
|
-
route = @router.add('/test/*variable/test').to(:test)
|
349
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test/one/two/three/test'))
|
350
|
-
response.route.should == route
|
351
|
-
response.params.should == [['one', 'two', 'three']]
|
352
|
-
end
|
353
|
-
|
354
|
-
it "should recognize with a regexp" do
|
355
|
-
route = @router.add('/test/*variable/anymore').matching(:variable => /\d+/).to(:test)
|
356
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test/123/345/567/anymore'))
|
357
|
-
response.route.should == route
|
358
|
-
response.params.should == [['123', '345', '567']]
|
359
|
-
response = @router.recognize(Rack::MockRequest.env_for('/test/123/345/567'))
|
360
|
-
response.should be_nil
|
361
|
-
end
|
362
|
-
|
363
|
-
it "should recognize /foo/*/test and map it to $1" do
|
364
|
-
@router.add("/foo/*/test").to(:test2)
|
365
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id1/id2/test')).dest.should == :test2
|
366
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id1/id2/test')).params_as_hash[:$1].should == ['id1', 'id2']
|
367
|
-
end
|
368
|
-
|
369
|
-
it "should recognize /foo/*/what/: and map it to $1 and $2" do
|
370
|
-
@router.add("/foo/*/what/:").to(:test2)
|
371
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id1/id2/what/more')).dest.should == :test2
|
372
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id1/id2/what/more')).params_as_hash[:$1].should == ['id1', 'id2']
|
373
|
-
@router.recognize(Rack::MockRequest.env_for('/foo/id1/id2/what/more')).params_as_hash[:$2].should == 'more'
|
374
|
-
end
|
375
|
-
end
|
376
|
-
|
377
|
-
end
|
378
|
-
|
379
|
-
context("with interstitial variables") do
|
380
|
-
it "should recognize" do
|
381
|
-
route = @router.add('/one-:variable-time').to(:test)
|
382
|
-
response = @router.recognize(Rack::MockRequest.env_for('/one-value-time'))
|
383
|
-
response.route.should == route
|
384
|
-
response.params_as_hash[:variable].should == 'value'
|
385
|
-
end
|
386
|
-
|
387
|
-
it "should recognize with a regex" do
|
388
|
-
route = @router.add('/one-:variable-time').matching(:variable => /\d+/).to(:test)
|
389
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value-time')).should be_nil
|
390
|
-
response = @router.recognize(Rack::MockRequest.env_for('/one-123-time'))
|
391
|
-
response.route.should == route
|
392
|
-
response.params_as_hash[:variable].should == '123'
|
393
|
-
end
|
394
|
-
|
395
|
-
it "should recognize with a regex as part of the options" do
|
396
|
-
route = @router.add('/one-:variable-time', :variable => /\d+/).to(:test)
|
397
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value-time')).should be_nil
|
398
|
-
response = @router.recognize(Rack::MockRequest.env_for('/one-123-time'))
|
399
|
-
response.route.should == route
|
400
|
-
response.params_as_hash[:variable].should == '123'
|
401
|
-
end
|
402
|
-
|
403
|
-
it "should recognize when there is an extension" do
|
404
|
-
route = @router.add('/hey.:greed.html').to(:test)
|
405
|
-
response = @router.recognize(Rack::MockRequest.env_for('/hey.greedyboy.html'))
|
406
|
-
response.route.should == route
|
407
|
-
response.params_as_hash[:greed].should == 'greedyboy'
|
408
|
-
end
|
409
|
-
|
410
|
-
it "should distinguish between very similar looking routes" do
|
411
|
-
@router.add('/:var1').to(:test1)
|
412
|
-
@router.add('/:var1-:var2').to(:test2)
|
413
|
-
@router.add('/:var1-:var2-:var3').to(:test3)
|
414
|
-
@router.add('/:var1-:var2-:var3-:var4').to(:test4)
|
415
|
-
@router.add('/:var1-:var2-:var3-:var4-:var5').to(:test5)
|
416
|
-
@router.add('/:var1-:var2-:var3-:var4-:var5-:var6').to(:test6)
|
417
|
-
@router.recognize(Rack::MockRequest.env_for('/one')).dest.should == :test1
|
418
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value')).dest.should == :test2
|
419
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value-time')).dest.should == :test3
|
420
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value-time-one')).dest.should == :test4
|
421
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value-time-one-variable')).dest.should == :test5
|
422
|
-
@router.recognize(Rack::MockRequest.env_for('/one-value-time-one-value-time')).dest.should == :test6
|
423
|
-
end
|
424
|
-
end
|
425
|
-
|
426
|
-
context("with dynamic greedy paths") do
|
427
|
-
it "should recognize" do
|
428
|
-
route = @router.add('/:variable').matching(:variable => /\d+/).to(:test)
|
429
|
-
response = @router.recognize(Rack::MockRequest.env_for('/123'))
|
430
|
-
response.route.should == route
|
431
|
-
response.params.should == ['123']
|
432
|
-
response.params_as_hash[:variable].should == '123'
|
433
|
-
response = @router.recognize(Rack::MockRequest.env_for('/asd'))
|
434
|
-
response.should be_nil
|
435
|
-
end
|
436
|
-
|
437
|
-
it "should continue on with normal if regex fails to match" do
|
438
|
-
@router.add("/:test/number").matching(:test => /\d+/).to(:test_number)
|
439
|
-
target = @router.add("/:test/anything").to(:test_anything)
|
440
|
-
@router.recognize(Rack::MockRequest.env_for('/123/anything')).route.should == target
|
441
|
-
end
|
442
|
-
|
443
|
-
it "should capture the trailing slash" do
|
444
|
-
route = @router.add("/:test").matching(:test => /.*/).to(:test)
|
445
|
-
@router.recognize(Rack::MockRequest.env_for('/test/')).params.first.should == 'test/'
|
446
|
-
end
|
447
|
-
|
448
|
-
it "should require the match to begin at the beginning" do
|
449
|
-
route = @router.add("/:test").matching(:test => /\d+/).to(:test)
|
450
|
-
@router.recognize(Rack::MockRequest.env_for('/a123')).should be_nil
|
451
|
-
end
|
452
|
-
|
453
|
-
it "should capture the extension" do
|
454
|
-
route = @router.add("/:test").matching(:test => /.*/).to(:test)
|
455
|
-
@router.recognize(Rack::MockRequest.env_for('/test.html')).params.first.should == 'test.html'
|
456
|
-
end
|
457
|
-
|
458
|
-
# BUG: http://gist.github.com/554909
|
459
|
-
context "when there is an additional route for the case of regex matching failure" do
|
460
|
-
before :each do
|
461
|
-
@matched = @router.add("/:common_variable/:matched").matching(:matched => /\d+/).to(:something)
|
462
|
-
@unmatched = @router.add("/:common_variable/:unmatched").to(:something_unmatched)
|
463
|
-
end
|
464
|
-
|
465
|
-
it "should use main route if pattern is matched" do
|
466
|
-
response = @router.recognize(Rack::MockRequest.env_for('/common/123'))
|
467
|
-
response.route.should == @matched
|
468
|
-
response.params.should == ['common', '123']
|
469
|
-
end
|
470
|
-
|
471
|
-
it "should use additional route if pattern is not matched" do
|
472
|
-
response = @router.recognize(Rack::MockRequest.env_for('/common/other'))
|
473
|
-
response.route.should == @unmatched
|
474
|
-
response.params.should == ['common', 'other']
|
475
|
-
end
|
476
|
-
|
477
|
-
context "when delimiter is not a slash" do
|
478
|
-
before :each do
|
479
|
-
@matched = @router.add("/:common_variable.:matched").matching(:matched => /\d+/).to(:something)
|
480
|
-
@unmatched = @router.add("/:common_variable.:unmatched").to(:something_unmatched)
|
481
|
-
end
|
482
|
-
|
483
|
-
it "should use main route if pattern is matched" do
|
484
|
-
response = @router.recognize(Rack::MockRequest.env_for('/common.123'))
|
485
|
-
response.route.should == @matched
|
486
|
-
response.params.should == ['common', '123']
|
487
|
-
end
|
488
|
-
|
489
|
-
it "should use additional route if pattern is not matched" do
|
490
|
-
response = @router.recognize(Rack::MockRequest.env_for('/common.other'))
|
491
|
-
response.route.should == @unmatched
|
492
|
-
response.params.should == ['common', 'other']
|
493
|
-
end
|
494
|
-
end
|
495
|
-
end
|
496
|
-
end
|
497
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
require 'http_router'
|
3
|
-
|
4
|
-
module CallWithMockRequestMixin
|
5
|
-
def call_with_mock_request(url = "/sample", method = "GET", params = Hash.new)
|
6
|
-
params.merge!(:method => method)
|
7
|
-
request = Rack::MockRequest.new(self)
|
8
|
-
request.request(method, url, params)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class MockApp
|
13
|
-
attr_accessor :status, :headers, :body, :env
|
14
|
-
def initialize(body)
|
15
|
-
@status = 200
|
16
|
-
@headers = {"Content-Type" => "text/html"}
|
17
|
-
@body = body
|
18
|
-
end
|
19
|
-
|
20
|
-
def call(env)
|
21
|
-
@env = env
|
22
|
-
@headers.merge("Content-Length" => @body.length.to_s)
|
23
|
-
[@status, @headers, [@body]]
|
24
|
-
end
|
25
|
-
end
|