deas 0.17.1 → 0.18.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/deas/logging.rb +13 -5
- data/lib/deas/redirect_proxy.rb +16 -2
- data/lib/deas/server.rb +50 -25
- data/lib/deas/url.rb +33 -0
- data/lib/deas/version.rb +1 -1
- data/test/unit/logging_tests.rb +22 -6
- data/test/unit/redirect_proxy_tests.rb +49 -25
- data/test/unit/server_configuration_tests.rb +18 -12
- data/test/unit/server_tests.rb +63 -5
- data/test/unit/sinatra_app_tests.rb +1 -1
- data/test/unit/url_tests.rb +90 -0
- metadata +8 -5
data/lib/deas/logging.rb
CHANGED
@@ -57,7 +57,8 @@ module Deas
|
|
57
57
|
|
58
58
|
RESPONSE_STATUS_NAMES = {
|
59
59
|
200 => 'OK',
|
60
|
-
|
60
|
+
302 => 'FOUND',
|
61
|
+
400 => 'BAD REQUEST',
|
61
62
|
401 => 'UNAUTHORIZED',
|
62
63
|
403 => 'FORBIDDEN',
|
63
64
|
404 => 'NOT FOUND',
|
@@ -75,6 +76,7 @@ module Deas
|
|
75
76
|
end
|
76
77
|
env['deas.logging'] = Proc.new{ |msg| log(msg) }
|
77
78
|
status, headers, body = super(env)
|
79
|
+
log " Redir: #{headers['Location']}" if headers.key?('Location')
|
78
80
|
log "===== Completed in #{env['deas.time_taken']}ms (#{response_display(status)}) ====="
|
79
81
|
[ status, headers, body ]
|
80
82
|
end
|
@@ -93,14 +95,18 @@ module Deas
|
|
93
95
|
env['deas.logging'] = Proc.new{ |msg| } # no-op
|
94
96
|
status, headers, body = super(env)
|
95
97
|
request = Rack::Request.new(env)
|
96
|
-
|
98
|
+
line_attrs = {
|
97
99
|
'method' => request.request_method,
|
98
100
|
'path' => request.path,
|
99
101
|
'handler' => env['deas.handler_class_name'],
|
100
102
|
'params' => env['sinatra.params'],
|
101
103
|
'time' => env['deas.time_taken'],
|
102
104
|
'status' => status
|
103
|
-
}
|
105
|
+
}
|
106
|
+
if headers.key?('Location')
|
107
|
+
line_attrs['redir'] = headers['Location']
|
108
|
+
end
|
109
|
+
log SummaryLine.new(line_attrs)
|
104
110
|
[ status, headers, body ]
|
105
111
|
end
|
106
112
|
|
@@ -108,10 +114,12 @@ module Deas
|
|
108
114
|
|
109
115
|
module SummaryLine
|
110
116
|
def self.keys
|
111
|
-
%w{time status method path handler params}
|
117
|
+
%w{time status method path handler params redir}
|
112
118
|
end
|
113
119
|
def self.new(line_attrs)
|
114
|
-
self.keys.
|
120
|
+
self.keys.select{ |k| line_attrs.key?(k) }.
|
121
|
+
map{ |k| "#{k}=#{line_attrs[k].inspect}" }.
|
122
|
+
join(' ')
|
115
123
|
end
|
116
124
|
end
|
117
125
|
|
data/lib/deas/redirect_proxy.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'deas/view_handler'
|
2
|
+
require 'deas/url'
|
2
3
|
|
3
4
|
module Deas
|
4
5
|
class RedirectProxy
|
@@ -16,12 +17,25 @@ module Deas
|
|
16
17
|
|
17
18
|
def self.name; 'Deas::RedirectHandler'; end
|
18
19
|
|
20
|
+
attr_reader :redirect_path
|
21
|
+
|
22
|
+
def init!
|
23
|
+
@redirect_path = self.instance_eval(&self.class.redirect_path)
|
24
|
+
end
|
25
|
+
|
19
26
|
def run!
|
20
|
-
redirect
|
27
|
+
redirect @redirect_path
|
21
28
|
end
|
22
29
|
|
23
30
|
end
|
24
|
-
|
31
|
+
|
32
|
+
@handler_class.redirect_path = if path.nil?
|
33
|
+
block
|
34
|
+
elsif path.kind_of?(Deas::Url)
|
35
|
+
proc{ path.path_for(params) }
|
36
|
+
else
|
37
|
+
proc{ path }
|
38
|
+
end
|
25
39
|
@handler_class_name = @handler_class.name
|
26
40
|
end
|
27
41
|
|
data/lib/deas/server.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
-
require 'ns-options'
|
2
|
-
require 'ns-options/boolean'
|
3
1
|
require 'pathname'
|
4
2
|
require 'set'
|
3
|
+
require 'ns-options'
|
4
|
+
require 'ns-options/boolean'
|
5
5
|
require 'deas/exceptions'
|
6
6
|
require 'deas/template'
|
7
7
|
require 'deas/logging'
|
8
8
|
require 'deas/redirect_proxy'
|
9
9
|
require 'deas/route_proxy'
|
10
10
|
require 'deas/route'
|
11
|
+
require 'deas/url'
|
11
12
|
require 'deas/show_exceptions'
|
12
13
|
require 'deas/sinatra_app'
|
13
14
|
|
@@ -17,7 +18,8 @@ module Deas::Server
|
|
17
18
|
class Configuration
|
18
19
|
include NsOptions::Proxy
|
19
20
|
|
20
|
-
# Sinatra
|
21
|
+
# Sinatra-based options
|
22
|
+
|
21
23
|
option :env, String, :default => 'development'
|
22
24
|
|
23
25
|
option :root, Pathname, :required => true
|
@@ -30,19 +32,16 @@ module Deas::Server
|
|
30
32
|
option :show_exceptions, NsOptions::Boolean, :default => false
|
31
33
|
option :static_files, NsOptions::Boolean, :default => true
|
32
34
|
option :reload_templates, NsOptions::Boolean, :default => false
|
35
|
+
option :default_charset, String, :default => 'utf-8'
|
33
36
|
|
34
37
|
# server handling options
|
35
|
-
|
36
|
-
option :
|
37
|
-
option :
|
38
|
-
option :middlewares, Array, :default => []
|
39
|
-
option :settings, Hash, :default => {}
|
40
|
-
option :verbose_logging, :default => true
|
41
|
-
option :routes, Array, :default => []
|
38
|
+
|
39
|
+
option :logger, :default => proc{ Deas::NullLogger.new }
|
40
|
+
option :verbose_logging, NsOptions::Boolean, :default => true
|
42
41
|
option :view_handler_ns, String
|
43
|
-
option :default_charset, String, :default => 'utf-8'
|
44
42
|
|
45
|
-
|
43
|
+
attr_accessor :settings, :error_procs, :init_procs, :template_helpers
|
44
|
+
attr_accessor :middlewares, :routes, :urls
|
46
45
|
|
47
46
|
def initialize(values=nil)
|
48
47
|
# these are defaulted here because we want to use the Configuration
|
@@ -53,7 +52,9 @@ module Deas::Server
|
|
53
52
|
:public_folder => proc{ self.root.join('public') },
|
54
53
|
:views_folder => proc{ self.root.join('views') }
|
55
54
|
}))
|
56
|
-
@
|
55
|
+
@settings, @urls = {}, {}
|
56
|
+
@error_procs, @init_procs, @template_helpers = [], [], []
|
57
|
+
@middlewares, @routes = [], []
|
57
58
|
@valid = nil
|
58
59
|
end
|
59
60
|
|
@@ -101,6 +102,10 @@ module Deas::Server
|
|
101
102
|
Deas::Route.new(http_method, path, proxy).tap{ |r| self.routes.push(r) }
|
102
103
|
end
|
103
104
|
|
105
|
+
def add_url(name, path)
|
106
|
+
self.urls[name] = Deas::Url.new(name, path)
|
107
|
+
end
|
108
|
+
|
104
109
|
end
|
105
110
|
|
106
111
|
def self.included(receiver)
|
@@ -202,39 +207,59 @@ module Deas::Server
|
|
202
207
|
self.configuration.default_charset *args
|
203
208
|
end
|
204
209
|
|
205
|
-
def get(path, handler_class_name)
|
206
|
-
self.route(:get, path, handler_class_name)
|
210
|
+
def get(path, handler_class_name, url_name = nil)
|
211
|
+
self.route(:get, path, handler_class_name, url_name)
|
207
212
|
end
|
208
213
|
|
209
|
-
def post(path, handler_class_name)
|
210
|
-
self.route(:post, path, handler_class_name)
|
214
|
+
def post(path, handler_class_name, url_name = nil)
|
215
|
+
self.route(:post, path, handler_class_name, url_name)
|
211
216
|
end
|
212
217
|
|
213
|
-
def put(path, handler_class_name)
|
214
|
-
self.route(:put, path, handler_class_name)
|
218
|
+
def put(path, handler_class_name, url_name = nil)
|
219
|
+
self.route(:put, path, handler_class_name, url_name)
|
215
220
|
end
|
216
221
|
|
217
|
-
def patch(path, handler_class_name)
|
218
|
-
self.route(:patch, path, handler_class_name)
|
222
|
+
def patch(path, handler_class_name, url_name = nil)
|
223
|
+
self.route(:patch, path, handler_class_name, url_name)
|
219
224
|
end
|
220
225
|
|
221
|
-
def delete(path, handler_class_name)
|
222
|
-
self.route(:delete, path, handler_class_name)
|
226
|
+
def delete(path, handler_class_name, url_name = nil)
|
227
|
+
self.route(:delete, path, handler_class_name, url_name)
|
223
228
|
end
|
224
229
|
|
225
230
|
def redirect(http_method, path, to_path = nil, &block)
|
226
|
-
|
231
|
+
url = self.configuration.urls[to_path]
|
232
|
+
if to_path.kind_of?(::Symbol) && url.nil?
|
233
|
+
raise ArgumentError, "no url named `#{to_path.inspect}`"
|
234
|
+
end
|
235
|
+
proxy = Deas::RedirectProxy.new(url || to_path, &block)
|
227
236
|
self.configuration.add_route(http_method, path, proxy)
|
228
237
|
end
|
229
238
|
|
230
|
-
def route(http_method, path, handler_class_name)
|
239
|
+
def route(http_method, path, handler_class_name, url_name = nil)
|
231
240
|
if self.view_handler_ns && !(handler_class_name =~ /^::/)
|
232
241
|
handler_class_name = "#{self.view_handler_ns}::#{handler_class_name}"
|
233
242
|
end
|
234
243
|
proxy = Deas::RouteProxy.new(handler_class_name)
|
244
|
+
|
245
|
+
if url_name && !url_name.to_s.empty?
|
246
|
+
if !path.kind_of?(::String)
|
247
|
+
raise ArgumentError, "invalid path `#{path.inspect}` - "\
|
248
|
+
"can only provide a url name with String paths"
|
249
|
+
end
|
250
|
+
self.configuration.add_url(url_name.to_sym, path)
|
251
|
+
end
|
235
252
|
self.configuration.add_route(http_method, path, proxy)
|
236
253
|
end
|
237
254
|
|
255
|
+
def url(name, *args)
|
256
|
+
url = self.configuration.urls[name.to_sym]
|
257
|
+
raise ArgumentError, "no route named `#{name.to_sym.inspect}`" unless url
|
258
|
+
|
259
|
+
url.path_for(*args)
|
260
|
+
end
|
261
|
+
|
238
262
|
end
|
239
263
|
|
240
264
|
end
|
265
|
+
|
data/lib/deas/url.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Deas
|
2
|
+
class Url
|
3
|
+
|
4
|
+
attr_reader :name, :path
|
5
|
+
|
6
|
+
def initialize(name, path)
|
7
|
+
@name, @path = name, path
|
8
|
+
end
|
9
|
+
|
10
|
+
def path_for(*args)
|
11
|
+
named, ordered = [
|
12
|
+
args.last.kind_of?(::Hash) ? args.pop : {},
|
13
|
+
args
|
14
|
+
]
|
15
|
+
apply_ordered_params(apply_named_params(@path, named), ordered)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def apply_named_params(path, params)
|
21
|
+
# ignore captures in applying params
|
22
|
+
captures = params.delete(:captures) || params.delete('captures') || []
|
23
|
+
splat = params.delete(:splat) || params.delete('splat') || []
|
24
|
+
splat_path = splat.inject(path){ |p, v| p.sub(/\*+/, v.to_s) }
|
25
|
+
params.inject(splat_path){ |p, (k, v)| p.gsub(":#{k}", v.to_s) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def apply_ordered_params(path, params)
|
29
|
+
params.inject(path){ |p, v| p.sub(/\*+|\:\w+/i, v.to_s) }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/deas/version.rb
CHANGED
data/test/unit/logging_tests.rb
CHANGED
@@ -50,21 +50,37 @@ module Deas::Logging
|
|
50
50
|
subject{ Deas::SummaryLine }
|
51
51
|
|
52
52
|
should "output its attributes in a specific order" do
|
53
|
-
assert_equal %w{time status method path handler params}, subject.keys
|
53
|
+
assert_equal %w{time status method path handler params redir}, subject.keys
|
54
54
|
end
|
55
55
|
|
56
56
|
should "output its attributes in a single line" do
|
57
57
|
line_attrs = {
|
58
|
-
'time'
|
59
|
-
'status'
|
60
|
-
'method'
|
61
|
-
'path'
|
58
|
+
'time' => 't',
|
59
|
+
'status' => 's',
|
60
|
+
'method' => 'm',
|
61
|
+
'path' => 'pth',
|
62
62
|
'handler' => 'h',
|
63
|
-
'params'
|
63
|
+
'params' => 'p',
|
64
|
+
'redir' => 'r'
|
64
65
|
}
|
65
66
|
exp_line = "time=\"t\" "\
|
66
67
|
"status=\"s\" "\
|
67
68
|
"method=\"m\" "\
|
69
|
+
"path=\"pth\" "\
|
70
|
+
"handler=\"h\" "\
|
71
|
+
"params=\"p\" "\
|
72
|
+
"redir=\"r\""
|
73
|
+
assert_equal exp_line, subject.new(line_attrs)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "only output keys if data exists for them" do
|
77
|
+
line_attrs = {
|
78
|
+
'status' => 's',
|
79
|
+
'path' => 'pth',
|
80
|
+
'handler' => 'h',
|
81
|
+
'params' => 'p'
|
82
|
+
}
|
83
|
+
exp_line = "status=\"s\" "\
|
68
84
|
"path=\"pth\" "\
|
69
85
|
"handler=\"h\" "\
|
70
86
|
"params=\"p\""
|
@@ -20,13 +20,15 @@ class Deas::RedirectProxy
|
|
20
20
|
end
|
21
21
|
|
22
22
|
class HandlerClassTests < BaseTests
|
23
|
-
|
23
|
+
include Deas::TestHelpers
|
24
|
+
|
25
|
+
desc "redir handler class"
|
24
26
|
setup do
|
25
27
|
@handler_class = @proxy.handler_class
|
26
28
|
end
|
27
29
|
subject{ @handler_class }
|
28
30
|
|
29
|
-
should
|
31
|
+
should have_accessor :redirect_path
|
30
32
|
should have_imeth :name
|
31
33
|
|
32
34
|
should "be a view handler" do
|
@@ -39,41 +41,63 @@ class Deas::RedirectProxy
|
|
39
41
|
assert_equal 'Deas::RedirectHandler', subject.name
|
40
42
|
end
|
41
43
|
|
42
|
-
should "
|
43
|
-
|
44
|
-
assert_equal '/somewhere', subject.redirect_path.call
|
45
|
-
end
|
44
|
+
should "store its redirect path as a proc" do
|
45
|
+
assert_kind_of Proc, subject.redirect_path
|
46
46
|
|
47
|
-
|
48
|
-
|
47
|
+
url = Deas::Url.new(:some_thing, '/:some/:thing')
|
48
|
+
handler_class = Deas::RedirectProxy.new(url).handler_class
|
49
|
+
assert_kind_of Proc, handler_class.redirect_path
|
49
50
|
|
51
|
+
path_proc = proc{ '/somewhere' }
|
50
52
|
handler_class = Deas::RedirectProxy.new(&path_proc).handler_class
|
51
|
-
|
52
|
-
assert_equal '/somewhere', subject.redirect_path.call
|
53
|
+
assert_kind_of Proc, handler_class.redirect_path
|
53
54
|
end
|
54
55
|
|
55
56
|
end
|
56
57
|
|
57
|
-
class
|
58
|
-
|
58
|
+
class HandlerTests < HandlerClassTests
|
59
|
+
desc "redir handler instance"
|
60
|
+
setup do
|
61
|
+
@handler = test_handler(@handler_class)
|
62
|
+
end
|
63
|
+
subject{ @handler }
|
59
64
|
|
60
|
-
|
65
|
+
should have_reader :redirect_path
|
61
66
|
|
62
|
-
should "
|
63
|
-
|
64
|
-
assert_equal true, render_args.redirect?
|
65
|
-
assert_equal '/somewhere', render_args.path
|
67
|
+
should "know its redir path if from a path string" do
|
68
|
+
assert_equal '/somewhere', subject.redirect_path
|
66
69
|
end
|
67
70
|
|
68
|
-
should "
|
69
|
-
|
70
|
-
handler_class = Deas::RedirectProxy.new(
|
71
|
+
should "know its redir path if from Url" do
|
72
|
+
url = Deas::Url.new(:some_thing, '/:some/:thing')
|
73
|
+
handler_class = Deas::RedirectProxy.new(url).handler_class
|
74
|
+
handler = test_handler(handler_class, {
|
75
|
+
:params => { 'some' => 'a', 'thing' => 'goose' }
|
76
|
+
})
|
77
|
+
|
78
|
+
assert_equal '/a/goose', handler.redirect_path
|
79
|
+
end
|
80
|
+
|
81
|
+
should "know its redir path if from a block" do
|
82
|
+
handler_class = Deas::RedirectProxy.new(&proc{'/from-block-arg'}).handler_class
|
83
|
+
handler = test_handler(handler_class)
|
84
|
+
|
85
|
+
assert_equal '/from-block-arg', handler.redirect_path
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
class RunTests < HandlerClassTests
|
91
|
+
desc "when run"
|
92
|
+
setup do
|
93
|
+
@runner = test_runner(@handler_class)
|
94
|
+
@handler = @runner.handler
|
95
|
+
@render_args = @runner.run
|
96
|
+
end
|
71
97
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
assert_equal true, render_args.redirect?
|
76
|
-
assert_equal '/go_here', render_args.path
|
98
|
+
should "redirect to the handler's redirect path" do
|
99
|
+
assert @render_args.redirect?
|
100
|
+
assert_equal @handler.redirect_path, @render_args.path
|
77
101
|
end
|
78
102
|
|
79
103
|
end
|
@@ -16,17 +16,19 @@ class Deas::Server::Configuration
|
|
16
16
|
end
|
17
17
|
subject{ @configuration }
|
18
18
|
|
19
|
-
# sinatra
|
19
|
+
# sinatra-based options
|
20
|
+
|
20
21
|
should have_imeths :env, :root, :public_folder, :views_folder
|
21
22
|
should have_imeths :dump_errors, :method_override, :sessions, :show_exceptions
|
22
|
-
should have_imeths :static_files, :reload_templates
|
23
|
+
should have_imeths :static_files, :reload_templates, :default_charset
|
23
24
|
|
24
25
|
# server handling options
|
25
|
-
should have_imeths :error_procs, :init_procs, :logger, :middlewares, :settings
|
26
|
-
should have_imeths :verbose_logging, :routes, :view_handler_ns, :default_charset
|
27
26
|
|
28
|
-
should
|
29
|
-
|
27
|
+
should have_imeths :logger, :verbose_logging, :view_handler_ns
|
28
|
+
|
29
|
+
should have_accessors :settings, :error_procs, :init_procs, :template_helpers
|
30
|
+
should have_accessors :middlewares, :routes, :urls
|
31
|
+
should have_imeths :valid?, :validate!, :template_scope, :add_route
|
30
32
|
|
31
33
|
should "default the env to 'development'" do
|
32
34
|
assert_equal 'development', subject.env
|
@@ -47,15 +49,19 @@ class Deas::Server::Configuration
|
|
47
49
|
end
|
48
50
|
|
49
51
|
should "default the handling options" do
|
52
|
+
assert_instance_of Deas::NullLogger, subject.logger
|
53
|
+
assert_equal true, subject.verbose_logging
|
54
|
+
assert_nil subject.view_handler_ns
|
55
|
+
end
|
56
|
+
|
57
|
+
should "default its stored configuration" do
|
58
|
+
assert_empty subject.settings
|
50
59
|
assert_empty subject.error_procs
|
51
60
|
assert_empty subject.init_procs
|
52
|
-
|
61
|
+
assert_empty subject.template_helpers
|
53
62
|
assert_empty subject.middlewares
|
54
|
-
assert_empty subject.settings
|
55
|
-
assert_equal true, subject.verbose_logging
|
56
63
|
assert_empty subject.routes
|
57
|
-
|
58
|
-
assert_empty subject.template_helpers
|
64
|
+
assert_empty subject.urls
|
59
65
|
end
|
60
66
|
|
61
67
|
should "build a template scope including its template helpers" do
|
@@ -102,8 +108,8 @@ class Deas::Server::Configuration
|
|
102
108
|
c.show_exceptions = true
|
103
109
|
c.static = true
|
104
110
|
c.reload_templates = true
|
105
|
-
c.routes = [ @route ]
|
106
111
|
c.middlewares = [ ['MyMiddleware'] ]
|
112
|
+
c.routes = [ @route ]
|
107
113
|
end
|
108
114
|
@configuration.init_procs << proc{ @initialized = true }
|
109
115
|
@configuration.init_procs << proc{ @other_initialized = true }
|
data/test/unit/server_tests.rb
CHANGED
@@ -15,16 +15,16 @@ module Deas::Server
|
|
15
15
|
|
16
16
|
should have_imeths :new, :configuration
|
17
17
|
|
18
|
-
# DSL for sinatra settings
|
18
|
+
# DSL for sinatra-based settings
|
19
19
|
should have_imeths :env, :root, :public_folder, :views_folder
|
20
20
|
should have_imeths :dump_errors, :method_override, :sessions, :show_exceptions
|
21
|
-
should have_imeths :static_files, :reload_templates
|
21
|
+
should have_imeths :static_files, :reload_templates, :default_charset
|
22
22
|
|
23
|
-
# DSL for server handling
|
23
|
+
# DSL for server handling settings
|
24
24
|
should have_imeths :init, :error, :template_helpers, :template_helper?
|
25
25
|
should have_imeths :use, :set, :view_handler_ns, :verbose_logging, :logger
|
26
|
-
should have_imeths :
|
27
|
-
should have_imeths :
|
26
|
+
should have_imeths :get, :post, :put, :patch, :delete
|
27
|
+
should have_imeths :redirect, :route, :url
|
28
28
|
|
29
29
|
should "allow setting it's configuration options" do
|
30
30
|
config = subject.configuration
|
@@ -176,6 +176,64 @@ module Deas::Server
|
|
176
176
|
assert_equal 'GetInfo', route.handler_proxy.handler_class_name
|
177
177
|
end
|
178
178
|
|
179
|
+
should "not define urls for routes created with no url name" do
|
180
|
+
assert_empty subject.configuration.urls
|
181
|
+
|
182
|
+
@server_class.route(:get, '/info', 'GetInfo')
|
183
|
+
assert_empty subject.configuration.urls
|
184
|
+
|
185
|
+
@server_class.route(:get, '/info', 'GetInfo', nil)
|
186
|
+
assert_empty subject.configuration.urls
|
187
|
+
|
188
|
+
@server_class.route(:get, '/info', 'GetInfo', '')
|
189
|
+
assert_empty subject.configuration.urls
|
190
|
+
|
191
|
+
@server_class.route(:get, '/info', 'GetInfo', 'get_info')
|
192
|
+
assert_not_empty subject.configuration.urls
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
class NamedUrlTests < BaseTests
|
198
|
+
desc "when defining a route with a url name"
|
199
|
+
setup do
|
200
|
+
@server_class.route(:get, '/info/:for', 'GetInfo', 'get_info')
|
201
|
+
end
|
202
|
+
|
203
|
+
should "define a url for the route on the server" do
|
204
|
+
url = subject.configuration.urls[:get_info]
|
205
|
+
|
206
|
+
assert_not_nil url
|
207
|
+
assert_kind_of Deas::Url, url
|
208
|
+
assert_equal :get_info, url.name
|
209
|
+
assert_equal '/info/:for', url.path
|
210
|
+
end
|
211
|
+
|
212
|
+
should "complain if given a non-string path" do
|
213
|
+
assert_raises ArgumentError do
|
214
|
+
subject.route(:get, /^\/info/, 'GetInfo', 'get_info')
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
should "build a path for a url given params" do
|
219
|
+
exp_path = "/info/now"
|
220
|
+
|
221
|
+
assert_equal exp_path, subject.url(:get_info, :for => 'now')
|
222
|
+
assert_equal exp_path, subject.url(:get_info, 'now')
|
223
|
+
end
|
224
|
+
|
225
|
+
should "complain if building a named url that hasn't been defined" do
|
226
|
+
assert_raises ArgumentError do
|
227
|
+
subject.url(:get_all_info, 'now')
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
should "complain if redirecting to a named url that hasn't been defined" do
|
232
|
+
assert_raises ArgumentError do
|
233
|
+
subject.redirect(:get, '/somewhere', :not_defined_url)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
179
237
|
end
|
180
238
|
|
181
239
|
end
|
@@ -58,7 +58,7 @@ module Deas::SinatraApp
|
|
58
58
|
|
59
59
|
should "define Sinatra routes for every route in the configuration" do
|
60
60
|
get_routes = subject.routes[@route.method.to_s.upcase] || []
|
61
|
-
sinatra_route = get_routes.detect{|route| route[0].match(@route.path) }
|
61
|
+
sinatra_route = get_routes.detect{ |route| route[0].match(@route.path) }
|
62
62
|
|
63
63
|
assert_not_nil sinatra_route
|
64
64
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'deas/url'
|
3
|
+
|
4
|
+
require 'test/support/view_handlers'
|
5
|
+
|
6
|
+
class Deas::Url
|
7
|
+
|
8
|
+
class BaseTests < Assert::Context
|
9
|
+
desc "Deas::Url"
|
10
|
+
setup do
|
11
|
+
@url = Deas::Url.new(:get_info, '/info')
|
12
|
+
end
|
13
|
+
subject{ @url }
|
14
|
+
|
15
|
+
should have_readers :name, :path
|
16
|
+
should have_imeth :path_for
|
17
|
+
|
18
|
+
should "know its name and path info" do
|
19
|
+
assert_equal :get_info, subject.name
|
20
|
+
assert_equal '/info', subject.path
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class PathForTests < BaseTests
|
26
|
+
desc "when generating paths"
|
27
|
+
setup do
|
28
|
+
@url = Deas::Url.new(:some_thing, '/:some/:thing/*/*')
|
29
|
+
end
|
30
|
+
|
31
|
+
should "generate given named params only" do
|
32
|
+
exp_path = "/a/goose/*/*"
|
33
|
+
assert_equal exp_path, subject.path_for({
|
34
|
+
'some' => 'a',
|
35
|
+
:thing => 'goose'
|
36
|
+
})
|
37
|
+
|
38
|
+
exp_path = "/a/goose/cooked-well/*"
|
39
|
+
assert_equal exp_path, subject.path_for({
|
40
|
+
'some' => 'a',
|
41
|
+
:thing => 'goose',
|
42
|
+
:splat => ['cooked-well']
|
43
|
+
})
|
44
|
+
|
45
|
+
exp_path = "/a/goose/cooked/well"
|
46
|
+
assert_equal exp_path, subject.path_for({
|
47
|
+
'some' => 'a',
|
48
|
+
:thing => 'goose',
|
49
|
+
'splat' => ['cooked', 'well']
|
50
|
+
})
|
51
|
+
end
|
52
|
+
|
53
|
+
should "generate given ordered params only" do
|
54
|
+
exp_path = "/a/:thing/*/*"
|
55
|
+
assert_equal exp_path, subject.path_for('a')
|
56
|
+
|
57
|
+
exp_path = "/a/goose/*/*"
|
58
|
+
assert_equal exp_path, subject.path_for('a', 'goose')
|
59
|
+
|
60
|
+
exp_path = "/a/goose/cooked-well/*"
|
61
|
+
assert_equal exp_path, subject.path_for('a', 'goose', 'cooked-well')
|
62
|
+
|
63
|
+
exp_path = "/a/goose/cooked/well"
|
64
|
+
assert_equal exp_path, subject.path_for('a', 'goose', 'cooked', 'well')
|
65
|
+
end
|
66
|
+
|
67
|
+
should "generate given mixed ordered and named params" do
|
68
|
+
exp_path = "/:some/:thing/*/*"
|
69
|
+
assert_equal exp_path, subject.path_for
|
70
|
+
|
71
|
+
exp_path = "/a/goose/*/*"
|
72
|
+
assert_equal exp_path, subject.path_for('a', 'thing' => 'goose')
|
73
|
+
|
74
|
+
exp_path = "/goose/a/well/*"
|
75
|
+
assert_equal exp_path, subject.path_for('a', 'well', 'some' => 'goose')
|
76
|
+
end
|
77
|
+
|
78
|
+
should "pass on this" do
|
79
|
+
|
80
|
+
exp_path = "/a/goose/cooked/well"
|
81
|
+
assert_equal exp_path, subject.path_for('ignore', 'these', 'params', {
|
82
|
+
'some' => 'a',
|
83
|
+
:thing => 'goose',
|
84
|
+
'splat' => ['cooked', 'well']
|
85
|
+
})
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
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: 87
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 18
|
9
|
+
- 0
|
10
|
+
version: 0.18.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-
|
19
|
+
date: 2013-07-10 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: ns-options
|
@@ -161,6 +161,7 @@ files:
|
|
161
161
|
- lib/deas/template.rb
|
162
162
|
- lib/deas/test_helpers.rb
|
163
163
|
- lib/deas/test_runner.rb
|
164
|
+
- lib/deas/url.rb
|
164
165
|
- lib/deas/version.rb
|
165
166
|
- lib/deas/view_handler.rb
|
166
167
|
- log/.gitkeep
|
@@ -197,6 +198,7 @@ files:
|
|
197
198
|
- test/unit/sinatra_app_tests.rb
|
198
199
|
- test/unit/sinatra_runner_tests.rb
|
199
200
|
- test/unit/template_tests.rb
|
201
|
+
- test/unit/url_tests.rb
|
200
202
|
- test/unit/view_handler_tests.rb
|
201
203
|
- tmp/.gitkeep
|
202
204
|
homepage: http://github.com/redding/deas
|
@@ -266,4 +268,5 @@ test_files:
|
|
266
268
|
- test/unit/sinatra_app_tests.rb
|
267
269
|
- test/unit/sinatra_runner_tests.rb
|
268
270
|
- test/unit/template_tests.rb
|
271
|
+
- test/unit/url_tests.rb
|
269
272
|
- test/unit/view_handler_tests.rb
|