Skip to content
Browse files

rewrite sinatra-test-helpers

  • Loading branch information...
1 parent 22ada43 commit 202a8b61b6043623a058c92b370eaa928ad83c8f @rkh rkh committed Mar 24, 2011
Showing with 79 additions and 5 deletions.
  1. +79 −0 lib/sinatra/test_helpers.rb
  2. +0 −5 spec/spec_helper.rb
View
79 lib/sinatra/test_helpers.rb
@@ -0,0 +1,79 @@
+require 'sinatra/base'
+require 'rack/test'
+require 'rack'
+require 'forwardable'
+
+module Sinatra
+ Base.set :environment, :test
+
+ module TestHelpers
+ class Session < Rack::Test::Session
+ def global_env
+ @global_env ||= {}
+ end
+
+ private
+
+ def default_env
+ super.merge global_env
+ end
+ end
+
+ include Rack::Test::Methods
+ extend Forwardable
+
+ def_delegators :last_response, :body, :headers, :status, :errors
+ def_delegators :app, :configure, :set, :enable, :disable, :use, :helpers, :register
+ def_delegators :current_session, :env_for
+
+ def mock_app(base = Sinatra::Base, &block)
+ @app = Sinatra.new(base, &block)
+ end
+
+ def app=(base)
+ @app = base
+ end
+
+ alias set_app app=
+
+ def app
+ @app ||= Class.new Sinatra::Base
+ Rack::Lint.new @app
+ end
+
+ unless method_defined? :options
+ def options(uri, params = {}, env = {}, &block)
+ env = env_for(uri, env.merge(:method => "OPTIONS", :params => params))
+ current_session.send(:process_request, uri, env, &block)
+ end
+ end
+
+ unless method_defined? :patch
+ def patch(uri, params = {}, env = {}, &block)
+ env = env_for(uri, env.merge(:method => "PATCH", :params => params))
+ current_session.send(:process_request, uri, env, &block)
+ end
+ end
+
+ def last_request?
+ last_request
+ true
+ rescue Rack::Test::Error
+ false
+ end
+
+ def session
+ return {} unless last_request?
+ raise Rack::Test:Error, "session not enabled for app" unless last_env["rack.session"] or app.session?
+ last_request.session
+ end
+
+ def last_env
+ last_request.env
+ end
+
+ def build_rack_test_session(name) # :nodoc:
+ Session.new rack_mock_session(name)
+ end
+ end
+end
View
5 spec/spec_helper.rb
@@ -1,8 +1,3 @@
-require 'forwardable'
-
-module TestHelpers
-end
-
require 'sinatra/contrib'
RSpec.configure do |config|

0 comments on commit 202a8b6

Please sign in to comment.
Something went wrong with that request. Please try again.