deas 0.23.1 → 0.23.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gem 'rake'
6
6
  gem 'pry'
7
7
  gem 'multi_json'
8
8
  gem 'yajl-ruby'
9
+ gem 'rack-test'
@@ -13,7 +13,7 @@ module Deas
13
13
  @sinatra_call = sinatra_call
14
14
  @app_settings = @sinatra_call.settings
15
15
  @logger = @sinatra_call.settings.logger
16
- @params = @sinatra_call.params
16
+ @params = normalize_params(@sinatra_call.params)
17
17
  @request = @sinatra_call.request
18
18
  @response = @sinatra_call.response
19
19
  @session = @sinatra_call.session
@@ -81,5 +81,21 @@ module Deas
81
81
  File.extname(template_name)[1..-1] || 'html'
82
82
  end
83
83
 
84
+ def normalize_params(params)
85
+ StringifiedKeys.new(params)
86
+ end
87
+
88
+ module StringifiedKeys
89
+ def self.new(value)
90
+ if value.is_a?(::Array)
91
+ value.map{ |i| StringifiedKeys.new(i) }
92
+ elsif Rack::Utils.params_hash_type?(value)
93
+ value.inject({}){ |h, (k, v)| h[k.to_s] = StringifiedKeys.new(v); h }
94
+ else
95
+ value
96
+ end
97
+ end
98
+ end
99
+
84
100
  end
85
101
  end
@@ -1,4 +1,5 @@
1
1
  require 'ostruct'
2
+ require 'rack/multipart'
2
3
  require 'deas/runner'
3
4
 
4
5
  module Deas
@@ -11,7 +12,7 @@ module Deas
11
12
  args = (args || {}).dup
12
13
  @app_settings = OpenStruct.new(args.delete(:app_settings))
13
14
  @logger = args.delete(:logger) || Deas::NullLogger.new
14
- @params = args.delete(:params) || {}
15
+ @params = normalize_params(args.delete(:params) || {})
15
16
  @request = args.delete(:request)
16
17
  @response = args.delete(:response)
17
18
  @session = args.delete(:session)
@@ -80,6 +81,32 @@ module Deas
80
81
 
81
82
  SendFileArgs = Struct.new(:file_path, :options, :block)
82
83
 
84
+ private
85
+
86
+ def normalize_params(params)
87
+ Stringify.new(params)
88
+ end
89
+
90
+ module Stringify
91
+ def self.new(value)
92
+ if value.is_a?(::Array)
93
+ value.map{ |i| Stringify.new(i) }
94
+ elsif Rack::Utils.params_hash_type?(value)
95
+ value.inject({}){ |h, (k, v)| h[k.to_s] = Stringify.new(v); h }
96
+ elsif self.file_type?(value)
97
+ value
98
+ else
99
+ value.to_s
100
+ end
101
+ end
102
+
103
+ def self.file_type?(value)
104
+ value.kind_of?(::File) ||
105
+ value.kind_of?(::Rack::Multipart::UploadedFile) ||
106
+ (defined?(::Rack::Test::UploadedFile) && value.kind_of?(::Rack::Test::UploadedFile))
107
+ end
108
+ end
109
+
83
110
  end
84
111
 
85
112
  end
data/lib/deas/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Deas
2
- VERSION = "0.23.1"
2
+ VERSION = "0.23.2"
3
3
  end
@@ -5,6 +5,17 @@ class TestViewHandler
5
5
 
6
6
  end
7
7
 
8
+ class TestRunnerViewHandler
9
+ include Deas::ViewHandler
10
+
11
+ attr_accessor :custom_value
12
+
13
+ def run!
14
+ 'run has run'
15
+ end
16
+
17
+ end
18
+
8
19
  class RenderViewHandler
9
20
  include Deas::ViewHandler
10
21
 
@@ -64,7 +75,7 @@ class HaltViewHandler
64
75
  include Deas::ViewHandler
65
76
 
66
77
  def run!
67
- halt_args = [ params['code'], params['headers'], params['body'] ].compact
78
+ halt_args = [ params['code'].to_i, params['headers'], params['body'] ].compact
68
79
  halt(*halt_args)
69
80
  end
70
81
 
@@ -6,7 +6,7 @@ require 'deas/sinatra_runner'
6
6
 
7
7
  class Deas::SinatraRunner
8
8
 
9
- class BaseTests < Assert::Context
9
+ class UnitTests < Assert::Context
10
10
  desc "Deas::SinatraRunner"
11
11
  setup do
12
12
  @fake_sinatra_call = FakeSinatraCall.new
@@ -82,7 +82,7 @@ class Deas::SinatraRunner
82
82
 
83
83
  end
84
84
 
85
- class RunTests < BaseTests
85
+ class RunTests < UnitTests
86
86
  desc "run"
87
87
  setup do
88
88
  @return_value = @runner.run
@@ -106,4 +106,43 @@ class Deas::SinatraRunner
106
106
 
107
107
  end
108
108
 
109
+ class ParamsTests < UnitTests
110
+ desc "normalizing params"
111
+
112
+ should "convert any non-string hash keys to string keys" do
113
+ exp_params = {
114
+ 'a' => 'aye',
115
+ 'b' => 'bee',
116
+ 'attachment' => {
117
+ 'tempfile' => 'a-file',
118
+ 'content_type' => 'whatever'
119
+ },
120
+ 'attachments' => [
121
+ { 'tempfile' => 'a-file' },
122
+ { 'tempfile' => 'b-file' }
123
+ ]
124
+ }
125
+ assert_equal exp_params, runner_params({
126
+ :a => 'aye',
127
+ 'b' => 'bee',
128
+ 'attachment' => {
129
+ :tempfile => 'a-file',
130
+ :content_type => 'whatever'
131
+ },
132
+ 'attachments' => [
133
+ { :tempfile => 'a-file' },
134
+ { 'tempfile' => 'b-file' }
135
+ ]
136
+ })
137
+ end
138
+
139
+ private
140
+
141
+ def runner_params(params)
142
+ @fake_sinatra_call.params = params
143
+ Deas::SinatraRunner.new(FlagViewHandler, @fake_sinatra_call).params
144
+ end
145
+
146
+ end
147
+
109
148
  end
@@ -0,0 +1,193 @@
1
+ require 'assert'
2
+ require 'deas/test_runner'
3
+
4
+ require 'rack/test'
5
+ require 'test/support/view_handlers'
6
+
7
+ class Deas::TestRunner
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "Deas::TestRunner"
11
+ setup do
12
+ @runner = Deas::TestRunner.new(TestRunnerViewHandler)
13
+ end
14
+ subject{ @runner }
15
+
16
+ should have_readers :handler, :return_value
17
+
18
+ should "build a handler instance" do
19
+ assert_kind_of TestRunnerViewHandler, subject.handler
20
+ end
21
+
22
+ should "default the handler settings" do
23
+ assert_kind_of OpenStruct, subject.app_settings
24
+ assert_kind_of Deas::NullLogger, subject.logger
25
+ assert_equal Hash.new, subject.params
26
+ assert_nil subject.request
27
+ assert_nil subject.response
28
+ assert_nil subject.session
29
+ end
30
+
31
+ should "write any non-standard settings on the handler" do
32
+ runner = Deas::TestRunner.new(TestRunnerViewHandler, :custom_value => 42)
33
+ assert_equal 42, runner.handler.custom_value
34
+ end
35
+
36
+ should "not set a return value on initialize" do
37
+ assert_nil subject.return_value
38
+ end
39
+
40
+ should "set its return value to the return value of `run!` on run" do
41
+ assert_nil subject.return_value
42
+ subject.run
43
+ assert_equal subject.handler.run!, subject.return_value
44
+ end
45
+
46
+ should "build halt args if halt is called" do
47
+ value = catch(:halt){ subject.halt }
48
+ assert_kind_of HaltArgs, value
49
+ [:body, :headers, :status].each do |meth|
50
+ assert_respond_to meth, value
51
+ end
52
+ end
53
+
54
+ should "build redirect args if redirect is called" do
55
+ value = subject.redirect '/some/path'
56
+ assert_kind_of RedirectArgs, value
57
+ [:path, :halt_args].each do |meth|
58
+ assert_respond_to meth, value
59
+ end
60
+ assert_equal '/some/path', value.path
61
+ assert value.redirect?
62
+ end
63
+
64
+ should "build content type args if content type is called" do
65
+ value = subject.content_type 'something'
66
+ assert_kind_of ContentTypeArgs, value
67
+ [:value, :opts].each do |meth|
68
+ assert_respond_to meth, value
69
+ end
70
+ assert_equal 'something', value.value
71
+ end
72
+
73
+ should "build status args if status is called" do
74
+ value = subject.status(432)
75
+ assert_kind_of StatusArgs, value
76
+ assert_respond_to :value, value
77
+ assert_equal 432, value.value
78
+ end
79
+
80
+ should "build headers args if headers is called" do
81
+ value = subject.headers(:some => 'thing')
82
+ assert_kind_of HeadersArgs, value
83
+ assert_respond_to :value, value
84
+ exp_val = {:some => 'thing'}
85
+ assert_equal exp_val, value.value
86
+ end
87
+
88
+ should "build render args if render is called" do
89
+ value = subject.render 'some/template'
90
+ assert_kind_of RenderArgs, value
91
+ [:template_name, :options, :block].each do |meth|
92
+ assert_respond_to meth, value
93
+ end
94
+ assert_equal 'some/template', value.template_name
95
+ end
96
+
97
+ should "build send file args if send file is called" do
98
+ value = subject.send_file 'some/file/path'
99
+ assert_kind_of SendFileArgs, value
100
+ [:file_path, :options, :block].each do |meth|
101
+ assert_respond_to meth, value
102
+ end
103
+ assert_equal 'some/file/path', value.file_path
104
+ end
105
+
106
+ end
107
+
108
+ class ParamsTests < UnitTests
109
+ desc "normalizing params"
110
+
111
+ should "convert any non-Array or non-Hash values to strings" do
112
+ exp_params = {
113
+ 'nil' => '',
114
+ 'int' => '42',
115
+ 'str' => 'string'
116
+ }
117
+ assert_equal exp_params, runner_params({
118
+ 'nil' => nil,
119
+ 'int' => 42,
120
+ 'str' => 'string'
121
+ })
122
+ end
123
+
124
+ should "recursively convert array values to strings" do
125
+ exp_params = {
126
+ 'array' => ['', '42', 'string']
127
+ }
128
+ assert_equal exp_params, runner_params({
129
+ 'array' => [nil, 42, 'string']
130
+ })
131
+ end
132
+
133
+ should "recursively convert hash values to strings" do
134
+ exp_params = {
135
+ 'values' => {
136
+ 'nil' => '',
137
+ 'int' => '42',
138
+ 'str' => 'string'
139
+ }
140
+ }
141
+ assert_equal exp_params, runner_params({
142
+ 'values' => {
143
+ 'nil' => nil,
144
+ 'int' => 42,
145
+ 'str' => 'string'
146
+ }
147
+ })
148
+ end
149
+
150
+ should "convert any non-string hash keys to string keys" do
151
+ exp_params = {
152
+ 'nil' => '',
153
+ 'vals' => { '42' => 'int', 'str' => 'string' }
154
+ }
155
+ assert_equal exp_params, runner_params({
156
+ 'nil' => '',
157
+ :vals => { 42 => :int, 'str' => 'string' }
158
+ })
159
+ end
160
+
161
+ should "not convert File param values to strings" do
162
+ tempfile = File.new(TEST_SUPPORT_ROOT.join('routes.rb'))
163
+ params = runner_params({
164
+ 'attachment' => { :tempfile => tempfile }
165
+ })
166
+ assert_kind_of File, params['attachment']['tempfile']
167
+ end
168
+
169
+ should "not convert Rack::Multipart::UploadedFile param values to strings" do
170
+ tempfile = Rack::Multipart::UploadedFile.new(TEST_SUPPORT_ROOT.join('routes.rb'))
171
+ params = runner_params({
172
+ 'attachment' => { :tempfile => tempfile }
173
+ })
174
+ assert_kind_of Rack::Multipart::UploadedFile, params['attachment']['tempfile']
175
+ end
176
+
177
+ should "not convert Rack::Test::UploadedFile param values to strings" do
178
+ tempfile = Rack::Test::UploadedFile.new(TEST_SUPPORT_ROOT.join('routes.rb'))
179
+ params = runner_params({
180
+ 'attachment' => { :tempfile => tempfile }
181
+ })
182
+ assert_kind_of Rack::Test::UploadedFile, params['attachment']['tempfile']
183
+ end
184
+
185
+ private
186
+
187
+ def runner_params(params)
188
+ Deas::TestRunner.new(TestRunnerViewHandler, :params => params).params
189
+ end
190
+
191
+ end
192
+
193
+ 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: 65
4
+ hash: 71
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 23
9
- - 1
10
- version: 0.23.1
9
+ - 2
10
+ version: 0.23.2
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-11-18 00:00:00 Z
19
+ date: 2013-11-26 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: ns-options
@@ -201,6 +201,7 @@ files:
201
201
  - test/unit/sinatra_app_tests.rb
202
202
  - test/unit/sinatra_runner_tests.rb
203
203
  - test/unit/template_tests.rb
204
+ - test/unit/test_runner_tests.rb
204
205
  - test/unit/url_tests.rb
205
206
  - test/unit/view_handler_tests.rb
206
207
  - tmp/.gitkeep
@@ -272,5 +273,6 @@ test_files:
272
273
  - test/unit/sinatra_app_tests.rb
273
274
  - test/unit/sinatra_runner_tests.rb
274
275
  - test/unit/template_tests.rb
276
+ - test/unit/test_runner_tests.rb
275
277
  - test/unit/url_tests.rb
276
278
  - test/unit/view_handler_tests.rb