sinatra-test-helper 0.4.1 → 0.5.0

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/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ copyright (c) 2010 Konstantin Haase. All rights reserved.
2
+
3
+ Developed by: Konstantin Haase
4
+ http://github.com/rkh/big_band
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to
8
+ deal with the Software without restriction, including without limitation the
9
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
+ sell copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+ 1. Redistributions of source code must retain the above copyright notice,
13
+ this list of conditions and the following disclaimers.
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimers in the
16
+ documentation and/or other materials provided with the distribution.
17
+ 3. Neither the name of Konstantin Haase, nor the names of other contributors
18
+ may be used to endorse or promote products derived from this Software without
19
+ specific prior written permission.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27
+ WITH THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require 'sinatra/rspec'
2
+ require 'sinatra/rspec/shared/routing'
3
+
4
+ shared_examples_for 'sinatra' do
5
+ it_should_behave_like 'sinatra routing'
6
+ end
@@ -0,0 +1,61 @@
1
+ require 'sinatra/rspec/shared'
2
+
3
+ shared_examples_for 'sinatra routing' do
4
+ %w[get head post put delete].each do |verb|
5
+ describe verb.upcase do
6
+ it "should route matching patterns" do
7
+ app.routes.clear
8
+ define_route(verb, '/foo') { 'bar' }
9
+ result = browse_route(verb, '/foo')
10
+ result.should be_ok
11
+ result.body.should == 'bar' unless verb == 'head'
12
+ end
13
+
14
+ it "404s when no route satisfies the request" do
15
+ app.routes.clear
16
+ define_route(verb, '/foo') { }
17
+ browse_route(verb, '/bar').status.should == 404
18
+ end
19
+
20
+ it "404s and sets X-Cascade header when no route satisfies the request" do
21
+ app.routes.clear
22
+ define_route(verb, '/foo') { }
23
+ result = browse_route(verb, '/bar')
24
+ result.status.should == 404
25
+ result.headers['X-Cascade'].should == 'pass'
26
+ end
27
+
28
+ it "overrides the content-type in error handlers" do
29
+ app.routes.clear
30
+ app.before { content_type 'text/plain' }
31
+ app.error Sinatra::NotFound do
32
+ content_type "text/html"
33
+ "<h1>Not Found</h1>"
34
+ end
35
+ result = browse_route(verb, '/bar')
36
+ result.status.should == 404
37
+ result["Content-Type"].should include('text/html')
38
+ result.body.should == "<h1>Not Found</h1>" unless verb == 'head'
39
+ end
40
+
41
+ it 'takes multiple definitions of a route' do
42
+ app.routes.clear
43
+ app.send(:user_agent, /Foo/)
44
+ define_route(verb, '/foo') { 'foo' }
45
+ define_route(verb, '/foo') { 'not foo' }
46
+ browse_route verb, '/foo', {}, 'HTTP_USER_AGENT' => 'Foo'
47
+ last_response.should be_ok
48
+ last_response.body.should == 'foo' unless verb == 'head'
49
+ last_response.should be_ok
50
+ browse_route verb, '/foo'
51
+ last_response.body.should == 'not foo' unless verb == 'head'
52
+ end
53
+ end
54
+ end
55
+
56
+ it "defines HEAD request handlers with HEAD" do
57
+ app.routes.clear
58
+ define_route(:head, '/hello') { response['X-Hello'] = 'World!' }
59
+ head('/hello')['X-Hello'].should == 'World!'
60
+ end
61
+ end
@@ -2,7 +2,7 @@ require "sinatra/base"
2
2
  require "sinatra/sugar"
3
3
  require "rack/test"
4
4
  require "forwardable"
5
- require "monkey"
5
+ require "monkey-lib"
6
6
 
7
7
  Sinatra::Base.set :environment, :test
8
8
 
@@ -10,10 +10,34 @@ module Sinatra
10
10
  Base.ignore_caller
11
11
  # This encapsulates general test helpers. See Bacon, RSpec, Test::Spec and Test::Unit for examples.
12
12
  module TestHelper
13
+ module AppMixin
14
+ def call!(env)
15
+ env['sinatra.test_helper'].last_sinatra_instance = self if env['sinatra.test_helper']
16
+ super
17
+ end
18
+ end
19
+
20
+ class Session < Rack::Test::Session
21
+ def global_env
22
+ @global_env ||= {}
23
+ end
24
+ private
25
+ def default_env
26
+ super.merge global_env
27
+ end
28
+ end
29
+
13
30
  include ::Rack::Test::Methods
14
31
  extend Forwardable
15
32
 
33
+ def build_rack_test_session(name) # :nodoc:
34
+ Session.new(rack_mock_session(name)).tap do |s|
35
+ s.global_env['sinatra.test_helper'] = self
36
+ end
37
+ end
38
+
16
39
  def_delegators :app, :configure, :set, :enable, :disable, :use, :helpers, :register
40
+ attr_writer :last_sinatra_instance
17
41
 
18
42
  attr_writer :app
19
43
  def app(*options, &block)
@@ -41,7 +65,26 @@ module Sinatra
41
65
  @app.class_eval "def self.inspect; #{inspection.inspect}; end"
42
66
  end
43
67
  end
44
- @app || Sinatra::Application
68
+ @app ||= Sinatra::Application
69
+ @app.extend AppMixin unless @app.is_a? AppMixin
70
+ @app
71
+ end
72
+
73
+ def last_request?
74
+ last_request
75
+ true
76
+ rescue Rack::Test::Error
77
+ false
78
+ end
79
+
80
+ def session
81
+ return {} unless last_request?
82
+ raise Rack::Test:Error, "session not enabled for app" unless last_env["rack.session"] or app.session?
83
+ last_request.session
84
+ end
85
+
86
+ def last_env
87
+ last_request.env
45
88
  end
46
89
 
47
90
  def define_route(verb, *args, &block)
@@ -1,6 +1,8 @@
1
1
  require File.expand_path("../../spec_helper", __FILE__)
2
2
 
3
3
  describe Sinatra::TestHelper do
4
+ it_should_behave_like 'sinatra'
5
+
4
6
  describe :app do
5
7
  before { @app = nil }
6
8
 
@@ -1 +1 @@
1
- require "sinatra/rspec"
1
+ require "sinatra/rspec/shared"
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-test-helper
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 11
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 4
8
- - 1
9
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Konstantin Haase
@@ -14,49 +15,54 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-04-16 00:00:00 +02:00
18
+ date: 2010-07-09 00:00:00 +02:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: monkey-lib
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ~>
26
28
  - !ruby/object:Gem::Version
29
+ hash: 11
27
30
  segments:
28
31
  - 0
29
- - 4
32
+ - 5
30
33
  - 0
31
- version: 0.4.0
34
+ version: 0.5.0
32
35
  type: :runtime
33
36
  version_requirements: *id001
34
37
  - !ruby/object:Gem::Dependency
35
38
  name: sinatra-sugar
36
39
  prerelease: false
37
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
38
42
  requirements:
39
43
  - - ~>
40
44
  - !ruby/object:Gem::Version
45
+ hash: 11
41
46
  segments:
42
47
  - 0
43
- - 4
48
+ - 5
44
49
  - 0
45
- version: 0.4.0
50
+ version: 0.5.0
46
51
  type: :runtime
47
52
  version_requirements: *id002
48
53
  - !ruby/object:Gem::Dependency
49
54
  name: sinatra
50
55
  prerelease: false
51
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
52
58
  requirements:
53
- - - ">="
59
+ - - ~>
54
60
  - !ruby/object:Gem::Version
61
+ hash: 15
55
62
  segments:
63
+ - 1
56
64
  - 0
57
- - 9
58
- - 4
59
- version: 0.9.4
65
+ version: "1.0"
60
66
  type: :runtime
61
67
  version_requirements: *id003
62
68
  description: Test helper for Sinatra (part of BigBand).
@@ -73,6 +79,8 @@ files:
73
79
  - lib/sinatra/minitest.rb
74
80
  - lib/sinatra/mspec.rb
75
81
  - lib/sinatra/protest.rb
82
+ - lib/sinatra/rspec/shared/routing.rb
83
+ - lib/sinatra/rspec/shared.rb
76
84
  - lib/sinatra/rspec.rb
77
85
  - lib/sinatra/test/spec.rb
78
86
  - lib/sinatra/test/unit.rb
@@ -82,6 +90,7 @@ files:
82
90
  - spec/sinatra/test_helper_spec.rb
83
91
  - spec/spec_helper.rb
84
92
  - README.md
93
+ - LICENSE
85
94
  has_rdoc: yard
86
95
  homepage: http://github.com/rkh/sinatra-test-helper
87
96
  licenses: []
@@ -92,23 +101,27 @@ rdoc_options: []
92
101
  require_paths:
93
102
  - lib
94
103
  required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
95
105
  requirements:
96
106
  - - ">="
97
107
  - !ruby/object:Gem::Version
108
+ hash: 3
98
109
  segments:
99
110
  - 0
100
111
  version: "0"
101
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
102
114
  requirements:
103
115
  - - ">="
104
116
  - !ruby/object:Gem::Version
117
+ hash: 3
105
118
  segments:
106
119
  - 0
107
120
  version: "0"
108
121
  requirements: []
109
122
 
110
123
  rubyforge_project:
111
- rubygems_version: 1.3.6
124
+ rubygems_version: 1.3.7
112
125
  signing_key:
113
126
  specification_version: 3
114
127
  summary: Test helper for Sinatra (part of BigBand).