deas 0.13.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/deas/runner.rb +6 -11
- data/lib/deas/server.rb +20 -15
- data/lib/deas/sinatra_app.rb +4 -3
- data/lib/deas/sinatra_runner.rb +30 -4
- data/lib/deas/test_runner.rb +25 -9
- data/lib/deas/version.rb +1 -1
- data/lib/deas/view_handler.rb +5 -2
- data/test/support/{fake_app.rb → fake_sinatra_call.rb} +11 -6
- data/test/support/routes.rb +56 -6
- data/test/support/view_handlers.rb +29 -0
- data/test/support/views/show.html.erb +0 -0
- data/test/support/views/show.json.erb +0 -0
- data/test/support/views/show_json.erb +0 -0
- data/test/system/rack_tests.rb +20 -0
- data/test/unit/error_handler_tests.rb +11 -11
- data/test/unit/logging_tests.rb +2 -2
- data/test/unit/route_tests.rb +1 -1
- data/test/unit/runner_tests.rb +17 -4
- data/test/unit/server_configuration_tests.rb +5 -1
- data/test/unit/server_tests.rb +38 -29
- data/test/unit/sinatra_runner_tests.rb +31 -11
- data/test/unit/template_tests.rb +5 -5
- data/test/unit/view_handler_tests.rb +41 -0
- metadata +12 -6
data/lib/deas/runner.rb
CHANGED
@@ -10,17 +10,12 @@ module Deas
|
|
10
10
|
@handler = @handler_class.new(self)
|
11
11
|
end
|
12
12
|
|
13
|
-
def halt(*args)
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
def
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def redirect(*args)
|
22
|
-
raise NotImplementedError
|
23
|
-
end
|
13
|
+
def halt(*args); raise NotImplementedError; end
|
14
|
+
def redirect(*args); raise NotImplementedError; end
|
15
|
+
def content_type(*args); raise NotImplementedError; end
|
16
|
+
def status(*args); raise NotImplementedError; end
|
17
|
+
def headers(*args); raise NotImplementedError; end
|
18
|
+
def render(*args); raise NotImplementedError; end
|
24
19
|
|
25
20
|
end
|
26
21
|
|
data/lib/deas/server.rb
CHANGED
@@ -31,14 +31,15 @@ module Deas::Server
|
|
31
31
|
option :reload_templates, NsOptions::Boolean, :default => false
|
32
32
|
|
33
33
|
# server handling options
|
34
|
-
option :error_procs, Array,
|
35
|
-
option :init_procs, Array,
|
36
|
-
option :logger,
|
37
|
-
option :middlewares, Array,
|
38
|
-
option :settings, Hash,
|
39
|
-
option :verbose_logging,
|
40
|
-
option :routes, Array,
|
34
|
+
option :error_procs, Array, :default => []
|
35
|
+
option :init_procs, Array, :default => []
|
36
|
+
option :logger, :default => proc{ Deas::NullLogger.new }
|
37
|
+
option :middlewares, Array, :default => []
|
38
|
+
option :settings, Hash, :default => {}
|
39
|
+
option :verbose_logging, :default => true
|
40
|
+
option :routes, Array, :default => []
|
41
41
|
option :view_handler_ns, String
|
42
|
+
option :default_charset, String, :default => 'utf-8'
|
42
43
|
|
43
44
|
attr_reader :template_helpers
|
44
45
|
|
@@ -159,6 +160,10 @@ module Deas::Server
|
|
159
160
|
self.configuration.init_procs << block
|
160
161
|
end
|
161
162
|
|
163
|
+
def error(&block)
|
164
|
+
self.configuration.error_procs << block
|
165
|
+
end
|
166
|
+
|
162
167
|
def template_helpers(*helper_modules)
|
163
168
|
helper_modules.each{ |m| self.configuration.template_helpers << m }
|
164
169
|
self.configuration.template_helpers
|
@@ -168,14 +173,6 @@ module Deas::Server
|
|
168
173
|
self.configuration.template_helpers.include?(helper_module)
|
169
174
|
end
|
170
175
|
|
171
|
-
def error(&block)
|
172
|
-
self.configuration.error_procs << block
|
173
|
-
end
|
174
|
-
|
175
|
-
def logger(*args)
|
176
|
-
self.configuration.logger *args
|
177
|
-
end
|
178
|
-
|
179
176
|
def use(*args)
|
180
177
|
self.configuration.middlewares << args
|
181
178
|
end
|
@@ -192,6 +189,14 @@ module Deas::Server
|
|
192
189
|
self.configuration.verbose_logging *args
|
193
190
|
end
|
194
191
|
|
192
|
+
def logger(*args)
|
193
|
+
self.configuration.logger *args
|
194
|
+
end
|
195
|
+
|
196
|
+
def default_charset(*args)
|
197
|
+
self.configuration.default_charset *args
|
198
|
+
end
|
199
|
+
|
195
200
|
def get(path, handler_class_name)
|
196
201
|
self.route(:get, path, handler_class_name)
|
197
202
|
end
|
data/lib/deas/sinatra_app.rb
CHANGED
@@ -30,9 +30,10 @@ module Deas
|
|
30
30
|
set :show_exceptions, false
|
31
31
|
|
32
32
|
# custom settings
|
33
|
-
set :deas_template_scope,
|
34
|
-
set :deas_error_procs,
|
35
|
-
set :
|
33
|
+
set :deas_template_scope, server_config.template_scope
|
34
|
+
set :deas_error_procs, server_config.error_procs
|
35
|
+
set :deas_default_charset, server_config.default_charset
|
36
|
+
set :logger, server_config.logger
|
36
37
|
|
37
38
|
server_config.settings.each{ |set_args| set *set_args }
|
38
39
|
server_config.middlewares.each{ |use_args| use *use_args }
|
data/lib/deas/sinatra_runner.rb
CHANGED
@@ -34,15 +34,37 @@ module Deas
|
|
34
34
|
@sinatra_call.halt(*args)
|
35
35
|
end
|
36
36
|
|
37
|
+
def redirect(*args)
|
38
|
+
@sinatra_call.redirect(*args)
|
39
|
+
end
|
40
|
+
|
41
|
+
def content_type(*args)
|
42
|
+
return @sinatra_call.content_type if args.empty?
|
43
|
+
|
44
|
+
opts, value = [
|
45
|
+
args.last.kind_of?(::Hash) ? args.pop : {},
|
46
|
+
args.first
|
47
|
+
]
|
48
|
+
@sinatra_call.content_type(value, {
|
49
|
+
:charset => @sinatra_call.settings.deas_default_charset
|
50
|
+
}.merge(opts || {}))
|
51
|
+
end
|
52
|
+
|
53
|
+
def status(*args)
|
54
|
+
@sinatra_call.status(*args)
|
55
|
+
end
|
56
|
+
|
57
|
+
def headers(*args)
|
58
|
+
@sinatra_call.headers(*args)
|
59
|
+
end
|
60
|
+
|
37
61
|
def render(template_name, options = nil, &block)
|
38
62
|
options ||= {}
|
39
63
|
options[:locals] = { :view => @handler }.merge(options[:locals] || {})
|
40
64
|
options[:layout] ||= @handler_class.layouts
|
41
|
-
Deas::Template.new(@sinatra_call, template_name, options).render(&block)
|
42
|
-
end
|
43
65
|
|
44
|
-
|
45
|
-
@sinatra_call.
|
66
|
+
self.content_type(get_content_type(template_name)) if self.content_type.nil?
|
67
|
+
Deas::Template.new(@sinatra_call, template_name, options).render(&block)
|
46
68
|
end
|
47
69
|
|
48
70
|
private
|
@@ -51,5 +73,9 @@ module Deas
|
|
51
73
|
callbacks.each{|proc| @handler.instance_eval(&proc) }
|
52
74
|
end
|
53
75
|
|
76
|
+
def get_content_type(template_name)
|
77
|
+
File.extname(template_name)[1..-1] || 'html'
|
78
|
+
end
|
79
|
+
|
54
80
|
end
|
55
81
|
end
|
data/lib/deas/test_runner.rb
CHANGED
@@ -32,22 +32,38 @@ module Deas
|
|
32
32
|
throw(:halt, args)
|
33
33
|
end
|
34
34
|
|
35
|
-
def render(template_name, options = nil, &block)
|
36
|
-
RenderArgs.new(template_name, options, block)
|
37
|
-
end
|
38
|
-
|
39
35
|
def redirect(path, *halt_args)
|
40
36
|
RedirectArgs.new(path, halt_args)
|
41
37
|
end
|
42
38
|
|
43
|
-
RenderArgs = Struct.new(:template_name, :options, :block)
|
44
|
-
|
45
39
|
class RedirectArgs < Struct.new(:path, :halt_args)
|
46
|
-
def redirect
|
47
|
-
|
48
|
-
|
40
|
+
def redirect?; true; end
|
41
|
+
end
|
42
|
+
|
43
|
+
def content_type(value, opts={})
|
44
|
+
ContentTypeArgs.new(value, opts)
|
45
|
+
end
|
46
|
+
|
47
|
+
ContentTypeArgs = Struct.new(:value, :opts)
|
48
|
+
|
49
|
+
def status(value)
|
50
|
+
StatusArgs.new(value)
|
49
51
|
end
|
50
52
|
|
53
|
+
StatusArgs = Struct.new(:value)
|
54
|
+
|
55
|
+
def headers(value)
|
56
|
+
HeadersArgs.new(value)
|
57
|
+
end
|
58
|
+
|
59
|
+
HeadersArgs = Struct.new(:value)
|
60
|
+
|
61
|
+
def render(template_name, options = nil, &block)
|
62
|
+
RenderArgs.new(template_name, options, block)
|
63
|
+
end
|
64
|
+
|
65
|
+
RenderArgs = Struct.new(:template_name, :options, :block)
|
66
|
+
|
51
67
|
end
|
52
68
|
|
53
69
|
end
|
data/lib/deas/version.rb
CHANGED
data/lib/deas/view_handler.rb
CHANGED
@@ -47,8 +47,11 @@ module Deas
|
|
47
47
|
|
48
48
|
# Helpers
|
49
49
|
|
50
|
-
def halt(*args);
|
51
|
-
def redirect(*args);
|
50
|
+
def halt(*args); @deas_runner.halt(*args); end
|
51
|
+
def redirect(*args); @deas_runner.redirect(*args); end
|
52
|
+
def content_type(*args); @deas_runner.content_type(*args); end
|
53
|
+
def status(*args); @deas_runner.status(*args); end
|
54
|
+
def headers(*args); @deas_runner.headers(*args); end
|
52
55
|
|
53
56
|
def render(*args, &block)
|
54
57
|
@deas_runner.render(*args, &block)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'deas'
|
2
2
|
require 'ostruct'
|
3
3
|
|
4
|
-
class
|
4
|
+
class FakeSinatraCall
|
5
5
|
|
6
6
|
# Mimic's the context that is accessible in a Sinatra' route. Should provide
|
7
7
|
# any methods needed to replace using an actual Sinatra app.
|
@@ -14,7 +14,8 @@ class FakeApp
|
|
14
14
|
@session = @request.session
|
15
15
|
@response = FakeResponse.new
|
16
16
|
@settings = OpenStruct.new(settings.merge({
|
17
|
-
:deas_template_scope => Deas::Template::Scope
|
17
|
+
:deas_template_scope => Deas::Template::Scope,
|
18
|
+
:deas_default_charset => 'utf-8'
|
18
19
|
}))
|
19
20
|
end
|
20
21
|
|
@@ -22,6 +23,14 @@ class FakeApp
|
|
22
23
|
throw :halt, args
|
23
24
|
end
|
24
25
|
|
26
|
+
def redirect(*args)
|
27
|
+
halt 302, { 'Location' => args[0] }
|
28
|
+
end
|
29
|
+
|
30
|
+
def content_type(*args); args; end
|
31
|
+
def status(*args); args; end
|
32
|
+
def headers(*args); args; end
|
33
|
+
|
25
34
|
# return the template name for each nested calls
|
26
35
|
def erb(name, opts, &block)
|
27
36
|
if block
|
@@ -31,10 +40,6 @@ class FakeApp
|
|
31
40
|
end
|
32
41
|
end
|
33
42
|
|
34
|
-
def redirect(*args)
|
35
|
-
halt 302, { 'Location' => args[0] }
|
36
|
-
end
|
37
|
-
|
38
43
|
end
|
39
44
|
|
40
45
|
class FakeRequest < Struct.new(:http_method, :path, :params, :session)
|
data/test/support/routes.rb
CHANGED
@@ -19,12 +19,18 @@ class DeasTestServer
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
get '/show',
|
23
|
-
get '/
|
24
|
-
get '/
|
25
|
-
get '/
|
26
|
-
|
27
|
-
get '/
|
22
|
+
get '/show', 'ShowHandler'
|
23
|
+
get '/show.html', 'ShowHtmlHandler'
|
24
|
+
get '/show.json', 'ShowJsonHandler'
|
25
|
+
get '/show-latin1-json', 'ShowLatinJsonHandler'
|
26
|
+
get '/show-text', 'ShowTextHandler'
|
27
|
+
get '/show-headers-text', 'ShowHeadersTextHandler'
|
28
|
+
|
29
|
+
get '/halt', 'HaltHandler'
|
30
|
+
get '/error', 'ErrorHandler'
|
31
|
+
get '/redirect', 'RedirectHandler'
|
32
|
+
post '/session', 'SetSessionHandler'
|
33
|
+
get '/session', 'UseSessionHandler'
|
28
34
|
|
29
35
|
get '/with_layout', 'WithLayoutHandler'
|
30
36
|
get '/alt_with_layout', 'AlternateWithLayoutHandler'
|
@@ -71,6 +77,50 @@ class ShowHandler
|
|
71
77
|
|
72
78
|
end
|
73
79
|
|
80
|
+
class ShowHtmlHandler
|
81
|
+
include Deas::ViewHandler
|
82
|
+
|
83
|
+
def run!; render 'show.html'; end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
class ShowJsonHandler
|
88
|
+
include Deas::ViewHandler
|
89
|
+
|
90
|
+
def run!; render 'show.json'; end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
class ShowLatinJsonHandler
|
95
|
+
include Deas::ViewHandler
|
96
|
+
|
97
|
+
def run!
|
98
|
+
content_type :json, :charset => 'latin1'
|
99
|
+
render 'show_json'
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
class ShowTextHandler
|
105
|
+
include Deas::ViewHandler
|
106
|
+
|
107
|
+
def run!
|
108
|
+
hdrs = {'Content-Type' => 'text/plain'}
|
109
|
+
halt 200, hdrs, render('show.json')
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
class ShowHeadersTextHandler
|
115
|
+
include Deas::ViewHandler
|
116
|
+
|
117
|
+
def run!
|
118
|
+
headers 'Content-Type' => 'text/plain'
|
119
|
+
render('show.json')
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
74
124
|
class HaltHandler
|
75
125
|
include Deas::ViewHandler
|
76
126
|
|
@@ -61,3 +61,32 @@ class HaltViewHandler
|
|
61
61
|
end
|
62
62
|
|
63
63
|
end
|
64
|
+
|
65
|
+
class ContentTypeViewHandler
|
66
|
+
include Deas::ViewHandler
|
67
|
+
|
68
|
+
def run!
|
69
|
+
content_type 'text/plain', :charset => 'latin1'
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
class StatusViewHandler
|
75
|
+
include Deas::ViewHandler
|
76
|
+
|
77
|
+
def run!
|
78
|
+
status 422
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
class HeadersViewHandler
|
84
|
+
include Deas::ViewHandler
|
85
|
+
|
86
|
+
def run!
|
87
|
+
headers \
|
88
|
+
'other' => "other",
|
89
|
+
'a-header' => 'some value'
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
data/test/system/rack_tests.rb
CHANGED
@@ -25,6 +25,26 @@ module Deas
|
|
25
25
|
assert_equal expected_body, last_response.body
|
26
26
|
end
|
27
27
|
|
28
|
+
should "set the content type appropriately" do
|
29
|
+
get '/show'
|
30
|
+
assert_equal 'text/html;charset=utf-8', last_response.headers['Content-Type']
|
31
|
+
|
32
|
+
get '/show.html'
|
33
|
+
assert_equal 'text/html;charset=utf-8', last_response.headers['Content-Type']
|
34
|
+
|
35
|
+
get '/show.json'
|
36
|
+
assert_equal 'application/json;charset=utf-8', last_response.headers['Content-Type']
|
37
|
+
|
38
|
+
get '/show-latin1-json'
|
39
|
+
assert_equal 'application/json;charset=latin1', last_response.headers['Content-Type']
|
40
|
+
|
41
|
+
get '/show-text'
|
42
|
+
assert_equal 'text/plain', last_response.headers['Content-Type']
|
43
|
+
|
44
|
+
get '/show-headers-text'
|
45
|
+
assert_equal 'text/plain', last_response.headers['Content-Type']
|
46
|
+
end
|
47
|
+
|
28
48
|
should "allow halting with a custom response" do
|
29
49
|
get '/halt', 'with' => 234
|
30
50
|
|
@@ -7,8 +7,8 @@ class Deas::ErrorHandler
|
|
7
7
|
desc "Deas::ErrorHandler"
|
8
8
|
setup do
|
9
9
|
@exception = RuntimeError.new
|
10
|
-
@
|
11
|
-
@error_handler = Deas::ErrorHandler.new(@exception, @
|
10
|
+
@fake_sinatra_call = FakeSinatraCall.new
|
11
|
+
@error_handler = Deas::ErrorHandler.new(@exception, @fake_sinatra_call, [])
|
12
12
|
end
|
13
13
|
subject{ @error_handler }
|
14
14
|
|
@@ -25,12 +25,12 @@ class Deas::ErrorHandler
|
|
25
25
|
"my return value"
|
26
26
|
end ]
|
27
27
|
|
28
|
-
@error_handler = Deas::ErrorHandler.new(@exception, @
|
28
|
+
@error_handler = Deas::ErrorHandler.new(@exception, @fake_sinatra_call, @error_procs)
|
29
29
|
@response = @error_handler.run
|
30
30
|
end
|
31
31
|
|
32
32
|
should "run the proc in the context of the app" do
|
33
|
-
assert_equal @exception, @
|
33
|
+
assert_equal @exception, @fake_sinatra_call.settings.exception_that_occurred
|
34
34
|
end
|
35
35
|
|
36
36
|
should "return whatever the proc returns" do
|
@@ -57,14 +57,14 @@ class Deas::ErrorHandler
|
|
57
57
|
end
|
58
58
|
]
|
59
59
|
|
60
|
-
@error_handler = Deas::ErrorHandler.new(@exception, @
|
60
|
+
@error_handler = Deas::ErrorHandler.new(@exception, @fake_sinatra_call, @error_procs)
|
61
61
|
@response = @error_handler.run
|
62
62
|
end
|
63
63
|
|
64
64
|
should "run all the error procs" do
|
65
|
-
assert_equal true, @
|
66
|
-
assert_equal true, @
|
67
|
-
assert_equal true, @
|
65
|
+
assert_equal true, @fake_sinatra_call.settings.first_proc_run
|
66
|
+
assert_equal true, @fake_sinatra_call.settings.second_proc_run
|
67
|
+
assert_equal true, @fake_sinatra_call.settings.third_proc_run
|
68
68
|
end
|
69
69
|
|
70
70
|
should "return the last non-nil response" do
|
@@ -86,13 +86,13 @@ class Deas::ErrorHandler
|
|
86
86
|
end
|
87
87
|
]
|
88
88
|
|
89
|
-
@error_handler = Deas::ErrorHandler.new(@exception, @
|
89
|
+
@error_handler = Deas::ErrorHandler.new(@exception, @fake_sinatra_call, @error_procs)
|
90
90
|
@response = catch(:halt){ @error_handler.run }
|
91
91
|
end
|
92
92
|
|
93
93
|
should "run error procs until one halts" do
|
94
|
-
assert_equal true, @
|
95
|
-
assert_nil @
|
94
|
+
assert_equal true, @fake_sinatra_call.settings.first_proc_run
|
95
|
+
assert_nil @fake_sinatra_call.settings.second_proc_run
|
96
96
|
end
|
97
97
|
|
98
98
|
should "return whatever was halted" do
|
data/test/unit/logging_tests.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'assert'
|
2
|
-
require 'test/support/
|
2
|
+
require 'test/support/fake_sinatra_call'
|
3
3
|
require 'deas/logging'
|
4
4
|
|
5
5
|
module Deas::Logging
|
@@ -7,7 +7,7 @@ module Deas::Logging
|
|
7
7
|
class BaseTests < Assert::Context
|
8
8
|
desc "Deas::Logging"
|
9
9
|
setup do
|
10
|
-
@app =
|
10
|
+
@app = FakeSinatraCall.new
|
11
11
|
end
|
12
12
|
subject{ Deas::Logging }
|
13
13
|
|
data/test/unit/route_tests.rb
CHANGED
data/test/unit/runner_tests.rb
CHANGED
@@ -13,19 +13,32 @@ class Deas::Runner
|
|
13
13
|
|
14
14
|
should have_reader :app_settings
|
15
15
|
should have_readers :request, :response, :params, :logger, :session
|
16
|
+
should have_imeths :halt, :redirect, :content_type, :status, :render
|
16
17
|
|
17
18
|
should "raise NotImplementedError with #halt" do
|
18
19
|
assert_raises(NotImplementedError){ subject.halt }
|
19
20
|
end
|
20
21
|
|
21
|
-
should "raise NotImplementedError with #render" do
|
22
|
-
assert_raises(NotImplementedError){ subject.render }
|
23
|
-
end
|
24
|
-
|
25
22
|
should "raise NotImplementedError with #redirect" do
|
26
23
|
assert_raises(NotImplementedError){ subject.redirect }
|
27
24
|
end
|
28
25
|
|
26
|
+
should "raise NotImplementedError with #content_type" do
|
27
|
+
assert_raises(NotImplementedError){ subject.content_type }
|
28
|
+
end
|
29
|
+
|
30
|
+
should "raise NotImplementedError with #status" do
|
31
|
+
assert_raises(NotImplementedError){ subject.status }
|
32
|
+
end
|
33
|
+
|
34
|
+
should "raise NotImplementedError with #headers" do
|
35
|
+
assert_raises(NotImplementedError){ subject.headers }
|
36
|
+
end
|
37
|
+
|
38
|
+
should "raise NotImplementedError with #render" do
|
39
|
+
assert_raises(NotImplementedError){ subject.render }
|
40
|
+
end
|
41
|
+
|
29
42
|
end
|
30
43
|
|
31
44
|
end
|
@@ -22,7 +22,7 @@ class Deas::Server::Configuration
|
|
22
22
|
|
23
23
|
# server handling options
|
24
24
|
should have_imeths :error_procs, :init_procs, :logger, :middlewares, :settings
|
25
|
-
should have_imeths :verbose_logging, :routes, :view_handler_ns
|
25
|
+
should have_imeths :verbose_logging, :routes, :view_handler_ns, :default_charset
|
26
26
|
|
27
27
|
should have_reader :template_helpers
|
28
28
|
should have_imeths :valid?, :validate!
|
@@ -78,6 +78,10 @@ class Deas::Server::Configuration
|
|
78
78
|
assert_nothing_raised{ config.root '/path/to/root'; config.validate! }
|
79
79
|
end
|
80
80
|
|
81
|
+
should "use `utf-8` as the default_charset by default" do
|
82
|
+
assert_equal 'utf-8', subject.default_charset
|
83
|
+
end
|
84
|
+
|
81
85
|
end
|
82
86
|
|
83
87
|
class ValidationTests < BaseTests
|
data/test/unit/server_tests.rb
CHANGED
@@ -21,9 +21,10 @@ module Deas::Server
|
|
21
21
|
should have_imeths :static_files, :reload_templates
|
22
22
|
|
23
23
|
# DSL for server handling
|
24
|
-
should have_imeths :init, :template_helpers, :template_helper
|
25
|
-
should have_imeths :
|
26
|
-
should have_imeths :
|
24
|
+
should have_imeths :init, :error, :template_helpers, :template_helper?
|
25
|
+
should have_imeths :use, :set, :view_handler_ns, :verbose_logging, :logger
|
26
|
+
should have_imeths :default_charset
|
27
|
+
should have_imeths :get, :post, :put, :patch, :delete, :redirect, :route
|
27
28
|
|
28
29
|
should "allow setting it's configuration options" do
|
29
30
|
config = subject.configuration
|
@@ -58,6 +59,18 @@ module Deas::Server
|
|
58
59
|
subject.reload_templates true
|
59
60
|
assert_equal true, config.reload_templates
|
60
61
|
|
62
|
+
assert_equal 0, config.init_procs.size
|
63
|
+
init_proc = proc{ }
|
64
|
+
subject.init(&init_proc)
|
65
|
+
assert_equal 1, config.init_procs.size
|
66
|
+
assert_equal init_proc, config.init_procs.first
|
67
|
+
|
68
|
+
assert_equal 0, config.error_procs.size
|
69
|
+
error_proc = proc{ }
|
70
|
+
subject.error(&error_proc)
|
71
|
+
assert_equal 1, config.error_procs.size
|
72
|
+
assert_equal error_proc, config.error_procs.first
|
73
|
+
|
61
74
|
subject.use 'MyMiddleware'
|
62
75
|
assert_equal [ ['MyMiddleware'] ], config.middlewares
|
63
76
|
|
@@ -68,11 +81,28 @@ module Deas::Server
|
|
68
81
|
subject.logger stdout_logger
|
69
82
|
assert_equal stdout_logger, config.logger
|
70
83
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
84
|
+
subject.default_charset 'latin1'
|
85
|
+
assert_equal 'latin1', config.default_charset
|
86
|
+
end
|
87
|
+
|
88
|
+
should "add and query helper modules" do
|
89
|
+
subject.template_helpers(helper_module = Module.new)
|
90
|
+
assert subject.template_helper?(helper_module)
|
91
|
+
end
|
92
|
+
|
93
|
+
should "set a namespace and use it when defining routes" do
|
94
|
+
subject.view_handler_ns 'MyStuff'
|
95
|
+
assert_equal 'MyStuff', subject.configuration.view_handler_ns
|
96
|
+
|
97
|
+
# should use the ns
|
98
|
+
subject.route(:get, '/ns_test', 'NsTest')
|
99
|
+
route = subject.configuration.routes.last
|
100
|
+
assert_equal 'MyStuff::NsTest', route.handler_class_name
|
101
|
+
|
102
|
+
# should ignore the ns when the leading colons are present
|
103
|
+
subject.route(:post, '/no_ns_test', '::NoNsTest')
|
104
|
+
route = subject.configuration.routes.last
|
105
|
+
assert_equal '::NoNsTest', route.handler_class_name
|
76
106
|
end
|
77
107
|
|
78
108
|
should "add a GET route using #get" do
|
@@ -146,27 +176,6 @@ module Deas::Server
|
|
146
176
|
assert_equal 'GetInfo', route.handler_class_name
|
147
177
|
end
|
148
178
|
|
149
|
-
should "set a namespace with #view_handler_ns and " \
|
150
|
-
"use it when defining routes" do
|
151
|
-
subject.view_handler_ns 'MyStuff'
|
152
|
-
assert_equal 'MyStuff', subject.configuration.view_handler_ns
|
153
|
-
|
154
|
-
# should use the ns
|
155
|
-
subject.route(:get, '/ns_test', 'NsTest')
|
156
|
-
route = subject.configuration.routes.last
|
157
|
-
assert_equal 'MyStuff::NsTest', route.handler_class_name
|
158
|
-
|
159
|
-
# should ignore the ns when the leading colons are present
|
160
|
-
subject.route(:post, '/no_ns_test', '::NoNsTest')
|
161
|
-
route = subject.configuration.routes.last
|
162
|
-
assert_equal '::NoNsTest', route.handler_class_name
|
163
|
-
end
|
164
|
-
|
165
|
-
should "add and query helper modules" do
|
166
|
-
subject.template_helpers(helper_module = Module.new)
|
167
|
-
assert subject.template_helper?(helper_module)
|
168
|
-
end
|
169
|
-
|
170
179
|
end
|
171
180
|
|
172
181
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'deas/sinatra_runner'
|
3
3
|
require 'deas/template'
|
4
|
-
require 'test/support/
|
4
|
+
require 'test/support/fake_sinatra_call'
|
5
5
|
require 'test/support/view_handlers'
|
6
6
|
|
7
7
|
class Deas::SinatraRunner
|
@@ -9,13 +9,13 @@ class Deas::SinatraRunner
|
|
9
9
|
class BaseTests < Assert::Context
|
10
10
|
desc "Deas::SinatraRunner"
|
11
11
|
setup do
|
12
|
-
@fake_sinatra_call =
|
12
|
+
@fake_sinatra_call = FakeSinatraCall.new
|
13
13
|
@runner = Deas::SinatraRunner.new(FlagViewHandler, @fake_sinatra_call)
|
14
14
|
end
|
15
15
|
subject{ @runner }
|
16
16
|
|
17
|
-
should have_imeths :run, :request, :response, :params, :logger, :
|
18
|
-
should have_imeths :
|
17
|
+
should have_imeths :run, :request, :response, :params, :logger, :session
|
18
|
+
should have_imeths :halt, :redirect, :content_type, :status, :render
|
19
19
|
|
20
20
|
should "return the sinatra_call's request with #request" do
|
21
21
|
assert_equal @fake_sinatra_call.request, subject.request
|
@@ -42,6 +42,33 @@ class Deas::SinatraRunner
|
|
42
42
|
assert_equal [ 'test' ], return_value
|
43
43
|
end
|
44
44
|
|
45
|
+
should "call the sinatra_call's redirect method with #redirect" do
|
46
|
+
return_value = catch(:halt){ subject.redirect('http://google.com') }
|
47
|
+
expected = [ 302, { 'Location' => 'http://google.com' } ]
|
48
|
+
|
49
|
+
assert_equal expected, return_value
|
50
|
+
end
|
51
|
+
|
52
|
+
should "call the sinatra_call's content_type method using the default_charset" do
|
53
|
+
expected = @fake_sinatra_call.content_type('text/plain', :charset => 'utf-8')
|
54
|
+
assert_equal expected, subject.content_type('text/plain')
|
55
|
+
|
56
|
+
expected = @fake_sinatra_call.content_type('text/plain', :charset => 'latin1')
|
57
|
+
assert_equal expected, subject.content_type('text/plain', :charset => 'latin1')
|
58
|
+
end
|
59
|
+
|
60
|
+
should "call the sinatra_call's status to set the response status" do
|
61
|
+
assert_equal [422], subject.status(422)
|
62
|
+
end
|
63
|
+
|
64
|
+
should "call the sinatra_call's status to set the response status" do
|
65
|
+
exp_headers = {
|
66
|
+
'a-header' => 'some value',
|
67
|
+
'other' => 'other'
|
68
|
+
}
|
69
|
+
assert_equal [exp_headers], subject.headers(exp_headers)
|
70
|
+
end
|
71
|
+
|
45
72
|
should "render the template with a :view local and the handler layouts with #render" do
|
46
73
|
exp_handler = FlagViewHandler.new(subject)
|
47
74
|
exp_layouts = FlagViewHandler.layouts
|
@@ -53,13 +80,6 @@ class Deas::SinatraRunner
|
|
53
80
|
assert_equal exp_result, subject.render('index')
|
54
81
|
end
|
55
82
|
|
56
|
-
should "call the sinatra_call's redirect method with #redirect" do
|
57
|
-
return_value = catch(:halt){ subject.redirect('http://google.com') }
|
58
|
-
expected = [ 302, { 'Location' => 'http://google.com' } ]
|
59
|
-
|
60
|
-
assert_equal expected, return_value
|
61
|
-
end
|
62
|
-
|
63
83
|
end
|
64
84
|
|
65
85
|
class RunTests < BaseTests
|
data/test/unit/template_tests.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'deas/template'
|
3
|
-
require 'test/support/
|
3
|
+
require 'test/support/fake_sinatra_call'
|
4
4
|
|
5
5
|
class Deas::Template
|
6
6
|
|
7
7
|
class BaseTests < Assert::Context
|
8
8
|
desc "Deas::Template"
|
9
9
|
setup do
|
10
|
-
@fake_sinatra_call =
|
10
|
+
@fake_sinatra_call = FakeSinatraCall.new
|
11
11
|
@template = Deas::Template.new(@fake_sinatra_call, 'users/index')
|
12
12
|
end
|
13
13
|
subject{ @template }
|
@@ -23,9 +23,9 @@ class Deas::Template
|
|
23
23
|
end
|
24
24
|
|
25
25
|
should "know a named template's render engine" do
|
26
|
-
|
26
|
+
fake_sinatra_call = FakeSinatraCall.new(:views => TEST_SUPPORT_ROOT.join('views'))
|
27
27
|
|
28
|
-
views_exist = Deas::Template.new(
|
28
|
+
views_exist = Deas::Template.new(fake_sinatra_call, 'whatever')
|
29
29
|
assert_equal 'erb', views_exist.engine('layout1')
|
30
30
|
assert_equal 'haml', views_exist.engine('haml_layout1')
|
31
31
|
assert_equal 'other', views_exist.engine('some.html.file')
|
@@ -33,7 +33,7 @@ class Deas::Template
|
|
33
33
|
assert_equal 'erb', views_exist.engine('some_no_engine_extension')
|
34
34
|
assert_equal 'erb', views_exist.engine('does_not_exist')
|
35
35
|
|
36
|
-
views_no_exist = Deas::Template.new(
|
36
|
+
views_no_exist = Deas::Template.new(fake_sinatra_call, 'whatever', {
|
37
37
|
:views => '/does/not/exist'
|
38
38
|
})
|
39
39
|
assert_equal 'erb', views_no_exist.engine('layout1')
|
@@ -173,4 +173,45 @@ module Deas::ViewHandler
|
|
173
173
|
|
174
174
|
end
|
175
175
|
|
176
|
+
class ContentTypeTests < BaseTests
|
177
|
+
desc "content_type"
|
178
|
+
|
179
|
+
should "should set the response content_type/charset" do
|
180
|
+
runner = test_runner(ContentTypeViewHandler)
|
181
|
+
content_type_args = runner.run
|
182
|
+
|
183
|
+
assert_equal 'text/plain', content_type_args.value
|
184
|
+
assert_equal({:charset => 'latin1'}, content_type_args.opts)
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
class StatusTests < BaseTests
|
190
|
+
desc "status"
|
191
|
+
|
192
|
+
should "should set the response status" do
|
193
|
+
runner = test_runner(StatusViewHandler)
|
194
|
+
status_args = runner.run
|
195
|
+
|
196
|
+
assert_equal 422, status_args.value
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
class HeadersTests < BaseTests
|
202
|
+
desc "headers"
|
203
|
+
|
204
|
+
should "should set the response status" do
|
205
|
+
runner = test_runner(HeadersViewHandler)
|
206
|
+
headers_args = runner.run
|
207
|
+
exp_headers = {
|
208
|
+
'a-header' => 'some value',
|
209
|
+
'other' => 'other'
|
210
|
+
}
|
211
|
+
|
212
|
+
assert_equal exp_headers, headers_args.value
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
176
217
|
end
|
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:
|
4
|
+
hash: 39
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 14
|
9
|
+
- 0
|
10
|
+
version: 0.14.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -163,7 +163,7 @@ files:
|
|
163
163
|
- lib/deas/view_handler.rb
|
164
164
|
- log/.gitkeep
|
165
165
|
- test/helper.rb
|
166
|
-
- test/support/
|
166
|
+
- test/support/fake_sinatra_call.rb
|
167
167
|
- test/support/routes.rb
|
168
168
|
- test/support/view_handlers.rb
|
169
169
|
- test/support/views/_info.erb
|
@@ -173,6 +173,9 @@ files:
|
|
173
173
|
- test/support/views/layout2.erb
|
174
174
|
- test/support/views/layout3.erb
|
175
175
|
- test/support/views/show.erb
|
176
|
+
- test/support/views/show.html.erb
|
177
|
+
- test/support/views/show.json.erb
|
178
|
+
- test/support/views/show_json.erb
|
176
179
|
- test/support/views/some.html.file.other
|
177
180
|
- test/support/views/some_file.engine
|
178
181
|
- test/support/views/some_no_engine_extension
|
@@ -227,7 +230,7 @@ specification_version: 3
|
|
227
230
|
summary: Handler-based web framework powered by Sinatra
|
228
231
|
test_files:
|
229
232
|
- test/helper.rb
|
230
|
-
- test/support/
|
233
|
+
- test/support/fake_sinatra_call.rb
|
231
234
|
- test/support/routes.rb
|
232
235
|
- test/support/view_handlers.rb
|
233
236
|
- test/support/views/_info.erb
|
@@ -237,6 +240,9 @@ test_files:
|
|
237
240
|
- test/support/views/layout2.erb
|
238
241
|
- test/support/views/layout3.erb
|
239
242
|
- test/support/views/show.erb
|
243
|
+
- test/support/views/show.html.erb
|
244
|
+
- test/support/views/show.json.erb
|
245
|
+
- test/support/views/show_json.erb
|
240
246
|
- test/support/views/some.html.file.other
|
241
247
|
- test/support/views/some_file.engine
|
242
248
|
- test/support/views/some_no_engine_extension
|