Sha256: d36c7e9c4f4e36153b97fffc07976452766c9ae36b7fb1a14f043c87071454d6

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

require_relative 'simulation'

module Pakyow
  module TestHelp
    class Simulator
      attr_reader :env, :path, :method, :params

      def initialize(name_or_path, method: :get, params: {}, session: {}, cookies: {}, env: {})
        @path   = router.path(name_or_path, params)
        @method = method
        @params = params
        @env    = {
          'REQUEST_METHOD'            => @method.to_s.upcase,
          'REQUEST_PATH'              => @path,
          'PATH_INFO'                 => @path,
          'QUERY_STRING'              => @params.to_a.map { |p| p.join('=') }.join('&'),
          'rack.session'              => session,
          'rack.request.cookie_hash'  => cookies,
          'rack.input'                => StringIO.new,
          'pakyow.params'             => @params,
          'rack.logger'               => Pakyow::TestHelp::ObservableLogger.new
        }.merge(Hash[env.map{ |k,v| [k.to_s, v] }])
      end

      def run(&block)
        app = Pakyow.app.dup
        app.process(env)

        sim = Simulation.new(app)

        return sim unless block_given?
        yield sim
      end

      private

      def router
        Pakyow::Router.instance
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pakyow-test-0.10.2 pakyow-test/lib/test_help/simulator.rb
pakyow-test-0.10.1 pakyow-test/lib/test_help/simulator.rb
pakyow-test-0.10.0 pakyow-test/lib/test_help/simulator.rb