sinatra 0.9.0.5 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

data/test/routing_test.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
+ # Helper method for easy route pattern matching testing
4
+ def route_def(pattern)
5
+ mock_app { get(pattern) { } }
6
+ end
7
+
3
8
  describe "Routing" do
4
9
  %w[get put post delete].each do |verb|
5
10
  it "defines #{verb.upcase} request handlers with #{verb}" do
@@ -39,6 +44,27 @@ describe "Routing" do
39
44
  assert_equal 404, status
40
45
  end
41
46
 
47
+ it 'takes multiple definitions of a route' do
48
+ mock_app {
49
+ user_agent(/Foo/)
50
+ get '/foo' do
51
+ 'foo'
52
+ end
53
+
54
+ get '/foo' do
55
+ 'not foo'
56
+ end
57
+ }
58
+
59
+ get '/foo', {}, 'HTTP_USER_AGENT' => 'Foo'
60
+ assert ok?
61
+ assert_equal 'foo', body
62
+
63
+ get '/foo'
64
+ assert ok?
65
+ assert_equal 'not foo', body
66
+ end
67
+
42
68
  it "exposes params with indifferent hash" do
43
69
  mock_app {
44
70
  get '/:foo' do
@@ -134,6 +160,64 @@ describe "Routing" do
134
160
  assert ok?
135
161
  end
136
162
 
163
+ it "matches a dot ('.') as part of a named param" do
164
+ mock_app {
165
+ get '/:foo/:bar' do
166
+ params[:foo]
167
+ end
168
+ }
169
+
170
+ get '/user@example.com/name'
171
+ assert_equal 200, response.status
172
+ assert_equal 'user@example.com', body
173
+ end
174
+
175
+ it "matches a literal dot ('.') outside of named params" do
176
+ mock_app {
177
+ get '/:file.:ext' do
178
+ assert_equal 'pony', params[:file]
179
+ assert_equal 'jpg', params[:ext]
180
+ 'right on'
181
+ end
182
+ }
183
+
184
+ get '/pony.jpg'
185
+ assert_equal 200, response.status
186
+ assert_equal 'right on', body
187
+ end
188
+
189
+ it "literally matches . in paths" do
190
+ route_def '/test.bar'
191
+
192
+ get '/test.bar'
193
+ assert ok?
194
+ get 'test0bar'
195
+ assert not_found?
196
+ end
197
+
198
+ it "literally matches $ in paths" do
199
+ route_def '/test$/'
200
+
201
+ get '/test$/'
202
+ assert ok?
203
+ end
204
+
205
+ it "literally matches + in paths" do
206
+ route_def '/te+st/'
207
+
208
+ get '/te%2Bst/'
209
+ assert ok?
210
+ get '/teeeeeeest/'
211
+ assert not_found?
212
+ end
213
+
214
+ it "literally matches () in paths" do
215
+ route_def '/test(bar)/'
216
+
217
+ get '/test(bar)/'
218
+ assert ok?
219
+ end
220
+
137
221
  it "supports basic nested params" do
138
222
  mock_app {
139
223
  get '/hi' do
@@ -186,7 +270,7 @@ describe "Routing" do
186
270
  'looks good'
187
271
  end
188
272
  }
189
- get "/foo?#{param_string(input)}"
273
+ get "/foo?#{build_query(input)}"
190
274
  assert ok?
191
275
  assert_equal 'looks good', body
192
276
  end
@@ -206,7 +290,7 @@ describe "Routing" do
206
290
  assert_equal 'looks good', body
207
291
  end
208
292
 
209
- it "supports paths that include spaces" do
293
+ it "matches paths that include spaces encoded with %20" do
210
294
  mock_app {
211
295
  get '/path with spaces' do
212
296
  'looks good'
@@ -218,6 +302,18 @@ describe "Routing" do
218
302
  assert_equal 'looks good', body
219
303
  end
220
304
 
305
+ it "matches paths that include spaces encoded with +" do
306
+ mock_app {
307
+ get '/path with spaces' do
308
+ 'looks good'
309
+ end
310
+ }
311
+
312
+ get '/path+with+spaces'
313
+ assert ok?
314
+ assert_equal 'looks good', body
315
+ end
316
+
221
317
  it "URL decodes named parameters and splats" do
222
318
  mock_app {
223
319
  get '/:foo/*' do
@@ -256,6 +352,11 @@ describe "Routing" do
256
352
  assert_equal 'right on', body
257
353
  end
258
354
 
355
+ it 'raises a TypeError when pattern is not a String or Regexp' do
356
+ @app = mock_app
357
+ assert_raise(TypeError) { @app.get(42){} }
358
+ end
359
+
259
360
  it "returns response immediately on halt" do
260
361
  mock_app {
261
362
  get '/' do
@@ -442,4 +543,170 @@ describe "Routing" do
442
543
  assert_equal type, response.headers['Content-Type']
443
544
  end
444
545
  end
546
+
547
+ it 'degrades gracefully when optional accept header is not provided' do
548
+ mock_app {
549
+ get '/', :provides => :xml do
550
+ request.env['HTTP_ACCEPT']
551
+ end
552
+ get '/' do
553
+ 'default'
554
+ end
555
+ }
556
+ get '/'
557
+ assert ok?
558
+ assert_equal 'default', body
559
+ end
560
+
561
+ it 'passes a single url param as block parameters when one param is specified' do
562
+ mock_app {
563
+ get '/:foo' do |foo|
564
+ assert_equal 'bar', foo
565
+ end
566
+ }
567
+
568
+ get '/bar'
569
+ assert ok?
570
+ end
571
+
572
+ it 'passes multiple params as block parameters when many are specified' do
573
+ mock_app {
574
+ get '/:foo/:bar/:baz' do |foo, bar, baz|
575
+ assert_equal 'abc', foo
576
+ assert_equal 'def', bar
577
+ assert_equal 'ghi', baz
578
+ end
579
+ }
580
+
581
+ get '/abc/def/ghi'
582
+ assert ok?
583
+ end
584
+
585
+ it 'passes regular expression captures as block parameters' do
586
+ mock_app {
587
+ get(/^\/fo(.*)\/ba(.*)/) do |foo, bar|
588
+ assert_equal 'orooomma', foo
589
+ assert_equal 'f', bar
590
+ 'looks good'
591
+ end
592
+ }
593
+
594
+ get '/foorooomma/baf'
595
+ assert ok?
596
+ assert_equal 'looks good', body
597
+ end
598
+
599
+ it "supports mixing multiple splat params like /*/foo/*/* as block parameters" do
600
+ mock_app {
601
+ get '/*/foo/*/*' do |foo, bar, baz|
602
+ assert_equal 'bar', foo
603
+ assert_equal 'bling', bar
604
+ assert_equal 'baz/boom', baz
605
+ 'looks good'
606
+ end
607
+ }
608
+
609
+ get '/bar/foo/bling/baz/boom'
610
+ assert ok?
611
+ assert_equal 'looks good', body
612
+ end
613
+
614
+ it 'raises an ArgumentError with block arity > 1 and too many values' do
615
+ mock_app {
616
+ get '/:foo/:bar/:baz' do |foo, bar|
617
+ 'quux'
618
+ end
619
+ }
620
+
621
+ assert_raise(ArgumentError) { get '/a/b/c' }
622
+ end
623
+
624
+ it 'raises an ArgumentError with block param arity > 1 and too few values' do
625
+ mock_app {
626
+ get '/:foo/:bar' do |foo, bar, baz|
627
+ 'quux'
628
+ end
629
+ }
630
+
631
+ assert_raise(ArgumentError) { get '/a/b' }
632
+ end
633
+
634
+ it 'succeeds if no block parameters are specified' do
635
+ mock_app {
636
+ get '/:foo/:bar' do
637
+ 'quux'
638
+ end
639
+ }
640
+
641
+ get '/a/b'
642
+ assert ok?
643
+ assert_equal 'quux', body
644
+ end
645
+
646
+ it 'passes all params with block param arity -1 (splat args)' do
647
+ mock_app {
648
+ get '/:foo/:bar' do |*args|
649
+ args.join
650
+ end
651
+ }
652
+
653
+ get '/a/b'
654
+ assert ok?
655
+ assert_equal 'ab', body
656
+ end
657
+
658
+ # NOTE Block params behaves differently under 1.8 and 1.9. Under 1.8, block
659
+ # param arity is lax: declaring a mismatched number of block params results
660
+ # in a warning. Under 1.9, block param arity is strict: mismatched block
661
+ # arity raises an ArgumentError.
662
+
663
+ if RUBY_VERSION >= '1.9'
664
+
665
+ it 'raises an ArgumentError with block param arity 1 and no values' do
666
+ mock_app {
667
+ get '/foo' do |foo|
668
+ 'quux'
669
+ end
670
+ }
671
+
672
+ assert_raise(ArgumentError) { get '/foo' }
673
+ end
674
+
675
+ it 'raises an ArgumentError with block param arity 1 and too many values' do
676
+ mock_app {
677
+ get '/:foo/:bar/:baz' do |foo|
678
+ 'quux'
679
+ end
680
+ }
681
+
682
+ assert_raise(ArgumentError) { get '/a/b/c' }
683
+ end
684
+
685
+ else
686
+
687
+ it 'does not raise an ArgumentError with block param arity 1 and no values' do
688
+ mock_app {
689
+ get '/foo' do |foo|
690
+ 'quux'
691
+ end
692
+ }
693
+
694
+ silence_warnings { get '/foo' }
695
+ assert ok?
696
+ assert_equal 'quux', body
697
+ end
698
+
699
+ it 'does not raise an ArgumentError with block param arity 1 and too many values' do
700
+ mock_app {
701
+ get '/:foo/:bar/:baz' do |foo|
702
+ 'quux'
703
+ end
704
+ }
705
+
706
+ silence_warnings { get '/a/b/c' }
707
+ assert ok?
708
+ assert_equal 'quux', body
709
+ end
710
+
711
+ end
445
712
  end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class Rack::Handler::Mock
4
+ extend Test::Unit::Assertions
5
+
6
+ def self.run(app, options={})
7
+ assert(app < Sinatra::Base)
8
+ assert_equal 9001, options[:Port]
9
+ assert_equal 'foo.local', options[:Host]
10
+ yield new
11
+ end
12
+
13
+ def stop
14
+ end
15
+ end
16
+
17
+ describe 'Sinatra::Base.run!' do
18
+ before do
19
+ mock_app {
20
+ set :server, 'mock'
21
+ set :host, 'foo.local'
22
+ set :port, 9001
23
+ }
24
+ $stdout = File.open('/dev/null', 'wb')
25
+ end
26
+
27
+ after { $stdout = STDOUT }
28
+
29
+ it "locates the appropriate Rack handler and calls ::run" do
30
+ @app.run!
31
+ end
32
+
33
+ it "sets options on the app before running" do
34
+ @app.run! :sessions => true
35
+ assert @app.sessions?
36
+ end
37
+
38
+ it "falls back on the next server handler when not found" do
39
+ @app.run! :server => %w[foo bar mock]
40
+ end
41
+ end
data/test/static_test.rb CHANGED
@@ -1,17 +1,15 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  describe 'Static' do
4
- F = ::File
5
-
6
4
  before do
7
5
  mock_app {
8
6
  set :static, true
9
- set :public, F.dirname(__FILE__)
7
+ set :public, File.dirname(__FILE__)
10
8
  }
11
9
  end
12
10
 
13
11
  it 'serves GET requests for files in the public directory' do
14
- get "/#{F.basename(__FILE__)}"
12
+ get "/#{File.basename(__FILE__)}"
15
13
  assert ok?
16
14
  assert_equal File.read(__FILE__), body
17
15
  assert_equal File.size(__FILE__).to_s, response['Content-Length']
@@ -19,7 +17,7 @@ describe 'Static' do
19
17
  end
20
18
 
21
19
  it 'produces a body that can be iterated over multiple times' do
22
- env = Rack::MockRequest.env_for("/#{F.basename(__FILE__)}")
20
+ env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
23
21
  status, headers, body = @app.call(env)
24
22
  buf1, buf2 = [], []
25
23
  body.each { |part| buf1 << part }
@@ -29,7 +27,7 @@ describe 'Static' do
29
27
  end
30
28
 
31
29
  it 'serves HEAD requests for files in the public directory' do
32
- head "/#{F.basename(__FILE__)}"
30
+ head "/#{File.basename(__FILE__)}"
33
31
  assert ok?
34
32
  assert_equal '', body
35
33
  assert_equal File.size(__FILE__).to_s, response['Content-Length']
@@ -37,8 +35,8 @@ describe 'Static' do
37
35
  end
38
36
 
39
37
  it 'serves files in preference to custom routes' do
40
- @app.get("/#{F.basename(__FILE__)}") { 'Hello World' }
41
- get "/#{F.basename(__FILE__)}"
38
+ @app.get("/#{File.basename(__FILE__)}") { 'Hello World' }
39
+ get "/#{File.basename(__FILE__)}"
42
40
  assert ok?
43
41
  assert body != 'Hello World'
44
42
  end
@@ -50,13 +48,13 @@ describe 'Static' do
50
48
 
51
49
  it 'passes to the next handler when the static option is disabled' do
52
50
  @app.set :static, false
53
- get "/#{F.basename(__FILE__)}"
51
+ get "/#{File.basename(__FILE__)}"
54
52
  assert not_found?
55
53
  end
56
54
 
57
55
  it 'passes to the next handler when the public option is nil' do
58
56
  @app.set :public, nil
59
- get "/#{F.basename(__FILE__)}"
57
+ get "/#{File.basename(__FILE__)}"
60
58
  assert not_found?
61
59
  end
62
60
 
@@ -64,19 +62,4 @@ describe 'Static' do
64
62
  get "/foobarbaz.txt"
65
63
  assert not_found?
66
64
  end
67
-
68
- it 'serves files when .. path traverses within public directory' do
69
- get "/data/../#{File.basename(__FILE__)}"
70
- assert ok?
71
- assert_equal File.read(__FILE__), body
72
- end
73
-
74
- it '404s when .. path traverses outside of public directory' do
75
- mock_app {
76
- set :static, true
77
- set :public, File.dirname(__FILE__) + '/data'
78
- }
79
- get "/../#{File.basename(__FILE__)}"
80
- assert not_found?
81
- end
82
65
  end
data/test/test_test.rb ADDED
@@ -0,0 +1,144 @@
1
+ require 'yaml'
2
+ require File.dirname(__FILE__) + '/helper'
3
+
4
+ describe 'Sinatra::Test' do
5
+ def request
6
+ YAML.load(body)
7
+ end
8
+
9
+ def request_body
10
+ request['test.body']
11
+ end
12
+
13
+ def request_params
14
+ YAML.load(request['test.params'])
15
+ end
16
+
17
+ before do
18
+ mock_app {
19
+ %w[get head post put delete].each { |verb|
20
+ send(verb, '/') do
21
+ redirect '/redirected' if params[:redirect]
22
+ env.update('test.body' => request.body.read)
23
+ env.update('test.params' => params.to_yaml)
24
+ env.to_yaml
25
+ end
26
+ }
27
+
28
+ get '/redirected' do
29
+ "you've been redirected"
30
+ end
31
+ }
32
+ end
33
+
34
+ it 'allows GET/HEAD/POST/PUT/DELETE' do
35
+ get '/'
36
+ assert_equal('GET', request['REQUEST_METHOD'])
37
+
38
+ post '/'
39
+ assert_equal('POST', request['REQUEST_METHOD'])
40
+
41
+ put '/'
42
+ assert_equal('PUT', request['REQUEST_METHOD'])
43
+
44
+ delete '/'
45
+ assert_equal('DELETE', request['REQUEST_METHOD'])
46
+
47
+ head '/'
48
+ assert_equal('596', response.headers['Content-Length'])
49
+ assert_equal('', response.body)
50
+ end
51
+
52
+ it 'allows to specify a body' do
53
+ post '/', '42'
54
+ assert_equal '42', request_body
55
+ end
56
+
57
+ it 'allows to specify params' do
58
+ get '/', :foo => 'bar'
59
+ assert_equal 'bar', request_params['foo']
60
+ end
61
+
62
+ it 'supports nested params' do
63
+ get '/', :foo => { :x => 'y', :chunky => 'bacon' }
64
+ assert_equal "y", request_params['foo']['x']
65
+ assert_equal "bacon", request_params['foo']['chunky']
66
+ end
67
+
68
+ it 'provides easy access to response status and body' do
69
+ get '/'
70
+ assert_equal 200, status
71
+ assert body =~ /^---/
72
+ end
73
+
74
+ it 'delegates methods to @response' do
75
+ get '/'
76
+ assert ok?
77
+ end
78
+
79
+ it 'follows redirect' do
80
+ get '/', :redirect => true
81
+ follow!
82
+ assert_equal "you've been redirected", body
83
+ end
84
+
85
+ it 'provides sugar for common HTTP headers' do
86
+ get '/', :env => { :accept => 'text/plain' }
87
+ assert_equal 'text/plain', request['HTTP_ACCEPT']
88
+
89
+ get '/', :env => { :agent => 'TATFT' }
90
+ assert_equal 'TATFT', request['HTTP_USER_AGENT']
91
+
92
+ get '/', :env => { :host => '1.2.3.4' }
93
+ assert_equal '1.2.3.4', request['HTTP_HOST']
94
+
95
+ get '/', :env => { :session => 'foo' }
96
+ assert_equal 'foo', request['rack.session']
97
+
98
+ get '/', :env => { :cookies => 'foo' }
99
+ assert_equal 'foo', request['HTTP_COOKIE']
100
+
101
+ get '/', :env => { :content_type => 'text/plain' }
102
+ assert_equal 'text/plain', request['CONTENT_TYPE']
103
+ end
104
+
105
+ it 'allow to test session easily' do
106
+ app = mock_app(Sinatra::Default) {
107
+ get '/' do
108
+ session['foo'] = 'bar'
109
+ 200
110
+ end
111
+
112
+ post '/' do
113
+ assert_equal 'bar', session['foo']
114
+ session['foo'] || "blah"
115
+ end
116
+ }
117
+
118
+ browser = Sinatra::TestHarness.new(app)
119
+ browser.get '/'
120
+ browser.post '/', {}, :session => { 'foo' => 'bar' }
121
+ assert_equal 'bar', browser.response.body
122
+ end
123
+
124
+ it 'yields the request object to the block before invoking the application' do
125
+ called = false
126
+ get '/' do |req|
127
+ called = true
128
+ assert req.kind_of?(Rack::MockRequest)
129
+ end
130
+ assert called
131
+ end
132
+
133
+ it 'sets the environment to :test on include' do
134
+ Sinatra::Default.set(:environment, :production)
135
+ Class.new { include Sinatra::Test }
136
+ assert_equal :test, Sinatra::Default.environment
137
+ end
138
+
139
+ def test_TestHarness
140
+ session = Sinatra::TestHarness.new(@app)
141
+ response = session.get('/')
142
+ assert_equal 200, response.status
143
+ end
144
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.5
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Mizerany
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-09 00:00:00 -07:00
12
+ date: 2009-03-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -21,6 +21,9 @@ dependencies:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.9.1
24
+ - - <
25
+ - !ruby/object:Gem::Version
26
+ version: "1.0"
24
27
  version:
25
28
  description: Classy web-development dressed in a DSL
26
29
  email: sinatrarb@googlegroups.com
@@ -88,6 +91,7 @@ files:
88
91
  - test/builder_test.rb
89
92
  - test/data/reload_app_file.rb
90
93
  - test/erb_test.rb
94
+ - test/extensions_test.rb
91
95
  - test/filter_test.rb
92
96
  - test/haml_test.rb
93
97
  - test/helper.rb
@@ -97,12 +101,15 @@ files:
97
101
  - test/options_test.rb
98
102
  - test/reload_test.rb
99
103
  - test/request_test.rb
104
+ - test/response_test.rb
100
105
  - test/result_test.rb
101
106
  - test/routing_test.rb
102
107
  - test/sass_test.rb
108
+ - test/server_test.rb
103
109
  - test/sinatra_test.rb
104
110
  - test/static_test.rb
105
111
  - test/templates_test.rb
112
+ - test/test_test.rb
106
113
  - test/views/hello.builder
107
114
  - test/views/hello.erb
108
115
  - test/views/hello.haml
@@ -147,6 +154,7 @@ test_files:
147
154
  - test/base_test.rb
148
155
  - test/builder_test.rb
149
156
  - test/erb_test.rb
157
+ - test/extensions_test.rb
150
158
  - test/filter_test.rb
151
159
  - test/haml_test.rb
152
160
  - test/helpers_test.rb
@@ -155,9 +163,12 @@ test_files:
155
163
  - test/options_test.rb
156
164
  - test/reload_test.rb
157
165
  - test/request_test.rb
166
+ - test/response_test.rb
158
167
  - test/result_test.rb
159
168
  - test/routing_test.rb
160
169
  - test/sass_test.rb
170
+ - test/server_test.rb
161
171
  - test/sinatra_test.rb
162
172
  - test/static_test.rb
163
173
  - test/templates_test.rb
174
+ - test/test_test.rb