bmizerany-sinatra 0.9.0 → 0.9.0.2

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.
data/lib/sinatra/test.rb CHANGED
@@ -1,110 +1,114 @@
1
1
  require 'sinatra/base'
2
- require 'test/unit'
3
2
 
4
- module Sinatra::Test
5
- include Rack::Utils
6
-
7
- attr_reader :app, :request, :response
8
-
9
- def mock_app(base=Sinatra::Base, &block)
10
- @app = Sinatra.new(base, &block)
11
- end
12
-
13
- def request(verb, path, *args)
14
- fail "@app not set - cannot make request" if @app.nil?
15
- @request = Rack::MockRequest.new(@app)
16
- opts, input =
17
- case args.size
18
- when 2 # input, env
19
- input, env = args
20
- if input.kind_of?(Hash) # params, env
21
- [env, param_string(input)]
22
- else
23
- [env, input]
24
- end
25
- when 1 # params
26
- if (data = args.first).kind_of?(Hash)
27
- env = (data.delete(:env) || {})
28
- [env, param_string(data)]
3
+ module Sinatra
4
+
5
+ module Test
6
+ include Rack::Utils
7
+
8
+ attr_reader :app, :request, :response
9
+
10
+ def test_request(verb, path, *args)
11
+ @app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
12
+ fail "@app not set - cannot make request" if @app.nil?
13
+ @request = Rack::MockRequest.new(@app)
14
+ opts, input =
15
+ case args.size
16
+ when 2 # input, env
17
+ input, env = args
18
+ if input.kind_of?(Hash) # params, env
19
+ [env, param_string(input)]
20
+ else
21
+ [env, input]
22
+ end
23
+ when 1 # params
24
+ if (data = args.first).kind_of?(Hash)
25
+ env = (data.delete(:env) || {})
26
+ [env, param_string(data)]
27
+ else
28
+ [{}, data]
29
+ end
30
+ when 0
31
+ [{}, '']
29
32
  else
30
- [{}, data]
33
+ raise ArgumentError, "zero, one, or two arguments expected"
31
34
  end
32
- when 0
33
- [{}, '']
34
- else
35
- raise ArgumentError, "zero, one, or two arguments expected"
36
- end
37
- opts = rack_opts(opts)
38
- opts[:input] ||= input
39
- yield @request if block_given?
40
- @response = @request.request(verb, path, opts)
41
- end
35
+ opts = rack_opts(opts)
36
+ opts[:input] ||= input
37
+ yield @request if block_given?
38
+ @response = @request.request(verb, path, opts)
39
+ end
42
40
 
43
- def get(path, *args, &b) ; request('GET', path, *args, &b) ; end
44
- def head(path, *args, &b) ; request('HEAD', path, *args, &b) ; end
45
- def post(path, *args, &b) ; request('POST', path, *args, &b) ; end
46
- def put(path, *args, &b) ; request('PUT', path, *args, &b) ; end
47
- def delete(path, *args, &b) ; request('DELETE', path, *args, &b) ; end
41
+ def get(path, *args, &b) ; test_request('GET', path, *args, &b) ; end
42
+ def head(path, *args, &b) ; test_request('HEAD', path, *args, &b) ; end
43
+ def post(path, *args, &b) ; test_request('POST', path, *args, &b) ; end
44
+ def put(path, *args, &b) ; test_request('PUT', path, *args, &b) ; end
45
+ def delete(path, *args, &b) ; test_request('DELETE', path, *args, &b) ; end
48
46
 
49
- def follow!
50
- request 'GET', @response.location
51
- end
47
+ def follow!
48
+ test_request 'GET', @response.location
49
+ end
52
50
 
53
- def should
54
- @response.should
55
- end
51
+ def body ; @response.body ; end
52
+ def status ; @response.status ; end
56
53
 
57
- def body
58
- @response.body
59
- end
54
+ # Delegate other missing methods to @response.
55
+ def method_missing(name, *args, &block)
56
+ if @response && @response.respond_to?(name)
57
+ @response.send(name, *args, &block)
58
+ else
59
+ super
60
+ end
61
+ end
60
62
 
61
- def status
62
- @response.status
63
- end
63
+ # Also check @response since we delegate there.
64
+ def respond_to?(symbol, include_private=false)
65
+ super || (@response && @response.respond_to?(symbol, include_private))
66
+ end
64
67
 
65
- RACK_OPT_NAMES = {
66
- :accept => "HTTP_ACCEPT",
67
- :agent => "HTTP_USER_AGENT",
68
- :host => "HTTP_HOST",
69
- :session => "HTTP_COOKIE",
70
- :cookies => "HTTP_COOKIE",
71
- :content_type => "CONTENT_TYPE"
72
- }
73
-
74
- def rack_opts(opts)
75
- opts.inject({}) do |hash,(key,val)|
76
- key = RACK_OPT_NAMES[key] || key
77
- hash[key] = val
78
- hash
68
+ RACK_OPT_NAMES = {
69
+ :accept => "HTTP_ACCEPT",
70
+ :agent => "HTTP_USER_AGENT",
71
+ :host => "HTTP_HOST",
72
+ :session => "HTTP_COOKIE",
73
+ :cookies => "HTTP_COOKIE",
74
+ :content_type => "CONTENT_TYPE"
75
+ }
76
+
77
+ def rack_opts(opts)
78
+ opts.inject({}) do |hash,(key,val)|
79
+ key = RACK_OPT_NAMES[key] || key
80
+ hash[key] = val
81
+ hash
82
+ end
79
83
  end
80
- end
81
84
 
82
- def env_for(opts={})
83
- opts = rack_opts(opts)
84
- Rack::MockRequest.env_for(opts)
85
- end
85
+ def env_for(opts={})
86
+ opts = rack_opts(opts)
87
+ Rack::MockRequest.env_for(opts)
88
+ end
86
89
 
87
- def param_string(hash)
88
- hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
89
- end
90
+ def param_string(hash)
91
+ hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
92
+ end
90
93
 
91
- if defined? Sinatra::Compat
92
- # Deprecated. Use: "get" instead of "get_it".
93
- %w(get head post put delete).each do |verb|
94
- alias_method "#{verb}_it", verb
95
- remove_method verb
94
+ if defined? Sinatra::Compat
95
+ # Deprecated. Use: "get" instead of "get_it".
96
+ %w(get head post put delete).each do |verb|
97
+ eval <<-RUBY, binding, __FILE__, __LINE__
98
+ def #{verb}_it(*args, &block)
99
+ sinatra_warn "The #{verb}_it method is deprecated; use #{verb} instead."
100
+ test_request('#{verb.upcase}', *args, &block)
101
+ end
102
+ RUBY
103
+ end
96
104
  end
105
+ end
97
106
 
98
- include Sinatra::Delegator
107
+ class TestHarness
108
+ include Test
99
109
 
100
- # Deprecated. Tests no longer delegate missing methods to the
101
- # mock response. Use: @response
102
- def method_missing(name, *args, &block)
103
- if @response && @response.respond_to?(name)
104
- @response.send(name, *args, &block)
105
- else
106
- super
107
- end
110
+ def initialize(app=nil)
111
+ @app = app || Sinatra::Application
108
112
  end
109
113
  end
110
114
  end
data/lib/sinatra.rb CHANGED
@@ -1,3 +1,8 @@
1
+ libdir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
3
+
1
4
  require 'sinatra/base'
2
5
  require 'sinatra/main'
3
6
  require 'sinatra/compat'
7
+
8
+ use_in_file_templates!
data/sinatra.gemspec CHANGED
@@ -3,16 +3,18 @@ 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 = '0.9.0'
7
- s.date = '2009-01-06'
6
+ s.version = '0.9.0.2'
7
+ s.date = '2009-01-18'
8
8
 
9
9
  s.description = "Classy web-development dressed in a DSL"
10
10
  s.summary = "Classy web-development dressed in a DSL"
11
11
 
12
12
  s.authors = ["Blake Mizerany"]
13
+ s.email = "sinatrarb@googlegroups.com"
13
14
 
14
15
  # = MANIFEST =
15
16
  s.files = %w[
17
+ AUTHORS
16
18
  CHANGES
17
19
  LICENSE
18
20
  README.rdoc
@@ -58,6 +60,7 @@ Gem::Specification.new do |s|
58
60
  lib/sinatra/images/500.png
59
61
  lib/sinatra/main.rb
60
62
  lib/sinatra/test.rb
63
+ lib/sinatra/test/bacon.rb
61
64
  lib/sinatra/test/rspec.rb
62
65
  lib/sinatra/test/spec.rb
63
66
  lib/sinatra/test/unit.rb
@@ -68,6 +71,7 @@ Gem::Specification.new do |s|
68
71
  test/erb_test.rb
69
72
  test/filter_test.rb
70
73
  test/haml_test.rb
74
+ test/helper.rb
71
75
  test/helpers_test.rb
72
76
  test/mapped_error_test.rb
73
77
  test/middleware_test.rb
@@ -95,7 +99,7 @@ Gem::Specification.new do |s|
95
99
  s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/}
96
100
 
97
101
  s.extra_rdoc_files = %w[README.rdoc LICENSE]
98
- s.add_dependency 'rack', '>= 0.9.0'
102
+ s.add_dependency 'rack', '>= 0.9.1'
99
103
 
100
104
  s.has_rdoc = true
101
105
  s.homepage = "http://sinatra.rubyforge.org"
data/test/base_test.rb CHANGED
@@ -1,12 +1,8 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe 'Sinatra::Base' do
6
- include Sinatra::Test
7
-
8
4
  it 'includes Rack::Utils' do
9
- Sinatra::Base.should.include Rack::Utils
5
+ assert Sinatra::Base.included_modules.include?(Rack::Utils)
10
6
  end
11
7
 
12
8
  it 'can be used as a Rack application' do
@@ -15,12 +11,12 @@ describe 'Sinatra::Base' do
15
11
  'Hello World'
16
12
  end
17
13
  }
18
- @app.should.respond_to :call
14
+ assert @app.respond_to?(:call)
19
15
 
20
16
  request = Rack::MockRequest.new(@app)
21
17
  response = request.get('/')
22
- response.should.be.ok
23
- response.body.should.equal 'Hello World'
18
+ assert response.ok?
19
+ assert_equal 'Hello World', response.body
24
20
  end
25
21
 
26
22
  it 'can be used as Rack middleware' do
@@ -35,15 +31,38 @@ describe 'Sinatra::Base' do
35
31
  end
36
32
  }
37
33
  middleware = mock_middleware.new(app)
38
- middleware.app.should.be app
34
+ assert_same app, middleware.app
39
35
 
40
36
  request = Rack::MockRequest.new(middleware)
41
37
  response = request.get('/')
42
- response.should.be.ok
43
- response.body.should.equal 'Hello World'
38
+ assert response.ok?
39
+ assert_equal 'Hello World', response.body
44
40
 
45
41
  response = request.get('/goodbye')
46
- response.should.be.ok
47
- response.body.should.equal 'Goodbye World'
42
+ assert response.ok?
43
+ assert_equal 'Goodbye World', response.body
44
+ end
45
+
46
+ it 'can take multiple definitions of a route' do
47
+ app = mock_app {
48
+ user_agent(/Foo/)
49
+ get '/foo' do
50
+ 'foo'
51
+ end
52
+
53
+ get '/foo' do
54
+ 'not foo'
55
+ end
56
+ }
57
+
58
+ request = Rack::MockRequest.new(app)
59
+ response = request.get('/foo', 'HTTP_USER_AGENT' => 'Foo')
60
+ assert response.ok?
61
+ assert_equal 'foo', response.body
62
+
63
+ request = Rack::MockRequest.new(app)
64
+ response = request.get('/foo')
65
+ assert response.ok?
66
+ assert_equal 'not foo', response.body
48
67
  end
49
68
  end
data/test/builder_test.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe "Builder Templates" do
6
- include Sinatra::Test
7
-
8
4
  def builder_app(&block)
9
5
  mock_app {
10
6
  set :views, File.dirname(__FILE__) + '/views'
@@ -15,8 +11,8 @@ describe "Builder Templates" do
15
11
 
16
12
  it 'renders inline Builder strings' do
17
13
  builder_app { builder 'xml.instruct!' }
18
- should.be.ok
19
- body.should.equal %{<?xml version="1.0" encoding="UTF-8"?>\n}
14
+ assert ok?
15
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?>\n}, body
20
16
  end
21
17
 
22
18
  it 'renders inline blocks' do
@@ -26,8 +22,8 @@ describe "Builder Templates" do
26
22
  xml.couple @name
27
23
  end
28
24
  }
29
- should.be.ok
30
- body.should.equal "<couple>Frank &amp; Mary</couple>\n"
25
+ assert ok?
26
+ assert_equal "<couple>Frank &amp; Mary</couple>\n", body
31
27
  end
32
28
 
33
29
  it 'renders .builder files in views path' do
@@ -35,8 +31,8 @@ describe "Builder Templates" do
35
31
  @name = "Blue"
36
32
  builder :hello
37
33
  }
38
- should.be.ok
39
- body.should.equal %(<exclaim>You're my boy, Blue!</exclaim>\n)
34
+ assert ok?
35
+ assert_equal %(<exclaim>You're my boy, Blue!</exclaim>\n), body
40
36
  end
41
37
 
42
38
  it "renders with inline layouts" do
@@ -47,22 +43,22 @@ describe "Builder Templates" do
47
43
  get('/') { builder %(xml.em 'Hello World') }
48
44
  }
49
45
  get '/'
50
- should.be.ok
51
- body.should.equal "<layout>\n<em>Hello World</em>\n</layout>\n"
46
+ assert ok?
47
+ assert_equal "<layout>\n<em>Hello World</em>\n</layout>\n", body
52
48
  end
53
49
 
54
50
  it "renders with file layouts" do
55
51
  builder_app {
56
52
  builder %(xml.em 'Hello World'), :layout => :layout2
57
53
  }
58
- should.be.ok
59
- body.should.equal "<layout>\n<em>Hello World</em>\n</layout>\n"
54
+ assert ok?
55
+ assert_equal "<layout>\n<em>Hello World</em>\n</layout>\n", body
60
56
  end
61
57
 
62
58
  it "raises error if template not found" do
63
59
  mock_app {
64
60
  get('/') { builder :no_such_template }
65
61
  }
66
- lambda { get('/') }.should.raise(Errno::ENOENT)
62
+ assert_raise(Errno::ENOENT) { get('/') }
67
63
  end
68
64
  end
data/test/erb_test.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe "ERB Templates" do
6
- include Sinatra::Test
7
-
8
4
  def erb_app(&block)
9
5
  mock_app {
10
6
  set :views, File.dirname(__FILE__) + '/views'
@@ -15,14 +11,14 @@ describe "ERB Templates" do
15
11
 
16
12
  it 'renders inline ERB strings' do
17
13
  erb_app { erb '<%= 1 + 1 %>' }
18
- should.be.ok
19
- body.should.equal '2'
14
+ assert ok?
15
+ assert_equal '2', body
20
16
  end
21
17
 
22
18
  it 'renders .erb files in views path' do
23
19
  erb_app { erb :hello }
24
- should.be.ok
25
- body.should.equal "Hello World\n"
20
+ assert ok?
21
+ assert_equal "Hello World\n", body
26
22
  end
27
23
 
28
24
  it 'takes a :locals option' do
@@ -30,8 +26,8 @@ describe "ERB Templates" do
30
26
  locals = {:foo => 'Bar'}
31
27
  erb '<%= foo %>', :locals => locals
32
28
  }
33
- should.be.ok
34
- body.should.equal 'Bar'
29
+ assert ok?
30
+ assert_equal 'Bar', body
35
31
  end
36
32
 
37
33
  it "renders with inline layouts" do
@@ -40,16 +36,15 @@ describe "ERB Templates" do
40
36
  get('/') { erb 'Sparta' }
41
37
  }
42
38
  get '/'
43
- should.be.ok
44
- body.should.equal 'THIS. IS. SPARTA!'
39
+ assert ok?
40
+ assert_equal 'THIS. IS. SPARTA!', body
45
41
  end
46
42
 
47
43
  it "renders with file layouts" do
48
44
  erb_app {
49
45
  erb 'Hello World', :layout => :layout2
50
46
  }
51
- should.be.ok
52
- body.should.equal "ERB Layout!\nHello World\n"
47
+ assert ok?
48
+ assert_equal "ERB Layout!\nHello World\n", body
53
49
  end
54
-
55
50
  end
data/test/filter_test.rb CHANGED
@@ -1,28 +1,24 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe "Filters" do
6
- include Sinatra::Test
7
-
8
4
  it "executes filters in the order defined" do
9
5
  count = 0
10
6
  mock_app do
11
7
  get('/') { 'Hello World' }
12
8
  before {
13
- count.should.be 0
9
+ assert_equal 0, count
14
10
  count = 1
15
11
  }
16
12
  before {
17
- count.should.be 1
13
+ assert_equal 1, count
18
14
  count = 2
19
15
  }
20
16
  end
21
17
 
22
18
  get '/'
23
- should.be.ok
24
- count.should.be 2
25
- body.should.equal 'Hello World'
19
+ assert ok?
20
+ assert_equal 2, count
21
+ assert_equal 'Hello World', body
26
22
  end
27
23
 
28
24
  it "allows filters to modify the request" do
@@ -33,7 +29,47 @@ describe "Filters" do
33
29
  }
34
30
 
35
31
  get '/foo'
36
- should.be.ok
37
- body.should.be == 'bar'
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 in filters" 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 '/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
38
74
  end
39
75
  end
data/test/haml_test.rb CHANGED
@@ -1,10 +1,6 @@
1
- require 'test/spec'
2
- require 'sinatra/base'
3
- require 'sinatra/test'
1
+ require File.dirname(__FILE__) + '/helper'
4
2
 
5
3
  describe "HAML Templates" do
6
- include Sinatra::Test
7
-
8
4
  def haml_app(&block)
9
5
  mock_app {
10
6
  set :views, File.dirname(__FILE__) + '/views'
@@ -15,14 +11,14 @@ describe "HAML Templates" do
15
11
 
16
12
  it 'renders inline HAML strings' do
17
13
  haml_app { haml '%h1 Hiya' }
18
- should.be.ok
19
- body.should.equal "<h1>Hiya</h1>\n"
14
+ assert ok?
15
+ assert_equal "<h1>Hiya</h1>\n", body
20
16
  end
21
17
 
22
18
  it 'renders .haml files in views path' do
23
19
  haml_app { haml :hello }
24
- should.be.ok
25
- body.should.equal "<h1>Hello From Haml</h1>\n"
20
+ assert ok?
21
+ assert_equal "<h1>Hello From Haml</h1>\n", body
26
22
  end
27
23
 
28
24
  it "renders with inline layouts" do
@@ -31,31 +27,31 @@ describe "HAML Templates" do
31
27
  get('/') { haml '%em Sparta' }
32
28
  }
33
29
  get '/'
34
- should.be.ok
35
- body.should.equal "<h1>THIS. IS. <EM>SPARTA</EM></h1>\n"
30
+ assert ok?
31
+ assert_equal "<h1>THIS. IS. <EM>SPARTA</EM></h1>\n", body
36
32
  end
37
33
 
38
34
  it "renders with file layouts" do
39
35
  haml_app {
40
36
  haml 'Hello World', :layout => :layout2
41
37
  }
42
- should.be.ok
43
- body.should.equal "<h1>HAML Layout!</h1>\n<p>Hello World</p>\n"
38
+ assert ok?
39
+ assert_equal "<h1>HAML Layout!</h1>\n<p>Hello World</p>\n", body
44
40
  end
45
41
 
46
42
  it "raises error if template not found" do
47
43
  mock_app {
48
44
  get('/') { haml :no_such_template }
49
45
  }
50
- lambda { get('/') }.should.raise(Errno::ENOENT)
46
+ assert_raise(Errno::ENOENT) { get('/') }
51
47
  end
52
48
 
53
49
  it "passes HAML options to the Haml engine" do
54
50
  haml_app {
55
51
  haml "!!!\n%h1 Hello World", :options => {:format => :html5}
56
52
  }
57
- should.be.ok
58
- body.should.equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n"
53
+ assert ok?
54
+ assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
59
55
  end
60
56
 
61
57
  it "passes default HAML options to the Haml engine" do
@@ -66,7 +62,7 @@ describe "HAML Templates" do
66
62
  end
67
63
  }
68
64
  get '/'
69
- should.be.ok
70
- body.should.equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n"
65
+ assert ok?
66
+ assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
71
67
  end
72
68
  end
data/test/helper.rb ADDED
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'test/spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'test/spec'
6
+ end
7
+
8
+ $:.unshift File.dirname(File.dirname(__FILE__)) + '/lib'
9
+ require 'sinatra/base'
10
+ require 'sinatra/test'
11
+ require 'sinatra/test/spec'
12
+
13
+ module Sinatra::Test
14
+ # Sets up a Sinatra::Base subclass defined with the block
15
+ # given. Used in setup or individual spec methods to establish
16
+ # the application.
17
+ def mock_app(base=Sinatra::Base, &block)
18
+ @app = Sinatra.new(base, &block)
19
+ end
20
+ end
21
+
22
+ class Sinatra::Base
23
+ # Allow assertions in request context
24
+ include Test::Unit::Assertions
25
+ end