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
File without changes
File without changes
@@ -26,21 +26,27 @@ module Deas
26
26
 
27
27
  should "set the content type appropriately" do
28
28
  get '/show'
29
+ assert_equal 200, last_response.status
29
30
  assert_equal 'text/html;charset=utf-8', last_response.headers['Content-Type']
30
31
 
31
32
  get '/show.html'
33
+ assert_equal 200, last_response.status
32
34
  assert_equal 'text/html;charset=utf-8', last_response.headers['Content-Type']
33
35
 
34
36
  get '/show.json'
37
+ assert_equal 200, last_response.status
35
38
  assert_equal 'application/json;charset=utf-8', last_response.headers['Content-Type']
36
39
 
37
40
  get '/show-latin1-json'
41
+ assert_equal 200, last_response.status
38
42
  assert_equal 'application/json;charset=latin1', last_response.headers['Content-Type']
39
43
 
40
44
  get '/show-text'
45
+ assert_equal 200, last_response.status
41
46
  assert_equal 'text/plain', last_response.headers['Content-Type']
42
47
 
43
48
  get '/show-headers-text'
49
+ assert_equal 200, last_response.status
44
50
  assert_equal 'text/plain', last_response.headers['Content-Type']
45
51
  end
46
52
 
@@ -81,25 +87,25 @@ module Deas
81
87
 
82
88
  should "return a 302 redirecting to the expected locations" do
83
89
  get '/redirect'
84
- expected_location = 'http://google.com'
90
+ exp_location = 'http://google.com'
85
91
 
86
- assert_equal 302, last_response.status
87
- assert_equal expected_location, last_response.headers['Location']
92
+ assert_equal 302, last_response.status
93
+ assert_equal exp_location, last_response.headers['Location']
88
94
  end
89
95
 
90
96
  should "return a 302 redirect to the expected location " \
91
97
  "when using a route redirect" do
92
98
  get '/route_redirect'
93
- expected_location = 'http://example.org/somewhere'
99
+ exp_location = 'http://example.org/somewhere'
94
100
 
95
- assert_equal 302, last_response.status
96
- assert_equal expected_location, last_response.headers['Location']
101
+ assert_equal 302, last_response.status
102
+ assert_equal exp_location, last_response.headers['Location']
97
103
 
98
104
  get '/my_prefix/redirect'
99
- expected_location = 'http://example.org/my_prefix/somewhere'
105
+ exp_location = 'http://example.org/my_prefix/somewhere'
100
106
 
101
- assert_equal 302, last_response.status
102
- assert_equal expected_location, last_response.headers['Location']
107
+ assert_equal 302, last_response.status
108
+ assert_equal exp_location, last_response.headers['Location']
103
109
  end
104
110
 
105
111
  end
@@ -133,11 +139,10 @@ module Deas
133
139
 
134
140
  should "be able to access sinatra call data" do
135
141
  exp = {
136
- 'logger_class_name' => 'Logger',
137
- 'request_method' => 'GET',
138
- 'response_firstheaderval' => 'Content-Type',
139
- 'params_a_param' => 'something',
140
- 'session_inspect' => '{}'
142
+ 'logger_class_name' => 'Logger',
143
+ 'request_method' => 'GET',
144
+ 'params_a_param' => 'something',
145
+ 'session_inspect' => '{}'
141
146
  }
142
147
  assert_equal exp.inspect, @data_inspect
143
148
  end
@@ -4,18 +4,18 @@ require 'deas/deas_runner'
4
4
  require 'deas/runner'
5
5
  require 'deas/template_source'
6
6
  require 'test/support/normalized_params_spy'
7
- require 'test/support/view_handlers'
8
7
 
9
8
  class Deas::DeasRunner
10
9
 
11
10
  class UnitTests < Assert::Context
12
11
  desc "Deas::DeasRunner"
13
12
  setup do
14
- @runner_class = Deas::DeasRunner
13
+ @handler_class = TestViewHandler
14
+ @runner_class = Deas::DeasRunner
15
15
  end
16
16
  subject{ @runner_class }
17
17
 
18
- should "be a `Runner`" do
18
+ should "be a runner" do
19
19
  assert subject < Deas::Runner
20
20
  end
21
21
 
@@ -28,43 +28,197 @@ class Deas::DeasRunner
28
28
  @norm_params_spy = Deas::Runner::NormalizedParamsSpy.new
29
29
  Assert.stub(NormalizedParams, :new){ |p| @norm_params_spy.new(p) }
30
30
 
31
- @runner = @runner_class.new(DeasRunnerViewHandler, :params => @params)
31
+ @runner = @runner_class.new(@handler_class, :params => @params)
32
32
  end
33
33
  subject{ @runner }
34
34
 
35
35
  should have_imeths :run
36
36
 
37
+ should "call to normalize its params" do
38
+ assert_equal @params, @norm_params_spy.params
39
+ assert_true @norm_params_spy.value_called
40
+ end
41
+
37
42
  should "super its params arg" do
38
43
  assert_equal @params, subject.params
39
44
  end
40
45
 
41
- should "call to normalize its params" do
42
- assert_equal @params, @norm_params_spy.params
43
- assert_true @norm_params_spy.value_called
46
+ end
47
+
48
+ class InitHandlerTests < InitTests
49
+ setup do
50
+ @handler = @runner.handler
51
+ end
52
+
53
+ private
54
+
55
+ def subject_to_rack_with_content_length
56
+ subject.to_rack.tap do |(s,h,b)|
57
+ h.merge!('Content-Length' => calc_content_length(b))
58
+ end
59
+ end
60
+
61
+ def calc_content_length(body)
62
+ body.inject(0){ |l, p| l + p.size }.to_s
44
63
  end
45
64
 
46
65
  end
47
66
 
48
- class RunTests < InitTests
67
+ class RunTests < InitHandlerTests
49
68
  desc "and run"
50
69
  setup do
51
- @response_value = @runner.run
52
- @handler = @runner.instance_variable_get("@handler")
70
+ @response = @runner.run
71
+ end
72
+
73
+ should "run the handler's before callbacks" do
74
+ assert_equal 1, @handler.first_before_call_order
75
+ assert_equal 2, @handler.second_before_call_order
76
+ end
77
+
78
+ should "run the handler's init and run methods" do
79
+ assert_equal 3, @handler.init_call_order
80
+ assert_equal 4, @handler.run_call_order
81
+ end
82
+
83
+ should "run the handler's after callbacks" do
84
+ assert_equal 5, @handler.first_after_call_order
85
+ assert_equal 6, @handler.second_after_call_order
86
+ end
87
+
88
+ should "set the content length header in its response" do
89
+ status, headers, body = *@response
90
+ exp = calc_content_length(body)
91
+ assert_equal exp, headers['Content-Length']
92
+ end
93
+
94
+ should "only set the content length if it is not already set" do
95
+ custom_content_length = Factory.integer.to_s
96
+ subject.headers['Content-Length'] = custom_content_length
97
+
98
+ headers = @runner.run[1]
99
+ assert_equal custom_content_length, headers['Content-Length']
100
+ end
101
+
102
+ should "return its `to_rack` value" do
103
+ assert_equal subject_to_rack_with_content_length, @response
104
+ end
105
+
106
+ end
107
+
108
+ class RunWithInitHaltTests < InitHandlerTests
109
+ desc "with a handler that halts on init"
110
+ setup do
111
+ @runner = @runner_class.new(@handler_class, :params => {
112
+ 'halt' => 'init'
113
+ })
114
+ @handler = @runner.handler
115
+ @response = @runner.run
53
116
  end
54
- subject{ @handler }
55
117
 
56
- should "run the before and after hooks" do
57
- assert_equal true, subject.before_called
58
- assert_equal true, subject.after_called
118
+ should "run the before and after callbacks despite the halt" do
119
+ assert_not_nil @handler.first_before_call_order
120
+ assert_not_nil @handler.second_before_call_order
121
+ assert_not_nil @handler.first_after_call_order
122
+ assert_not_nil @handler.second_after_call_order
59
123
  end
60
124
 
61
- should "run the handler's init and run" do
62
- assert_equal true, subject.init_bang_called
63
- assert_equal true, subject.run_bang_called
125
+ should "stop processing when the halt is called" do
126
+ assert_not_nil @handler.init_call_order
127
+ assert_nil @handler.run_call_order
64
128
  end
65
129
 
66
- should "use the handler's run! return value as its response value" do
67
- assert_equal true, @response_value
130
+ should "return its `to_rack` value despite the halt" do
131
+ assert_equal subject_to_rack_with_content_length, @response
132
+ end
133
+
134
+ end
135
+
136
+ class RunWithRunHaltTests < InitHandlerTests
137
+ desc "with a handler that halts on run"
138
+ setup do
139
+ @runner = @runner_class.new(@handler_class, :params => {
140
+ 'halt' => 'run'
141
+ })
142
+ @handler = @runner.handler
143
+ @response = @runner.run
144
+ end
145
+
146
+ should "run the before and after callbacks despite the halt" do
147
+ assert_not_nil @handler.first_before_call_order
148
+ assert_not_nil @handler.second_before_call_order
149
+ assert_not_nil @handler.first_after_call_order
150
+ assert_not_nil @handler.second_after_call_order
151
+ end
152
+
153
+ should "stop processing when the halt is called" do
154
+ assert_not_nil @handler.init_call_order
155
+ assert_not_nil @handler.run_call_order
156
+ end
157
+
158
+ should "return its `to_rack` value despite the halt" do
159
+ assert_equal subject_to_rack_with_content_length, @response
160
+ end
161
+
162
+ end
163
+
164
+ class RunWithBeforeHaltTests < InitHandlerTests
165
+ desc "with a handler that halts in a before callback"
166
+ setup do
167
+ @runner = @runner_class.new(@handler_class, :params => {
168
+ 'halt' => 'before'
169
+ })
170
+ @handler = @runner.handler
171
+ @response = @runner.run
172
+ end
173
+
174
+ should "stop processing when the halt is called" do
175
+ assert_not_nil @handler.first_before_call_order
176
+ assert_nil @handler.second_before_call_order
177
+ end
178
+
179
+ should "not run the after callbacks b/c of the halt" do
180
+ assert_nil @handler.first_after_call_order
181
+ assert_nil @handler.second_after_call_order
182
+ end
183
+
184
+ should "not run the handler's init and run b/c of the halt" do
185
+ assert_nil @handler.init_call_order
186
+ assert_nil @handler.run_call_order
187
+ end
188
+
189
+ should "return its `to_rack` value despite the halt" do
190
+ assert_equal subject_to_rack_with_content_length, @response
191
+ end
192
+
193
+ end
194
+
195
+ class RunWithAfterHaltTests < InitHandlerTests
196
+ desc "with a handler that halts in an after callback"
197
+ setup do
198
+ @runner = @runner_class.new(@handler_class, :params => {
199
+ 'halt' => 'after'
200
+ })
201
+ @handler = @runner.handler
202
+ @response = @runner.run
203
+ end
204
+
205
+ should "run the before callback despite the halt" do
206
+ assert_not_nil @handler.first_before_call_order
207
+ assert_not_nil @handler.second_before_call_order
208
+ end
209
+
210
+ should "run the handler's init and run despite the halt" do
211
+ assert_not_nil @handler.init_call_order
212
+ assert_not_nil @handler.run_call_order
213
+ end
214
+
215
+ should "stop processing when the halt is called" do
216
+ assert_not_nil @handler.first_after_call_order
217
+ assert_nil @handler.second_after_call_order
218
+ end
219
+
220
+ should "return its `to_rack` value despite the halt" do
221
+ assert_equal subject_to_rack_with_content_length, @response
68
222
  end
69
223
 
70
224
  end
@@ -72,7 +226,7 @@ class Deas::DeasRunner
72
226
  class NormalizedParamsTests < UnitTests
73
227
  desc "NormalizedParams"
74
228
  setup do
75
- @norm_params_class = Deas::SinatraRunner::NormalizedParams
229
+ @norm_params_class = Deas::DeasRunner::NormalizedParams
76
230
  end
77
231
 
78
232
  should "be a normalized params subclass" do
@@ -95,4 +249,39 @@ class Deas::DeasRunner
95
249
 
96
250
  end
97
251
 
252
+ class TestViewHandler
253
+ include Deas::ViewHandler
254
+
255
+ attr_accessor :halt_in_before, :halt_in_after
256
+ attr_reader :first_before_call_order, :second_before_call_order
257
+ attr_reader :first_after_call_order, :second_after_call_order
258
+ attr_reader :init_call_order, :run_call_order
259
+
260
+ before{ @first_before_call_order = next_call_order; halt_if('before') }
261
+ before{ @second_before_call_order = next_call_order }
262
+
263
+ after{ @first_after_call_order = next_call_order; halt_if('after') }
264
+ after{ @second_after_call_order = next_call_order }
265
+
266
+ def init!
267
+ @init_call_order = next_call_order
268
+ halt_if('init')
269
+ end
270
+
271
+ def run!
272
+ @run_call_order = next_call_order
273
+ halt_if('run')
274
+ body Factory.integer(3).times.map{ Factory.text }
275
+ end
276
+
277
+ private
278
+
279
+ def next_call_order; @order ||= 0; @order += 1; end
280
+
281
+ def halt_if(value)
282
+ halt Factory.integer if params['halt'] == value
283
+ end
284
+
285
+ end
286
+
98
287
  end
@@ -2,8 +2,8 @@ require 'assert'
2
2
  require 'deas/handler_proxy'
3
3
 
4
4
  require 'deas/exceptions'
5
- require 'deas/sinatra_runner'
6
- require 'test/support/view_handlers'
5
+ require 'deas/deas_runner'
6
+ require 'test/support/empty_view_handler'
7
7
 
8
8
  class Deas::HandlerProxy
9
9
 
@@ -30,8 +30,8 @@ class Deas::HandlerProxy
30
30
  class RunTests < UnitTests
31
31
  desc "when run"
32
32
  setup do
33
- @runner_spy = SinatraRunnerSpy.new
34
- Assert.stub(Deas::SinatraRunner, :new) do |*args|
33
+ @runner_spy = RunnerSpy.new
34
+ Assert.stub(Deas::DeasRunner, :new) do |*args|
35
35
  @runner_spy.build(*args)
36
36
  @runner_spy
37
37
  end
@@ -40,21 +40,32 @@ class Deas::HandlerProxy
40
40
 
41
41
  @server_data = Factory.server_data
42
42
  @fake_sinatra_call = Factory.sinatra_call
43
+ @fake_sinatra_call.params = {
44
+ :splat => Factory.string,
45
+ 'splat' => Factory.string,
46
+ :captures => [Factory.string],
47
+ 'captures' => [Factory.string]
48
+ }
49
+
43
50
  @proxy.run(@server_data, @fake_sinatra_call)
44
51
  end
45
52
 
46
- should "build and run a sinatra runner" do
53
+ should "remove any 'splat' or 'captures' params added by Sinatra's router" do
54
+ [:splat, 'splat', :captures, 'captures'].each do |param_name|
55
+ assert_nil @fake_sinatra_call.params[param_name]
56
+ end
57
+ end
58
+
59
+ should "build and run a deas runner" do
47
60
  assert_equal subject.handler_class, @runner_spy.handler_class
48
61
 
49
62
  exp_args = {
50
- :sinatra_call => @fake_sinatra_call,
51
- :request => @fake_sinatra_call.request,
52
- :response => @fake_sinatra_call.response,
53
- :session => @fake_sinatra_call.session,
54
- :params => @fake_sinatra_call.params,
55
63
  :logger => @server_data.logger,
56
64
  :router => @server_data.router,
57
- :template_source => @server_data.template_source
65
+ :template_source => @server_data.template_source,
66
+ :request => @fake_sinatra_call.request,
67
+ :session => @fake_sinatra_call.session,
68
+ :params => @fake_sinatra_call.params
58
69
  }
59
70
  assert_equal exp_args, @runner_spy.args
60
71
 
@@ -82,13 +93,12 @@ class Deas::HandlerProxy
82
93
 
83
94
  end
84
95
 
85
- class SinatraRunnerSpy
96
+ class RunnerSpy
86
97
 
87
98
  attr_reader :run_called
88
99
  attr_reader :handler_class, :handler, :args
89
- attr_reader :sinatra_call
90
- attr_reader :request, :response, :session, :params
91
100
  attr_reader :logger, :router, :template_source
101
+ attr_reader :request, :session, :params
92
102
 
93
103
  def initialize
94
104
  @run_called = false
@@ -99,14 +109,12 @@ class Deas::HandlerProxy
99
109
  @handler = handler_class.new(self)
100
110
  @args = args
101
111
 
102
- @sinatra_call = args[:sinatra_call]
103
- @request = args[:request]
104
- @response = args[:response]
105
- @session = args[:session]
106
- @params = args[:params]
107
112
  @logger = args[:logger]
108
113
  @router = args[:router]
109
114
  @template_source = args[:template_source]
115
+ @request = args[:request]
116
+ @session = args[:session]
117
+ @params = args[:params]
110
118
  end
111
119
 
112
120
  def run