jellyfish 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/CHANGES.md +23 -0
  2. data/Gemfile +7 -0
  3. data/README.md +2 -2
  4. data/example/config.ru +11 -0
  5. data/jellyfish.gemspec +48 -3
  6. data/lib/jellyfish.rb +21 -25
  7. data/lib/jellyfish/test.rb +22 -0
  8. data/lib/jellyfish/version.rb +1 -1
  9. data/sinatra/builder_test.rb +95 -0
  10. data/sinatra/coffee_test.rb +92 -0
  11. data/sinatra/contest.rb +98 -0
  12. data/sinatra/creole_test.rb +65 -0
  13. data/sinatra/delegator_test.rb +162 -0
  14. data/sinatra/encoding_test.rb +20 -0
  15. data/sinatra/erb_test.rb +104 -0
  16. data/sinatra/extensions_test.rb +100 -0
  17. data/sinatra/filter_test.rb +428 -0
  18. data/sinatra/haml_test.rb +101 -0
  19. data/sinatra/helper.rb +123 -0
  20. data/sinatra/helpers_test.rb +1783 -0
  21. data/sinatra/integration/app.rb +62 -0
  22. data/sinatra/integration_helper.rb +214 -0
  23. data/sinatra/integration_test.rb +85 -0
  24. data/sinatra/less_test.rb +67 -0
  25. data/sinatra/liquid_test.rb +59 -0
  26. data/sinatra/mapped_error_test.rb +259 -0
  27. data/sinatra/markaby_test.rb +80 -0
  28. data/sinatra/markdown_test.rb +81 -0
  29. data/sinatra/middleware_test.rb +68 -0
  30. data/sinatra/nokogiri_test.rb +69 -0
  31. data/sinatra/rack_test.rb +45 -0
  32. data/sinatra/radius_test.rb +59 -0
  33. data/sinatra/rdoc_test.rb +66 -0
  34. data/sinatra/readme_test.rb +136 -0
  35. data/sinatra/request_test.rb +45 -0
  36. data/sinatra/response_test.rb +61 -0
  37. data/sinatra/result_test.rb +98 -0
  38. data/sinatra/route_added_hook_test.rb +59 -0
  39. data/sinatra/routing_test.rb +1104 -0
  40. data/sinatra/sass_test.rb +116 -0
  41. data/sinatra/scss_test.rb +89 -0
  42. data/sinatra/server_test.rb +48 -0
  43. data/sinatra/settings_test.rb +538 -0
  44. data/sinatra/sinatra_test.rb +17 -0
  45. data/sinatra/slim_test.rb +88 -0
  46. data/sinatra/static_test.rb +178 -0
  47. data/sinatra/streaming_test.rb +140 -0
  48. data/sinatra/templates_test.rb +298 -0
  49. data/sinatra/textile_test.rb +65 -0
  50. data/test/sinatra/test_base.rb +123 -0
  51. metadata +48 -3
@@ -0,0 +1,100 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class ExtensionsTest < Test::Unit::TestCase
4
+ module FooExtensions
5
+ def foo
6
+ end
7
+
8
+ private
9
+ def im_hiding_in_ur_foos
10
+ end
11
+ end
12
+
13
+ module BarExtensions
14
+ def bar
15
+ end
16
+ end
17
+
18
+ module BazExtensions
19
+ def baz
20
+ end
21
+ end
22
+
23
+ module QuuxExtensions
24
+ def quux
25
+ end
26
+ end
27
+
28
+ module PainExtensions
29
+ def foo=(name); end
30
+ def bar?(name); end
31
+ def fizz!(name); end
32
+ end
33
+
34
+ it 'will add the methods to the DSL for the class in which you register them and its subclasses' do
35
+ Sinatra::Base.register FooExtensions
36
+ assert Sinatra::Base.respond_to?(:foo)
37
+
38
+ Sinatra::Application.register BarExtensions
39
+ assert Sinatra::Application.respond_to?(:bar)
40
+ assert Sinatra::Application.respond_to?(:foo)
41
+ assert !Sinatra::Base.respond_to?(:bar)
42
+ end
43
+
44
+ it 'allows extending by passing a block' do
45
+ Sinatra::Base.register {
46
+ def im_in_ur_anonymous_module; end
47
+ }
48
+ assert Sinatra::Base.respond_to?(:im_in_ur_anonymous_module)
49
+ end
50
+
51
+ it 'will make sure any public methods added via Application#register are delegated to Sinatra::Delegator' do
52
+ Sinatra::Application.register FooExtensions
53
+ assert Sinatra::Delegator.private_instance_methods.
54
+ map { |m| m.to_sym }.include?(:foo)
55
+ assert !Sinatra::Delegator.private_instance_methods.
56
+ map { |m| m.to_sym }.include?(:im_hiding_in_ur_foos)
57
+ end
58
+
59
+ it 'will handle special method names' do
60
+ Sinatra::Application.register PainExtensions
61
+ assert Sinatra::Delegator.private_instance_methods.
62
+ map { |m| m.to_sym }.include?(:foo=)
63
+ assert Sinatra::Delegator.private_instance_methods.
64
+ map { |m| m.to_sym }.include?(:bar?)
65
+ assert Sinatra::Delegator.private_instance_methods.
66
+ map { |m| m.to_sym }.include?(:fizz!)
67
+ end
68
+
69
+ it 'will not delegate methods on Base#register' do
70
+ Sinatra::Base.register QuuxExtensions
71
+ assert !Sinatra::Delegator.private_instance_methods.include?("quux")
72
+ end
73
+
74
+ it 'will extend the Sinatra::Application application by default' do
75
+ Sinatra.register BazExtensions
76
+ assert !Sinatra::Base.respond_to?(:baz)
77
+ assert Sinatra::Application.respond_to?(:baz)
78
+ end
79
+
80
+ module BizzleExtension
81
+ def bizzle
82
+ bizzle_option
83
+ end
84
+
85
+ def self.registered(base)
86
+ fail "base should be BizzleApp" unless base == BizzleApp
87
+ fail "base should have already extended BizzleExtension" unless base.respond_to?(:bizzle)
88
+ base.set :bizzle_option, 'bizzle!'
89
+ end
90
+ end
91
+
92
+ class BizzleApp < Sinatra::Base
93
+ end
94
+
95
+ it 'sends .registered to the extension module after extending the class' do
96
+ BizzleApp.register BizzleExtension
97
+ assert_equal 'bizzle!', BizzleApp.bizzle_option
98
+ assert_equal 'bizzle!', BizzleApp.bizzle
99
+ end
100
+ end
@@ -0,0 +1,428 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class BeforeFilterTest < Test::Unit::TestCase
4
+ it "executes filters in the order defined" do
5
+ count = 0
6
+ mock_app do
7
+ get('/') { 'Hello World' }
8
+ before {
9
+ assert_equal 0, count
10
+ count = 1
11
+ }
12
+ before {
13
+ assert_equal 1, count
14
+ count = 2
15
+ }
16
+ end
17
+
18
+ get '/'
19
+ assert ok?
20
+ assert_equal 2, count
21
+ assert_equal 'Hello World', body
22
+ end
23
+
24
+ it "can modify the request" do
25
+ mock_app {
26
+ get('/foo') { 'foo' }
27
+ get('/bar') { 'bar' }
28
+ before { request.path_info = '/bar' }
29
+ }
30
+
31
+ get '/foo'
32
+ assert ok?
33
+ assert_equal 'bar', body
34
+ end
35
+
36
+ it "can modify instance variables available to routes" do
37
+ mock_app {
38
+ before { @foo = 'bar' }
39
+ get('/foo') { @foo }
40
+ }
41
+
42
+ get '/foo'
43
+ assert ok?
44
+ assert_equal 'bar', body
45
+ end
46
+
47
+ it "allows redirects" do
48
+ mock_app {
49
+ before { redirect '/bar' }
50
+ get('/foo') do
51
+ fail 'before block should have halted processing'
52
+ 'ORLY?!'
53
+ end
54
+ }
55
+
56
+ get '/foo'
57
+ assert redirect?
58
+ assert_equal 'http://example.org/bar', response['Location']
59
+ assert_equal '', body
60
+ end
61
+
62
+ it "does not modify the response with its return value" do
63
+ mock_app {
64
+ before { 'Hello World!' }
65
+ get '/foo' do
66
+ assert_equal [], response.body
67
+ 'cool'
68
+ end
69
+ }
70
+
71
+ get '/foo'
72
+ assert ok?
73
+ assert_equal 'cool', body
74
+ end
75
+
76
+ it "does modify the response with halt" do
77
+ mock_app {
78
+ before { halt 302, 'Hi' }
79
+ get '/foo' do
80
+ "should not happen"
81
+ end
82
+ }
83
+
84
+ get '/foo'
85
+ assert_equal 302, response.status
86
+ assert_equal 'Hi', body
87
+ end
88
+
89
+ it "gives you access to params" do
90
+ mock_app {
91
+ before { @foo = params['foo'] }
92
+ get('/foo') { @foo }
93
+ }
94
+
95
+ get '/foo?foo=cool'
96
+ assert ok?
97
+ assert_equal 'cool', body
98
+ end
99
+
100
+ it "properly unescapes parameters" do
101
+ mock_app {
102
+ before { @foo = params['foo'] }
103
+ get('/foo') { @foo }
104
+ }
105
+
106
+ get '/foo?foo=bar%3Abaz%2Fbend'
107
+ assert ok?
108
+ assert_equal 'bar:baz/bend', body
109
+ end
110
+
111
+ it "runs filters defined in superclasses" do
112
+ base = Class.new(Sinatra::Base)
113
+ base.before { @foo = 'hello from superclass' }
114
+
115
+ mock_app(base) {
116
+ get('/foo') { @foo }
117
+ }
118
+
119
+ get '/foo'
120
+ assert_equal 'hello from superclass', body
121
+ end
122
+
123
+ it 'does not run before filter when serving static files' do
124
+ ran_filter = false
125
+ mock_app {
126
+ before { ran_filter = true }
127
+ set :static, true
128
+ set :public_folder, File.dirname(__FILE__)
129
+ }
130
+ get "/#{File.basename(__FILE__)}"
131
+ assert ok?
132
+ assert_equal File.read(__FILE__), body
133
+ assert !ran_filter
134
+ end
135
+
136
+ it 'takes an optional route pattern' do
137
+ ran_filter = false
138
+ mock_app do
139
+ before("/b*") { ran_filter = true }
140
+ get('/foo') { }
141
+ get('/bar') { }
142
+ end
143
+ get '/foo'
144
+ assert !ran_filter
145
+ get '/bar'
146
+ assert ran_filter
147
+ end
148
+
149
+ it 'generates block arguments from route pattern' do
150
+ subpath = nil
151
+ mock_app do
152
+ before("/foo/:sub") { |s| subpath = s }
153
+ get('/foo/*') { }
154
+ end
155
+ get '/foo/bar'
156
+ assert_equal subpath, 'bar'
157
+ end
158
+ end
159
+
160
+ class AfterFilterTest < Test::Unit::TestCase
161
+ it "executes before and after filters in correct order" do
162
+ invoked = 0
163
+ mock_app do
164
+ before { invoked = 2 }
165
+ get('/') { invoked += 2; 'hello' }
166
+ after { invoked *= 2 }
167
+ end
168
+
169
+ get '/'
170
+ assert ok?
171
+
172
+ assert_equal 8, invoked
173
+ end
174
+
175
+ it "executes filters in the order defined" do
176
+ count = 0
177
+ mock_app do
178
+ get('/') { 'Hello World' }
179
+ after {
180
+ assert_equal 0, count
181
+ count = 1
182
+ }
183
+ after {
184
+ assert_equal 1, count
185
+ count = 2
186
+ }
187
+ end
188
+
189
+ get '/'
190
+ assert ok?
191
+ assert_equal 2, count
192
+ assert_equal 'Hello World', body
193
+ end
194
+
195
+ it "allows redirects" do
196
+ mock_app {
197
+ get('/foo') { 'ORLY' }
198
+ after { redirect '/bar' }
199
+ }
200
+
201
+ get '/foo'
202
+ assert redirect?
203
+ assert_equal 'http://example.org/bar', response['Location']
204
+ assert_equal '', body
205
+ end
206
+
207
+ it "does not modify the response with its return value" do
208
+ mock_app {
209
+ get('/foo') { 'cool' }
210
+ after { 'Hello World!' }
211
+ }
212
+
213
+ get '/foo'
214
+ assert ok?
215
+ assert_equal 'cool', body
216
+ end
217
+
218
+ it "does modify the response with halt" do
219
+ mock_app {
220
+ get '/foo' do
221
+ "should not be returned"
222
+ end
223
+ after { halt 302, 'Hi' }
224
+ }
225
+
226
+ get '/foo'
227
+ assert_equal 302, response.status
228
+ assert_equal 'Hi', body
229
+ end
230
+
231
+ it "runs filters defined in superclasses" do
232
+ count = 2
233
+ base = Class.new(Sinatra::Base)
234
+ base.after { count *= 2 }
235
+ mock_app(base) do
236
+ get('/foo') do
237
+ count += 2
238
+ "ok"
239
+ end
240
+ end
241
+
242
+ get '/foo'
243
+ assert_equal 8, count
244
+ end
245
+
246
+ it 'does not run after filter when serving static files' do
247
+ ran_filter = false
248
+ mock_app {
249
+ after { ran_filter = true }
250
+ set :static, true
251
+ set :public_folder, File.dirname(__FILE__)
252
+ }
253
+ get "/#{File.basename(__FILE__)}"
254
+ assert ok?
255
+ assert_equal File.read(__FILE__), body
256
+ assert !ran_filter
257
+ end
258
+
259
+ it 'takes an optional route pattern' do
260
+ ran_filter = false
261
+ mock_app do
262
+ after("/b*") { ran_filter = true }
263
+ get('/foo') { }
264
+ get('/bar') { }
265
+ end
266
+ get '/foo'
267
+ assert !ran_filter
268
+ get '/bar'
269
+ assert ran_filter
270
+ end
271
+
272
+ it 'changes to path_info from a pattern matching before filter are respoected when routing' do
273
+ mock_app do
274
+ before('/foo') { request.path_info = '/bar' }
275
+ get('/bar') { 'blah' }
276
+ end
277
+ get '/foo'
278
+ assert ok?
279
+ assert_equal 'blah', body
280
+ end
281
+
282
+ it 'generates block arguments from route pattern' do
283
+ subpath = nil
284
+ mock_app do
285
+ after("/foo/:sub") { |s| subpath = s }
286
+ get('/foo/*') { }
287
+ end
288
+ get '/foo/bar'
289
+ assert_equal subpath, 'bar'
290
+ end
291
+
292
+ it 'is possible to access url params from the route param' do
293
+ ran = false
294
+ mock_app do
295
+ get('/foo/*') { }
296
+ before('/foo/:sub') do
297
+ assert_equal params[:sub], 'bar'
298
+ ran = true
299
+ end
300
+ end
301
+ get '/foo/bar'
302
+ assert ran
303
+ end
304
+
305
+ it 'is possible to apply host_name conditions to before filters with no path' do
306
+ ran = false
307
+ mock_app do
308
+ before(:host_name => 'example.com') { ran = true }
309
+ get('/') { 'welcome' }
310
+ end
311
+ get '/', {}, { 'HTTP_HOST' => 'example.org' }
312
+ assert !ran
313
+ get '/', {}, { 'HTTP_HOST' => 'example.com' }
314
+ assert ran
315
+ end
316
+
317
+ it 'is possible to apply host_name conditions to before filters with a path' do
318
+ ran = false
319
+ mock_app do
320
+ before('/foo', :host_name => 'example.com') { ran = true }
321
+ get('/') { 'welcome' }
322
+ end
323
+ get '/', {}, { 'HTTP_HOST' => 'example.com' }
324
+ assert !ran
325
+ get '/foo', {}, { 'HTTP_HOST' => 'example.org' }
326
+ assert !ran
327
+ get '/foo', {}, { 'HTTP_HOST' => 'example.com' }
328
+ assert ran
329
+ end
330
+
331
+ it 'is possible to apply host_name conditions to after filters with no path' do
332
+ ran = false
333
+ mock_app do
334
+ after(:host_name => 'example.com') { ran = true }
335
+ get('/') { 'welcome' }
336
+ end
337
+ get '/', {}, { 'HTTP_HOST' => 'example.org' }
338
+ assert !ran
339
+ get '/', {}, { 'HTTP_HOST' => 'example.com' }
340
+ assert ran
341
+ end
342
+
343
+ it 'is possible to apply host_name conditions to after filters with a path' do
344
+ ran = false
345
+ mock_app do
346
+ after('/foo', :host_name => 'example.com') { ran = true }
347
+ get('/') { 'welcome' }
348
+ end
349
+ get '/', {}, { 'HTTP_HOST' => 'example.com' }
350
+ assert !ran
351
+ get '/foo', {}, { 'HTTP_HOST' => 'example.org' }
352
+ assert !ran
353
+ get '/foo', {}, { 'HTTP_HOST' => 'example.com' }
354
+ assert ran
355
+ end
356
+
357
+ it 'is possible to apply user_agent conditions to before filters with no path' do
358
+ ran = false
359
+ mock_app do
360
+ before(:user_agent => /foo/) { ran = true }
361
+ get('/') { 'welcome' }
362
+ end
363
+ get '/', {}, { 'HTTP_USER_AGENT' => 'bar' }
364
+ assert !ran
365
+ get '/', {}, { 'HTTP_USER_AGENT' => 'foo' }
366
+ assert ran
367
+ end
368
+
369
+ it 'is possible to apply user_agent conditions to before filters with a path' do
370
+ ran = false
371
+ mock_app do
372
+ before('/foo', :user_agent => /foo/) { ran = true }
373
+ get('/') { 'welcome' }
374
+ end
375
+ get '/', {}, { 'HTTP_USER_AGENT' => 'foo' }
376
+ assert !ran
377
+ get '/foo', {}, { 'HTTP_USER_AGENT' => 'bar' }
378
+ assert !ran
379
+ get '/foo', {}, { 'HTTP_USER_AGENT' => 'foo' }
380
+ assert ran
381
+ end
382
+
383
+ it 'can add params' do
384
+ mock_app do
385
+ before { params['foo'] = 'bar' }
386
+ get('/') { params['foo'] }
387
+ end
388
+
389
+ get '/'
390
+ assert_body 'bar'
391
+ end
392
+
393
+ it 'can remove params' do
394
+ mock_app do
395
+ before { params.delete('foo') }
396
+ get('/') { params['foo'].to_s }
397
+ end
398
+
399
+ get '/?foo=bar'
400
+ assert_body ''
401
+ end
402
+
403
+ it 'is possible to apply user_agent conditions to after filters with no path' do
404
+ ran = false
405
+ mock_app do
406
+ after(:user_agent => /foo/) { ran = true }
407
+ get('/') { 'welcome' }
408
+ end
409
+ get '/', {}, { 'HTTP_USER_AGENT' => 'bar' }
410
+ assert !ran
411
+ get '/', {}, { 'HTTP_USER_AGENT' => 'foo' }
412
+ assert ran
413
+ end
414
+
415
+ it 'is possible to apply user_agent conditions to after filters with a path' do
416
+ ran = false
417
+ mock_app do
418
+ after('/foo', :user_agent => /foo/) { ran = true }
419
+ get('/') { 'welcome' }
420
+ end
421
+ get '/', {}, { 'HTTP_USER_AGENT' => 'foo' }
422
+ assert !ran
423
+ get '/foo', {}, { 'HTTP_USER_AGENT' => 'bar' }
424
+ assert !ran
425
+ get '/foo', {}, { 'HTTP_USER_AGENT' => 'foo' }
426
+ assert ran
427
+ end
428
+ end