http_router 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,150 +0,0 @@
1
- require "sinatra"
2
- require "http_router/interface/sinatra"
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 TestRecognize < MiniTest::Unit::TestCase
13
-
14
- def setup
15
- @app = Sinatra.new { register HttpRouter::Interface::Sinatra::Extension }
16
- @app.extend(CallWithMockRequestMixin)
17
- @app.reset!
18
- end
19
-
20
- def test_basic
21
- response = @app.call_with_mock_request('/bar')
22
- assert_equal 404, response.status
23
- end
24
-
25
- def test_map_index
26
- @app.get("/") { "index" }
27
- response = @app.call_with_mock_request('/')
28
- assert_equal 200, response.status
29
- assert_equal "index", response.body
30
- end
31
-
32
- def test_trailing_slash
33
- @app.get("/foo") { "foo" }
34
- response = @app.call_with_mock_request('/foo')
35
- assert_equal 200, response.status
36
- assert_equal "foo", response.body
37
- response = @app.call_with_mock_request('/foo/')
38
- assert_equal 200, response.status
39
- assert_equal "foo", response.body
40
- end
41
-
42
- def test_trailing_slash2
43
- @app.get("/foo") { "foo" }
44
- @app.get("/foo/bar") { "bar" }
45
- response = @app.call_with_mock_request('/foo')
46
- assert_equal 200, response.status
47
- assert_equal "foo", response.body
48
- response = @app.call_with_mock_request('/foo/bar')
49
- assert_equal 200, response.status
50
- assert_equal "bar", response.body
51
- response = @app.call_with_mock_request('/foo/')
52
- assert_equal 200, response.status
53
- assert_equal "foo", response.body
54
- response = @app.call_with_mock_request('/foo/bar/')
55
- assert_equal 200, response.status
56
- assert_equal "bar", response.body
57
- end
58
-
59
- def test_trailing_slash_with_optional_param
60
- @app.get("/foo/(:bar)") { params[:bar] }
61
- @app.get("/bar(/:foo)") { params[:foo] }
62
- response = @app.call_with_mock_request('/foo/bar')
63
- assert_equal 200, response.status
64
- assert_equal "bar", response.body
65
- response = @app.call_with_mock_request('/bar/foo')
66
- assert_equal 200, response.status
67
- assert_equal "foo", response.body
68
- response = @app.call_with_mock_request('/bar')
69
- assert_equal 200, response.status
70
- assert_equal "", response.body
71
- response = @app.call_with_mock_request('/bar/')
72
- assert_equal 200, response.status
73
- assert_equal "", response.body
74
- end
75
-
76
- def test_trailing_question_mark
77
- @app.get("/foo/?") { "foo" }
78
- response = @app.call_with_mock_request('/foo')
79
- assert_equal 200, response.status
80
- assert_equal "foo", response.body
81
- response = @app.call_with_mock_request('/foo/')
82
- assert_equal 200, response.status
83
- assert_equal "foo", response.body
84
- end
85
-
86
- def test_map_basic
87
- @app.get('/hi', :name => :hi) { generate(:hi) }
88
- response = @app.call_with_mock_request('/hi')
89
- assert_equal 200, response.status
90
- assert_equal "/hi", response.body
91
- end
92
-
93
- def test_map_basic2
94
- @app.get('/hi', :name => :hi) { generate(:hi) }
95
- response = @app.call_with_mock_request('/hi/')
96
- assert_equal 200, response.status
97
- assert_equal "/hi", response.body
98
- end
99
-
100
- def test_map_param
101
- @app.get('/hi/:id', :name => :hi) { generate(:hi, :id => 18) }
102
- response = @app.call_with_mock_request('/hi/1')
103
- assert_equal 200, response.status
104
- assert_equal "/hi/18", response.body
105
- end
106
-
107
- def test_map_param2
108
- @app.get('/hi-:id', :name => :hi) { generate(:hi, :id => 18) }
109
- response = @app.call_with_mock_request('/hi-1')
110
- assert_equal 200, response.status
111
- assert_equal "/hi-18", response.body
112
- end
113
-
114
- def test_map_complex
115
- @app.get('/hi/:foo/:bar/:baz(.:format)') { "/#{params[:foo]}/#{params[:bar]}/#{params[:baz]}/#{params[:format]}" }
116
- response = @app.call_with_mock_request('/hi/foo/bar/baz')
117
- assert_equal 200, response.status
118
- assert_equal "/foo/bar/baz/", response.body
119
- response = @app.call_with_mock_request('/hi/foo/bar-bax/baz')
120
- assert_equal 200, response.status
121
- assert_equal "/foo/bar-bax/baz/", response.body
122
- end
123
-
124
- def test_map_regexp
125
- @app.get('/numbers/:digits', :matching => { :digits => /\d+/ }) { params[:digits] }
126
- response = @app.call_with_mock_request('/numbers/2010')
127
- assert_equal 200, response.status
128
- assert_equal "2010", response.body
129
- end
130
-
131
- def test_not_map_regex
132
- @app.get('/numbers/:digits', :matching => { :digits => /\d+/ }) { params[:digits] }
133
- response = @app.call_with_mock_request('/numbers/nan')
134
- assert_equal 404, response.status
135
- end
136
-
137
- def test_404
138
- response = @app.call_with_mock_request('/bar')
139
- assert_equal 404, response.status
140
- assert_match response.body, /Sinatra doesn't know this ditty/
141
- end
142
-
143
- def test_405
144
- @app.post('/bar') { 'found' }
145
- @app.put('/bar') { 'found' }
146
- response = @app.call_with_mock_request('/bar')
147
- assert_equal 405, response.status
148
- assert_equal ['POST', 'PUT'], response.headers['Allow'].split(/\s*,\s*/).sort
149
- end
150
- end