deas 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/deas/server.rb CHANGED
@@ -7,6 +7,7 @@ require 'deas/template'
7
7
  require 'deas/logging'
8
8
  require 'deas/redirect_handler'
9
9
  require 'deas/route'
10
+ require 'deas/show_exceptions'
10
11
  require 'deas/sinatra_app'
11
12
 
12
13
  module Deas; end
@@ -77,9 +78,10 @@ module Deas::Server
77
78
  self.settings[:erb] ||= {}
78
79
  self.settings[:erb][:outvar] ||= '@_out_buf'
79
80
 
80
- # add the logging middleware args last. This ensures that the logging
81
- # happens just before the app gets the request and just after the app
82
- # sends a response.
81
+ # append the show exceptions and loggine middlewares last. This ensures
82
+ # that the logging and exception showing happens just before the app gets
83
+ # the request and just after the app sends a response.
84
+ self.middlewares << [Deas::ShowExceptions] if self.show_exceptions
83
85
  [*Deas::Logging.middleware(self.verbose_logging)].tap do |mw_args|
84
86
  self.middlewares << mw_args
85
87
  end
@@ -0,0 +1,56 @@
1
+ require 'rack/utils'
2
+
3
+ module Deas
4
+
5
+ class ShowExceptions
6
+
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ # The Rack call interface. The receiver acts as a prototype and runs
12
+ # each request in a clone object unless the +rack.run_once+ variable is
13
+ # set in the environment. Ripped from:
14
+ # http://github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/context.rb
15
+ def call(env)
16
+ if env['rack.run_once']
17
+ call! env
18
+ else
19
+ clone.call! env
20
+ end
21
+ end
22
+
23
+ # The real Rack call interface.
24
+ def call!(env)
25
+ status, headers, body = @app.call(env)
26
+ if error = env['sinatra.error']
27
+ error_body = Body.new(error)
28
+
29
+ headers['Content-Length'] = error_body.size.to_s
30
+ headers['Content-Type'] = error_body.mime_type.to_s
31
+ body = [error_body.content]
32
+ end
33
+ [ status, headers, body ]
34
+ end
35
+
36
+ class Body
37
+ def initialize(error)
38
+ @error = error
39
+ end
40
+
41
+ def content
42
+ @content ||= "#{@error.class}: #{@error.message}\n#{@error.backtrace.join("\n")}"
43
+ end
44
+
45
+ def size
46
+ @size ||= Rack::Utils.bytesize(self.content)
47
+ end
48
+
49
+ def mime_type
50
+ @mime_type ||= "text/plain"
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -19,11 +19,16 @@ module Deas
19
19
  set :dump_errors, server_config.dump_errors
20
20
  set :method_override, server_config.method_override
21
21
  set :sessions, server_config.sessions
22
- set :show_exceptions, server_config.show_exceptions
23
22
  set :static, server_config.static_files
24
23
  set :reload_templates, server_config.reload_templates
25
24
  set :logging, false
26
25
 
26
+ # raise_errors and show_exceptions prevent Deas error handlers from
27
+ # being called and Deas' logging doesn't finish. They should always be
28
+ # false.
29
+ set :raise_errors, false
30
+ set :show_exceptions, false
31
+
27
32
  # custom settings
28
33
  set :deas_template_scope, server_config.template_scope
29
34
  set :deas_error_procs, server_config.error_procs
data/lib/deas/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Deas
2
- VERSION = "0.11.0"
2
+ VERSION = "0.12.0"
3
3
  end
@@ -35,6 +35,23 @@ class DeasTestServer
35
35
 
36
36
  end
37
37
 
38
+ class DeasDevServer
39
+ include Deas::Server
40
+
41
+ # this server mimics a server in a "development" mode, that is, it has
42
+ # show_exceptions set to true
43
+
44
+ root TEST_SUPPORT_ROOT
45
+
46
+ logger TEST_LOGGER
47
+ verbose_logging true
48
+
49
+ show_exceptions true
50
+
51
+ get '/error', 'ErrorHandler'
52
+
53
+ end
54
+
38
55
  class ShowHandler
39
56
  include Deas::ViewHandler
40
57
 
@@ -4,16 +4,18 @@ require 'deas'
4
4
 
5
5
  module Deas
6
6
 
7
- class RackTests < Assert::Context
7
+ class RackTestContext < Assert::Context
8
8
  include Assert::Rack::Test
9
9
 
10
+ def app; @app; end
11
+ end
12
+
13
+ class RackTests < RackTestContext
10
14
  desc "a Deas server rack app"
11
15
  setup do
12
16
  @app = DeasTestServer.new
13
17
  end
14
18
 
15
- def app; @app; end
16
-
17
19
  should "return a 200 response with a GET to '/show'" do
18
20
  get '/show', 'message' => 'this is a test'
19
21
 
@@ -123,4 +125,28 @@ module Deas
123
125
 
124
126
  end
125
127
 
128
+ class ShowExceptionsTests < RackTestContext
129
+ desc "a Deas server rack app with show exceptions enabled"
130
+ setup do
131
+ @app = DeasDevServer.new
132
+ end
133
+
134
+ should "return a text/plain body when a 404 occurs" do
135
+ get '/not_defined'
136
+
137
+ assert_equal 404, last_response.status
138
+ assert_equal "text/plain", last_response.headers['Content-Type']
139
+ assert_match "Sinatra::NotFound: Sinatra::NotFound", last_response.body
140
+ end
141
+
142
+ should "return a text/plain body when an exception occurs" do
143
+ get '/error'
144
+
145
+ assert_equal 500, last_response.status
146
+ assert_equal "text/plain", last_response.headers['Content-Type']
147
+ assert_match "RuntimeError: test", last_response.body
148
+ end
149
+
150
+ end
151
+
126
152
  end
@@ -130,15 +130,17 @@ class Deas::Server::Configuration
130
130
  assert_equal '@_out_buf', subject.settings[:erb][:outvar]
131
131
  end
132
132
 
133
- should "add the Deas::Logging middleware to the end of the middlewares" do
133
+ should "add the Logging and ShowExceptions middleware to the end" do
134
+ num_middlewares = subject.middlewares.size
134
135
  assert subject.verbose_logging
135
- assert_equal 1, subject.middlewares.size
136
- assert_not_equal [Deas::VerboseLogging], subject.middlewares.last
136
+ assert_not_equal [Deas::ShowExceptions], subject.middlewares[-2]
137
+ assert_not_equal [Deas::VerboseLogging], subject.middlewares[-1]
137
138
 
138
139
  subject.validate!
139
140
 
140
- assert_equal 2, subject.middlewares.size
141
- assert_equal [Deas::VerboseLogging], subject.middlewares.last
141
+ assert_equal (num_middlewares+2), subject.middlewares.size
142
+ assert_equal [Deas::ShowExceptions], subject.middlewares[-2]
143
+ assert_equal [Deas::VerboseLogging], subject.middlewares[-1]
142
144
  end
143
145
 
144
146
  end
@@ -0,0 +1,36 @@
1
+ require 'assert'
2
+ require 'rack/utils'
3
+ require 'deas/show_exceptions'
4
+
5
+ class Deas::ShowExceptions
6
+
7
+ class BaseTests < Assert::Context
8
+ desc "Deas::ShowExceptions"
9
+ setup do
10
+ exception = nil
11
+ begin; raise 'test'; rescue Exception => exception; end
12
+ @app = proc do |env|
13
+ env['sinatra.error'] = exception
14
+ [ 500, {}, [] ]
15
+ end
16
+ @exception = exception
17
+ @show_exceptions = Deas::ShowExceptions.new(@app)
18
+ end
19
+ subject{ @show_exceptions }
20
+
21
+ should have_imeths :call, :call!
22
+
23
+ should "return a body that contains details about the exception" do
24
+ status, headers, body = subject.call({})
25
+ expected_body = "#{@exception.class}: #{@exception.message}\n" \
26
+ "#{@exception.backtrace.join("\n")}"
27
+ expected_body_size = Rack::Utils.bytesize(expected_body).to_s
28
+
29
+ assert_equal expected_body_size, headers['Content-Length']
30
+ assert_equal "text/plain", headers['Content-Type']
31
+ assert_equal [expected_body], body
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -41,13 +41,16 @@ module Deas::SinatraApp
41
41
  assert_equal 'path/to/somewhere/public', settings.public_folder.to_s
42
42
  assert_equal 'path/to/somewhere/views', settings.views.to_s
43
43
  assert_equal true, settings.dump_errors
44
- assert_equal false, settings.logging
45
44
  assert_equal false, settings.method_override
46
45
  assert_equal false, settings.sessions
47
- assert_equal true, settings.show_exceptions
48
46
  assert_equal true, settings.static
49
47
  assert_equal true, settings.reload_templates
50
48
  assert_instance_of Deas::NullLogger, settings.logger
49
+
50
+ # settings that are set but can't be changed
51
+ assert_equal false, settings.logging
52
+ assert_equal false, settings.raise_errors
53
+ assert_equal false, settings.show_exceptions
51
54
  end
52
55
  end
53
56
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deas
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 47
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 11
8
+ - 12
9
9
  - 0
10
- version: 0.11.0
10
+ version: 0.12.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-05-17 00:00:00 Z
19
+ date: 2013-05-21 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: ns-options
@@ -139,6 +139,7 @@ files:
139
139
  - lib/deas/route.rb
140
140
  - lib/deas/runner.rb
141
141
  - lib/deas/server.rb
142
+ - lib/deas/show_exceptions.rb
142
143
  - lib/deas/sinatra_app.rb
143
144
  - lib/deas/sinatra_runner.rb
144
145
  - lib/deas/template.rb
@@ -166,6 +167,7 @@ files:
166
167
  - test/unit/runner_tests.rb
167
168
  - test/unit/server_configuration_tests.rb
168
169
  - test/unit/server_tests.rb
170
+ - test/unit/show_exceptions_tests.rb
169
171
  - test/unit/sinatra_app_tests.rb
170
172
  - test/unit/sinatra_runner_tests.rb
171
173
  - test/unit/template_tests.rb
@@ -224,6 +226,7 @@ test_files:
224
226
  - test/unit/runner_tests.rb
225
227
  - test/unit/server_configuration_tests.rb
226
228
  - test/unit/server_tests.rb
229
+ - test/unit/show_exceptions_tests.rb
227
230
  - test/unit/sinatra_app_tests.rb
228
231
  - test/unit/sinatra_runner_tests.rb
229
232
  - test/unit/template_tests.rb