Sha256: 19606a8d8f417e5044bd8dcf9ad45a275f4296881b3645f6041cf1dcbd826e4d

Contents?: true

Size: 1.94 KB

Versions: 24

Compression:

Stored size: 1.94 KB

Contents

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
    attr_accessor :settings

    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_delegators :rack_mock_session, :cookie_jar

    def mock_app(base = Sinatra::Base, &block)
      inner = nil
      @app  = Sinatra.new(base) do
        inner = self
        class_eval(&block)
      end
      @settings = inner
      app
    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

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/sinatra-contrib-1.4.7/lib/sinatra/test_helpers.rb
sinatra-contrib-2.0.0.beta2 lib/sinatra/test_helpers.rb
sinatra-contrib-2.0.0.beta1 lib/sinatra/test_helpers.rb
sinatra-contrib-1.4.7 lib/sinatra/test_helpers.rb
sinatra-contrib-1.4.6 lib/sinatra/test_helpers.rb
sinatra-contrib-1.4.4 lib/sinatra/test_helpers.rb
sinatra-json-0.1.0 spec/test_helpers.rb
scout_realtime-1.0.5 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-1.0.4 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-1.0.3 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-1.0.3.pre lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-1.0.2 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-1.0.1 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-1.0.0 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-0.5.5 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-0.5.5.pre lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-0.5.4 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-0.5.3 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-0.5.2 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb
scout_realtime-0.5.1 lib/vendor/sinatra-contrib-1.4.1/lib/sinatra/test_helpers.rb