http_router 0.4.1 → 0.5.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/Rakefile +6 -5
  2. data/benchmarks/rack_mount.rb +16 -45
  3. data/benchmarks/rec2.rb +8 -8
  4. data/http_router.gemspec +5 -4
  5. data/lib/http_router/interface/sinatra.rb +7 -7
  6. data/lib/http_router/node.rb +106 -105
  7. data/lib/http_router/optional_compiler.rb +14 -5
  8. data/lib/http_router/path.rb +18 -28
  9. data/lib/http_router/root.rb +17 -29
  10. data/lib/http_router/route.rb +47 -16
  11. data/lib/http_router/static.rb +5 -0
  12. data/lib/http_router/variable.rb +2 -5
  13. data/lib/http_router/version.rb +1 -1
  14. data/lib/http_router.rb +38 -65
  15. data/test/helper.rb +74 -0
  16. data/test/rack/test_dispatch.rb +120 -0
  17. data/test/rack/test_route.rb +44 -0
  18. data/test/rack/test_urlmap.rb +12 -0
  19. data/{spec → test}/sinatra/recognize_spec.rb +0 -0
  20. data/test/sinatra/test_recognize.rb +150 -0
  21. data/test/test_arbitrary.rb +50 -0
  22. data/test/test_generate.rb +93 -0
  23. data/test/test_greedy.rb +24 -0
  24. data/test/test_interstitial.rb +47 -0
  25. data/test/test_misc.rb +30 -0
  26. data/test/test_mounting.rb +89 -0
  27. data/test/test_recognize.rb +56 -0
  28. data/test/test_request.rb +85 -0
  29. data/test/test_trailing_slash.rb +28 -0
  30. data/test/test_variable.rb +108 -0
  31. metadata +41 -32
  32. data/lib/http_router/response.rb +0 -46
  33. data/spec/generate_spec.rb +0 -234
  34. data/spec/misc_spec.rb +0 -65
  35. data/spec/mounting_spec.rb +0 -5
  36. data/spec/rack/dispatch_spec.rb +0 -119
  37. data/spec/rack/generate_spec.rb +0 -29
  38. data/spec/rack/middleware_spec.rb +0 -22
  39. data/spec/rack/route_spec.rb +0 -72
  40. data/spec/rack/urlmap_spec.rb +0 -13
  41. data/spec/recognize_spec.rb +0 -497
  42. data/spec/spec_helper.rb +0 -25
@@ -1,119 +0,0 @@
1
- describe "HttpRouter route dispatching with redirect_on_trailing_delimiters" do
2
- before(:each) do
3
- @route_set = HttpRouter.new(:redirect_trailing_slash => true)
4
- @route_set.extend(CallWithMockRequestMixin)
5
- @app = MockApp.new("Hello World!")
6
- @route_set.add('/sample').to(@app)
7
- end
8
-
9
- it "should dispatch a request" do
10
- response = @route_set.call_with_mock_request('/sample/')
11
- response.headers["Location"].should == "/sample"
12
- end
13
-
14
- end
15
-
16
- describe "HttpRouter route dispatching" do
17
- before(:each) do
18
- @route_set = HttpRouter.new(:redirect_trailing_slash => true)
19
- @route_set.extend(CallWithMockRequestMixin)
20
- @app = MockApp.new("Hello World!")
21
- end
22
-
23
- describe "HTTP GET" do
24
- before(:each) do
25
- @route_set.reset!
26
- @route_set.add('/sample').request_method('GET').to(@app)
27
- end
28
-
29
- it "should dispatch a request" do
30
- response = @route_set.call_with_mock_request
31
- response.body.should eql("Hello World!")
32
- end
33
-
34
- it "should write router.params" do
35
- response = @route_set.call_with_mock_request
36
- @app.env["router.params"].should == {}
37
- end
38
-
39
- it "should write router.params for default values" do
40
- @route_set.add("/foobar", :default_values => {:hi => :there}).compile
41
- response = @route_set.call_with_mock_request("/foobar")
42
- env = Rack::MockRequest.env_for("/foobar")
43
- @route_set.call(env)
44
- env['router.params'].should == {:hi => :there}
45
- end
46
- end
47
-
48
- describe "HTTP POST" do
49
- before(:each) do
50
- @route_set.reset!
51
- @route_set.add('/sample').post.to(@app)
52
- @route_set.add('/sample').to(MockApp.new("You shouldn't get here if you are using POST"))
53
- end
54
-
55
- it "should dispatch a POST request" do
56
- response = @route_set.call_with_mock_request('/sample', 'POST')
57
- response.body.should eql("Hello World!")
58
- end
59
-
60
- it "shouldn't dispatch a GET request" do
61
- response = @route_set.call_with_mock_request('/sample', 'GET')
62
- response.body.should eql("You shouldn't get here if you are using POST")
63
- end
64
-
65
- it "should write router.params" do
66
- response = @route_set.call_with_mock_request("/sample", 'POST')
67
- @app.env["router.params"].should == {}
68
- end
69
- end
70
-
71
- it "should returns HTTP 405 if the method mis-matches" do
72
- @route_set.reset!
73
- @route_set.post('/sample').to(@app)
74
- @route_set.put('/sample').to(@app)
75
- response = @route_set.call_with_mock_request('/sample', 'GET')
76
- response.status.should eql(405)
77
- response['Allow'].should == 'POST, PUT'
78
- end
79
-
80
- it "should returns HTTP 404 if route doesn't exist" do
81
- response = @route_set.call_with_mock_request("/not-existing-url")
82
- response.status.should eql(404)
83
- end
84
-
85
- describe "shortcuts" do
86
- describe "get" do
87
- before(:each) do
88
- @route_set.reset!
89
- @route_set.get('/sample').head.to(@app)
90
- end
91
-
92
- it "should dispatch a GET request" do
93
- response = @route_set.call_with_mock_request("/sample", "GET")
94
- response.body.should eql("Hello World!")
95
- end
96
-
97
- it "should dispatch a HEAD request" do
98
- response = @route_set.call_with_mock_request("/sample", "HEAD")
99
- response.body.should eql("Hello World!")
100
- end
101
- end
102
- end
103
-
104
- describe "non rack app destinations" do
105
- it "should route to a default application when using a hash" do
106
- $captures = []
107
- @default_app = lambda do |e|
108
- $captures << :default
109
- Rack::Response.new("Default").finish
110
- end
111
- @router = HttpRouter.new
112
- @router.default(@default_app)
113
- @router.add("/default").to(:action => "default")
114
- response = @router.call(Rack::MockRequest.env_for("/default"))
115
- $captures.should == [:default]
116
- end
117
- end
118
-
119
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- route_set = HttpRouter.new
4
- route_set.extend(CallWithMockRequestMixin)
5
-
6
- describe "HttpRouter route generation" do
7
- before(:each) do
8
- route_set.reset!
9
- @app = MockApp.new("Hello World!")
10
- route_set.add("/fixed").name(:fixed).compile
11
- route_set.add("/named/simple/:named_simple_var").name(:simple).compile
12
- route_set.add("/named/optional(/:named_optional_var)").name(:optional).compile
13
- end
14
-
15
- describe "named routes" do
16
- it "should generate a fixed path" do
17
- route_set.url(:fixed).should == "/fixed"
18
- end
19
-
20
- it "should generate a named path route" do
21
- route_set.url(:simple, :named_simple_var => "the_var").should == "/named/simple/the_var"
22
- end
23
-
24
- it "should generate a named route with options" do
25
- route_set.url(:optional).should == "/named/optional"
26
- route_set.url(:optional, :named_optional_var => "the_var").should == "/named/optional/the_var"
27
- end
28
- end
29
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "HttpRouter as middleware" do
4
- before(:each) do
5
- @builder = Rack::Builder.new do
6
- use(HttpRouter, :middleware => true) {
7
- add('/test').name(:test).to(:test)
8
- }
9
- end
10
- end
11
-
12
- it "should always have the router" do
13
- @builder.run proc{|env| [200, {}, [env['router'].url(:test)]]}
14
- @builder.call(Rack::MockRequest.env_for('/some-path')).last.join.should == '/test'
15
- end
16
-
17
- it "should stash the match if it exists" do
18
- @builder.run proc{|env| [200, {}, [env['router.response'].dest.to_s]]}
19
- @builder.call(Rack::MockRequest.env_for('/test')).last.join.should == 'test'
20
- end
21
- end
22
-
@@ -1,72 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Rack interface extensions for Usher::Route" do
4
- before(:each) do
5
- @route_set = HttpRouter.new
6
- @app = MockApp.new("Hello World!")
7
- @env = Rack::MockRequest.env_for("/index.html")
8
- end
9
-
10
- describe "basic functinality" do
11
- it "should set redirect headers" do
12
- @route_set.get("/index.html").redirect("/")
13
- raw_response = @route_set.call(@env)
14
- response = Rack::MockResponse.new(*raw_response)
15
- response.should be_redirect
16
- end
17
-
18
- it "should redirect '/index.html' to '/'" do
19
- @route_set.get("/index.html").redirect("/")
20
- status, headers, body = @route_set.call(@env)
21
- headers["Location"].should eql("/")
22
- end
23
-
24
- it "should redirect '/:id.html' to '/:id'" do
25
- @route_set.get("/:id.html").redirect('/#{params[:id]}')
26
- @env = Rack::MockRequest.env_for("/123.html")
27
- status, headers, body = @route_set.call(@env)
28
- headers["Location"].should eql("/123")
29
- end
30
- end
31
-
32
- describe "static file serving" do
33
- it "should serve from a static directory" do
34
- @route_set.get("/static").static(File.dirname(__FILE__))
35
- @env = Rack::MockRequest.env_for("/static/#{File.basename(__FILE__)}")
36
- status, headers, body = @route_set.call(@env)
37
- body.path.should == File.join(File.dirname(__FILE__), File.basename(__FILE__))
38
- end
39
-
40
- it "should serve a specific file" do
41
- @route_set.get("/static-file").static(__FILE__)
42
- @env = Rack::MockRequest.env_for("/static-file")
43
- status, headers, body = @route_set.call(@env)
44
- body.path.should == __FILE__
45
- end
46
- end
47
-
48
- describe "chaining" do
49
- it "should be chainable" do
50
- @route_set.get("/index.html").redirect("/").name(:root)
51
- url = @route_set.url(:root)
52
- url.should eql("/index.html")
53
- end
54
-
55
- it "should not influence actual invoking" do
56
- @route_set.get("/index.html").redirect("/").name(:root)
57
- @route_set.call(@env)
58
- end
59
- end
60
-
61
- describe "custom status" do
62
- it "should enable to set custom HTTP status" do
63
- @route_set.get("/index.html").redirect("/", 303)
64
- status, headers, body = @route_set.call(@env)
65
- status.should eql(303)
66
- end
67
-
68
- it "should raise an exception if given HTTP code isn't a redirection" do
69
- lambda { @route_set.get("/index.html").redirect("/", 200) }.should raise_error(ArgumentError)
70
- end
71
- end
72
- end
@@ -1,13 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Rack::Urlmap replacement" do
4
- it "should map urls" do
5
- HttpRouter::Rack.override_rack_urlmap!
6
- map = Rack::URLMap.new(
7
- "http://www.example.org/test" => proc {|env| [200, {}, ['test']]},
8
- "http://www.example.org/:test" => proc {|env| [200, {}, ['variable']]}
9
- )
10
- map.call(Rack::MockRequest.env_for('http://www.example.org/test')).last.join.should == 'test'
11
- map.call(Rack::MockRequest.env_for('http://www.example.org/whhhaaa')).last.join.should == 'variable'
12
- end
13
- end