http_router 0.8.10 → 0.8.11

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.
@@ -1,24 +0,0 @@
1
- class TestGreedy < MiniTest::Unit::TestCase
2
- def test_mix_regex_with_greedy
3
- regex, greedy = router {
4
- add("/:test/number").matching(:test => /\d+/)
5
- add("/:test/anything")
6
- }
7
- assert_route regex, '/123/number', {:test => '123'}
8
- assert_route greedy, '/123/anything', {:test => '123'}
9
- end
10
-
11
- def test_trailing_slash
12
- assert_route router.add("/:test").matching(:test => /.*/), '/test/', {:test => 'test/'}
13
- end
14
-
15
- def test_extension
16
- assert_route router.add("/:test").matching(:test => /.*/), '/test.html', {:test => 'test.html'}
17
- end
18
-
19
- def test_match_at_beginning
20
- r = router { add(':test', :test => /\d+/)}
21
- assert_route r, '/123', {:test => '123'}
22
- assert_route nil, '/a123'
23
- end
24
- end
@@ -1,47 +0,0 @@
1
- class TestInterstitial < MiniTest::Unit::TestCase
2
- def test_recognize
3
- assert_route '/one-:variable-time', '/one-value-time', {:variable => 'value'}
4
- end
5
-
6
- def test_regex
7
- r = router { add('/one-:variable-time').matching(:variable => /\d+/) }
8
- assert_route r, '/one-123-time', {:variable => '123'}
9
- assert_route nil, '/one-value-time'
10
- end
11
-
12
- def test_regex_as_options
13
- r = router { add('/one-:variable-time', :variable => /\d+/) }
14
- assert_route r, '/one-123-time', {:variable => '123'}
15
- assert_route nil, '/one-value-time'
16
- end
17
-
18
- def test_extension
19
- assert_route 'hey.:greed.html', '/hey.greedybody.html', {:greed => 'greedybody'}
20
- end
21
-
22
- def test_multi
23
- r6, r5, r4, r3, r2, r1 = router {
24
- add('/:var1-:var2-:var3-:var4-:var5-:var6')
25
- add('/:var1-:var2-:var3-:var4-:var5')
26
- add('/:var1-:var2-:var3-:var4')
27
- add('/:var1-:var2-:var3')
28
- add('/:var1-:var2')
29
- add('/:var1')
30
- }
31
- assert_route r1, '/one', {:var1 => 'one'}
32
- assert_route r2, '/one-value', {:var1 => 'one', :var2 => 'value'}
33
- assert_route r3, '/one-value-time', {:var1 => 'one', :var2 => 'value', :var3 => 'time'}
34
- assert_route r4, '/one-value-time-one', {:var1 => 'one', :var2 => 'value', :var3 => 'time', :var4 => 'one'}
35
- assert_route r5, '/one-value-time-one-variable', {:var1 => 'one', :var2 => 'value', :var3 => 'time', :var4 => 'one', :var5 => 'variable'}
36
- assert_route r6, '/one-value-time-one-value-time', {:var1 => 'one', :var2 => 'value', :var3 => 'time', :var4 => 'one', :var5 => 'value', :var6 => 'time'}
37
- end
38
-
39
- def test_regex_with_mutliple_variables
40
- with_regex, without_regex = router {
41
- add("/:common_variable.:matched").matching(:matched => /\d+/)
42
- add("/:common_variable.:unmatched")
43
- }
44
- assert_route with_regex, '/common.123', {:common_variable => 'common', :matched => '123'}
45
- assert_route without_regex, '/common.other', {:common_variable => 'common', :unmatched => 'other'}
46
- end
47
- end
@@ -1,106 +0,0 @@
1
- # encoding: utf-8
2
- class TestRecognition < MiniTest::Unit::TestCase
3
- def test_empty
4
- assert_route router.add(''), '/'
5
- end
6
-
7
- def test_simple1
8
- assert_route router.add('/'), '/'
9
- end
10
-
11
- def test_simple2
12
- assert_route router.add('/test'), '/test'
13
- end
14
-
15
- def test_simple3
16
- assert_route router.add('/test/one'), '/test/one'
17
- end
18
-
19
- def test_simple4
20
- assert_route router.add('/test/one/two'), '/test/one/two'
21
- end
22
-
23
- def test_simple5
24
- assert_route router.add('/test.html'), '/test.html'
25
- end
26
-
27
- def test_simple6
28
- assert_route router.add('/.html'), '/.html'
29
- end
30
-
31
- def test_passing
32
- passed, working = router {
33
- add('/').to { |env| throw :pass; [200, {}, ['pass']] }
34
- add('/').to { |env| [200, {}, ['working']] }
35
- }
36
- assert_body 'working', router.call(Rack::MockRequest.env_for('/'))
37
- end
38
-
39
- def test_passing_with_cascade
40
- passed, working = router {
41
- add('/').to { |env| [200, {'X-Cascade' => 'pass'}, ['pass']] }
42
- add('/').to { |env| [200, {}, ['working']] }
43
- }
44
- assert_body 'working', router.call(Rack::MockRequest.env_for('/'))
45
- end
46
-
47
- def test_optional
48
- route = router {
49
- add 'one(/two(/three(/four)(/five)))'
50
- }
51
- assert_route route, '/one'
52
- assert_route route, '/one/two'
53
- assert_route route, '/one/two/three'
54
- assert_route route, '/one/two/three/four'
55
- assert_route route, '/one/two/three/five'
56
- assert_route route, '/one/two/three/four/five'
57
- end
58
-
59
- def test_escape_paren
60
- assert_route router.add('/test\(:variable\)'), '/test(hello)', {:variable => 'hello'}
61
- end
62
-
63
- def test_escape_colon
64
- assert_route router.add('/test\:variable'), '/test:variable'
65
- end
66
-
67
- def test_escape_star
68
- assert_route router.add('/test\*variable'), '/test*variable'
69
- end
70
-
71
- def test_unicode
72
- assert_route router.add('/føø'), '/f%C3%B8%C3%B8'
73
- end
74
-
75
- def test_partial
76
- router.add("/test*").to { |env| Rack::Response.new(env['PATH_INFO']).finish }
77
- assert_body '/optional', router.call(Rack::MockRequest.env_for('/test/optional'))
78
- assert_body '/', router.call(Rack::MockRequest.env_for('/test'))
79
- end
80
-
81
- def test_partial_root
82
- router.add("/*").to { |env| Rack::Response.new(env['PATH_INFO']).finish }
83
- assert_body '/optional', router.call(Rack::MockRequest.env_for('/optional'))
84
- assert_body '/', router.call(Rack::MockRequest.env_for('/'))
85
- end
86
-
87
- def test_request_mutation
88
- got_this_far = false
89
- non_matching, matching = router {
90
- add("/test/:var/:var2/*glob").matching(:var2 => /123/, :glob => /[a-z]+/).get.arbitrary{|env, params| got_this_far = true; false}
91
- add("/test/:var/:var2/*glob").matching(:var2 => /123/, :glob => /[a-z]+/).get
92
- }
93
- assert_route matching, '/test/123/123/asd/aasd/zxcqwe/asdzxc', {:var => '123', :var2 => '123', :glob => %w{asd aasd zxcqwe asdzxc}}
94
- assert got_this_far, "matching should have gotten this far"
95
- end
96
-
97
- def test_multiple_partial
98
- test, root = router {
99
- add("/test").partial.to{|env| [200, {}, ['/test',env['PATH_INFO']]]}
100
- add("/").partial.to{|env| [200, {}, ['/',env['PATH_INFO']]]}
101
- }
102
- assert_body ['/test', '/optional'], router.call(Rack::MockRequest.env_for('/test/optional'))
103
- assert_body ['/test', '/optional/'], router.call(Rack::MockRequest.env_for('/test/optional/'))
104
- assert_body ['/', '/testing/optional'], router.call(Rack::MockRequest.env_for('/testing/optional'))
105
- end
106
- end
@@ -1,102 +0,0 @@
1
- class TestRequest < MiniTest::Unit::TestCase
2
- def test_simple_case
3
- r = router { add('test').post }
4
- assert_route r, Rack::MockRequest.env_for('/test', :method => 'POST')
5
- assert_header({'Allow' => 'POST'}, Rack::MockRequest.env_for('/test', :method => 'GET'))
6
- assert_status(405, Rack::MockRequest.env_for('/test', :method => 'GET'))
7
- end
8
-
9
- def test_simple_case_with_array
10
- r = router { add('test', :conditions => {:request_method => ['POST', 'GET']}) }
11
- assert_route r, Rack::MockRequest.env_for('/test', :method => 'POST')
12
- assert_route r, Rack::MockRequest.env_for('/test', :method => 'GET')
13
- end
14
-
15
- def test_simple_case_with_non_array
16
- r = router { add('test', :conditions => {:request_method => 'GET'}) }
17
- assert_route r, Rack::MockRequest.env_for('/test', :method => 'GET')
18
- end
19
-
20
- def test_single_app_with_404
21
- r = router { add('test').post.to{|env| [404, {}, []]} }
22
- assert_route nil, Rack::MockRequest.env_for('/test', :method => 'POST')
23
- assert_status(404, Rack::MockRequest.env_for('/test', :method => 'POST'))
24
- end
25
-
26
- def test_with_optional_parts_and_405
27
- get, post, delete = router {
28
- get('test(.:format)')
29
- post('test(.:format)')
30
- delete('test(.:format)')
31
- }
32
- assert_route get, Rack::MockRequest.env_for('/test', :method => 'GET')
33
- assert_route post, Rack::MockRequest.env_for('/test', :method => 'POST')
34
- assert_route delete, Rack::MockRequest.env_for('/test', :method => 'DELETE')
35
- assert_route get, Rack::MockRequest.env_for('/test.html', :method => 'GET'), {:format => 'html'}
36
- assert_route post, Rack::MockRequest.env_for('/test.html', :method => 'POST'), {:format => 'html'}
37
- assert_route delete, Rack::MockRequest.env_for('/test.html', :method => 'DELETE'), {:format => 'html'}
38
- put = router.call(Rack::MockRequest.env_for('/test', :method => 'PUT'))
39
- assert_status 405, put
40
- assert_equal %w{DELETE GET POST}, put[1]['Allow'].split(/\s*,\s*/).sort
41
- end
42
-
43
- def test_deeply
44
- test_post, test_post_post, test_get, test_post_get, test_post_catchall, test_catchall = router {
45
- post("/test")
46
- post("/test/post")
47
- get("/test")
48
- get("/test/post")
49
- add("/test/post")
50
- add("/test")
51
- }
52
- assert_route test_post, Rack::MockRequest.env_for('/test', :method => 'POST')
53
- assert_route test_get, Rack::MockRequest.env_for('/test', :method => 'GET')
54
- assert_route test_catchall, Rack::MockRequest.env_for('/test', :method => 'PUT')
55
- assert_route test_post_post, Rack::MockRequest.env_for('/test/post', :method => 'POST')
56
- assert_route test_post_get, Rack::MockRequest.env_for('/test/post', :method => 'GET')
57
- assert_route test_post_catchall, Rack::MockRequest.env_for('/test/post', :method => 'PUT')
58
- end
59
-
60
- def test_move_node
61
- post, general = router {
62
- post("/test").default_destination
63
- add("/test").default_destination
64
- }
65
- assert_route post, Rack::MockRequest.env_for('/test', :method => 'POST')
66
- assert_route general, Rack::MockRequest.env_for('/test', :method => 'PUT')
67
- end
68
-
69
- def test_complex_routing
70
- host2_post, host2_get, host2, post = router {
71
- add("/test").post.host('host2')
72
- add("/test").host('host2').get
73
- add("/test").host('host2')
74
- add("/test").post
75
- }
76
- assert_route host2, Rack::MockRequest.env_for('http://host2/test', :method => 'PUT')
77
- assert_route post, Rack::MockRequest.env_for('http://host1/test', :method => 'POST')
78
- assert_route host2_get, Rack::MockRequest.env_for('http://host2/test', :method => 'GET')
79
- assert_route host2_post, Rack::MockRequest.env_for('http://host2/test', :method => 'POST')
80
- end
81
-
82
- def test_regexp
83
- with, without = router {
84
- get("/test").host(/host1/)
85
- get("/test")
86
- }
87
- assert_route without, 'http://host2/test'
88
- assert_route with, 'http://host2.host1.com/test'
89
- end
90
-
91
- def test_all_routes
92
- router {
93
- post("/test").host('host1')
94
- }
95
- assert_route router.add("/test").host('host2'), Rack::MockRequest.env_for('http://host2/test', :method => 'POST')
96
- end
97
-
98
- def test_match_on_scheme
99
- http, https = router { get("/test").scheme('http'); get("/test").scheme('https') }
100
- assert_status 405, Rack::MockRequest.env_for('https://example.org/test', :method => 'POST')
101
- end
102
- end
@@ -1,163 +0,0 @@
1
- # encoding: utf-8
2
- class TestVariable < MiniTest::Unit::TestCase
3
- def test_variable
4
- assert_route ':one', '/two', {:one => 'two'}
5
- assert_route 'test/:one', '/test/three', {:one => 'three'}
6
- end
7
-
8
- def test_variable_vs_static
9
- static, dynamic = router { add 'one'; add ':one' }
10
- assert_route dynamic, '/two', {:one => 'two'}
11
- assert_route static, '/one'
12
- end
13
-
14
- def test_variable_with_static_after
15
- variable, static = router {
16
- add '/:var/one'
17
- add 'one'
18
- }
19
- assert_route variable, '/two/one', {:var => 'two'}
20
- assert_route static, '/one'
21
- assert_route nil, '/two'
22
- end
23
-
24
- def test_variable_and_static
25
- dynamic, static = router {
26
- add("/foo/:id")
27
- add("/foo")
28
- }
29
- assert_route dynamic, '/foo/id', {:id => 'id'}
30
- assert_route static, '/foo'
31
- end
32
-
33
- def test_variable_mixed_with_static
34
- static, dynamic = router {
35
- add("/foo/foo")
36
- add("/:foo/foo2")
37
- }
38
- assert_route dynamic, '/foo/foo2', {:foo => 'foo'}
39
- assert_route static, '/foo/foo'
40
- end
41
-
42
- def test_encoding
43
- assert_route '/:var', '/%E6%AE%BA%E3%81%99', {:var => "\346\256\272\343\201\231"}
44
- end
45
-
46
- def test_match_path
47
- r = router { add %r{/(test123|\d+)} }
48
- assert_route r, '/test123'
49
- assert_route r, '/123'
50
- assert_route nil, '/test123andmore'
51
- assert_route nil, '/lesstest123'
52
- end
53
-
54
- def test_format
55
- assert_route '/test.:format', '/test.html', {:format => 'html'}
56
- end
57
-
58
- def test_optional_format
59
- r = router {add('/test(.:format)')}
60
- assert_route r, '/test.html', {:format => 'html'}
61
- assert_route r, '/test'
62
- end
63
-
64
- def test_bare_optional_format
65
- r = router {add('(.:format)')}
66
- assert_route r, '/.html', {:format => 'html'}
67
- assert_route r, '/'
68
- end
69
-
70
- def test_var_with_format
71
- assert_route '/:test.:format', '/foo.bar', {:test => 'foo', :format => 'bar'}
72
- end
73
-
74
- def test_var_with_optional_format
75
- r = router { add('/:test(.:format)') }
76
- assert_route r, '/foo.bar', {:test => 'foo', :format => 'bar'}
77
- assert_route r, '/foo', {:test => 'foo'}
78
- end
79
-
80
- def test_var_with_optional_format_and_regex
81
- r = router { add('/:test(.:format)', :format => /[^\.]+/) }
82
- assert_route r, '/asd@asd.com.json', {:test => 'asd@asd.com', :format => 'json'}
83
- end
84
-
85
- def test_glob
86
- assert_route '/test/*variable', 'test/one/two/three', {:variable => ['one', 'two', 'three']}
87
- end
88
-
89
- def test_glob_with_static
90
- assert_route '/test/*variable/test', '/test/one/two/three/test', {:variable => ['one', 'two', 'three']}
91
- end
92
-
93
- def test_double_glob_all_the_way
94
- assert_route '/test/*variable/test/*variable2', '/test/one/two/three/test/four/five/six', {:variable => ['one', 'two', 'three'], :variable2 => ['four', 'five', 'six']}
95
- end
96
-
97
- def test_var_glob_var
98
- assert_route '/test/:test-*variable.:format', '/test/one-two/three/four/five.six', {:test=> 'one', :variable => ['two', 'three', 'four', 'five'], :format => 'six'}
99
- end
100
-
101
- def test_glob_with_regexp
102
- r = router { add('/test/*variable', :variable => /[a-z]+/) }
103
- assert_route nil, '/test/asd/123'
104
- assert_route nil, '/test/asd/asd123'
105
- assert_route r, '/test/asd/qwe', :variable => ['asd', 'qwe']
106
- end
107
-
108
- def test_glob_with_regexp_and_static
109
- r = router { add('/test/*variable/test', :variable => /[a-z]+/) }
110
- assert_route nil, '/test/asd/123/test'
111
- assert_route r, '/test/asd/qwe/test', :variable => ['asd', 'qwe']
112
- end
113
-
114
- def test_glob_with_variable
115
- assert_route '/test/*variable/:test', '/test/one/two/three', {:variable => ['one', 'two'], :test => 'three'}
116
- end
117
-
118
- def test_glob_with_format
119
- assert_route '/test/*variable.:format', 'test/one/two/three.html', {:variable => ['one', 'two', 'three'], :format => 'html'}
120
- end
121
-
122
- def test_glob_regexp_with_format
123
- r = router { add('/test/*variable.:format', :variable => /[a-z]+/, :format => 'html') }
124
- assert_route nil, 'test/one/2/three.html'
125
- assert_route nil, 'test/one/two/three.xml'
126
- assert_route r, 'test/one/two/three.html', {:variable => ['one', 'two', 'three'], :format => 'html'}
127
- end
128
-
129
- def test_glob_with_optional_format
130
- assert_route '/test/*variable(.:format)', 'test/one/two/three.html', {:variable => ['one', 'two', 'three'], :format => 'html'}
131
- end
132
-
133
- def test_glob_with_literal
134
- assert_route '/test/*variable.html', 'test/one/two/three.html', {:variable => ['one', 'two', 'three']}
135
- end
136
-
137
- def test_glob_with_regex
138
- r = router { add('/test/*variable/anymore')}
139
- assert_route r, '/test/123/345/567/anymore', {:variable => ['123', '345', '567']}
140
- assert_route nil, '/test/123/345/567'
141
- end
142
-
143
- def test_regex_and_greedy
144
- with_regex, with_post, without_regex = router {
145
- add("/:common_variable/:matched").matching(:matched => /\d+/)
146
- post("/:common_variable/:unmatched")
147
- add("/:common_variable/:unmatched")
148
- }
149
- assert_route with_regex, '/common/123', {:common_variable => 'common', :matched => '123'}
150
- assert_route without_regex, '/common/other', {:common_variable => 'common', :unmatched => 'other'}
151
- assert_route with_regex, Rack::MockRequest.env_for('/common/123', :method => 'POST'), {:common_variable => 'common', :matched => '123'}
152
- assert_route with_post, Rack::MockRequest.env_for('/common/other', :method => 'POST'), {:common_variable => 'common', :unmatched => 'other'}
153
- end
154
-
155
- if //.respond_to?(:names)
156
- eval "
157
- def test_match_path_with_groups
158
- r = router { add(%r{/(?<year>\\d{4})/(?<month>\\d{2})/(?<day>\\d{2})/?}) }
159
- assert_route r, '/1234/23/56', {:year => '1234', :month => '23', :day => '56'}
160
- end
161
- "
162
- end
163
- end