Sha256: ffedbb5b28507599c4838eaa89bed79b4909a6ad56e9fd041fefca50e4d725f4

Contents?: true

Size: 1002 Bytes

Versions: 3

Compression:

Stored size: 1002 Bytes

Contents

module Innate
  module Mock
    HTTP_METHODS = %w[ CONNECT DELETE GET HEAD OPTIONS POST PUT TRACE ]

    HTTP_METHODS.each do |method|
      (class << self; self; end).
        send(:define_method, method.downcase){|*args|
        mock(method, *args)
      }
    end

    def self.mock(method, *args)
      mock_request.request(method, *args)
    end

    def self.mock_request
      Rack::MockRequest.new(Innate)
    end

    def self.session
      yield Session.new
    end

    class Session
      attr_accessor :cookie

      def initialize
        @cookie = nil
      end

      HTTP_METHODS.each do |method|
        define_method(method.downcase){|*args|
          extract_cookie(method, *args)
        }
      end

      def extract_cookie(method, path, hash = {})
        hash['HTTP_COOKIE'] ||= @cookie if @cookie
        response = Mock::mock(method, path, hash)

        if cookie = response['Set-Cookie']
          @cookie = cookie
        end

        response
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
manveru-innate-2009.02.06 lib/innate/mock.rb
manveru-innate-2009.02.21 lib/innate/mock.rb
manveru-innate-2009.02.25 lib/innate/mock.rb