deas 0.40.0 → 0.41.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.
@@ -2,19 +2,19 @@ module Deas
2
2
 
3
3
  class ServerData
4
4
 
5
- # The server uses this to "compile" its configuration for speed. NsOptions
6
- # is relatively slow everytime an option is read. To avoid this, we read the
7
- # options one time here and memoize their values. This way, we don't pay the
8
- # NsOptions overhead when reading them while handling a request.
5
+ # The server uses this to "compile" the common configuration data used
6
+ # by the server instances, error handlers and routes. The goal here is
7
+ # to provide these with a simplified interface with the minimal data needed
8
+ # and to decouple the configuration from each thing that needs its data.
9
9
 
10
- attr_reader :error_procs, :logger, :router, :template_source
10
+ attr_reader :error_procs, :template_source, :logger, :router
11
11
 
12
12
  def initialize(args = nil)
13
13
  args ||= {}
14
14
  @error_procs = args[:error_procs] || []
15
+ @template_source = args[:template_source]
15
16
  @logger = args[:logger]
16
17
  @router = args[:router]
17
- @template_source = args[:template_source]
18
18
  end
19
19
 
20
20
  end
@@ -11,23 +11,27 @@ module Deas
11
11
  # This is generic server initialization stuff. Eventually do this in the
12
12
  # server's initialization logic more like Sanford does.
13
13
  server_config.validate!
14
- server_data = ServerData.new(server_config.to_hash)
14
+ server_data = ServerData.new({
15
+ :error_procs => server_config.error_procs,
16
+ :logger => server_config.logger,
17
+ :router => server_config.router,
18
+ :template_source => server_config.template_source
19
+ })
15
20
 
16
21
  Sinatra.new do
17
22
  # built-in settings
18
23
  set :environment, server_config.env
19
24
  set :root, server_config.root
20
- set :public_folder, server_config.public_root
21
25
  set :views, server_config.views_root
26
+ set :public_folder, server_config.public_root
27
+ set :default_encoding, server_config.default_encoding
22
28
  set :dump_errors, server_config.dump_errors
23
29
  set :method_override, server_config.method_override
30
+ set :reload_templates, server_config.reload_templates
24
31
  set :sessions, server_config.sessions
25
32
  set :static, server_config.static_files
26
- set :reload_templates, server_config.reload_templates
27
- set :default_encoding, server_config.default_encoding
28
- set :logging, false
29
33
 
30
- # TODO: sucks to have to do this but b/c or Rack there is no better way
34
+ # TODO: sucks to have to do this but b/c of Rack there is no better way
31
35
  # to make the server data available to middleware. We should remove this
32
36
  # once we remove Sinatra. Whatever rack app implemenation will needs to
33
37
  # provide the server data or maybe the server data *will be* the rack app.
@@ -39,6 +43,9 @@ module Deas
39
43
  set :raise_errors, false
40
44
  set :show_exceptions, false
41
45
 
46
+ # turn off logging b/c Deas handles its own logging logic
47
+ set :logging, false
48
+
42
49
  # TODO: rework with `server_config.default_encoding` once we move off of using Sinatra
43
50
  # TODO: could maybe move into a deas-json mixin once off of Sinatra
44
51
  # Add charset to json content type responses - by default only added to these:
@@ -87,7 +87,7 @@ module Deas
87
87
  super(*[
88
88
  a.first.instance_of?(::Fixnum) ? a.shift : nil,
89
89
  a.first.kind_of?(::Hash) ? a.shift : nil,
90
- a.first.respond_to?(:each) ? a.shift : nil
90
+ a.shift
91
91
  ])
92
92
  end
93
93
  end
data/lib/deas/url.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Deas
2
+
2
3
  class Url
3
4
 
4
5
  def self.http_query(hash, &escape_value_proc)
@@ -17,56 +18,55 @@ module Deas
17
18
  @escape_query_value_proc = options[:escape_query_value]
18
19
  end
19
20
 
20
- def path_for(*args)
21
- hashed, ordered = [
22
- args.last.kind_of?(::Hash) ? args.pop : {},
23
- args
24
- ]
25
- apply_ordered(apply_hashed(@path, hashed), ordered)
26
- end
21
+ def path_for(params = {})
22
+ raise NonHashParamsError if !params.kind_of?(::Hash)
27
23
 
28
- private
24
+ h = params.dup # don't alter the given params
25
+ c = h.delete(:captures) || h.delete('captures') || []
26
+ s = h.delete(:splat) || h.delete('splat') || []
27
+ a = h.delete(:'#') || h.delete('#') || nil
29
28
 
30
- def apply_ordered(path, params)
31
- params.inject(path){ |p, v| p.sub(/\*+|\:\w+/i, v.to_s) }.gsub(/\/\/+/, '/')
29
+ # ignore captures when setting params
30
+ # remove duplicate forward slashes
31
+ set_anchor(set_extra(set_named(set_splat(@path, s), h), h), a).gsub(/\/\/+/, '/')
32
32
  end
33
33
 
34
- def apply_hashed(path, params)
35
- # don't alter the given params
36
- h = params.dup
37
-
38
- # ignore captures in applying params
39
- captures = h.delete(:captures) || h.delete('captures') || []
40
- splat = h.delete(:splat) || h.delete('splat') || []
41
- anchor = h.delete(:'#') || h.delete('#') || nil
42
-
43
- apply_anchor(apply_extra(apply_named(apply_splat(path, splat), h), h), anchor)
44
- end
34
+ private
45
35
 
46
- def apply_splat(path, params)
47
- params.inject(path){ |p, v| p.sub(/\*+/, v.to_s) }
36
+ def set_splat(path, params)
37
+ params.inject(path) do |path_string, value|
38
+ path_string.sub(/\*+/, value.to_s)
39
+ end
48
40
  end
49
41
 
50
- def apply_named(path, params)
51
- params.inject(path) do |p, (k, v)|
52
- if p.include?(":#{k}")
53
- params.delete(k)
54
- p.gsub(":#{k}", v.to_s)
42
+ def set_named(path, params)
43
+ params.inject(path) do |path_string, (name, value)|
44
+ if path_string.include?(":#{name}")
45
+ if (v = value.to_s).empty?
46
+ raise EmptyNamedValueError , "an empty value (`#{value.inspect}`) " \
47
+ "was given for the `#{name}` url param"
48
+ end
49
+ params.delete(name)
50
+ path_string.gsub(":#{name}", v)
55
51
  else
56
- p
52
+ path_string
57
53
  end
58
54
  end
59
55
  end
60
56
 
61
- def apply_extra(path, params)
57
+ def set_extra(path, params)
62
58
  return path if params.empty?
63
59
  query_string = Deas::Url.http_query(params, &self.escape_query_value_proc)
64
60
  "#{path}?#{query_string}"
65
61
  end
66
62
 
67
- def apply_anchor(path, anchor)
63
+ def set_anchor(path, anchor)
68
64
  anchor.to_s.empty? ? path : "#{path}##{anchor}"
69
65
  end
70
66
 
67
+ NonHashParamsError = Class.new(ArgumentError)
68
+ EmptyNamedValueError = Class.new(ArgumentError)
69
+
71
70
  end
71
+
72
72
  end
data/lib/deas/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Deas
2
- VERSION = "0.40.0"
2
+ VERSION = "0.41.0"
3
3
  end
data/test/helper.rb CHANGED
@@ -12,6 +12,15 @@ require 'pry'
12
12
 
13
13
  require 'test/support/factory'
14
14
 
15
+ # 1.8.7 backfills
16
+
17
+ # Array#sample
18
+ if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
19
+ class Array
20
+ alias_method :sample, :choice
21
+ end
22
+ end
23
+
15
24
  require 'fileutils'
16
25
  require 'logger'
17
26
  log_file_path = ROOT.join("log/test.log")
@@ -19,30 +19,32 @@ class DeasTestServer
19
19
  end
20
20
  end
21
21
 
22
- default_request_type_name 'desktop'
23
- add_request_type('regular'){ |r| r.path_info =~ /regular/ }
24
- add_request_type('mobile'){ |r| r.path_info =~ /mobile/ }
22
+ router do
23
+ default_request_type_name 'desktop'
24
+ add_request_type('regular'){ |r| r.path_info =~ /regular/ }
25
+ add_request_type('mobile'){ |r| r.path_info =~ /mobile/ }
25
26
 
26
- get '/show', 'ShowHandler'
27
- get '/show.html', 'ShowHtmlHandler'
28
- get '/show.json', 'ShowJsonHandler'
29
- get '/show-latin1-json', 'ShowLatinJsonHandler'
30
- get '/show-text', 'ShowTextHandler'
31
- get '/show-headers-text', 'ShowHeadersTextHandler'
27
+ get '/show', 'ShowHandler'
28
+ get '/show.html', 'ShowHtmlHandler'
29
+ get '/show.json', 'ShowJsonHandler'
30
+ get '/show-latin1-json', 'ShowLatinJsonHandler'
31
+ get '/show-text', 'ShowTextHandler'
32
+ get '/show-headers-text', 'ShowHeadersTextHandler'
32
33
 
33
- get '/req-type-show/:type', 'regular' => 'ShowHandler',
34
- 'mobile' => 'ShowMobileHandler'
34
+ get '/req-type-show/:type', 'regular' => 'ShowHandler',
35
+ 'mobile' => 'ShowMobileHandler'
35
36
 
36
- get '/halt', 'HaltHandler'
37
- get '/error', 'ErrorHandler'
38
- get '/redirect', 'RedirectHandler'
39
- post '/session', 'SetSessionHandler'
40
- get '/session', 'UseSessionHandler'
37
+ get '/halt', 'HaltHandler'
38
+ get '/error', 'ErrorHandler'
39
+ get '/redirect', 'RedirectHandler'
40
+ post '/session', 'SetSessionHandler'
41
+ get '/session', 'UseSessionHandler'
41
42
 
42
- get '/handler/tests', 'HandlerTestsHandler'
43
+ get '/handler/tests', 'HandlerTestsHandler'
43
44
 
44
- redirect '/route_redirect', '/somewhere'
45
- redirect('/:prefix/redirect'){ "/#{params['prefix']}/somewhere" }
45
+ redirect '/route_redirect', '/somewhere'
46
+ redirect('/:prefix/redirect'){ "/#{params['prefix']}/somewhere" }
47
+ end
46
48
 
47
49
  end
48
50
 
@@ -59,7 +61,9 @@ class DeasDevServer
59
61
 
60
62
  show_exceptions true
61
63
 
62
- get '/error', 'ErrorHandler'
64
+ router do
65
+ get '/error', 'ErrorHandler'
66
+ end
63
67
 
64
68
  end
65
69
 
@@ -128,7 +128,7 @@ module Deas::Logging
128
128
  class VerboseLoggingInitTests < VerboseLoggingTests
129
129
  desc "when init"
130
130
  setup do
131
- @resp_status = @middleware_class::RESPONSE_STATUS_NAMES.keys.choice
131
+ @resp_status = @middleware_class::RESPONSE_STATUS_NAMES.keys.sample
132
132
  @app_response[0] = @resp_status
133
133
 
134
134
  @middleware = Deas::VerboseLogging.new(@app)
@@ -12,7 +12,7 @@ class Deas::RespondWithProxy
12
12
  setup do
13
13
  @status = Factory.integer
14
14
  @headers = { Factory.string => Factory.string }
15
- @body = Factory.string
15
+ @body = [Factory.string]
16
16
  @proxy = Deas::RespondWithProxy.new([@status, @headers, @body])
17
17
  end
18
18
  subject{ @proxy }
@@ -26,7 +26,7 @@ class Deas::Router
26
26
 
27
27
  desc "when init"
28
28
  setup do
29
- @base_url = a_base_url = [Factory.url, nil].choice
29
+ @base_url = a_base_url = [Factory.url, nil].sample
30
30
  @router = @router_class.new{ base_url a_base_url }
31
31
  end
32
32
  subject{ @router }
@@ -405,18 +405,28 @@ class Deas::Router
405
405
  should "build a path for a url given params" do
406
406
  exp_path = subject.prepend_base_url("/info/now")
407
407
  assert_equal exp_path, subject.url_for(:get_info, :for => 'now')
408
- assert_equal exp_path, subject.url_for(:get_info, 'now')
409
408
  end
410
409
 
411
410
  should "'squash' duplicate forward-slashes when building urls" do
412
411
  exp_path = subject.prepend_base_url("/info/now")
413
412
  assert_equal exp_path, subject.url_for(:get_info, :for => '/now')
414
- assert_equal exp_path, subject.url_for(:get_info, '/now')
413
+ end
414
+
415
+ should "complain if buiding a named url with non-hash params" do
416
+ assert_raises ArgumentError do
417
+ subject.url_for(:get_info, ['now', :now, nil].sample)
418
+ end
419
+ end
420
+
421
+ should "complain if given an empty named param value" do
422
+ assert_raises ArgumentError do
423
+ subject.url_for(:get_info, :for => [nil, ''].sample)
424
+ end
415
425
  end
416
426
 
417
427
  should "complain if building a named url that hasn't been defined" do
418
428
  assert_raises ArgumentError do
419
- subject.url_for(:get_all_info, 'now')
429
+ subject.url_for(:not_defined_url)
420
430
  end
421
431
  end
422
432
 
@@ -139,13 +139,12 @@ class Deas::Runner
139
139
  should "know and set its response body" do
140
140
  assert_nil subject.body
141
141
 
142
- exp = Factory.string
142
+ exp = [Factory.string]
143
143
  subject.body exp
144
144
  assert_equal exp, subject.body
145
145
 
146
- assert_raises(ArgumentError) do
147
- subject.body Factory.integer
148
- end
146
+ subject.body exp.first
147
+ assert_equal exp, subject.body
149
148
  end
150
149
 
151
150
  should "know and set its response content type header" do
@@ -463,7 +462,7 @@ class Deas::Runner
463
462
  class SourceRenderTests < RenderSetupTests
464
463
  desc "source render method"
465
464
  setup do
466
- body = @body = Factory.text
465
+ body = @body = [Factory.text]
467
466
  @render_called_with = nil
468
467
  @source = Deas::TemplateSource.new(Factory.path)
469
468
  Assert.stub(@source, :render){ |*args| @render_called_with = args; body }
@@ -672,7 +671,7 @@ class Deas::Runner
672
671
  class NonPartialSendFileBodyTests < SendFileBodyIOTests
673
672
  desc "for non/multi/invalid partial content requests"
674
673
  setup do
675
- range = [nil, 'bytes=', 'bytes=0-1,2-3', 'bytes=3-2', 'bytes=abc'].choice
674
+ range = [nil, 'bytes=', 'bytes=0-1,2-3', 'bytes=3-2', 'bytes=abc'].sample
676
675
  env = range.nil? ? {} : { 'HTTP_RANGE' => range }
677
676
  @body = SendFileBody.new(env, @path_name)
678
677
  end
@@ -18,221 +18,262 @@ module Deas::Server
18
18
  end
19
19
  subject{ @server_class }
20
20
 
21
- should have_imeths :new, :configuration
22
-
23
- # DSL for sinatra-based settings
24
- should have_imeths :env, :root, :public_root, :views_root
25
- should have_imeths :dump_errors, :method_override, :sessions, :show_exceptions
26
- should have_imeths :static_files, :reload_templates
27
-
28
- # DSL for server handling settings
29
- should have_imeths :init, :error, :template_helpers, :template_helper?
30
- should have_imeths :use, :set, :verbose_logging, :logger, :default_encoding
31
- should have_imeths :template_source
32
-
33
- # DSL for server routing settings
34
- should have_imeths :router, :view_handler_ns, :base_url
35
- should have_imeths :url, :url_for
36
- should have_imeths :default_request_type_name, :add_request_type
37
- should have_imeths :request_type_name
38
- should have_imeths :get, :post, :put, :patch, :delete
39
- should have_imeths :route, :redirect
21
+ should have_imeths :new, :config
22
+
23
+ should have_imeths :env, :root, :views_path, :views_root
24
+ should have_imeths :public_path, :public_root, :default_encoding
25
+ should have_imeths :set, :settings, :template_helpers, :template_helper?
26
+ should have_imeths :use, :middlewares, :init, :init_procs, :error, :error_procs
27
+ should have_imeths :template_source, :logger, :router, :url_for
28
+
29
+ should have_imeths :dump_errors, :method_override, :reload_templates
30
+ should have_imeths :sessions, :show_exceptions, :static_files
31
+ should have_imeths :verbose_logging
40
32
 
41
33
  should "use much-plugin" do
42
34
  assert_includes MuchPlugin, Deas::Server
43
35
  end
44
36
 
45
- should "allow setting it's configuration options" do
46
- config = subject.configuration
47
-
48
- subject.env 'staging'
49
- assert_equal 'staging', config.env
50
-
51
- subject.root '/path/to/root'
52
- assert_equal '/path/to/root', config.root.to_s
37
+ should "allow setting its config values" do
38
+ config = subject.config
53
39
 
54
- subject.public_root '/path/to/public'
55
- assert_equal '/path/to/public', config.public_root.to_s
40
+ exp = Factory.string
41
+ subject.env exp
42
+ assert_equal exp, config.env
56
43
 
57
- subject.views_root '/path/to/views'
58
- assert_equal '/path/to/views', config.views_root.to_s
44
+ exp = Factory.path
45
+ subject.root exp
46
+ assert_equal exp, config.root
59
47
 
60
- subject.dump_errors true
61
- assert_equal true, config.dump_errors
48
+ exp = Factory.path
49
+ subject.views_path exp
50
+ assert_equal exp, config.views_path
62
51
 
63
- subject.method_override false
64
- assert_equal false, config.method_override
52
+ exp = Factory.path
53
+ subject.public_path exp
54
+ assert_equal exp, config.public_path
65
55
 
66
- subject.sessions false
67
- assert_equal false, config.sessions
56
+ exp = Factory.string
57
+ subject.default_encoding exp
58
+ assert_equal exp, config.default_encoding
68
59
 
69
- subject.show_exceptions true
70
- assert_equal true, config.show_exceptions
60
+ exp = { Factory.string.to_sym => Factory.string }
61
+ subject.set exp.keys.first, exp.values.first
62
+ assert_equal exp, config.settings
71
63
 
72
- subject.static_files false
73
- assert_equal false, config.static_files
74
-
75
- subject.reload_templates true
76
- assert_equal true, config.reload_templates
64
+ exp = ['MyMiddleware', Factory.string]
65
+ subject.use *exp
66
+ assert_equal [exp], config.middlewares
77
67
 
68
+ exp = proc{ }
78
69
  assert_equal 0, config.init_procs.size
79
- init_proc = proc{ }
80
- subject.init(&init_proc)
70
+ subject.init(&exp)
81
71
  assert_equal 1, config.init_procs.size
82
- assert_equal init_proc, config.init_procs.first
72
+ assert_equal exp, config.init_procs.first
83
73
 
74
+ exp = proc{ }
84
75
  assert_equal 0, config.error_procs.size
85
- error_proc = proc{ }
86
- subject.error(&error_proc)
76
+ subject.error(&exp)
87
77
  assert_equal 1, config.error_procs.size
88
- assert_equal error_proc, config.error_procs.first
78
+ assert_equal exp, config.error_procs.first
79
+
80
+ exp = Deas::TemplateSource.new(Factory.path)
81
+ subject.template_source exp
82
+ assert_equal exp, config.template_source
83
+
84
+ exp = Logger.new(STDOUT)
85
+ subject.logger exp
86
+ assert_equal exp, config.logger
89
87
 
90
- subject.use 'MyMiddleware'
91
- assert_equal [ ['MyMiddleware'] ], config.middlewares
88
+ exp = Factory.boolean
89
+ subject.dump_errors exp
90
+ assert_equal exp, config.dump_errors
92
91
 
93
- subject.set :testing_set_meth, 'it works!'
94
- assert_equal({ :testing_set_meth => 'it works!'}, config.settings)
92
+ exp = Factory.boolean
93
+ subject.method_override exp
94
+ assert_equal exp, config.method_override
95
95
 
96
- stdout_logger = Logger.new(STDOUT)
97
- subject.logger stdout_logger
98
- assert_equal stdout_logger, config.logger
96
+ exp = Factory.boolean
97
+ subject.reload_templates exp
98
+ assert_equal exp, config.reload_templates
99
99
 
100
- a_source = Deas::TemplateSource.new(Factory.path)
101
- subject.template_source a_source
102
- assert_equal a_source, config.template_source
100
+ exp = Factory.boolean
101
+ subject.sessions exp
102
+ assert_equal exp, config.sessions
103
103
 
104
- subject.default_encoding 'latin1'
105
- assert_equal 'latin1', config.default_encoding
104
+ exp = Factory.boolean
105
+ subject.show_exceptions exp
106
+ assert_equal exp, config.show_exceptions
107
+
108
+ exp = Factory.boolean
109
+ subject.static_files exp
110
+ assert_equal exp, config.static_files
111
+
112
+ exp = Factory.boolean
113
+ subject.verbose_logging exp
114
+ assert_equal exp, config.verbose_logging
115
+ end
116
+
117
+ should "demeter its config values that aren't directly set" do
118
+ assert_equal subject.config.views_root, subject.views_root
119
+ assert_equal subject.config.public_root, subject.public_root
120
+ assert_equal subject.config.settings, subject.settings
121
+ assert_equal subject.config.middlewares, subject.middlewares
122
+ assert_equal subject.config.init_procs, subject.init_procs
123
+ assert_equal subject.config.error_procs, subject.error_procs
106
124
  end
107
125
 
108
126
  should "add and query helper modules" do
109
127
  subject.template_helpers(helper_module = Module.new)
110
- assert subject.template_helper?(helper_module)
128
+ assert_true subject.template_helper?(helper_module)
111
129
  end
112
130
 
113
131
  should "have a router by default and allow overriding it" do
114
132
  assert_kind_of Deas::Router, subject.router
115
- assert_equal subject.router.view_handler_ns, subject.view_handler_ns
116
- assert_equal subject.router.base_url, subject.base_url
117
133
 
118
134
  new_router = Deas::Router.new
119
135
  subject.router new_router
136
+ assert_same new_router, subject.config.router
120
137
  assert_same new_router, subject.router
121
138
  end
122
139
 
140
+ should "allow configuring the router by passing a block to `router`" do
141
+ block_scope = nil
142
+ subject.router{ block_scope = self }
143
+ assert_equal subject.router, block_scope
144
+ end
145
+
146
+ should "call the router's `url_for` method" do
147
+ url_for_called_args = nil
148
+ url_for_called_proc = nil
149
+ Assert.stub(subject.router, :url_for) do |*args, &block|
150
+ url_for_called_args = args
151
+ url_for_called_proc = block
152
+ end
153
+
154
+ exp_args = [Factory.string]
155
+ exp_proc = proc{ }
156
+ subject.url_for(*exp_args, &exp_proc)
157
+ assert_equal exp_args, url_for_called_args
158
+ assert_equal exp_proc, url_for_called_proc
159
+ end
160
+
123
161
  end
124
162
 
125
- class ConfigurationTests < UnitTests
126
- desc "Configuration"
163
+ class ConfigTests < UnitTests
164
+ desc "Config"
127
165
  setup do
128
- @configuration = Configuration.new
129
- @configuration.root = TEST_SUPPORT_ROOT
166
+ @config_class = Config
167
+ @config = @config_class.new
130
168
  end
131
- subject{ @configuration }
169
+ subject{ @config }
132
170
 
133
- # sinatra-based options
171
+ should have_accessors :env, :root, :views_path, :public_path, :default_encoding
172
+ should have_accessors :settings, :template_helpers, :middlewares
173
+ should have_accessors :init_procs, :error_procs, :template_source, :logger, :router
134
174
 
135
- should have_imeths :env, :root, :public_root, :views_root
136
- should have_imeths :dump_errors, :method_override, :sessions, :show_exceptions
137
- should have_imeths :static_files, :reload_templates, :default_encoding
175
+ should have_accessors :dump_errors, :method_override, :reload_templates
176
+ should have_accessors :sessions, :show_exceptions, :static_files
177
+ should have_accessors :verbose_logging
138
178
 
139
- # server handling options
179
+ should have_imeths :views_root, :public_root, :urls, :routes
180
+ should have_imeths :valid?, :validate!
140
181
 
141
- should have_imeths :verbose_logging, :logger, :template_source
182
+ should "know its default attr values" do
183
+ assert_equal 'development', @config_class::DEFAULT_ENV
184
+ assert_equal 'views', @config_class::DEFAULT_VIEWS_PATH
185
+ assert_equal 'public', @config_class::DEFAULT_PUBLIC_PATH
186
+ assert_equal 'utf-8', @config_class::DEFAULT_ENCODING
187
+ end
142
188
 
143
- should have_accessors :settings, :init_procs, :error_procs, :template_helpers
144
- should have_accessors :middlewares, :router
145
- should have_imeths :valid?, :validate!, :urls, :routes
146
- should have_imeths :to_hash
189
+ should "default its attrs" do
190
+ exp = @config_class::DEFAULT_ENV
191
+ assert_equal exp, subject.env
147
192
 
148
- should "default the env to 'development'" do
149
- assert_equal 'development', subject.env
150
- end
193
+ exp = ENV['PWD']
194
+ assert_equal exp, subject.root
151
195
 
152
- should "default the public and views folders based off the root" do
153
- assert_equal subject.root.join('public'), subject.public_root
154
- assert_equal subject.root.join('views'), subject.views_root
155
- end
196
+ exp = @config_class::DEFAULT_VIEWS_PATH
197
+ assert_equal exp, subject.views_path
198
+
199
+ exp = @config_class::DEFAULT_PUBLIC_PATH
200
+ assert_equal exp, subject.public_path
201
+
202
+ exp = @config_class::DEFAULT_ENCODING
203
+ assert_equal exp, subject.default_encoding
204
+
205
+ assert_equal Hash.new, subject.settings
206
+ assert_equal [], subject.template_helpers
207
+ assert_equal [], subject.middlewares
208
+ assert_equal [], subject.init_procs
209
+ assert_equal [], subject.error_procs
210
+
211
+ assert_instance_of Deas::NullTemplateSource, subject.template_source
212
+ assert_equal subject.root, subject.template_source.path
213
+
214
+ assert_instance_of Deas::NullLogger, subject.logger
215
+ assert_instance_of Deas::Router, subject.router
156
216
 
157
- should "default the Sinatra flags" do
158
217
  assert_equal false, subject.dump_errors
159
218
  assert_equal true, subject.method_override
219
+ assert_equal false, subject.reload_templates
160
220
  assert_equal false, subject.sessions
161
221
  assert_equal false, subject.show_exceptions
162
222
  assert_equal true, subject.static_files
163
- assert_equal false, subject.reload_templates
223
+ assert_equal true, subject.verbose_logging
164
224
  end
165
225
 
166
- should "default the handling options" do
167
- assert_equal true, subject.verbose_logging
168
- assert_instance_of Deas::NullLogger, subject.logger
169
- assert_instance_of Deas::NullTemplateSource, subject.template_source
226
+ should "know its views root and public root" do
227
+ exp = File.expand_path(subject.views_path.to_s, subject.root.to_s)
228
+ assert_equal exp, subject.views_root
229
+
230
+ exp = File.expand_path(subject.public_path.to_s, subject.root.to_s)
231
+ assert_equal exp, subject.public_root
170
232
  end
171
233
 
172
- should "default its stored configuration" do
173
- assert_empty subject.settings
174
- assert_empty subject.error_procs
175
- assert_empty subject.init_procs
176
- assert_empty subject.template_helpers
177
- assert_empty subject.middlewares
178
- assert_empty subject.routes
179
- assert_empty subject.urls
180
- assert_kind_of Deas::Router, subject.router
234
+ should "demeter its router" do
235
+ assert_equal subject.router.urls, subject.urls
236
+ assert_equal subject.router.routes, subject.routes
181
237
  end
182
238
 
183
239
  should "not be valid until validate! has been run" do
184
- assert_not subject.valid?
240
+ assert_false subject.valid?
185
241
 
186
242
  subject.validate!
187
- assert subject.valid?
243
+ assert_true subject.valid?
188
244
  end
189
245
 
190
- should "complain if validating and `root` isn't set" do
191
- config = Configuration.new
246
+ should "complain if validating and its root value is nil" do
247
+ config = Config.new
248
+ config.root = nil
192
249
  assert_raises(Deas::ServerRootError){ config.validate! }
193
- assert_nothing_raised{ config.root '/path/to/root'; config.validate! }
194
- end
195
-
196
- should "use `utf-8` as the default encoding by default" do
197
- assert_equal 'utf-8', subject.default_encoding
198
- end
199
-
200
- should "include its error procs and router in its `to_hash`" do
201
- config_hash = subject.to_hash
202
-
203
- assert_equal subject.error_procs, config_hash[:error_procs]
204
- assert_equal subject.router, config_hash[:router]
205
250
  end
206
251
 
207
252
  end
208
253
 
209
- class ValidationTests < ConfigurationTests
254
+ class ValidationTests < ConfigTests
210
255
  desc "when successfully validated"
211
256
  setup do
212
- @initialized = false
213
- @other_initialized = false
214
257
  @router = Deas::Router.new
215
-
216
258
  @router_validate_called = false
217
259
  Assert.stub(@router, :validate!){ @router_validate_called = true }
218
260
 
219
- @configuration = Configuration.new.tap do |c|
220
- c.env = 'staging'
221
- c.root = 'path/to/somewhere'
222
- c.dump_errors = true
223
- c.method_override = false
224
- c.sessions = false
261
+ @config = Config.new.tap do |c|
262
+ c.root = Factory.path
225
263
  c.show_exceptions = true
226
- c.static = true
227
- c.reload_templates = true
228
- c.middlewares = [ ['MyMiddleware'] ]
264
+ c.verbose_logging = true
265
+ c.middlewares = Factory.integer(3).times.map{ [Factory.string] }
229
266
  c.router = @router
230
267
  end
231
- @configuration.init_procs << proc{ @initialized = true }
232
- @configuration.init_procs << proc{ @other_initialized = true }
268
+
269
+ @initialized = false
270
+ @config.init_procs << proc{ @initialized = true }
271
+
272
+ @other_initialized = false
273
+ @config.init_procs << proc{ @other_initialized = true }
233
274
  end
234
275
 
235
- should "call init procs" do
276
+ should "call its init procs" do
236
277
  assert_equal false, @initialized
237
278
  assert_equal false, @other_initialized
238
279
 
@@ -250,8 +291,10 @@ module Deas::Server
250
291
  end
251
292
 
252
293
  should "add the Logging and ShowExceptions middleware to the end" do
294
+ assert_true subject.show_exceptions
295
+ assert_true subject.verbose_logging
296
+
253
297
  num_middlewares = subject.middlewares.size
254
- assert subject.verbose_logging
255
298
  assert_not_equal [Deas::ShowExceptions], subject.middlewares[-2]
256
299
  assert_not_equal [Deas::VerboseLogging], subject.middlewares[-1]
257
300
 
@@ -262,6 +305,15 @@ module Deas::Server
262
305
  assert_equal [Deas::VerboseLogging], subject.middlewares[-1]
263
306
  end
264
307
 
308
+ should "only be able to be validated once" do
309
+ called = 0
310
+ subject.init_procs << proc{ called += 1 }
311
+ subject.validate!
312
+ assert_equal 1, called
313
+ subject.validate!
314
+ assert_equal 1, called
315
+ end
316
+
265
317
  end
266
318
 
267
319
  end