Sha256: 3b489592005ecf94987a9ec63b6d22e9506fd8dc4f0145cdc00ae462c4836aba

Contents?: true

Size: 1.9 KB

Versions: 30

Compression:

Stored size: 1.9 KB

Contents

module Sinatra

  module Test

    module Methods
      include Rack::Utils

      ENV_KEY_NAMES = {
        :accept => "HTTP_ACCEPT",
        :agent => "HTTP_USER_AGENT",
        :host => "HTTP_HOST",
        :session => "HTTP_COOKIE",
        :cookies => "HTTP_COOKIE",
        :content_type => "CONTENT_TYPE"
      }

      def session(data, key = 'rack.session')
        data = data.from_params if data.respond_to?(:from_params)
        "#{escape(key)}=#{[Marshal.dump(data)].pack("m*")}"
      end

      def normalize_rack_environment(env)
        env.inject({}) do |hash,(k,v)|
          hash[ENV_KEY_NAMES[k] || k] = v
          hash
        end
      end

      def hash_to_param_string(hash)
        hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
      end

      %w(get head post put delete).each do |verb|
        http_method = verb.upcase
        define_method("#{verb}_it") do |path, *args|
          @request = Rack::MockRequest.new(Sinatra.build_application)
          opts, input =
            case args.size
            when 2 # input, env
              input, env = args
              [env, input]
            when 1 # params
              if (data = args.first).kind_of?(Hash)
                env = (data.delete(:env) || {})
                [env, hash_to_param_string(data)]
              else
                [{}, data]
              end
            when 0
              [{}, '']
            else
              raise ArgumentError, "zero, one, or two arguments expected"
            end
          opts = normalize_rack_environment(opts)
          opts[:input] ||= input
          @response = @request.request(http_method, path, opts)
        end
      end

      def follow!
        get_it(@response.location)
      end

      def method_missing(name, *args)
        if @response.respond_to?(name)
          @response.send(name, *args)
        else
          super
        end
      end

    end

  end

end

Version data entries

30 entries across 30 versions & 7 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/sinatra/lib/sinatra/test/methods.rb
Syd-sinatra-0.3.2 lib/sinatra/test/methods.rb
bmizerany-sinatra-0.3.2 lib/sinatra/test/methods.rb
masover-castronaut-0.4.4.4 vendor/sinatra/lib/sinatra/test/methods.rb
masover-castronaut-0.4.4.5 vendor/sinatra/lib/sinatra/test/methods.rb
masover-castronaut-0.5.0.1 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.2.0 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.2.5 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.3.0 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.3.5 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.3.6 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.4.1 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.4.2 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.4.3 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.4.4 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.4.5 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.4.6 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.5.0 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.5.1 vendor/sinatra/lib/sinatra/test/methods.rb
relevance-castronaut-0.5.2 vendor/sinatra/lib/sinatra/test/methods.rb