Sha256: fab2dd9dc66c731e32a6e5d0adaad0984d34d0c30aab334cd7c3ee1d27844a06

Contents?: true

Size: 1.86 KB

Versions: 17

Compression:

Stored size: 1.86 KB

Contents

require "webrat"
require "merb-core"
require "webrat/merb_multipart_support"

module Webrat
  class MerbSession < Session #:nodoc:
    include Merb::Test::MakeRequest

    # Include Webrat's own version of multipart_post/put because the officially
    # supported methods in Merb don't perform the request correctly.
    include MerbMultipartSupport

    attr_accessor :response

    def get(url, data, headers = nil)
      do_request(url, data, headers, "GET")
    end

    def post(url, data, headers = nil)
      do_request(url, data, headers, "POST")
    end

    def put(url, data, headers = nil)
      do_request(url, data, headers, "PUT")
    end

    def delete(url, data, headers = nil)
      do_request(url, data, headers, "DELETE")
    end

    def response_body
      @response.body.to_s
    end

    def response_code
      @response.status
    end

    def do_request(url, data, headers, method)
      if method == "POST" && has_file?(data)
        @response = multipart_post(url, data, :headers => headers)

      elsif method == "PUT" && has_file?(data)
        @response = multipart_put(url, data, :headers => headers)

      else
        @response = request(url,
          :params => (data && data.any?) ? data : nil,
          :headers => headers,
          :method => method)
      end
    end

    protected

      # Recursively search the data for a file attachment.
      def has_file?(data)
        data.each do |key, value|
          if value.is_a?(Hash)
            return has_file?(value)
          else
            return true if value.is_a?(File)
          end
        end
        return false
      end

  end
end

module Merb #:nodoc:
  module Test #:nodoc:
    module RequestHelper #:nodoc:
      def request(uri, env = {})
        @_webrat_session ||= Webrat::MerbSession.new
        @_webrat_session.response = @_webrat_session.request(uri, env)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 6 rubygems

Version Path
dbrady-webrat-0.4.4.2 lib/webrat/merb_session.rb
emipair-webrat-0.0.1 lib/webrat/merb_session.rb
garnierjm-webrat-0.4.5 lib/webrat/merb_session.rb
honkster-webrat-0.4.4.2 lib/webrat/merb_session.rb
webrat-0.4.5 lib/webrat/merb_session.rb
metry-2.0.4 vendor/webrat/lib/webrat/merb_session.rb
metry-2.0.5 vendor/webrat/lib/webrat/merb_session.rb
metry-2.1.0 vendor/webrat/lib/webrat/merb_session.rb
metry-2.1.1 vendor/webrat/lib/webrat/merb_session.rb
metry-2.0.0 vendor/webrat/lib/webrat/merb_session.rb
metry-2.0.1 vendor/webrat/lib/webrat/merb_session.rb
metry-2.0.2 vendor/webrat/lib/webrat/merb_session.rb
metry-2.0.3 vendor/webrat/lib/webrat/merb_session.rb
metry-1.0.0 vendor/webrat/lib/webrat/merb_session.rb
metry-1.0.1 vendor/webrat/lib/webrat/merb_session.rb
metry-1.1.0 vendor/webrat/lib/webrat/merb_session.rb
metry-1.2.0 vendor/webrat/lib/webrat/merb_session.rb