deas 0.38.0 → 0.39.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/Gemfile +2 -1
  2. data/{LICENSE.txt → LICENSE} +0 -0
  3. data/deas.gemspec +4 -3
  4. data/lib/deas/deas_runner.rb +17 -12
  5. data/lib/deas/handler_proxy.rb +14 -8
  6. data/lib/deas/redirect_proxy.rb +12 -12
  7. data/lib/deas/runner.rb +169 -19
  8. data/lib/deas/server.rb +14 -10
  9. data/lib/deas/template_source.rb +2 -2
  10. data/lib/deas/test_runner.rb +59 -60
  11. data/lib/deas/version.rb +1 -1
  12. data/lib/deas/view_handler.rb +55 -30
  13. data/test/support/empty_view_handler.rb +7 -0
  14. data/test/support/factory.rb +5 -0
  15. data/test/support/fake_request.rb +29 -0
  16. data/test/support/fake_sinatra_call.rb +11 -16
  17. data/test/support/file1.txt +1 -0
  18. data/test/support/file2.txt +1 -0
  19. data/test/support/routes.rb +7 -7
  20. data/test/support/show.html +0 -0
  21. data/test/support/show.json +0 -0
  22. data/test/system/{rack_tests.rb → deas_tests.rb} +19 -14
  23. data/test/unit/deas_runner_tests.rb +209 -20
  24. data/test/unit/handler_proxy_tests.rb +27 -19
  25. data/test/unit/redirect_proxy_tests.rb +32 -33
  26. data/test/unit/respond_with_proxy_tests.rb +1 -2
  27. data/test/unit/route_proxy_tests.rb +1 -1
  28. data/test/unit/route_tests.rb +1 -1
  29. data/test/unit/router_tests.rb +5 -5
  30. data/test/unit/runner_tests.rb +619 -76
  31. data/test/unit/server_tests.rb +6 -1
  32. data/test/unit/sinatra_app_tests.rb +1 -1
  33. data/test/unit/test_runner_tests.rb +377 -106
  34. data/test/unit/url_tests.rb +1 -1
  35. data/test/unit/view_handler_tests.rb +325 -182
  36. metadata +43 -24
  37. data/lib/deas/sinatra_runner.rb +0 -55
  38. data/lib/deas/test_helpers.rb +0 -23
  39. data/test/support/view_handlers.rb +0 -83
  40. data/test/unit/sinatra_runner_tests.rb +0 -79
  41. data/test/unit/test_helpers_tests.rb +0 -53
@@ -2,11 +2,12 @@ require 'assert'
2
2
  require 'deas/server'
3
3
 
4
4
  require 'logger'
5
+ require 'much-plugin'
5
6
  require 'deas/exceptions'
6
7
  require 'deas/logger'
7
8
  require 'deas/router'
8
9
  require 'deas/template_source'
9
- require 'test/support/view_handlers'
10
+ require 'test/support/empty_view_handler'
10
11
 
11
12
  module Deas::Server
12
13
 
@@ -37,6 +38,10 @@ module Deas::Server
37
38
  should have_imeths :get, :post, :put, :patch, :delete
38
39
  should have_imeths :route, :redirect
39
40
 
41
+ should "use much-plugin" do
42
+ assert_includes MuchPlugin, Deas::Server
43
+ end
44
+
40
45
  should "allow setting it's configuration options" do
41
46
  config = subject.configuration
42
47
 
@@ -8,7 +8,7 @@ require 'deas/route'
8
8
  require 'deas/router'
9
9
  require 'deas/server'
10
10
  require 'deas/server_data'
11
- require 'test/support/view_handlers'
11
+ require 'test/support/empty_view_handler'
12
12
 
13
13
  module Deas::SinatraApp
14
14
 
@@ -5,19 +5,18 @@ require 'rack/test'
5
5
  require 'deas/runner'
6
6
  require 'deas/template_source'
7
7
  require 'test/support/normalized_params_spy'
8
- require 'test/support/view_handlers'
9
8
 
10
9
  class Deas::TestRunner
11
10
 
12
11
  class UnitTests < Assert::Context
13
12
  desc "Deas::TestRunner"
14
13
  setup do
15
- @handler_class = TestRunnerViewHandler
16
- @runner_class = Deas::TestRunner
14
+ @handler_class = TestViewHandler
15
+ @runner_class = Deas::TestRunner
17
16
  end
18
17
  subject{ @runner_class }
19
18
 
20
- should "be a `Runner`" do
19
+ should "be a runner" do
21
20
  assert subject < Deas::Runner
22
21
  end
23
22
 
@@ -26,41 +25,44 @@ class Deas::TestRunner
26
25
  class InitTests < UnitTests
27
26
  desc "when init"
28
27
  setup do
29
- @params = { 'value' => '1' }
28
+ @request = Factory.request
29
+ @params = { Factory.string => Factory.string }
30
+
30
31
  @args = {
31
- :request => 'a-request',
32
- :response => 'a-response',
33
- :session => 'a-session',
34
- :params => @params,
35
- :logger => 'a-logger',
36
- :router => 'a-router',
37
- :template_source => 'a-source'
32
+ :logger => Factory.string,
33
+ :router => Factory.string,
34
+ :template_source => Factory.string,
35
+ :request => @request,
36
+ :session => Factory.string,
37
+ :params => @params,
38
+ :custom_value => Factory.integer
38
39
  }
39
40
 
40
41
  @norm_params_spy = Deas::Runner::NormalizedParamsSpy.new
41
42
  Assert.stub(NormalizedParams, :new){ |p| @norm_params_spy.new(p) }
42
43
 
44
+ @original_args = @args.dup
43
45
  @runner = @runner_class.new(@handler_class, @args)
46
+ @handler = @runner.handler
44
47
  end
45
48
  subject{ @runner }
46
49
 
47
- should have_readers :response_value
48
- should have_imeths :run
50
+ should have_readers :content_type_args
51
+ should have_imeths :halted?, :run
49
52
 
50
- should "raise an invalid error when not passed a view handler" do
51
- assert_raises(Deas::InvalidServiceHandlerError) do
53
+ should "raise an invalid error when passed a non view handler" do
54
+ assert_raises(Deas::InvalidViewHandlerError) do
52
55
  @runner_class.new(Class.new)
53
56
  end
54
57
  end
55
58
 
56
59
  should "super its standard args" do
57
- assert_equal 'a-request', subject.request
58
- assert_equal 'a-response', subject.response
59
- assert_equal 'a-session', subject.session
60
- assert_equal @params, subject.params
61
- assert_equal 'a-logger', subject.logger
62
- assert_equal 'a-router', subject.router
63
- assert_equal 'a-source', subject.template_source
60
+ assert_equal @args[:logger], subject.logger
61
+ assert_equal @args[:router], subject.router
62
+ assert_equal @args[:template_source], subject.template_source
63
+ assert_equal @args[:request], subject.request
64
+ assert_equal @args[:session], subject.session
65
+ assert_equal @args[:params], subject.params
64
66
  end
65
67
 
66
68
  should "call to normalize its params" do
@@ -68,86 +70,188 @@ class Deas::TestRunner
68
70
  assert_true @norm_params_spy.value_called
69
71
  end
70
72
 
71
- should "write any non-standard args on the handler" do
72
- runner = @runner_class.new(@handler_class, :custom_value => 42)
73
- assert_equal 42, runner.handler.custom_value
73
+ should "write any non-standard args to its handler" do
74
+ assert_equal @args[:custom_value], @handler.custom_value
75
+ end
76
+
77
+ should "not alter the args passed to it" do
78
+ assert_equal @original_args, @args
79
+ end
80
+
81
+ should "not call its handler's before callbacks" do
82
+ assert_nil @handler.before_called
83
+ end
84
+
85
+ should "call its handler's init" do
86
+ assert_true @handler.init_called
87
+ end
88
+
89
+ should "not call its handler's run" do
90
+ assert_nil @handler.run_called
91
+ end
92
+
93
+ should "not call its handler's after callbacks" do
94
+ assert_nil @handler.after_called
74
95
  end
75
96
 
76
- should "not have called its service handlers before callbacks" do
77
- assert_not_true subject.handler.before_called
97
+ should "not have set a run return value" do
98
+ assert_nil subject.run
78
99
  end
79
100
 
80
- should "have called init on its service handler" do
81
- assert_true subject.handler.init_called
101
+ should "have no content type args by default" do
102
+ assert_nil subject.content_type_args
82
103
  end
83
104
 
84
- should "not set a response value on initialize" do
85
- assert_nil subject.response_value
105
+ should "not be halted by default" do
106
+ assert_false subject.halted?
86
107
  end
87
108
 
88
- should "set its response value to the return value of `run!` on run" do
89
- assert_nil subject.response_value
109
+ should "not call `run` on its handler if halted when run" do
110
+ catch(:halt){ subject.halt }
111
+ assert_true subject.halted?
90
112
  subject.run
91
- assert_equal subject.handler.run!, subject.response_value
113
+ assert_nil @handler.run_called
92
114
  end
93
115
 
94
- should "build halt args if halt is called" do
95
- value = catch(:halt){ subject.halt }
96
- assert_kind_of HaltArgs, value
97
- [:body, :headers, :status].each do |meth|
98
- assert_respond_to meth, value
99
- end
116
+ should "return its run return value when run" do
117
+ catch(:halt){ subject.halt }
118
+ return_val = subject.run
119
+ assert_kind_of HaltArgs, return_val
100
120
  end
101
121
 
102
- should "build redirect args if redirect is called" do
103
- value = catch(:halt){ subject.redirect '/some/path' }
104
- assert_kind_of RedirectArgs, value
105
- [:path, :halt_args].each do |meth|
106
- assert_respond_to meth, value
107
- end
108
- assert_equal '/some/path', value.path
109
- assert value.redirect?
122
+ end
123
+
124
+ class ContentTypeTests < InitTests
125
+ desc "the `content_type` method"
126
+ setup do
127
+ @extname = ".#{Factory.string}"
128
+ @params = { Factory.string => Factory.string }
129
+ @runner.content_type(@extname, @params)
110
130
  end
111
131
 
112
- should "build content type args if content type is called" do
113
- value = subject.content_type 'something'
114
- assert_kind_of ContentTypeArgs, value
115
- [:value, :opts].each do |meth|
116
- assert_respond_to meth, value
117
- end
118
- assert_equal 'something', value.value
132
+ should "set content type args" do
133
+ args = subject.content_type_args
134
+
135
+ assert_kind_of ContentTypeArgs, args
136
+ assert_equal @extname, args.extname
137
+ assert_equal @params, args.params
138
+ end
119
139
 
120
- assert_same value, subject.content_type
140
+ should "super to the base runner" do
141
+ e = @extname; p = @params
142
+ exp = subject.instance_eval{ get_content_type(e, p) }
143
+ assert_equal exp, subject.headers['Content-Type']
121
144
  end
122
145
 
123
- should "build status args if status is called" do
124
- value = subject.status(432)
125
- assert_kind_of StatusArgs, value
126
- assert_respond_to :value, value
127
- assert_equal 432, value.value
146
+ end
128
147
 
129
- assert_same value, subject.status
148
+ class HaltWithArgsTests < InitTests
149
+ setup do
150
+ @status = Factory.integer
151
+ @headers = { Factory.string => Factory.string }
152
+ @body = [Factory.text]
153
+ @halt_args = [@status, @headers, @body]
130
154
  end
131
155
 
132
- should "build headers args if headers is called" do
133
- value = subject.headers(:some => 'thing')
134
- assert_kind_of HeadersArgs, value
135
- assert_respond_to :value, value
136
- exp_val = {:some => 'thing'}
137
- assert_equal exp_val, value.value
156
+ end
157
+
158
+ class HaltTests < HaltWithArgsTests
159
+ desc "the `halt` method"
160
+ setup do
161
+ catch(:halt){ @runner.halt(*@halt_args) }
162
+ end
138
163
 
139
- assert_same value, subject.headers
164
+ should "put the runner in the halted state" do
165
+ assert_true subject.halted?
140
166
  end
141
167
 
142
- should "build send file args if send file is called" do
143
- path = Factory.path
144
- args = subject.send_file path
168
+ should "set halt args as the run return value" do
169
+ return_val = subject.run
145
170
 
146
- assert_kind_of SendFileArgs, args
147
- [:file_path, :options, :block].each do |meth|
148
- assert_respond_to meth, args
149
- end
150
- assert_equal path, args.file_path
171
+ assert_kind_of HaltArgs, return_val
172
+ assert_equal @status, return_val.status
173
+ assert_equal @headers, return_val.headers
174
+ assert_equal @body, return_val.body
175
+ end
176
+
177
+ should "super to the base runner" do
178
+ assert_equal @status, subject.status
179
+ assert_equal @headers, subject.headers
180
+ assert_equal @body, subject.body
181
+ end
182
+
183
+ end
184
+
185
+ class RedirectTests < HaltWithArgsTests
186
+ desc "the `redirect` method"
187
+ setup do
188
+ @location = Factory.string
189
+ catch(:halt){ @runner.redirect(@location, *@halt_args) }
190
+ end
191
+
192
+ should "put the runner in the halted state" do
193
+ assert_true subject.halted?
194
+ end
195
+
196
+ should "set redirect args as the run return value" do
197
+ return_val = subject.run
198
+
199
+ assert_kind_of RedirectArgs, return_val
200
+ assert_equal @location, return_val.location
201
+
202
+ assert_kind_of HaltArgs, return_val.halt_args
203
+ assert_equal @status, return_val.halt_args.status
204
+ assert_equal @headers, return_val.halt_args.headers
205
+ assert_equal @body, return_val.halt_args.body
206
+ end
207
+
208
+ should "super to the base runner" do
209
+ assert_equal @status, subject.status
210
+
211
+ exp = { 'Location' => get_absolute_url(@location) }.merge(@headers)
212
+ assert_equal exp, subject.headers
213
+
214
+ assert_equal @body, subject.body
215
+ end
216
+
217
+ private
218
+
219
+ def get_absolute_url(url)
220
+ File.join("#{@request.env['rack.url_scheme']}://#{@request.env['HTTP_HOST']}", url)
221
+ end
222
+
223
+ end
224
+
225
+ class SendFileTests < InitTests
226
+ desc "the `send_file` method"
227
+ setup do
228
+ # set an existing file path so the base method will look to the opts
229
+ @file_path = TEST_SUPPORT_ROOT.join("file1.txt")
230
+ # set an opt that the base method will actually do something with
231
+ @opts = { :filename => Factory.string }
232
+
233
+ catch(:halt){ @runner.send_file(@file_path, @opts) }
234
+ end
235
+
236
+ should "put the runner in the halted state" do
237
+ assert_true subject.halted?
238
+ end
239
+
240
+ should "set send file args as the run return value" do
241
+ return_val = subject.run
242
+
243
+ assert_kind_of SendFileArgs, return_val
244
+ assert_equal @file_path, return_val.file_path
245
+ assert_equal @opts, return_val.opts
246
+ end
247
+
248
+ should "super to the base runner" do
249
+ assert_equal 200, subject.status
250
+
251
+ exp = "attachment;filename=\"#{@opts[:filename]}\""
252
+ assert_equal exp, subject.headers['Content-Disposition']
253
+
254
+ assert_instance_of Deas::Runner::SendFileBody, subject.body
151
255
  end
152
256
 
153
257
  end
@@ -162,49 +266,196 @@ class Deas::TestRunner
162
266
  end
163
267
 
164
268
  class SourceRenderTests < RenderSetupTests
165
- desc "source render method"
269
+ desc "the `source_render` method"
166
270
  setup do
167
- @source_render_args = nil
168
- Assert.stub(@source, :render){ |*args| @source_render_args = args }
271
+ @source_render_called_with = nil
272
+ Assert.stub(@source, :render){ |*args| @source_render_called_with = args }
273
+
274
+ @runner.source_render(@source, @template_name, @locals)
169
275
  end
170
276
 
171
- should "render the template, discard its output and build render args" do
172
- args = subject.source_render(@source, @template_name, @locals)
277
+ should "set render args as the run return value" do
278
+ return_val = subject.run
173
279
 
174
- exp = [@template_name, subject.handler, @locals]
175
- assert_equal exp, @source_render_args
280
+ assert_kind_of RenderArgs, return_val
281
+ assert_equal @source, return_val.source
282
+ assert_equal @template_name, return_val.template_name
283
+ assert_equal @locals, return_val.locals
284
+ end
176
285
 
177
- assert_kind_of RenderArgs, args
178
- [:source, :template_name, :locals].each do |meth|
179
- assert_respond_to meth, args
180
- end
181
- assert_equal @source, args.source
182
- assert_equal @template_name, args.template_name
183
- assert_equal @locals, args.locals
286
+ should "super to the base runner" do
287
+ exp = [@template_name, @handler, @locals]
288
+ assert_equal exp, @source_render_called_with
184
289
  end
185
290
 
186
291
  end
187
292
 
188
293
  class SourcePartialTests < RenderSetupTests
189
- desc "source partial method"
294
+ desc "the `source_partial` method"
190
295
  setup do
191
- @source_partial_args = nil
192
- Assert.stub(@source, :partial){ |*args| @source_partial_args = args }
193
- end
296
+ @source_partial_called_with = nil
297
+ Assert.stub(@source, :partial){ |*args| @source_partial_called_with = args }
194
298
 
195
- should "render the template, discard its output build render args" do
196
- args = subject.source_partial(@source, @template_name, @locals)
299
+ @return_val = @runner.source_partial(@source, @template_name, @locals)
300
+ end
197
301
 
302
+ should "super to the base runner" do
198
303
  exp = [@template_name, @locals]
199
- assert_equal exp, @source_partial_args
304
+ assert_equal exp, @source_partial_called_with
305
+ end
200
306
 
201
- assert_kind_of RenderArgs, args
202
- [:source, :template_name, :locals].each do |meth|
203
- assert_respond_to meth, args
204
- end
205
- assert_equal @source, args.source
206
- assert_equal @template_name, args.template_name
207
- assert_equal @locals, args.locals
307
+ should "return render args" do
308
+ assert_kind_of RenderArgs, @return_val
309
+ assert_equal @source, @return_val.source
310
+ assert_equal @template_name, @return_val.template_name
311
+ assert_equal @locals, @return_val.locals
312
+ end
313
+
314
+ should "not affect the run return val" do
315
+ assert_nil subject.run
316
+ end
317
+
318
+ end
319
+
320
+ class ContentTypeArgsTests < UnitTests
321
+ desc "ContentTypeArgs"
322
+ setup do
323
+ @extname = ".#{Factory.string}"
324
+ @params = { Factory.string => Factory.string }
325
+
326
+ @args = ContentTypeArgs.new(@extname, @params)
327
+ end
328
+ subject{ @args }
329
+
330
+ should have_imeths :extname, :params
331
+
332
+ should "know its attrs" do
333
+ assert_equal @extname, subject.extname
334
+ assert_equal @params, subject.params
335
+ end
336
+
337
+ end
338
+
339
+ class HaltArgsTests < UnitTests
340
+ desc "HaltArgs"
341
+ setup do
342
+ @status = Factory.integer
343
+ @headers = { Factory.string => Factory.string }
344
+ @body = [Factory.text]
345
+ @halt_args = [@status, @headers, @body]
346
+ @orig_args = @halt_args.dup
347
+
348
+ @args = HaltArgs.new(@halt_args)
349
+ end
350
+ subject{ @args }
351
+
352
+ should have_imeths :status, :headers, :body
353
+
354
+ should "know its attrs" do
355
+ args = HaltArgs.new([])
356
+ assert_nil args.status
357
+ assert_nil args.headers
358
+ assert_nil args.body
359
+
360
+ args = HaltArgs.new([@status])
361
+ assert_equal @status, args.status
362
+ assert_nil args.headers
363
+ assert_nil args.body
364
+
365
+ args = HaltArgs.new([@headers])
366
+ assert_nil args.status
367
+ assert_equal @headers, args.headers
368
+ assert_nil args.body
369
+
370
+ args = HaltArgs.new([@body])
371
+ assert_nil args.status
372
+ assert_nil args.headers
373
+ assert_equal @body, args.body
374
+
375
+ args = HaltArgs.new([@status, @headers])
376
+ assert_equal @status, args.status
377
+ assert_equal @headers, args.headers
378
+ assert_nil args.body
379
+
380
+ args = HaltArgs.new([@status, @body])
381
+ assert_equal @status, args.status
382
+ assert_nil args.headers
383
+ assert_equal @body, args.body
384
+
385
+ args = HaltArgs.new([@headers, @body])
386
+ assert_nil args.status
387
+ assert_equal @headers, args.headers
388
+ assert_equal @body, args.body
389
+
390
+ args = HaltArgs.new([@status, @headers, @body])
391
+ assert_equal @status, args.status
392
+ assert_equal @headers, args.headers
393
+ assert_equal @body, args.body
394
+ end
395
+
396
+ should "not alter the given args" do
397
+ assert_equal @orig_args, @halt_args
398
+ end
399
+
400
+ end
401
+
402
+ class RedirectArgsTests < UnitTests
403
+ desc "RedirectArgs"
404
+ setup do
405
+ @location = Factory.string
406
+ @halt_args = HaltArgs.new([])
407
+
408
+ @args = RedirectArgs.new(@location, @halt_args)
409
+ end
410
+ subject{ @args }
411
+
412
+ should have_imeths :location, :halt_args
413
+ should have_imeths :redirect?
414
+
415
+ should "know its attrs" do
416
+ assert_equal @location, subject.location
417
+ assert_equal @halt_args, subject.halt_args
418
+ assert_true subject.redirect?
419
+ end
420
+
421
+ end
422
+
423
+ class SendFileArgsTests < UnitTests
424
+ desc "SendFileArgs"
425
+ setup do
426
+ @file_path = Factory.path
427
+ @opts = { Factory.string => Factory.string }
428
+
429
+ @args = SendFileArgs.new(@file_path, @opts)
430
+ end
431
+ subject{ @args }
432
+
433
+ should have_imeths :file_path, :opts
434
+
435
+ should "know its attrs" do
436
+ assert_equal @file_path, subject.file_path
437
+ assert_equal @opts, subject.opts
438
+ end
439
+
440
+ end
441
+
442
+ class RenderArgsTests < UnitTests
443
+ desc "RenderArgs"
444
+ setup do
445
+ @source = Factory.string
446
+ @template_name = Factory.path
447
+ @locals = { Factory.string => Factory.string }
448
+
449
+ @args = RenderArgs.new(@source, @template_name, @locals)
450
+ end
451
+ subject{ @args }
452
+
453
+ should have_imeths :source, :template_name, :locals
454
+
455
+ should "know its attrs" do
456
+ assert_equal @source, subject.source
457
+ assert_equal @template_name, subject.template_name
458
+ assert_equal @locals, subject.locals
208
459
  end
209
460
 
210
461
  end
@@ -259,4 +510,24 @@ class Deas::TestRunner
259
510
 
260
511
  end
261
512
 
513
+ class TestViewHandler
514
+ include Deas::ViewHandler
515
+
516
+ attr_reader :before_called, :after_called
517
+ attr_reader :init_called, :run_called
518
+ attr_accessor :custom_value
519
+
520
+ before{ @before_called = true }
521
+ after{ @after_called = true }
522
+
523
+ def init!
524
+ @init_called = true
525
+ end
526
+
527
+ def run!
528
+ @run_called = true
529
+ end
530
+
531
+ end
532
+
262
533
  end