sinatra 1.2.9 → 1.3.0.a

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.

@@ -16,7 +16,7 @@ module Sinatra
16
16
  op.on('-x') { set :lock, true }
17
17
  op.on('-e env') { |val| set :environment, val.to_sym }
18
18
  op.on('-s server') { |val| set :server, val }
19
- op.on('-p port') { |val| set :port, Integer(val) }
19
+ op.on('-p port') { |val| set :port, val.to_i }
20
20
  op.on('-o addr') { |val| set :bind, val }
21
21
  }.parse!(ARGV.dup)
22
22
  end
@@ -0,0 +1,44 @@
1
+ # Rack is rather slow on releases at the moment. Moreover, we cannot upgrade to
2
+ # a new Rack version right away, as this would make us incompatible with Rails.
3
+ # We therefore apply fixes that have not yet made it into a Rack release here.
4
+ #
5
+ # The code in here is extracted from the Rack project.
6
+ #
7
+ # Copyright (C) 2007, 2008, 2009, 2010 Christian Neukirchen <purl.org/net/chneukirchen>
8
+ #
9
+ # Rack is freely distributable under the terms of an MIT-style license.
10
+ # See http://www.opensource.org/licenses/mit-license.php.
11
+ require 'rack'
12
+
13
+ if Rack.release <= "1.2"
14
+ require 'rack/request'
15
+ require 'rack/logger'
16
+ require 'rack/methodoverride'
17
+
18
+ module Rack
19
+ class Request
20
+ def ssl?
21
+ @env['HTTPS'] == 'on' or
22
+ @env['HTTP_X_FORWARDED_PROTO'] == 'https' or
23
+ @env['rack.url_scheme'] == 'https'
24
+ end
25
+ end
26
+
27
+ class Logger
28
+ # In Rack 1.2 and earlier, Rack::Logger called env['rack.errors'].close,
29
+ # which is forbidden in the SPEC and made it impossible to use Rack::Lint.
30
+ # Also, it might close your log file after the first request, potentially
31
+ # crashing your web server.
32
+ def call(env)
33
+ logger = ::Logger.new(env['rack.errors'])
34
+ logger.level = @level
35
+ env['rack.logger'] = logger
36
+ @app.call(env)
37
+ end
38
+ end
39
+
40
+ class MethodOverride
41
+ HTTP_METHODS << "PATCH"
42
+ end
43
+ end
44
+ end
@@ -174,7 +174,7 @@ TEMPLATE = <<-HTML # :nodoc:
174
174
  <body>
175
175
  <div id="wrap">
176
176
  <div id="header">
177
- <img src="<%= env['SCRIPT_NAME'] %>/__sinatra__/500.png" alt="application error" height="161" width="313" />
177
+ <img src="/__sinatra__/500.png" alt="application error" height="161" width="313" />
178
178
  <div id="summary">
179
179
  <h1><strong><%=h exception.class %></strong> at <strong><%=h path %>
180
180
  </strong></h1>
@@ -252,7 +252,7 @@ TEMPLATE = <<-HTML # :nodoc:
252
252
 
253
253
  <div id="get">
254
254
  <h3 id="get-info">GET</h3>
255
- <% if req.GET and not req.GET.empty? %>
255
+ <% unless req.GET.empty? %>
256
256
  <table class="req">
257
257
  <tr>
258
258
  <th>Variable</th>
@@ -273,7 +273,7 @@ TEMPLATE = <<-HTML # :nodoc:
273
273
 
274
274
  <div id="post">
275
275
  <h3 id="post-info">POST</h3>
276
- <% if req.POST and not req.POST.empty? %>
276
+ <% unless req.POST.empty? %>
277
277
  <table class="req">
278
278
  <tr>
279
279
  <th>Variable</th>
@@ -3,7 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'sinatra'
6
- s.version = '1.2.9'
6
+ s.version = '1.3.0.a'
7
+ s.date = '2011-03-22'
7
8
 
8
9
  s.description = "Classy web-development dressed in a DSL"
9
10
  s.summary = "Classy web-development dressed in a DSL"
@@ -33,6 +34,7 @@ Gem::Specification.new do |s|
33
34
  lib/sinatra/images/404.png
34
35
  lib/sinatra/images/500.png
35
36
  lib/sinatra/main.rb
37
+ lib/sinatra/rack.rb
36
38
  lib/sinatra/showexceptions.rb
37
39
  sinatra.gemspec
38
40
  test/base_test.rb
@@ -57,7 +59,6 @@ Gem::Specification.new do |s|
57
59
  test/middleware_test.rb
58
60
  test/nokogiri_test.rb
59
61
  test/public/favicon.ico
60
- test/rack_test.rb
61
62
  test/radius_test.rb
62
63
  test/rdoc_test.rb
63
64
  test/request_test.rb
@@ -122,11 +123,11 @@ Gem::Specification.new do |s|
122
123
  s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/}
123
124
 
124
125
  s.extra_rdoc_files = %w[README.rdoc README.de.rdoc README.jp.rdoc README.fr.rdoc README.es.rdoc README.hu.rdoc README.zh.rdoc LICENSE]
125
- s.add_dependency 'rack', '~> 1.1', '< 1.5'
126
+ s.add_dependency 'rack', '~> 1.2'
126
127
  s.add_dependency 'tilt', '>= 1.2.2', '< 2.0'
127
- s.add_dependency 'backports'
128
128
  s.add_development_dependency 'shotgun', '~> 0.6'
129
129
 
130
+ s.has_rdoc = true
130
131
  s.homepage = "http://sinatra.rubyforge.org"
131
132
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra", "--main", "README.rdoc"]
132
133
  s.require_paths = %w[lib]
@@ -2,13 +2,6 @@ require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  begin
4
4
  require 'coffee-script'
5
- require 'execjs'
6
-
7
- begin
8
- ExecJS.compile '1'
9
- rescue Exception
10
- raise LoadError, 'unable to execute JavaScript'
11
- end
12
5
 
13
6
  class CoffeeTest < Test::Unit::TestCase
14
7
  def coffee_app(options = {}, &block)
@@ -23,7 +16,7 @@ class CoffeeTest < Test::Unit::TestCase
23
16
  it 'renders inline Coffee strings' do
24
17
  coffee_app { coffee "alert 'Aye!'\n" }
25
18
  assert ok?
26
- assert body.include?("alert('Aye!');")
19
+ assert_equal "(function() {\n alert('Aye!');\n}).call(this);\n", body
27
20
  end
28
21
 
29
22
  it 'defaults content type to javascript' do
@@ -52,13 +45,13 @@ class CoffeeTest < Test::Unit::TestCase
52
45
  it 'renders .coffee files in views path' do
53
46
  coffee_app { coffee :hello }
54
47
  assert ok?
55
- assert_include body, "alert(\"Aye!\");"
48
+ assert_equal "(function() {\n alert(\"Aye!\");\n}).call(this);\n", body
56
49
  end
57
50
 
58
51
  it 'ignores the layout option' do
59
52
  coffee_app { coffee :hello, :layout => :layout2 }
60
53
  assert ok?
61
- assert_include body, "alert(\"Aye!\");"
54
+ assert_equal "(function() {\n alert(\"Aye!\");\n}).call(this);\n", body
62
55
  end
63
56
 
64
57
  it "raises error if template not found" do
@@ -69,21 +62,24 @@ class CoffeeTest < Test::Unit::TestCase
69
62
  end
70
63
 
71
64
  it "passes coffee options to the coffee engine" do
72
- coffee_app { coffee "alert 'Aye!'\n", :no_wrap => true }
65
+ coffee_app {
66
+ coffee "alert 'Aye!'\n",
67
+ :no_wrap => true
68
+ }
73
69
  assert ok?
74
- assert_equal "alert('Aye!');", body.gsub(/\s/, '')
70
+ assert_equal "alert('Aye!');", body
75
71
  end
76
72
 
77
73
  it "passes default coffee options to the coffee engine" do
78
- mock_app do
74
+ mock_app {
79
75
  set :coffee, :no_wrap => true # default coffee style is :nested
80
76
  get '/' do
81
77
  coffee "alert 'Aye!'\n"
82
78
  end
83
- end
79
+ }
84
80
  get '/'
85
81
  assert ok?
86
- assert_equal "alert('Aye!');", body.gsub(/\s/, '')
82
+ assert_equal "alert('Aye!');", body
87
83
  end
88
84
  end
89
85
 
@@ -1,5 +1,3 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
1
  class DelegatorTest < Test::Unit::TestCase
4
2
  class Mirror
5
3
  attr_reader :last_call
@@ -48,7 +46,7 @@ class DelegatorTest < Test::Unit::TestCase
48
46
 
49
47
  def delegate(&block)
50
48
  assert Sinatra::Delegator.target != Sinatra::Application
51
- Object.new.extend(Sinatra::Delegator).instance_eval(&block) if block
49
+ Object.new.extend(Sinatra::Delegator).instance_eval(&block)
52
50
  Sinatra::Delegator.target
53
51
  end
54
52
 
@@ -60,7 +58,7 @@ class DelegatorTest < Test::Unit::TestCase
60
58
  assert_equal Sinatra::Delegator.target, Sinatra::Application
61
59
  end
62
60
 
63
- %w[get put post delete options].each do |verb|
61
+ %w[get put post delete options patch].each do |verb|
64
62
  it "delegates #{verb} correctly" do
65
63
  delegation_app do
66
64
  send verb, '/hello' do
@@ -90,46 +88,8 @@ class DelegatorTest < Test::Unit::TestCase
90
88
  assert_equal '', response.body
91
89
  end
92
90
 
93
- it "registers extensions with the delegation target" do
94
- app, mixin = mirror, Module.new
95
- Sinatra.register mixin
96
- assert_equal app.last_call, ["register", mixin.to_s ]
97
- end
98
-
99
- it "registers helpers with the delegation target" do
100
- app, mixin = mirror, Module.new
101
- Sinatra.helpers mixin
102
- assert_equal app.last_call, ["helpers", mixin.to_s ]
103
- end
104
-
105
- it "should work with method_missing proxies for options" do
106
- mixin = Module.new do
107
- def respond_to?(method, *)
108
- method.to_sym == :options or super
109
- end
110
-
111
- def method_missing(method, *args, &block)
112
- return super unless method.to_sym == :options
113
- {:some => :option}
114
- end
115
- end
116
-
117
- value = nil
118
- mirror do
119
- extend mixin
120
- value = options
121
- end
122
-
123
- assert_equal({:some => :option}, value)
124
- end
125
-
126
- it "delegates crazy method names" do
127
- Sinatra::Delegator.delegate "foo:bar:"
128
- method = mirror { send "foo:bar:" }.last_call.first
129
- assert_equal "foo:bar:", method
130
- end
131
-
132
91
  delegates 'get'
92
+ delegates 'patch'
133
93
  delegates 'put'
134
94
  delegates 'post'
135
95
  delegates 'delete'
@@ -65,10 +65,6 @@ class Test::Unit::TestCase
65
65
  assert_equal value.lstrip.gsub(/\s*\n\s*/, ""), body.lstrip.gsub(/\s*\n\s*/, "")
66
66
  end
67
67
 
68
- def assert_include(str, substr)
69
- assert str.include?(substr), "expected #{str.inspect} to include #{substr.inspect}"
70
- end
71
-
72
68
  # Delegate other missing methods to response.
73
69
  def method_missing(name, *args, &block)
74
70
  if response && response.respond_to?(name)
@@ -312,6 +312,19 @@ class HelpersTest < Test::Unit::TestCase
312
312
  get '/'
313
313
  assert_body 'ok'
314
314
  end
315
+
316
+ it 'accepts an options hash' do
317
+ mock_app do
318
+ set :sessions, :foo => :bar
319
+ get '/' do
320
+ assert_equal env['rack.session.options'][:foo], :bar
321
+ 'ok'
322
+ end
323
+ end
324
+
325
+ get '/'
326
+ assert_body 'ok'
327
+ end
315
328
  end
316
329
 
317
330
  describe 'mime_type' do
@@ -419,7 +432,6 @@ class HelpersTest < Test::Unit::TestCase
419
432
  assert_equal content_type(:xml), 'application/xml;charset=utf-8'
420
433
  assert_equal content_type(:xhtml), 'application/xhtml+xml;charset=utf-8'
421
434
  assert_equal content_type(:js), 'application/javascript;charset=utf-8'
422
- assert_equal content_type(:json), 'application/json;charset=utf-8'
423
435
  assert_equal content_type(:bar), 'application/bar'
424
436
  assert_equal content_type(:png), 'image/png'
425
437
  assert_equal content_type(:baz), 'application/baz;charset=utf-8'
@@ -430,28 +442,6 @@ class HelpersTest < Test::Unit::TestCase
430
442
  get '/'
431
443
  assert tests_ran
432
444
  end
433
-
434
- it 'handles already present params' do
435
- mock_app do
436
- get '/' do
437
- content_type 'foo/bar;level=1', :charset => 'utf-8'
438
- 'ok'
439
- end
440
- end
441
- get '/'
442
- assert_equal 'foo/bar;level=1, charset=utf-8', response['Content-Type']
443
- end
444
-
445
- it 'does not add charset if present' do
446
- mock_app do
447
- get '/' do
448
- content_type 'text/plain;charset=utf-16'
449
- 'ok'
450
- end
451
- end
452
- get '/'
453
- assert_equal 'text/plain;charset=utf-16', response['Content-Type']
454
- end
455
445
  end
456
446
 
457
447
  describe 'send_file' do
@@ -626,14 +616,6 @@ class HelpersTest < Test::Unit::TestCase
626
616
  get '/baz' do
627
617
  expires Time.at(0)
628
618
  end
629
-
630
- get '/blah' do
631
- obj = Object.new
632
- def obj.method_missing(*a, &b) 60.send(*a, &b) end
633
- def obj.is_a?(thing) 60.is_a?(thing) end
634
- expires obj, :public, :no_cache
635
- 'Hello World'
636
- end
637
619
  end
638
620
  end
639
621
 
@@ -656,11 +638,6 @@ class HelpersTest < Test::Unit::TestCase
656
638
  get '/baz'
657
639
  assert_equal 'Thu, 01 Jan 1970 00:00:00 GMT', response['Expires']
658
640
  end
659
-
660
- it 'accepts values pretending to be a Numeric (like ActiveSupport::Duration)' do
661
- get '/blah'
662
- assert_equal ['public', 'no-cache', 'max-age=60'], response['Cache-Control'].split(', ')
663
- end
664
641
  end
665
642
 
666
643
  describe 'last_modified' do
@@ -739,7 +716,7 @@ class HelpersTest < Test::Unit::TestCase
739
716
  get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2010 23:43:52 GMT' }
740
717
  assert_equal 200, status
741
718
  assert_equal 'foo', body
742
- get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2012 23:43:52 GMT' }
719
+ get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2100 23:43:52 GMT' }
743
720
  assert_equal 304, status
744
721
  assert_equal '', body
745
722
  end
@@ -884,6 +861,36 @@ class HelpersTest < Test::Unit::TestCase
884
861
  end
885
862
  end
886
863
 
864
+ describe 'logger' do
865
+ it 'logging works when logging is enabled' do
866
+ mock_app do
867
+ enable :logging
868
+ get '/' do
869
+ logger.info "Program started"
870
+ logger.warn "Nothing to do!"
871
+ end
872
+ end
873
+ io = StringIO.new
874
+ get '/', {}, 'rack.errors' => io
875
+ assert io.string.include?("INFO -- : Program started")
876
+ assert io.string.include?("WARN -- : Nothing to do")
877
+ end
878
+
879
+ it 'logging works when logging is disable, but no output is produced' do
880
+ mock_app do
881
+ disable :logging
882
+ get '/' do
883
+ logger.info "Program started"
884
+ logger.warn "Nothing to do!"
885
+ end
886
+ end
887
+ io = StringIO.new
888
+ get '/', {}, 'rack.errors' => io
889
+ assert !io.string.include?("INFO -- : Program started")
890
+ assert !io.string.include?("WARN -- : Nothing to do")
891
+ end
892
+ end
893
+
887
894
  module ::HelperOne; def one; '1'; end; end
888
895
  module ::HelperTwo; def two; '2'; end; end
889
896
 
@@ -15,7 +15,7 @@ class NokogiriTest < Test::Unit::TestCase
15
15
  it 'renders inline Nokogiri strings' do
16
16
  nokogiri_app { nokogiri 'xml' }
17
17
  assert ok?
18
- assert_body %(<?xml version="1.0"?>\n)
18
+ assert_body %{<?xml version="1.0"?>\n}
19
19
  end
20
20
 
21
21
  it 'renders inline blocks' do
@@ -26,7 +26,7 @@ class NokogiriTest < Test::Unit::TestCase
26
26
  end
27
27
  end
28
28
  assert ok?
29
- assert_body %(<?xml version="1.0"?>\n<couple>Frank &amp; Mary</couple>\n)
29
+ assert_body "<?xml version=\"1.0\"?>\n<couple>Frank &amp; Mary</couple>\n"
30
30
  end
31
31
 
32
32
  it 'renders .nokogiri files in views path' do
@@ -35,7 +35,7 @@ class NokogiriTest < Test::Unit::TestCase
35
35
  nokogiri :hello
36
36
  end
37
37
  assert ok?
38
- assert_body "<?xml version=\"1.0\"?>\n<exclaim>You're my boy, Blue!</exclaim>\n"
38
+ assert_body %(<?xml version="1.0"?>\n<exclaim>You're my boy, Blue!</exclaim>\n)
39
39
  end
40
40
 
41
41
  it "renders with inline layouts" do
@@ -46,16 +46,17 @@ class NokogiriTest < Test::Unit::TestCase
46
46
  end
47
47
  get '/'
48
48
  assert ok?
49
- assert_body %(<?xml version="1.0"?>\n<layout>\n <em>Hello World</em>\n</layout>\n)
49
+ assert_body "<?xml version=\"1.0\"?>\n<layout>\n <em>Hello World</em>\n</layout>\n"
50
50
  end
51
51
 
52
52
  it "renders with file layouts" do
53
53
  next if Tilt::VERSION <= "1.1"
54
54
  nokogiri_app do
55
+ @name = "Blue"
55
56
  nokogiri %(xml.em 'Hello World'), :layout => :layout2
56
57
  end
57
58
  assert ok?
58
- assert_body %(<?xml version="1.0"?>\n<layout>\n <em>Hello World</em>\n</layout>\n)
59
+ assert_body "<?xml version=\"1.0\"?>\n<layout>\n <em>Hello World</em>\n</layout>\n"
59
60
  end
60
61
 
61
62
  it "raises error if template not found" do
@@ -54,12 +54,12 @@ class ResultTest < Test::Unit::TestCase
54
54
  it "sets status, headers, and body when result is a Rack response tuple" do
55
55
  mock_app {
56
56
  get '/' do
57
- [203, {'Content-Type' => 'foo/bar'}, 'Hello World']
57
+ [205, {'Content-Type' => 'foo/bar'}, 'Hello World']
58
58
  end
59
59
  }
60
60
 
61
61
  get '/'
62
- assert_equal 203, status
62
+ assert_equal 205, status
63
63
  assert_equal 'foo/bar', response['Content-Type']
64
64
  assert_equal 'Hello World', body
65
65
  end
@@ -88,11 +88,11 @@ class ResultTest < Test::Unit::TestCase
88
88
 
89
89
  it "sets status when result is a Fixnum status code" do
90
90
  mock_app {
91
- get('/') { 204 }
91
+ get('/') { 205 }
92
92
  }
93
93
 
94
94
  get '/'
95
- assert_equal 204, status
95
+ assert_equal 205, status
96
96
  assert_equal '', body
97
97
  end
98
98
  end