hobbit-contrib 0.5.2 → 0.5.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7605fffd9d4c2602f35c6556529d11bafb64883
4
- data.tar.gz: 58af370162d53892f0f966e18b0fd8daaf49c1e7
3
+ metadata.gz: f338c24eef1f1138d3a0156b79bcad360882e454
4
+ data.tar.gz: dbbe040ee3d61f823fc4867b5baba1dc39d96d76
5
5
  SHA512:
6
- metadata.gz: 9ccf2711a81fd56a5405cf9989843263261a8436d40e984b23e5b9500a3591f9a73dc25d597cb91507e847bfaeecb3f47b2903181b975a663cb17755f624d684
7
- data.tar.gz: 239c9527fbdd4485e311414b319f75d7ba8764e64368dd28f72daafeb8c6449b5348ba0079f3335393cb1cb62aae3820bfb3e271bbe5876a07aacd374874b1ed
6
+ metadata.gz: 235309c1b90a30a6609fc691eea4b3a5f73f09a51d6883539087a0c5b5b894f94563f651129f24c40f746a23cf6fa82eb2110e6cf897e9f054346688aa2fdf7c
7
+ data.tar.gz: b2a5b5b597630b52aa930c81c4731c2054c518794cef2287cd8e9d4ab1b09331b422446d9808f0c1838803b37a971e4aa9336194d55dd8965d7723d2f16da0cd
data/README.md CHANGED
@@ -24,6 +24,11 @@ Or install it yourself as:
24
24
  $ gem install hobbit-contrib
25
25
  ```
26
26
 
27
+ ### Optional dependencies
28
+
29
+ * [mote](https://github.com/soveran/mote) if you want to use `Hobbit::Mote`.
30
+ * [tilt](https://github.com/rtomayko/tilt) if you want to use `Hobbit::Render`.
31
+
27
32
  ## Usage
28
33
 
29
34
  Each extension may have its own usage. In general, including the module will be
data/Rakefile CHANGED
@@ -2,8 +2,8 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs << 'spec'
6
- t.pattern = 'spec/**/*_spec.rb'
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
7
  end
8
8
 
9
- task default: :test
9
+ task default: :test
@@ -20,12 +20,12 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'codeclimate-test-reporter'
23
- spec.add_development_dependency 'minitest'
23
+ spec.add_development_dependency 'erubis'
24
+ spec.add_development_dependency 'mote'
25
+ spec.add_development_dependency 'oktobertest'
24
26
  spec.add_development_dependency 'rack-test'
25
27
  spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'tilt'
26
29
 
27
- spec.add_runtime_dependency 'erubis'
28
30
  spec.add_runtime_dependency 'hobbit'
29
- spec.add_runtime_dependency 'mote'
30
- spec.add_runtime_dependency 'tilt'
31
31
  end
@@ -2,6 +2,12 @@ require 'hobbit/contrib/version'
2
2
  require 'hobbit/environment'
3
3
  require 'hobbit/error_handling'
4
4
  require 'hobbit/filter'
5
- require 'hobbit/mote'
6
- require 'hobbit/render'
5
+ begin
6
+ require 'hobbit/mote'
7
+ rescue LoadError
8
+ end
9
+ begin
10
+ require 'hobbit/render'
11
+ rescue LoadError
12
+ end
7
13
  require 'hobbit/session'
@@ -1,5 +1,5 @@
1
1
  module Hobbit
2
2
  module Contrib
3
- VERSION = '0.5.2'
3
+ VERSION = '0.5.3'
4
4
  end
5
5
  end
@@ -2,9 +2,7 @@ require 'mote'
2
2
 
3
3
  module Hobbit
4
4
  module Mote
5
- def self.included(othermod)
6
- othermod.send :include, ::Mote::Helpers
7
- end
5
+ include ::Mote::Helpers
8
6
 
9
7
  def default_layout
10
8
  "#{layouts_path}/application.mote"
@@ -0,0 +1,103 @@
1
+ require 'helper'
2
+
3
+ scope Hobbit::Environment do
4
+ setup do
5
+ mock_app do
6
+ include Hobbit::Environment
7
+ end
8
+ end
9
+
10
+ def with_env(environment)
11
+ env, ENV['RACK_ENV'] = ENV['RACK_ENV'], environment
12
+ yield
13
+ ENV['RACK_ENV'] = env
14
+ end
15
+
16
+ scope '::environment' do
17
+ test 'returns the current environment' do
18
+ with_env('development') do
19
+ assert app.to_app.class.environment == :development
20
+ end
21
+ end
22
+ end
23
+
24
+ scope '#environment' do
25
+ test 'returns the current environment' do
26
+ with_env('development') do
27
+ assert app.to_app.environment == :development
28
+ end
29
+ end
30
+ end
31
+
32
+ scope '::development?' do
33
+ test "returns true if ENV['RACK_ENV'] = :development" do
34
+ with_env('development') do
35
+ assert app.to_app.class.development? == true
36
+ end
37
+ end
38
+
39
+ test "returns false if ENV['RACK_ENV'] != :development" do
40
+ assert app.to_app.class.development? == false
41
+ end
42
+ end
43
+
44
+ scope '#development?' do
45
+ test "returns true if ENV['RACK_ENV'] = :development" do
46
+ with_env('development') do
47
+ assert app.to_app.development? == true
48
+ end
49
+ end
50
+
51
+ test "returns false if ENV['RACK_ENV'] != :development" do
52
+ assert app.to_app.development? == false
53
+ end
54
+ end
55
+
56
+ scope '::production?' do
57
+ test "returns true if ENV['RACK_ENV'] = :production" do
58
+ with_env('production') do
59
+ assert app.to_app.class.production? == true
60
+ end
61
+ end
62
+
63
+ test "returns false if ENV['RACK_ENV'] != :production" do
64
+ assert app.to_app.class.production? == false
65
+ end
66
+ end
67
+
68
+ scope '#production?' do
69
+ test "returns true if ENV['RACK_ENV'] = :production" do
70
+ with_env('production') do
71
+ assert app.to_app.production? == true
72
+ end
73
+ end
74
+
75
+ test "returns false if ENV['RACK_ENV'] != :production" do
76
+ assert app.to_app.production? == false
77
+ end
78
+ end
79
+
80
+ scope '::test?' do
81
+ test "returns true if ENV['RACK_ENV'] = :test" do
82
+ assert app.to_app.class.test? == true
83
+ end
84
+
85
+ test "returns false if ENV['RACK_ENV'] != :test" do
86
+ with_env('development') do
87
+ assert app.to_app.class.test? == false
88
+ end
89
+ end
90
+ end
91
+
92
+ scope '#test?' do
93
+ test "returns true if ENV['RACK_ENV'] = :test" do
94
+ assert app.to_app.test? == true
95
+ end
96
+
97
+ test "returns false if ENV['RACK_ENV'] != :test" do
98
+ with_env('development') do
99
+ assert app.to_app.test? == false
100
+ end
101
+ end
102
+ end
103
+ end
@@ -1,11 +1,8 @@
1
- require 'minitest_helper'
1
+ require 'helper'
2
2
 
3
- describe 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
4
- include Hobbit::Contrib::Mock
5
- include Rack::Test::Methods
6
-
7
- describe 'when the exception ocurrs in a route' do
8
- let :app do
3
+ scope 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
4
+ scope 'when the exception ocurrs in a route' do
5
+ setup do
9
6
  mock_app do
10
7
  include Hobbit::Filter
11
8
  include Hobbit::ErrorHandling
@@ -29,17 +26,17 @@ describe 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
29
26
  end
30
27
  end
31
28
 
32
- it 'must call the before filter' do
29
+ test 'calls the before filter' do
33
30
  get '/'
34
- last_response.must_be :ok?
35
- last_response.body.must_equal 'Sorry'
36
- last_request.env.must_include 'hobbit.before'
37
- last_request.env.wont_include 'hobbit.after'
31
+ assert last_response.ok?
32
+ assert last_response.body == 'Sorry'
33
+ assert last_request.env.include? 'hobbit.before'
34
+ assert !last_request.env.include?('hobbit.after')
38
35
  end
39
36
  end
40
37
 
41
- describe 'when the exception ocurrs in a before filter' do
42
- let :app do
38
+ scope 'when the exception ocurrs in a before filter' do
39
+ setup do
43
40
  mock_app do
44
41
  include Hobbit::Filter
45
42
  include Hobbit::ErrorHandling
@@ -63,16 +60,16 @@ describe 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
63
60
  end
64
61
  end
65
62
 
66
- it 'must call the before filter' do
63
+ test 'calls the before filter' do
67
64
  get '/'
68
- last_response.must_be :ok?
69
- last_response.body.must_equal 'Sorry'
70
- last_request.env.wont_include 'hobbit.after'
65
+ assert last_response.ok?
66
+ assert last_response.body == 'Sorry'
67
+ assert !last_request.env.include?('hobbit.after')
71
68
  end
72
69
  end
73
70
 
74
- describe 'when the exception ocurrs in an after filter' do
75
- let :app do
71
+ scope 'when the exception ocurrs in an after filter' do
72
+ setup do
76
73
  mock_app do
77
74
  include Hobbit::Filter
78
75
  include Hobbit::ErrorHandling
@@ -96,17 +93,17 @@ describe 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
96
93
  end
97
94
  end
98
95
 
99
- it 'must call the before filter' do
96
+ test 'calls the before filter' do
100
97
  get '/'
101
- last_response.must_be :ok?
102
- last_response.body.must_equal 'this is written in the body. Sorry'
103
- last_request.env.must_include 'hobbit.before'
98
+ assert last_response.ok?
99
+ assert last_response.body == 'this is written in the body. Sorry'
100
+ assert last_request.env.include? 'hobbit.before'
104
101
  end
105
102
  end
106
103
 
107
- describe 'the order of the modules inclusion matters' do
108
- describe 'when ErrorHandling is included before Filter' do
109
- let :app do
104
+ scope 'the order of the modules inclusion matters' do
105
+ scope 'when ErrorHandling is included before Filter' do
106
+ setup do
110
107
  mock_app do
111
108
  include Hobbit::ErrorHandling
112
109
  include Hobbit::Filter
@@ -130,14 +127,14 @@ describe 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
130
127
  end
131
128
  end
132
129
 
133
- it 'wont work as expected' do
130
+ test 'does not work as expected' do
134
131
  get '/'
135
- last_response.must_be :ok?
136
- last_response.body.must_equal 'Sorry'
137
- last_request.env.must_include 'hobbit.before'
132
+ assert last_response.ok?
133
+ assert last_response.body == 'Sorry'
134
+ assert last_request.env.include? 'hobbit.before'
138
135
  # this is contrary to a previous test, which is not the desired workflow
139
136
  # or is it?
140
- last_request.env.must_include 'hobbit.after'
137
+ assert last_request.env.include? 'hobbit.after'
141
138
  end
142
139
  end
143
140
  end
@@ -0,0 +1,136 @@
1
+ require 'helper'
2
+
3
+ scope Hobbit::ErrorHandling do
4
+ NotFoundError = Class.new StandardError
5
+ SpecificNotFoundError = Class.new NotFoundError
6
+ UnknownError = Class.new ScriptError
7
+ MustUseResponseError = Class.new StandardError
8
+
9
+ setup do
10
+ mock_app do
11
+ include Hobbit::ErrorHandling
12
+
13
+ error NotFoundError do
14
+ 'Not Found'
15
+ end
16
+
17
+ error MustUseResponseError do
18
+ response.redirect '/'
19
+ end
20
+
21
+ error StandardError do
22
+ exception = env['hobbit.error']
23
+ exception.message
24
+ end
25
+
26
+ get '/' do
27
+ 'hello'
28
+ end
29
+
30
+ get '/raises' do
31
+ raise RuntimeError, 'StandardError'
32
+ 'not this'
33
+ end
34
+
35
+ get '/other_raises' do
36
+ raise NotFoundError
37
+ response.write 'not this'
38
+ end
39
+
40
+ get '/same_other_raises' do
41
+ raise SpecificNotFoundError
42
+ response.write 'not this'
43
+ end
44
+
45
+ get '/must_use_response' do
46
+ raise MustUseResponseError
47
+ response.write 'not this'
48
+ end
49
+ end
50
+ end
51
+
52
+ scope '::error' do
53
+ test do
54
+ p = Proc.new { 'error' }
55
+ app = mock_app do
56
+ include Hobbit::ErrorHandling
57
+ error StandardError, &p
58
+ end
59
+
60
+ assert app.to_app.class.errors.include? StandardError
61
+ assert app.to_app.class.errors[StandardError].call == p.call
62
+ end
63
+ end
64
+
65
+ scope '::errors' do
66
+ test 'returns a Hash' do
67
+ assert app.to_app.class.errors.kind_of? Hash
68
+ end
69
+ end
70
+
71
+ scope 'when does not raise exception' do
72
+ test 'works as expected' do
73
+ get '/'
74
+ assert last_response.ok?
75
+ assert last_response.body == 'hello'
76
+ end
77
+ end
78
+
79
+ scope 'when does raise an unknown exception class' do
80
+ test 'does not halt default propagation of the unknown class' do
81
+ mock_app do
82
+ get '/uncaught_raise' do
83
+ raise RuntimeError
84
+ end
85
+ end
86
+
87
+ assert_raises RuntimeError do
88
+ get '/uncaught_raise'
89
+ end
90
+ end
91
+ end
92
+
93
+ scope 'when raises a known exception class' do
94
+ test 'calls the block set in error' do
95
+ get '/raises'
96
+ assert last_response.ok?
97
+ assert last_response.body == 'StandardError'
98
+ end
99
+
100
+ test 'allows to define more than one exception' do
101
+ get '/other_raises'
102
+ assert last_response.ok?
103
+ assert last_response.body == 'Not Found'
104
+ end
105
+
106
+ test 'allows to define a general exception class to catch' do
107
+ get '/same_other_raises'
108
+ assert last_response.ok?
109
+ assert last_response.body == 'Not Found'
110
+ end
111
+
112
+ test 'sets the returned value of the error block as the body' do
113
+ get '/other_raises'
114
+ assert last_response.ok?
115
+ assert last_response.body == 'Not Found'
116
+ assert last_response.body != 'not this'
117
+ end
118
+
119
+ test 'overrides a previous block if a new one is passed' do
120
+ app.to_app.class.error StandardError do
121
+ 'other handler!'
122
+ end
123
+
124
+ get '/raises'
125
+ assert last_response.ok?
126
+ assert last_response.body == 'other handler!'
127
+ end
128
+
129
+ test 'uses the response object' do
130
+ get '/must_use_response'
131
+ assert last_response.redirection?
132
+ follow_redirect!
133
+ assert last_response.body == 'hello'
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,273 @@
1
+ require 'helper'
2
+
3
+ scope Hobbit::Filter do
4
+ scope 'basic specs' do
5
+ setup do
6
+ mock_app do
7
+ include Hobbit::Filter
8
+
9
+ before do
10
+ env['hobbit.before'] = 'this is before'
11
+ end
12
+
13
+ get '/' do
14
+ 'GET /'
15
+ end
16
+
17
+ after do
18
+ env['hobbit.after'] = 'this is after'
19
+ end
20
+ end
21
+ end
22
+
23
+ %w(after before).each do |kind|
24
+ str = <<EOS
25
+ scope '::#{kind}' do
26
+ test do
27
+ p = Proc.new { 'do something' }
28
+ app = mock_app do
29
+ include Hobbit::Filter
30
+ #{kind}('', &p)
31
+ end
32
+
33
+ assert app.to_app.class.filters[:#{kind}].size == 1
34
+ assert app.to_app.class.filters[:#{kind}].first[:block].call == p.call
35
+ end
36
+ end
37
+
38
+ scope 'when a filter matches' do
39
+ test "calls the filters' block" do
40
+ get '/'
41
+ assert last_response.ok?
42
+ assert last_request.env.include? 'hobbit.#{kind}'
43
+ assert last_request.env['hobbit.#{kind}'] == 'this is #{kind}'
44
+ end
45
+ end
46
+ EOS
47
+ instance_eval str
48
+ end
49
+
50
+ scope '::filters' do
51
+ test 'returns a Hash' do
52
+ assert app.to_app.class.filters.kind_of? Hash
53
+ end
54
+ end
55
+
56
+ scope '::compile_filter' do
57
+ def block
58
+ Proc.new { |env| [200, {}, []] }
59
+ end
60
+
61
+ test 'compiles /' do
62
+ path = '/'
63
+ route = app.to_app.class.send :compile_filter, path, &block
64
+ assert route[:block].call({}) == block.call({})
65
+ assert route[:compiled_path].to_s == /^\/$/.to_s
66
+ end
67
+
68
+ test 'compiles with .' do
69
+ path = '/route.json'
70
+ route = app.to_app.class.send :compile_filter, path, &block
71
+ assert route[:block].call({}) == block.call({})
72
+ assert route[:compiled_path].to_s == /^\/route.json$/.to_s
73
+ end
74
+
75
+ test 'compiles with -' do
76
+ path = '/hello-world'
77
+ route = app.to_app.class.send :compile_filter, path, &block
78
+ assert route[:block].call({}) == block.call({})
79
+ assert route[:compiled_path].to_s == /^\/hello-world$/.to_s
80
+ end
81
+
82
+ test 'compiles with params' do
83
+ path = '/hello/:name'
84
+ route = app.to_app.class.send :compile_filter, path, &block
85
+ assert route[:block].call({}) == block.call({})
86
+ assert route[:compiled_path].to_s == /^\/hello\/([^\/?#]+)$/.to_s
87
+
88
+ path = '/say/:something/to/:someone'
89
+ route = app.to_app.class.send :compile_filter, path, &block
90
+ assert route[:block].call({}) == block.call({})
91
+ assert route[:compiled_path].to_s == /^\/say\/([^\/?#]+)\/to\/([^\/?#]+)$/.to_s
92
+ end
93
+
94
+ test 'compiles with . and params' do
95
+ path = '/route/:id.json'
96
+ route = app.to_app.class.send :compile_filter, path, &block
97
+ assert route[:block].call({}) == block.call({})
98
+ assert route[:compiled_path].to_s == /^\/route\/([^\/?#]+).json$/.to_s
99
+ end
100
+ end
101
+
102
+ test 'calls before and after filters' do
103
+ get '/'
104
+ assert last_response.ok?
105
+ assert last_request.env.include? 'hobbit.before'
106
+ assert last_request.env['hobbit.before'] == 'this is before'
107
+ assert last_request.env.include? 'hobbit.after'
108
+ assert last_request.env['hobbit.after'] == 'this is after'
109
+ end
110
+ end
111
+
112
+ scope 'filters with parameters' do
113
+ setup do
114
+ mock_app do
115
+ include Hobbit::Filter
116
+
117
+ before '/:name' do
118
+ env['hobbit.before'] = 'this is before'
119
+ end
120
+
121
+ after '/:name' do
122
+ env['hobbit.after'] = 'this is after'
123
+ end
124
+
125
+ get('/:name') { env['hobbit.before'] }
126
+ end
127
+ end
128
+
129
+ test 'calls the before and after filters' do
130
+ get '/hobbit'
131
+ assert last_response.ok?
132
+ assert last_request.env.include? 'hobbit.before'
133
+ assert last_request.env['hobbit.before'] == 'this is before'
134
+ assert last_request.env.include? 'hobbit.after'
135
+ assert last_request.env['hobbit.after'] == 'this is after'
136
+ end
137
+ end
138
+
139
+ scope 'when multiple filters are declared' do
140
+ setup do
141
+ mock_app do
142
+ include Hobbit::Filter
143
+
144
+ before do
145
+ env['hobbit.before'] = 'this will match'
146
+ end
147
+
148
+ before '/' do
149
+ env['hobbit.before'] = 'this wont match'
150
+ end
151
+
152
+ after do
153
+ env['hobbit.after'] = 'this will match'
154
+ end
155
+
156
+ after '/' do
157
+ env['hobbit.after'] = 'this wont match'
158
+ end
159
+
160
+ get('/') { 'GET /' }
161
+ end
162
+ end
163
+
164
+ test 'calls the first that matches' do
165
+ get '/'
166
+ assert last_response.ok?
167
+ assert last_request.env.include? 'hobbit.before'
168
+ assert last_request.env['hobbit.before'] == 'this will match'
169
+ assert last_request.env.include? 'hobbit.after'
170
+ assert last_request.env['hobbit.after'] == 'this will match'
171
+ end
172
+ end
173
+
174
+ scope 'when a before filter redirects the response' do
175
+ setup do
176
+ mock_app do
177
+ include Hobbit::Filter
178
+
179
+ before do
180
+ response.redirect '/goodbye' unless request.path_info == '/goodbye'
181
+ end
182
+
183
+ get '/' do
184
+ 'hello world'
185
+ end
186
+
187
+ get '/goodbye' do
188
+ 'goodbye world'
189
+ end
190
+ end
191
+ end
192
+
193
+ test 'redirects on before filters' do
194
+ get '/'
195
+ assert last_response.redirection?
196
+ follow_redirect!
197
+ assert last_response.ok?
198
+ assert last_response.body =~ /goodbye world/
199
+ end
200
+ end
201
+
202
+ scope 'when halting in a before filter' do
203
+ setup do
204
+ mock_app do
205
+ include Hobbit::Filter
206
+
207
+ before do
208
+ halt 401
209
+ end
210
+
211
+ get '/' do
212
+ 'hello world'
213
+ end
214
+ end
215
+ end
216
+
217
+ test 'does not execute the route' do
218
+ get '/'
219
+ assert last_response.status == 401
220
+ assert last_response.body.empty?
221
+ end
222
+ end
223
+
224
+ scope 'when halting in a route' do
225
+ setup do
226
+ mock_app do
227
+ include Hobbit::Filter
228
+
229
+ before do
230
+ response.headers['Content-Type'] = 'text/plain'
231
+ end
232
+
233
+ after do
234
+ response.headers['Content-Type'] = 'application/json'
235
+ end
236
+
237
+ get '/' do
238
+ halt 401, 'Unauthenticated'
239
+ end
240
+ end
241
+ end
242
+
243
+ test 'does not execute the after filter' do
244
+ get '/'
245
+ assert last_response.status == 401
246
+ assert last_response.headers.include? 'Content-Type'
247
+ assert last_response.headers['Content-Type'] == 'text/plain'
248
+ assert last_response.body == 'Unauthenticated'
249
+ end
250
+ end
251
+
252
+ scope 'when halting in an after filter' do
253
+ setup do
254
+ mock_app do
255
+ include Hobbit::Filter
256
+
257
+ after do
258
+ halt 401
259
+ end
260
+
261
+ get '/' do
262
+ 'hello world'
263
+ end
264
+ end
265
+ end
266
+
267
+ test 'does not execute the route' do
268
+ get '/'
269
+ assert last_response.status == 401
270
+ assert last_response.body == 'hello world'
271
+ end
272
+ end
273
+ end