deas 0.42.0 → 0.43.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,23 +38,19 @@ class Deas::HandlerProxy
38
38
 
39
39
  Assert.stub(@proxy, :handler_class){ EmptyViewHandler }
40
40
 
41
- @server_data = Factory.server_data
42
- @fake_sinatra_call = Factory.sinatra_call
43
- @splat_sym_param = Factory.string
44
- @splat_string_param = Factory.string
45
- @fake_sinatra_call.params = {
46
- :splat => [@splat_sym_param],
47
- 'splat' => [@splat_string_param],
41
+ @server_data = Factory.server_data
42
+ @request_data = Factory.request_data(:params => {
43
+ :splat => [Factory.string],
44
+ 'splat' => [Factory.string],
48
45
  :captures => [Factory.string],
49
46
  'captures' => [Factory.string]
50
- }
51
-
52
- @proxy.run(@server_data, @fake_sinatra_call)
47
+ })
48
+ @proxy.run(@server_data, @request_data)
53
49
  end
54
50
 
55
51
  should "remove any 'splat' or 'captures' params added by Sinatra's router" do
56
52
  [:splat, 'splat', :captures, 'captures'].each do |param_name|
57
- assert_nil @fake_sinatra_call.params[param_name]
53
+ assert_nil @request_data.params[param_name]
58
54
  end
59
55
  end
60
56
 
@@ -65,44 +61,40 @@ class Deas::HandlerProxy
65
61
  :logger => @server_data.logger,
66
62
  :router => @server_data.router,
67
63
  :template_source => @server_data.template_source,
68
- :request => @fake_sinatra_call.request,
69
- :session => @fake_sinatra_call.session,
70
- :params => @fake_sinatra_call.params,
71
- :splat => @splat_sym_param
64
+ :request => @request_data.request,
65
+ :params => @request_data.params,
66
+ :route_path => @request_data.route_path
72
67
  }
73
68
  assert_equal exp_args, @runner_spy.args
74
69
 
75
70
  assert_true @runner_spy.run_called
76
71
  end
77
72
 
78
- should "prefer splat sym params over splat string params" do
79
- assert_equal @splat_sym_param, @runner_spy.args[:splat]
80
-
81
- @fake_sinatra_call.params['splat'] = [@splat_string_param]
82
- proxy = Deas::HandlerProxy.new('EmptyViewHandler')
83
- Assert.stub(proxy, :handler_class){ EmptyViewHandler }
84
- proxy.run(@server_data, @fake_sinatra_call)
85
- assert_equal @splat_string_param, @runner_spy.args[:splat]
86
- end
87
-
88
73
  should "add data to the request env to make it available to Rack" do
89
74
  exp = subject.handler_class
90
- assert_equal exp, @fake_sinatra_call.request.env['deas.handler_class']
75
+ assert_equal exp, @request_data.request.env['deas.handler_class']
91
76
 
92
77
  exp = @runner_spy.handler
93
- assert_equal exp, @fake_sinatra_call.request.env['deas.handler']
78
+ assert_equal exp, @request_data.request.env['deas.handler']
94
79
 
95
80
  exp = @runner_spy.params
96
- assert_equal exp, @fake_sinatra_call.request.env['deas.params']
81
+ assert_equal exp, @request_data.request.env['deas.params']
82
+
83
+ exp = @runner_spy.splat
84
+ assert_equal exp, @request_data.request.env['deas.splat']
85
+
86
+ exp = @runner_spy.route_path
87
+ assert_equal exp, @request_data.request.env['deas.route_path']
97
88
  end
98
89
 
99
90
  should "log the handler class name and the params" do
100
91
  exp_msgs = [
101
92
  " Handler: #{subject.handler_class.name}",
102
93
  " Params: #{@runner_spy.params.inspect}",
103
- " Splat: #{@runner_spy.splat.inspect}"
94
+ " Splat: #{@runner_spy.splat.inspect}",
95
+ " Route: #{@runner_spy.route_path.inspect}"
104
96
  ]
105
- assert_equal exp_msgs, @fake_sinatra_call.request.logging_msgs
97
+ assert_equal exp_msgs, @request_data.request.logging_msgs
106
98
  end
107
99
 
108
100
  end
@@ -112,7 +104,7 @@ class Deas::HandlerProxy
112
104
  attr_reader :run_called
113
105
  attr_reader :handler_class, :handler, :args
114
106
  attr_reader :logger, :router, :template_source
115
- attr_reader :request, :session, :params, :splat
107
+ attr_reader :request, :params, :splat, :route_path
116
108
 
117
109
  def initialize
118
110
  @run_called = false
@@ -127,9 +119,12 @@ class Deas::HandlerProxy
127
119
  @router = args[:router]
128
120
  @template_source = args[:template_source]
129
121
  @request = args[:request]
130
- @session = args[:session]
131
122
  @params = args[:params]
132
- @splat = args[:splat]
123
+ @route_path = args[:route_path]
124
+
125
+ # runners parse the splat value from the PATH_INFO and route path. just
126
+ # use a placeholder value for the spy.
127
+ @splat = Factory.string
133
128
  end
134
129
 
135
130
  def run
@@ -7,11 +7,11 @@ module Deas::Logging
7
7
  desc "Deas::Logging"
8
8
  subject{ Deas::Logging }
9
9
 
10
- should have_imeths :middleware
10
+ should have_imeths :middleware_args
11
11
 
12
- should "return a middleware class given a verbose flag" do
13
- assert_equal Deas::VerboseLogging, subject.middleware(true)
14
- assert_equal Deas::SummaryLogging, subject.middleware(false)
12
+ should "return middleware args given a verbose flag" do
13
+ assert_equal [Deas::VerboseLogging], subject.middleware_args(true)
14
+ assert_equal [Deas::SummaryLogging], subject.middleware_args(false)
15
15
  end
16
16
 
17
17
  end
@@ -205,9 +205,11 @@ module Deas::Logging
205
205
  desc "when init"
206
206
  setup do
207
207
  @params = { Factory.string => Factory.string }
208
+ @splat = Factory.string
208
209
  @handler_class = TestHandler
209
210
  @env.merge!({
210
211
  'deas.params' => @params,
212
+ 'deas.splat' => @splat,
211
213
  'deas.handler_class' => @handler_class
212
214
  })
213
215
 
@@ -237,6 +239,7 @@ module Deas::Logging
237
239
  'method' => @env['REQUEST_METHOD'],
238
240
  'path' => @env['PATH_INFO'],
239
241
  'params' => @env['deas.params'],
242
+ 'splat' => @env['deas.splat'],
240
243
  'time' => @env['deas.time_taken'],
241
244
  'status' => @resp_status,
242
245
  'handler' => @handler_class.name,
@@ -253,6 +256,7 @@ module Deas::Logging
253
256
  'method' => @env['REQUEST_METHOD'],
254
257
  'path' => @env['PATH_INFO'],
255
258
  'params' => @env['deas.params'],
259
+ 'splat' => @env['deas.splat'],
256
260
  'time' => @env['deas.time_taken'],
257
261
  'status' => @resp_status,
258
262
  'redir' => @resp_headers['Location']
@@ -268,6 +272,7 @@ module Deas::Logging
268
272
  'method' => @env['REQUEST_METHOD'],
269
273
  'path' => @env['PATH_INFO'],
270
274
  'params' => @env['deas.params'],
275
+ 'splat' => @env['deas.splat'],
271
276
  'time' => @env['deas.time_taken'],
272
277
  'status' => @resp_status,
273
278
  'handler' => @handler_class.name,
@@ -282,7 +287,7 @@ module Deas::Logging
282
287
  subject{ Deas::SummaryLine }
283
288
 
284
289
  should "output its attributes in a specific order" do
285
- assert_equal %w{time status method path handler params redir}, subject.keys
290
+ assert_equal %w{time status method path handler params splat redir}, subject.keys
286
291
  end
287
292
 
288
293
  should "output its attributes in a single line" do
@@ -293,6 +298,7 @@ module Deas::Logging
293
298
  'path' => 'pth',
294
299
  'handler' => 'h',
295
300
  'params' => 'p',
301
+ 'splat' => 'spl',
296
302
  'redir' => 'r'
297
303
  }
298
304
  exp_line = "time=\"t\" "\
@@ -301,6 +307,7 @@ module Deas::Logging
301
307
  "path=\"pth\" "\
302
308
  "handler=\"h\" "\
303
309
  "params=\"p\" "\
310
+ "splat=\"spl\" "\
304
311
  "redir=\"r\""
305
312
  assert_equal exp_line, subject.new(line_attrs)
306
313
  end
@@ -0,0 +1,57 @@
1
+ require 'assert'
2
+ require 'deas/request_data'
3
+
4
+ class Deas::RequestData
5
+
6
+ class UnitTests < Assert::Context
7
+ desc "Deas::RequestData"
8
+ setup do
9
+ @request = Factory.string
10
+ @response = Factory.string
11
+ @params = Factory.string
12
+ @route_path = Factory.string
13
+
14
+ @request_data = Deas::RequestData.new({
15
+ :request => @request,
16
+ :response => @response,
17
+ :params => @params,
18
+ :route_path => @route_path
19
+ })
20
+ end
21
+ subject{ @request_data }
22
+
23
+ should have_readers :request, :response, :params, :route_path
24
+
25
+ should "know its attributes" do
26
+ assert_equal @request, subject.request
27
+ assert_equal @response, subject.response
28
+ assert_equal @params, subject.params
29
+ assert_equal @route_path, subject.route_path
30
+ end
31
+
32
+ should "default its attributes when they aren't provided" do
33
+ request_data = Deas::RequestData.new({})
34
+
35
+ assert_nil request_data.request
36
+ assert_nil request_data.response
37
+ assert_nil request_data.params
38
+ assert_nil request_data.route_path
39
+ end
40
+
41
+ should "know if it is equal to another request data" do
42
+ request_data = Deas::RequestData.new({
43
+ :request => @request,
44
+ :response => @response,
45
+ :params => @params,
46
+ :route_path => @route_path
47
+ })
48
+ assert_equal request_data, subject
49
+
50
+ request_data = Deas::RequestData.new({})
51
+ assert_not_equal request_data, subject
52
+ end
53
+
54
+
55
+ end
56
+
57
+ end
@@ -10,8 +10,8 @@ class Deas::Route
10
10
  class UnitTests < Assert::Context
11
11
  desc "Deas::Route"
12
12
  setup do
13
- @req_type_name = Factory.string
14
- @proxy = HandlerProxySpy.new
13
+ @req_type_name = Factory.string
14
+ @proxy = HandlerProxySpy.new
15
15
  @handler_proxies = Hash.new{ |h, k| raise(Deas::HandlerProxyNotFound) }.tap do |h|
16
16
  h[@req_type_name] = @proxy
17
17
  end
@@ -40,46 +40,46 @@ class Deas::Route
40
40
  class RunTests < UnitTests
41
41
  desc "when run"
42
42
  setup do
43
- @server_data = Factory.server_data
44
- @fake_sinatra_call = Factory.sinatra_call
43
+ @server_data = Factory.server_data
44
+ @request_data = Factory.request_data
45
45
  end
46
46
 
47
47
  should "run the proxy for the given request type name" do
48
48
  Assert.stub(@server_data.router, :request_type_name).with(
49
- @fake_sinatra_call.request
49
+ @request_data.request
50
50
  ){ @req_type_name }
51
51
 
52
- @route.run(@server_data, @fake_sinatra_call)
52
+ @route.run(@server_data, @request_data)
53
53
  assert_true @proxy.run_called
54
+ assert_equal @server_data, @proxy.server_data
55
+ assert_equal @request_data, @proxy.request_data
54
56
  end
55
57
 
56
58
  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(@server_data, @fake_sinatra_call)
59
- end
60
- assert_equal [404], halt_value
59
+ exp = [404, Rack::Utils::HeaderHash.new, []]
60
+ assert_equal exp, @route.run(@server_data, @request_data)
61
61
  end
62
62
 
63
63
  end
64
64
 
65
65
  class HandlerProxySpy
66
66
 
67
- attr_reader :validate_called, :run_called, :server_data, :sinatra_call
67
+ attr_reader :validate_called, :run_called, :server_data, :request_data
68
68
 
69
69
  def initialize
70
70
  @run_called = false
71
71
  @validate_called = false
72
72
  @server_data = nil
73
- @sinatra_call = nil
73
+ @request_data = nil
74
74
  end
75
75
 
76
76
  def validate!
77
77
  @validate_called = true
78
78
  end
79
79
 
80
- def run(server_data, sinatra_call)
81
- @server_data = sinatra_call
82
- @sinatra_call = sinatra_call
80
+ def run(server_data, request_data)
81
+ @server_data = server_data
82
+ @request_data = request_data
83
83
  @run_called = true
84
84
  end
85
85
 
@@ -43,7 +43,7 @@ class Deas::Router
43
43
  should have_imeths :route, :redirect, :not_found
44
44
  should have_imeths :apply_definitions!, :validate!
45
45
 
46
- should "default its settings" do
46
+ should "default its attrs" do
47
47
  router = @router_class.new
48
48
  assert_nil router.view_handler_ns
49
49
  assert_nil router.base_url
@@ -370,11 +370,40 @@ class Deas::Router
370
370
  assert_equal [404, {}, body], handler.halt_args
371
371
  end
372
372
 
373
+ should "complain if adding a route with invalid splats in its path" do
374
+ [ "/something/other*/",
375
+ "/something/other*",
376
+ "/something/*other",
377
+ "/something*/other",
378
+ "/*something/other",
379
+ "/something/*/other/*",
380
+ "/something/*/other",
381
+ "/something/*/",
382
+ "/*/something",
383
+ ].each do |path|
384
+ assert_raises InvalidSplatError do
385
+ router = @router_class.new
386
+ router.route(:get, path)
387
+ router.apply_definitions!
388
+ end
389
+ end
390
+
391
+ [ "/something/*",
392
+ "/*"
393
+ ].each do |path|
394
+ assert_nothing_raised do
395
+ router = @router_class.new
396
+ router.route(:get, path)
397
+ router.apply_definitions!
398
+ end
399
+ end
400
+ end
401
+
373
402
  end
374
403
 
375
404
  class NamedUrlTests < InitTests
376
405
  setup do
377
- @router.url('get_info', '/info/:for')
406
+ @router.url(:get_info, '/info/:for')
378
407
  end
379
408
 
380
409
  should "define a url given a name and a path" do
@@ -388,38 +417,23 @@ class Deas::Router
388
417
  end
389
418
 
390
419
  should "define a url with a custom escape query value proc" do
391
- name = Factory.string
420
+ name = Factory.string.to_sym
392
421
  escape_proc = proc{ Factory.string }
393
422
  @router.url(name, Factory.path, :escape_query_value => escape_proc)
394
423
 
395
- url = subject.urls[name.to_sym]
424
+ url = subject.urls[name]
396
425
  assert_equal escape_proc, url.escape_query_value_proc
397
426
  end
398
427
 
399
- should "complain if defining a url with a non-string path" do
428
+ should "complain if defining a url with a non-symbol name" do
400
429
  assert_raises ArgumentError do
401
- subject.url(:get_info, /^\/info/)
430
+ subject.url('get_info', '/info')
402
431
  end
403
432
  end
404
433
 
405
- should "complain if defining a url with invalid splats" do
406
- assert_raises ArgumentError do
407
- subject.url(:get_info, "/something/*/other/*")
408
- end
409
- assert_raises ArgumentError do
410
- subject.url(:get_info, "/something/*/other")
411
- end
412
- assert_raises ArgumentError do
413
- subject.url(:get_info, "/something/*/")
414
- end
434
+ should "complain if defining a url with a non-string path" do
415
435
  assert_raises ArgumentError do
416
- subject.url(:get_info, "/*/something")
417
- end
418
- assert_nothing_raised do
419
- subject.url(:get_info, "/something/*")
420
- end
421
- assert_nothing_raised do
422
- subject.url(:get_info, "/*")
436
+ subject.url(:get_info, /^\/info/)
423
437
  end
424
438
  end
425
439
 
@@ -510,7 +524,7 @@ class Deas::Router
510
524
  url = Factory.url
511
525
  subject.base_url url
512
526
  path = Factory.path
513
- subject.url('base_get_info', path)
527
+ subject.url(:base_get_info, path)
514
528
 
515
529
  exp_path = subject.prepend_base_url(path)
516
530
  assert_equal exp_path, subject.url_for(:base_get_info)
@@ -45,7 +45,7 @@ class Deas::Runner
45
45
 
46
46
  should have_readers :handler_class, :handler
47
47
  should have_readers :logger, :router, :template_source
48
- should have_readers :request, :session, :params, :splat
48
+ should have_readers :request, :params, :route_path, :splat
49
49
  should have_imeths :run, :to_rack
50
50
  should have_imeths :status, :headers, :body, :content_type
51
51
  should have_imeths :halt, :redirect, :send_file
@@ -63,20 +63,21 @@ class Deas::Runner
63
63
  assert_kind_of Deas::NullTemplateSource, runner.template_source
64
64
 
65
65
  assert_nil runner.request
66
- assert_nil runner.session
67
66
 
68
- assert_equal({}, runner.params)
67
+ assert_equal Hash.new, runner.params
68
+ assert_equal '', runner.route_path
69
+
70
+ assert_nil runner.splat
69
71
  end
70
72
 
71
73
  should "know its attrs" do
72
74
  args = {
73
- :logger => 'a-logger',
74
- :router => 'a-router',
75
- :template_source => 'a-source',
76
- :request => 'a-request',
77
- :session => 'a-session',
78
- :params => {},
79
- :splat => 'a-splat'
75
+ :logger => Factory.string,
76
+ :router => Factory.string,
77
+ :template_source => Factory.string,
78
+ :request => Factory.request,
79
+ :params => { Factory.string => Factory.string },
80
+ :route_path => Factory.string
80
81
  }
81
82
 
82
83
  runner = @runner_class.new(@handler_class, args)
@@ -85,9 +86,62 @@ class Deas::Runner
85
86
  assert_equal args[:router], runner.router
86
87
  assert_equal args[:template_source], runner.template_source
87
88
  assert_equal args[:request], runner.request
88
- assert_equal args[:session], runner.session
89
89
  assert_equal args[:params], runner.params
90
- assert_equal args[:splat], runner.splat
90
+ assert_equal args[:route_path], runner.route_path
91
+ end
92
+
93
+ should "know its splat value" do
94
+ route_path = [
95
+ '/some/:value/other/:value/*',
96
+ '/some/:value/*'
97
+ ].sample
98
+
99
+ params = { 'value' => Factory.string }
100
+ splat = Factory.integer(3).times.map{ Factory.string}.join('/')
101
+ path_info = route_path.gsub(':value', params['value']).sub('*', splat)
102
+ request_env = { 'PATH_INFO' => path_info }
103
+
104
+ args = {
105
+ :request => Factory.request(:env => request_env),
106
+ :params => params,
107
+ :route_path => route_path
108
+ }
109
+
110
+ runner = @runner_class.new(@handler_class, args)
111
+ assert_equal splat, runner.splat
112
+ end
113
+
114
+ should "not have a splat value if there is no splat in the route path" do
115
+ route_path = '/some/:value'
116
+ params = { 'value' => Factory.string }
117
+ path_info = route_path.gsub(':value', params['value'])
118
+ request_env = { 'PATH_INFO' => path_info }
119
+
120
+ args = {
121
+ :request => Factory.request(:env => request_env),
122
+ :params => params,
123
+ :route_path => route_path
124
+ }
125
+
126
+ runner = @runner_class.new(@handler_class, args)
127
+ assert_nil runner.splat
128
+ end
129
+
130
+ should "complain if it can't parse the splat param" do
131
+ route_path = '/some/:value/*'
132
+ params = { 'value' => Factory.string }
133
+ splat = Factory.integer(3).times.map{ Factory.string}.join('/')
134
+ path_info = "/some/#{Factory.string}"
135
+ request_env = { 'PATH_INFO' => path_info }
136
+
137
+ args = {
138
+ :request => Factory.request(:env => request_env),
139
+ :params => params,
140
+ :route_path => route_path
141
+ }
142
+
143
+ runner = @runner_class.new(@handler_class, args)
144
+ assert_raises(SplatParseError){ runner.splat }
91
145
  end
92
146
 
93
147
  should "not implement its run method" do