hobbit-contrib 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,136 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Hobbit::ErrorHandling do
4
- include Hobbit::Contrib::Mock
5
- include Rack::Test::Methods
6
-
7
- class NotFoundException < Exception ; end
8
- class SpecificNotFoundException < NotFoundException ; end
9
- class UnknownException < Exception ; end
10
- class MustUseResponseException < Exception ; end
11
-
12
- let :app do
13
- mock_app do
14
- include Hobbit::ErrorHandling
15
-
16
- error NotFoundException do
17
- 'Not Found'
18
- end
19
-
20
- error StandardError do
21
- exception = env['hobbit.error']
22
- exception.message
23
- end
24
-
25
- error MustUseResponseException do
26
- response.redirect '/'
27
- end
28
-
29
- get '/' do
30
- 'hello'
31
- end
32
-
33
- get '/raises' do
34
- raise StandardError, 'StandardError'
35
- 'not this'
36
- end
37
-
38
- get '/other_raises' do
39
- raise NotFoundException
40
- response.write 'not this'
41
- end
42
-
43
- get '/same_other_raises' do
44
- raise SpecificNotFoundException
45
- response.write 'not this'
46
- end
47
-
48
- get '/uncaught_raise' do
49
- raise UnknownException
50
- response.write 'not this'
51
- end
52
-
53
- get '/must_use_response' do
54
- raise MustUseResponseException
55
- response.write 'not this'
56
- end
57
- end
58
- end
59
-
60
- describe '::error' do
61
- specify do
62
- p = Proc.new { 'error' }
63
- app = mock_app do
64
- include Hobbit::ErrorHandling
65
- error StandardError, &p
66
- end
67
-
68
- app.to_app.class.errors.must_include StandardError
69
- app.to_app.class.errors[StandardError].call.must_equal p.call
70
- end
71
- end
72
-
73
- describe '::errors' do
74
- it 'must return a Hash' do
75
- app.to_app.class.errors.must_be_kind_of Hash
76
- end
77
- end
78
-
79
- describe 'when does not raise exception' do
80
- it 'must work as expected' do
81
- get '/'
82
- last_response.must_be :ok?
83
- last_response.body.must_equal 'hello'
84
- end
85
- end
86
-
87
- describe 'when does raise an unknown exception class' do
88
- it 'must not halt default propagation of the unknown class' do
89
- proc { get '/uncaught_raise' }.must_raise UnknownException
90
- end
91
- end
92
-
93
- describe 'when raises a known exception class' do
94
- it 'must call the block set in error' do
95
- get '/raises'
96
- last_response.must_be :ok?
97
- last_response.body.must_equal 'StandardError'
98
- end
99
-
100
- it 'must allow to define more than one exception' do
101
- get '/other_raises'
102
- last_response.must_be :ok?
103
- last_response.body.must_equal 'Not Found'
104
- end
105
-
106
- it 'must allow to define a general exception class to catch' do
107
- get '/same_other_raises'
108
- last_response.must_be :ok?
109
- last_response.body.must_equal 'Not Found'
110
- end
111
-
112
- it 'must set the returned value of the error block as the body' do
113
- get '/other_raises'
114
- last_response.must_be :ok?
115
- last_response.body.must_equal 'Not Found'
116
- last_response.body.wont_equal 'not this'
117
- end
118
-
119
- it 'must override 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
- last_response.must_be :ok?
126
- last_response.body.must_equal 'other handler!'
127
- end
128
-
129
- it 'must use response object' do
130
- get '/must_use_response'
131
- last_response.must_be :redirection?
132
- follow_redirect!
133
- last_response.body.must_equal 'hello'
134
- end
135
- end
136
- end
@@ -1,274 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Hobbit::Filter do
4
- include Hobbit::Contrib::Mock
5
- include Rack::Test::Methods
6
-
7
- describe 'basic specs' do
8
- let :app do
9
- mock_app do
10
- include Hobbit::Filter
11
-
12
- before do
13
- env['hobbit.before'] = 'this is before'
14
- end
15
-
16
- get '/' do
17
- 'GET /'
18
- end
19
-
20
- after do
21
- env['hobbit.after'] = 'this is after'
22
- end
23
- end
24
- end
25
-
26
- %w(after before).each do |kind|
27
- str = <<EOS
28
- describe '::#{kind}' do
29
- specify do
30
- p = Proc.new { 'do something' }
31
- app = mock_app do
32
- include Hobbit::Filter
33
- #{kind}('', &p)
34
- end
35
-
36
- app.to_app.class.filters[:#{kind}].size.must_equal 1
37
- app.to_app.class.filters[:#{kind}].first[:block].call.must_equal p.call
38
- end
39
- end
40
-
41
- describe 'when a filter matches' do
42
- it "must call the filters' block" do
43
- get '/'
44
- last_response.must_be :ok?
45
- last_request.env.must_include 'hobbit.#{kind}'
46
- last_request.env['hobbit.#{kind}'].must_equal 'this is #{kind}'
47
- end
48
- end
49
- EOS
50
- class_eval str
51
- end
52
-
53
- describe '::filters' do
54
- it 'must return a Hash' do
55
- app.to_app.class.filters.must_be_kind_of Hash
56
- end
57
- end
58
-
59
- describe '::compile_filter' do
60
- let(:block) { block = Proc.new { |env| [200, {}, []] } }
61
-
62
- it 'must compile /' do
63
- path = '/'
64
- route = app.to_app.class.send :compile_filter, path, &block
65
- route[:block].call({}).must_equal block.call({})
66
- route[:compiled_path].to_s.must_equal /^\/$/.to_s
67
- end
68
-
69
- it 'must compile with .' do
70
- path = '/route.json'
71
- route = app.to_app.class.send :compile_filter, path, &block
72
- route[:block].call({}).must_equal block.call({})
73
- route[:compiled_path].to_s.must_equal /^\/route.json$/.to_s
74
- end
75
-
76
- it 'must compile with -' do
77
- path = '/hello-world'
78
- route = app.to_app.class.send :compile_filter, path, &block
79
- route[:block].call({}).must_equal block.call({})
80
- route[:compiled_path].to_s.must_equal /^\/hello-world$/.to_s
81
- end
82
-
83
- it 'must compile with params' do
84
- path = '/hello/:name'
85
- route = app.to_app.class.send :compile_filter, path, &block
86
- route[:block].call({}).must_equal block.call({})
87
- route[:compiled_path].to_s.must_equal /^\/hello\/([^\/?#]+)$/.to_s
88
-
89
- path = '/say/:something/to/:someone'
90
- route = app.to_app.class.send :compile_filter, path, &block
91
- route[:block].call({}).must_equal block.call({})
92
- route[:compiled_path].to_s.must_equal /^\/say\/([^\/?#]+)\/to\/([^\/?#]+)$/.to_s
93
- end
94
-
95
- it 'must compile with . and params' do
96
- path = '/route/:id.json'
97
- route = app.to_app.class.send :compile_filter, path, &block
98
- route[:block].call({}).must_equal block.call({})
99
- route[:compiled_path].to_s.must_equal /^\/route\/([^\/?#]+).json$/.to_s
100
- end
101
- end
102
-
103
- it 'must call before and after filters' do
104
- get '/'
105
- last_response.must_be :ok?
106
- last_request.env.must_include 'hobbit.before'
107
- last_request.env['hobbit.before'].must_equal 'this is before'
108
- last_request.env.must_include 'hobbit.after'
109
- last_request.env['hobbit.after'].must_equal 'this is after'
110
- end
111
- end
112
-
113
- describe 'filters with parameters' do
114
- let :app do
115
- mock_app do
116
- include Hobbit::Filter
117
-
118
- before '/:name' do
119
- env['hobbit.before'] = 'this is before'
120
- end
121
-
122
- after '/:name' do
123
- env['hobbit.after'] = 'this is after'
124
- end
125
-
126
- get('/:name') { env['hobbit.before'] }
127
- end
128
- end
129
-
130
- it 'must call the before and after filters' do
131
- get '/hobbit'
132
- last_response.must_be :ok?
133
- last_request.env.must_include 'hobbit.before'
134
- last_request.env['hobbit.before'].must_equal 'this is before'
135
- last_request.env.must_include 'hobbit.after'
136
- last_request.env['hobbit.after'].must_equal 'this is after'
137
- end
138
- end
139
-
140
- describe 'when multiple filters are declared' do
141
- let :app do
142
- mock_app do
143
- include Hobbit::Filter
144
-
145
- before do
146
- env['hobbit.before'] = 'this will match'
147
- end
148
-
149
- before '/' do
150
- env['hobbit.before'] = 'this wont match'
151
- end
152
-
153
- after do
154
- env['hobbit.after'] = 'this will match'
155
- end
156
-
157
- after '/' do
158
- env['hobbit.after'] = 'this wont match'
159
- end
160
-
161
- get('/') { 'GET /' }
162
- end
163
- end
164
-
165
- it 'must call the first that matches' do
166
- get '/'
167
- last_response.must_be :ok?
168
- last_request.env.must_include 'hobbit.before'
169
- last_request.env['hobbit.before'].must_equal 'this will match'
170
- last_request.env.must_include 'hobbit.after'
171
- last_request.env['hobbit.after'].must_equal 'this will match'
172
- end
173
- end
174
-
175
- describe 'when a before filter redirects the response' do
176
- let :app do
177
- mock_app do
178
- include Hobbit::Filter
179
-
180
- before do
181
- response.redirect '/goodbye' unless request.path_info == '/goodbye'
182
- end
183
-
184
- get '/' do
185
- 'hello world'
186
- end
187
-
188
- get '/goodbye' do
189
- 'goodbye world'
190
- end
191
- end
192
- end
193
-
194
- it 'must redirect on before filters' do
195
- get '/'
196
- last_response.must_be :redirection?
197
- follow_redirect!
198
- last_response.must_be :ok?
199
- last_response.body.must_match /goodbye world/
200
- end
201
- end
202
-
203
- describe 'when halting in a before filter' do
204
- let :app do
205
- mock_app do
206
- include Hobbit::Filter
207
-
208
- before do
209
- halt 401
210
- end
211
-
212
- get '/' do
213
- 'hello world'
214
- end
215
- end
216
- end
217
-
218
- it 'wont execute the route' do
219
- get '/'
220
- last_response.status.must_equal 401
221
- last_response.body.must_be :empty?
222
- end
223
- end
224
-
225
- describe 'when halting in a route' do
226
- let :app do
227
- mock_app do
228
- include Hobbit::Filter
229
-
230
- before do
231
- response.headers['Content-Type'] = 'text/plain'
232
- end
233
-
234
- after do
235
- response.headers['Content-Type'] = 'application/json'
236
- end
237
-
238
- get '/' do
239
- halt 401, 'Unauthenticated'
240
- end
241
- end
242
- end
243
-
244
- it 'wont execute the after filter' do
245
- get '/'
246
- last_response.status.must_equal 401
247
- last_response.headers.must_include 'Content-Type'
248
- last_response.headers['Content-Type'].must_equal 'text/plain'
249
- last_response.body.must_equal 'Unauthenticated'
250
- end
251
- end
252
-
253
- describe 'when halting in an after filter' do
254
- let :app do
255
- mock_app do
256
- include Hobbit::Filter
257
-
258
- after do
259
- halt 401
260
- end
261
-
262
- get '/' do
263
- 'hello world'
264
- end
265
- end
266
- end
267
-
268
- it 'wont execute the route' do
269
- get '/'
270
- last_response.status.must_equal 401
271
- last_response.body.must_equal 'hello world'
272
- end
273
- end
274
- end
@@ -1,25 +0,0 @@
1
- require 'codeclimate-test-reporter'
2
- CodeClimate::TestReporter.start
3
-
4
- ENV['RACK_ENV'] ||= 'test'
5
-
6
- require 'bundler'
7
- Bundler.require :default, ENV['RACK_ENV'].to_sym
8
-
9
- require 'minitest/autorun'
10
- require 'rack'
11
- require 'rack/test'
12
-
13
- require 'hobbit'
14
- require 'hobbit/contrib'
15
-
16
- module Hobbit
17
- module Contrib
18
- module Mock
19
- def mock_app(&block)
20
- app = Class.new Hobbit::Base, &block
21
- app.new
22
- end
23
- end
24
- end
25
- end
@@ -1,89 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Hobbit::Mote do
4
- include Hobbit::Contrib::Mock
5
- include Rack::Test::Methods
6
-
7
- def app
8
- mock_app do
9
- include Hobbit::Mote
10
-
11
- def views_path
12
- File.expand_path '../fixtures/mote/views/', __FILE__
13
- end
14
-
15
- def name
16
- 'Hobbit'
17
- end
18
-
19
- get('/') { render 'index' }
20
- get('/partial') { partial 'partial' }
21
- get('/using-context') { render 'hello' }
22
- get('/without-layout') { render '_partial', layout: false }
23
- end
24
- end
25
-
26
- describe '#default_layout' do
27
- it 'defaults to application.mote' do
28
- app.to_app.default_layout.must_equal "#{app.to_app.layouts_path}/application.mote"
29
- end
30
- end
31
-
32
- describe '#find_template' do
33
- it 'must return a template path' do
34
- app.to_app.find_template('index').must_equal "#{app.to_app.views_path}/index.mote"
35
- end
36
- end
37
-
38
- describe '#layouts_path' do
39
- it 'must return the path to the layouts directory' do
40
- app.to_app.layouts_path.must_equal "#{app.to_app.views_path}/layouts"
41
- end
42
- end
43
-
44
- describe '#partial' do
45
- it 'must render a template without a layout' do
46
- get '/partial'
47
- last_response.must_be :ok?
48
- last_response.body.wont_match /From application.mote/
49
- last_response.body.must_match /Hello World!/
50
- end
51
- end
52
-
53
- describe '#render' do
54
- it 'must render a template (using a layout)' do
55
- get '/'
56
- last_response.must_be :ok?
57
- last_response.body.must_match /From application.mote/
58
- last_response.body.must_match /Hello World!/
59
- end
60
-
61
- it 'must render a template (not using a layout)' do
62
- get '/without-layout'
63
- last_response.must_be :ok?
64
- last_response.body.wont_match /From application.mote/
65
- last_response.body.must_match /Hello World!/
66
- end
67
-
68
- it 'must render a template using the app as context' do
69
- get '/using-context'
70
- last_response.must_be :ok?
71
- last_response.body.must_match /From application.mote/
72
- last_response.body.must_match /Hello Hobbit!/
73
- end
74
- end
75
-
76
- describe '#views_path' do
77
- it 'must return the path to the views directory' do
78
- app.to_app.views_path.must_equal File.expand_path('../fixtures/mote/views', __FILE__)
79
- end
80
-
81
- it 'defaults to "views"' do
82
- a = mock_app do
83
- include Hobbit::Mote
84
- end
85
-
86
- a.to_app.views_path.must_equal 'views'
87
- end
88
- end
89
- end