deas 0.31.0 → 0.32.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.
@@ -1,7 +1,10 @@
1
1
  require 'assert'
2
2
  require 'deas/redirect_proxy'
3
3
 
4
+ require 'deas/handler_proxy'
4
5
  require 'deas/test_helpers'
6
+ require 'deas/url'
7
+ require 'deas/view_handler'
5
8
 
6
9
  class Deas::RedirectProxy
7
10
 
@@ -12,11 +15,8 @@ class Deas::RedirectProxy
12
15
  end
13
16
  subject{ @proxy }
14
17
 
15
- should have_readers :handler_class_name, :handler_class
16
- should have_imeths :validate!
17
-
18
- should "know its handler class name" do
19
- assert_equal subject.handler_class.name, subject.handler_class_name
18
+ should "be a HandlerProxy" do
19
+ assert_kind_of Deas::HandlerProxy, subject
20
20
  end
21
21
 
22
22
  end
@@ -24,7 +24,7 @@ class Deas::RedirectProxy
24
24
  class HandlerClassTests < UnitTests
25
25
  include Deas::TestHelpers
26
26
 
27
- desc "redir handler class"
27
+ desc "handler class"
28
28
  setup do
29
29
  @handler_class = @proxy.handler_class
30
30
  end
@@ -58,7 +58,7 @@ class Deas::RedirectProxy
58
58
  end
59
59
 
60
60
  class HandlerTests < HandlerClassTests
61
- desc "redir handler instance"
61
+ desc "handler instance"
62
62
  setup do
63
63
  @handler = test_handler(@handler_class)
64
64
  end
@@ -1,7 +1,8 @@
1
1
  require 'assert'
2
2
  require 'deas/route_proxy'
3
3
 
4
- require 'deas/test_helpers'
4
+ require 'deas/exceptions'
5
+ require 'deas/handler_proxy'
5
6
  require 'test/support/view_handlers'
6
7
 
7
8
  class Deas::RouteProxy
@@ -13,13 +14,30 @@ class Deas::RouteProxy
13
14
  end
14
15
  subject{ @proxy }
15
16
 
16
- should have_readers :handler_class_name, :handler_class
17
- should have_imeths :validate!
17
+ should "be a HandlerProxy" do
18
+ assert_kind_of Deas::HandlerProxy, subject
19
+ end
20
+
21
+ should "complain if given a nil handler class name" do
22
+ assert_raises(Deas::NoHandlerClassError) do
23
+ Deas::RouteProxy.new(nil)
24
+ end
25
+ end
18
26
 
19
- should "know its handler class name" do
27
+ should "apply no view handler ns if none given" do
20
28
  assert_equal 'EmptyViewHandler', subject.handler_class_name
21
29
  end
22
30
 
31
+ should "apply an optional view handler ns if it is given" do
32
+ proxy = Deas::RouteProxy.new('NsTest', 'MyStuff')
33
+ assert_equal 'MyStuff::NsTest', proxy.handler_class_name
34
+ end
35
+
36
+ should "ignore the ns when given a class name with leading colons" do
37
+ proxy = Deas::RouteProxy.new('::NoNsTest', 'MyStuff')
38
+ assert_equal '::NoNsTest', proxy.handler_class_name
39
+ end
40
+
23
41
  should "set its handler class on `validate!`" do
24
42
  assert_nil subject.handler_class
25
43
 
@@ -1,7 +1,7 @@
1
1
  require 'assert'
2
2
  require 'deas/route'
3
3
 
4
- require 'deas/sinatra_runner'
4
+ require 'deas/exceptions'
5
5
  require 'deas/route_proxy'
6
6
  require 'test/support/fake_sinatra_call'
7
7
  require 'test/support/view_handlers'
@@ -11,32 +11,29 @@ class Deas::Route
11
11
  class UnitTests < Assert::Context
12
12
  desc "Deas::Route"
13
13
  setup do
14
- @route_proxy = Deas::RouteProxy.new('EmptyViewHandler')
15
- @route = Deas::Route.new(:get, '/test', @route_proxy)
14
+ @req_type_name = Factory.string
15
+ @proxy = HandlerProxySpy.new
16
+ @handler_proxies = Hash.new{ |h, k| raise(Deas::HandlerProxyNotFound) }.tap do |h|
17
+ h[@req_type_name] = @proxy
18
+ end
19
+
20
+ @route = Deas::Route.new(:get, '/test', @handler_proxies)
16
21
  end
17
22
  subject{ @route }
18
23
 
19
- should have_readers :method, :path, :route_proxy, :handler_class
24
+ should have_readers :method, :path
20
25
  should have_imeths :validate!, :run
21
26
 
22
- should "know its method, path and route proxy" do
27
+ should "know its method and path" do
23
28
  assert_equal :get, subject.method
24
29
  assert_equal '/test', subject.path
25
- assert_equal @route_proxy, subject.route_proxy
26
30
  end
27
31
 
28
- should "set its handler class on `validate!`" do
29
- assert_nil subject.handler_class
32
+ should "validate its proxies on validate" do
33
+ assert_false @proxy.validate_called
30
34
 
31
35
  assert_nothing_raised{ subject.validate! }
32
- assert_equal EmptyViewHandler, subject.handler_class
33
- end
34
-
35
- should "complain given an invalid handler class" do
36
- proxy = Deas::RouteProxy.new('SomethingNotDefined')
37
- assert_raises(Deas::NoHandlerClassError) do
38
- Deas::Route.new(:get, '/test', proxy).validate!
39
- end
36
+ assert_true @proxy.validate_called
40
37
  end
41
38
 
42
39
  end
@@ -45,81 +42,43 @@ class Deas::Route
45
42
  desc "when run"
46
43
  setup do
47
44
  @fake_sinatra_call = FakeSinatraCall.new
48
- @runner_spy = SinatraRunnerSpy.new
49
- Assert.stub(Deas::SinatraRunner, :new) do |*args|
50
- @runner_spy.build(*args)
51
- @runner_spy
52
- end
53
-
54
- @route.validate!
55
- @route.run(@fake_sinatra_call)
56
- end
57
-
58
- should "build and run a sinatra runner" do
59
- assert_equal subject.handler_class, @runner_spy.handler_class
60
-
61
- exp_args = {
62
- :sinatra_call => @fake_sinatra_call,
63
- :request => @fake_sinatra_call.request,
64
- :response => @fake_sinatra_call.response,
65
- :session => @fake_sinatra_call.session,
66
- :params => @fake_sinatra_call.params,
67
- :logger => @fake_sinatra_call.settings.logger,
68
- :router => @fake_sinatra_call.settings.router,
69
- :template_source => @fake_sinatra_call.settings.template_source
70
- }
71
- assert_equal exp_args, @runner_spy.args
72
-
73
- assert_true @runner_spy.run_called
74
45
  end
75
46
 
76
- should "add the runner params to the request env" do
77
- exp = @runner_spy.params
78
- assert_equal exp, @fake_sinatra_call.request.env['deas.params']
79
- end
47
+ should "run the proxy for the given request type name" do
48
+ Assert.stub(@fake_sinatra_call.settings.router, :request_type_name).with(
49
+ @fake_sinatra_call.request
50
+ ){ @req_type_name }
80
51
 
81
- should "add the handler class name to the request env" do
82
- exp = subject.handler_class.name
83
- assert_equal exp, @fake_sinatra_call.request.env['deas.handler_class_name']
52
+ @route.run(@fake_sinatra_call)
53
+ assert_true @proxy.run_called
84
54
  end
85
55
 
86
- should "log the handler and params" do
87
- exp_msgs = [
88
- " Handler: #{subject.handler_class}",
89
- " Params: #{@runner_spy.params.inspect}"
90
- ]
91
- assert_equal exp_msgs, @fake_sinatra_call.request.logging_msgs
56
+ should "halt 404 if it can't find a proxy for the given request type name" do
57
+ halt_value = catch(:halt) do
58
+ @route.run(@fake_sinatra_call)
59
+ end
60
+ assert_equal [404], halt_value
92
61
  end
93
62
 
94
63
  end
95
64
 
96
- class SinatraRunnerSpy
65
+ class HandlerProxySpy
97
66
 
98
- attr_reader :run_called
99
- attr_reader :handler_class, :args
100
- attr_reader :sinatra_call
101
- attr_reader :request, :response, :session, :params
102
- attr_reader :logger, :router, :template_source
67
+ attr_reader :validate_called, :run_called, :sinatra_call
103
68
 
104
69
  def initialize
105
- @run_called = false
70
+ @run_called = false
71
+ @validate_called = false
72
+ @sinatra_call = nil
106
73
  end
107
74
 
108
- def build(handler_class, args)
109
- @handler_class, @args = handler_class, args
110
-
111
- @sinatra_call = args[:sinatra_call]
112
- @request = args[:request]
113
- @response = args[:response]
114
- @session = args[:session]
115
- @params = args[:params]
116
- @logger = args[:logger]
117
- @router = args[:router]
118
- @template_source = args[:template_source]
75
+ def validate!
76
+ @validate_called = true
119
77
  end
120
78
 
121
- def run
122
- @run_called = true
79
+ def run(sinatra_call)
80
+ @sinatra_call = sinatra_call
81
+ @run_called = true
123
82
  end
124
83
 
125
84
  end
@@ -1,154 +1,222 @@
1
1
  require 'assert'
2
2
  require 'deas/router'
3
3
 
4
+ require 'deas/exceptions'
5
+ require 'deas/route'
6
+ require 'test/support/view_handlers'
7
+
4
8
  class Deas::Router
5
9
 
6
10
  class UnitTests < Assert::Context
7
11
  desc "Deas::Router"
8
12
  setup do
9
- @router = Deas::Router.new
13
+ @router_class = Deas::Router
14
+ end
15
+ subject{ @router_class }
16
+
17
+ should "know its default request type name" do
18
+ assert_equal 'default', subject::DEFAULT_REQUEST_TYPE_NAME
19
+ end
20
+
21
+ end
22
+
23
+ class InitTests < UnitTests
24
+ desc "when init"
25
+ setup do
26
+ @router = @router_class.new
10
27
  end
11
28
  subject{ @router }
12
29
 
13
- should have_accessors :urls, :routes
30
+ should have_readers :request_types, :urls, :routes
31
+
14
32
  should have_imeths :view_handler_ns, :base_url, :prepend_base_url
15
33
  should have_imeths :url, :url_for
34
+ should have_imeths :default_request_type_name, :add_request_type
35
+ should have_imeths :request_type_name
16
36
  should have_imeths :get, :post, :put, :patch, :delete
17
37
  should have_imeths :route, :redirect
18
38
 
19
- should "have no view_handler_ns, base_url, urls, or routes by default" do
39
+ should "default its settings" do
20
40
  assert_nil subject.view_handler_ns
21
41
  assert_nil subject.base_url
42
+ assert_empty subject.request_types
22
43
  assert_empty subject.urls
23
44
  assert_empty subject.routes
45
+
46
+ exp = @router_class::DEFAULT_REQUEST_TYPE_NAME
47
+ assert_equal exp, subject.default_request_type_name
24
48
  end
25
49
 
26
- should "add a GET route using #get" do
27
- subject.get('/things', 'ListThings')
50
+ should "set a view handler namespace" do
51
+ subject.view_handler_ns(exp = Factory.string)
52
+ assert_equal exp, subject.view_handler_ns
53
+ end
28
54
 
29
- route = subject.routes[0]
30
- assert_instance_of Deas::Route, route
31
- assert_equal :get, route.method
32
- assert_equal '/things', route.path
33
- assert_equal 'ListThings', route.route_proxy.handler_class_name
55
+ should "set a base url" do
56
+ subject.base_url(exp = Factory.url)
57
+ assert_equal exp, subject.base_url
34
58
  end
35
59
 
36
- should "add a POST route using #post" do
37
- subject.post('/things', 'CreateThing')
60
+ should "prepend the base url to any url path" do
61
+ url_path = Factory.path
62
+ base_url = Factory.url
38
63
 
39
- route = subject.routes[0]
40
- assert_instance_of Deas::Route, route
41
- assert_equal :post, route.method
42
- assert_equal '/things', route.path
43
- assert_equal 'CreateThing', route.route_proxy.handler_class_name
64
+ assert_equal url_path, subject.prepend_base_url(url_path)
65
+
66
+ subject.base_url base_url
67
+ assert_equal "#{base_url}#{url_path}", subject.prepend_base_url(url_path)
44
68
  end
45
69
 
46
- should "add a PUT route using #put" do
47
- subject.put('/things/:id', 'UpdateThing')
70
+ should "prepend the base url when adding routes" do
71
+ url = Factory.url
72
+ subject.base_url url
73
+ path = Factory.path
74
+ route = subject.get(path, Object)
48
75
 
49
- route = subject.routes[0]
50
- assert_instance_of Deas::Route, route
51
- assert_equal :put, route.method
52
- assert_equal '/things/:id', route.path
53
- assert_equal 'UpdateThing', route.route_proxy.handler_class_name
76
+ exp_path = subject.prepend_base_url(path)
77
+ assert_equal exp_path, route.path
54
78
  end
55
79
 
56
- should "add a PATCH route using #patch" do
57
- subject.patch('/things/:id', 'UpdateThing')
80
+ should "prepend the base url when adding redirects" do
81
+ url = Factory.url
82
+ subject.base_url url
83
+ path = Factory.path
84
+ redirect = subject.redirect(path, Factory.path)
58
85
 
59
- route = subject.routes[0]
60
- assert_instance_of Deas::Route, route
61
- assert_equal :patch, route.method
62
- assert_equal '/things/:id', route.path
63
- assert_equal 'UpdateThing', route.route_proxy.handler_class_name
86
+ exp_path = subject.prepend_base_url(path)
87
+ assert_equal exp_path, redirect.path
64
88
  end
65
89
 
66
- should "add a DELETE route using #delete" do
67
- subject.delete('/things/:id', 'DeleteThing')
90
+ should "set a default request type name" do
91
+ subject.default_request_type_name(exp = Factory.string)
92
+ assert_equal exp, subject.default_request_type_name
93
+ end
68
94
 
69
- route = subject.routes[0]
70
- assert_instance_of Deas::Route, route
71
- assert_equal :delete, route.method
72
- assert_equal '/things/:id', route.path
73
- assert_equal 'DeleteThing', route.route_proxy.handler_class_name
95
+ should "add request types" do
96
+ assert_empty subject.request_types
97
+
98
+ name, proc = Factory.string, Proc.new{}
99
+ subject.add_request_type(name, &proc)
100
+ assert_not_empty subject.request_types
101
+
102
+ rt = subject.request_types.last
103
+ assert_equal name, rt.name
104
+ assert_equal proc, rt.proc
74
105
  end
75
106
 
76
- should "allow defining any kind of route using #route" do
77
- subject.route(:options, '/get_info', 'GetInfo')
107
+ should "lookup request type names" do
108
+ request = Factory.string
109
+ name = Factory.string
110
+ proc = Proc.new{ |r| r == request }
111
+ subject.add_request_type(name, &proc)
112
+ subject.add_request_type(Factory.string, &proc)
78
113
 
79
- route = subject.routes[0]
80
- assert_instance_of Deas::Route, route
81
- assert_equal :options, route.method
82
- assert_equal '/get_info', route.path
83
- assert_equal 'GetInfo', route.route_proxy.handler_class_name
114
+ exp = name
115
+ assert_equal exp, subject.request_type_name(request)
116
+
117
+ exp = subject.default_request_type_name
118
+ assert_equal exp, subject.request_type_name(Factory.string)
84
119
  end
85
120
 
86
- should "set a view handler namespace and use it when defining routes" do
87
- subject.view_handler_ns 'MyStuff'
88
- assert_equal 'MyStuff', subject.view_handler_ns
121
+ should "add get, post, put, patch and delete routes" do
122
+ Assert.stub(subject, :route){ |*args| RouteSpy.new(*args) }
123
+ path = Factory.path
124
+ args = [Factory.string]
125
+
126
+ [:get, :post, :put, :patch, :delete].each do |meth|
127
+ route = subject.send(meth, path, *args)
128
+ assert_equal meth, route.method
129
+ assert_equal path, route.path
130
+ assert_equal args, route.args
131
+ end
132
+ end
89
133
 
90
- # should use the ns
91
- route = subject.route(:get, '/ns_test', 'NsTest')
92
- assert_equal 'MyStuff::NsTest', route.route_proxy.handler_class_name
134
+ should "instance eval any given block" do
135
+ ns = Factory.string
136
+ router = Deas::Router.new do
137
+ view_handler_ns ns
138
+ end
93
139
 
94
- # should ignore the ns when the leading colons are present
95
- route = subject.route(:post, '/no_ns_test', '::NoNsTest')
96
- assert_equal '::NoNsTest', route.route_proxy.handler_class_name
140
+ assert_equal ns, router.view_handler_ns
97
141
  end
98
142
 
99
- should "set a base url" do
100
- url = Factory.url
101
- subject.base_url url
143
+ end
102
144
 
103
- assert_equal url, subject.base_url
145
+ class RouteTests < InitTests
146
+ setup do
147
+ @method = Factory.string
148
+ @path1 = Factory.path
149
+ @path2 = Factory.path
150
+ @handler_class_name1 = Factory.string
151
+ @handler_class_name2 = Factory.string
104
152
  end
105
153
 
106
- should "prepend the base url to any url path" do
107
- url_path = Factory.path
108
- base_url = Factory.url
154
+ should "add a Route with the given method and path" do
155
+ assert_empty subject.routes
109
156
 
110
- assert_equal url_path, subject.prepend_base_url(url_path)
157
+ subject.route(@method, @path1)
158
+ assert_not_empty subject.routes
111
159
 
112
- subject.base_url base_url
113
- assert_equal "#{base_url}#{url_path}", subject.prepend_base_url(url_path)
160
+ route = subject.routes.last
161
+ assert_instance_of Deas::Route, route
162
+ assert_equal @method, route.method
163
+ assert_equal @path1, route.path
164
+
165
+ proxies = route.handler_proxies
166
+ assert_kind_of HandlerProxies, proxies
167
+ assert_empty proxies
168
+ assert_equal subject.default_request_type_name, proxies.default_type
114
169
  end
115
170
 
116
- should "prepend the base url when adding routes" do
117
- url = Factory.url
118
- subject.base_url url
119
- path = Factory.path
120
- route = subject.get(path, Object)
171
+ should "proxy any handler class given for the default request type" do
172
+ subject.route(@method, @path1, @handler_class_name1)
173
+ route = subject.routes.last
174
+ proxy = route.handler_proxies[subject.default_request_type_name]
175
+ assert_kind_of Deas::RouteProxy, proxy
176
+ assert_equal @handler_class_name1, proxy.handler_class_name
121
177
 
122
- exp_path = subject.prepend_base_url(path)
123
- assert_equal exp_path, route.path
178
+ subject.route(@method, @path1, @handler_class_name1, {
179
+ subject.default_request_type_name => @handler_class_name2
180
+ })
181
+ route = subject.routes.last
182
+ proxy = route.handler_proxies[subject.default_request_type_name]
183
+ assert_kind_of Deas::RouteProxy, proxy
184
+ assert_not_nil proxy
185
+ assert_equal @handler_class_name2, proxy.handler_class_name
124
186
  end
125
187
 
126
- should "add a redirect route using #redirect" do
127
- subject.redirect('/invalid', '/assets')
188
+ should "proxy handler classes for their specified request types" do
189
+ subject.route(@method, @path1, {
190
+ '1' => @handler_class_name1,
191
+ '2' => @handler_class_name2,
192
+ })
193
+ route = subject.routes.last
128
194
 
129
- route = subject.routes[0]
130
- assert_instance_of Deas::Route, route
131
- assert_equal :get, route.method
132
- assert_equal '/invalid', route.path
133
- assert_equal 'Deas::RedirectHandler', route.route_proxy.handler_class_name
195
+ proxy = route.handler_proxies['1']
196
+ assert_kind_of Deas::RouteProxy, proxy
197
+ assert_equal @handler_class_name1, proxy.handler_class_name
134
198
 
135
- route.validate!
136
- assert_not_nil route.handler_class
199
+ proxy = route.handler_proxies['2']
200
+ assert_kind_of Deas::RouteProxy, proxy
201
+ assert_equal @handler_class_name2, proxy.handler_class_name
137
202
  end
138
203
 
139
- should "instance eval any given block" do
140
- router = Deas::Router.new do
141
- get('/things', 'ListThings')
142
- end
204
+ should "add redirect routes" do
205
+ subject.redirect(@path1, @path2)
143
206
 
144
- assert_equal 1, router.routes.size
145
- assert_instance_of Deas::Route, router.routes.first
207
+ route = subject.routes.last
208
+ assert_instance_of Deas::Route, route
209
+ assert_equal :get, route.method
210
+ assert_equal @path1, route.path
211
+
212
+ proxy = route.handler_proxies[subject.default_request_type_name]
213
+ assert_kind_of Deas::RedirectProxy, proxy
214
+ assert_equal 'Deas::RedirectHandler', proxy.handler_class_name
146
215
  end
147
216
 
148
217
  end
149
218
 
150
- class NamedUrlTests < UnitTests
151
- desc "when using named urls"
219
+ class NamedUrlTests < InitTests
152
220
  setup do
153
221
  @router.url('get_info', '/info/:for')
154
222
  end
@@ -220,4 +288,63 @@ class Deas::Router
220
288
 
221
289
  end
222
290
 
291
+ class HandlerProxiesTests < UnitTests
292
+ desc "HandlerProxies"
293
+ setup do
294
+ @default_type = Factory.string
295
+ @other_type = Factory.string
296
+ @proxies = {
297
+ @default_type => Factory.string,
298
+ @other_type => Factory.string
299
+ }
300
+ @handler_proxies = HandlerProxies.new(@proxies, @default_type)
301
+ end
302
+ subject{ @handler_proxies }
303
+
304
+ should have_reader :default_type
305
+ should have_imeths :[], :each, :empty?
306
+
307
+ should "know its default type" do
308
+ assert_equal @default_type, subject.default_type
309
+ end
310
+
311
+ should "find the proxy for the given type" do
312
+ assert_equal @proxies[@other_type], subject[@other_type]
313
+ end
314
+
315
+ should "find the default proxy if there is no proxy for the given type" do
316
+ assert_equal @proxies[subject.default_type], subject[Factory.string]
317
+ end
318
+
319
+ should "complain if there is no proxy for the given type and no default proxy" do
320
+ handler_proxies = HandlerProxies.new({}, @default_type)
321
+ assert_raises(Deas::HandlerProxyNotFound) do
322
+ handler_proxies[Factory.string]
323
+ end
324
+ end
325
+
326
+ should "demeter its given proxies each method" do
327
+ exp = ''
328
+ @proxies.each{ |k, v| exp << v }
329
+ act = ''
330
+ subject.each{ |k, v| act << v }
331
+
332
+ assert_equal exp, act
333
+ end
334
+
335
+ should "demeter its given proxies empty? method" do
336
+ Assert.stub(@proxies, :empty?){ false }
337
+ assert_false subject.empty?
338
+
339
+ Assert.stub(@proxies, :empty?){ true }
340
+ assert_true subject.empty?
341
+ end
342
+
343
+ end
344
+
345
+ class RouteSpy < Struct.new(:method, :path, :args)
346
+ def initialize(method, path, *args)
347
+ super(method, path, args)
348
+ end
349
+ end
223
350
  end