Sha256: b2d9fd0d42715a8b7216e1f612df4ac1370df065c62a78f8453bc90a9b18a44f

Contents?: true

Size: 1021 Bytes

Versions: 3

Compression:

Stored size: 1021 Bytes

Contents

module Enigma
  module Spec
    class RackTestClient
      def initialize(app)
        @app = app
      end

      attr_reader :app

      def session
        Rack::Test::Session.new(Rack::MockSession.new(app))
      end

      def execute(opts)
        method = opts[:method]
        path = opts[:path]
        body = opts[:payload] || ""
        headers = opts[:headers].inject({}) {|_, (k, v)| _.merge(convert_header(k) => v) }

        rack_response = session.send(method, path, body, headers)

        OpenStruct.new :code => rack_response.status.to_i, :body => rack_response.body

      end

      [:get, :post, :put, :delete].each do |method|
        define_method(method) do |path, options|
          execute(
            :method => method,
            :path => path,
            :payload => options[:body],
            :headers => options[:headers]
          )
        end
      end

      private

      def convert_header(header)
        "HTTP_" + header.to_s.upcase.gsub("-", "_")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
enigma-0.0.5 spec/rack_test_client.rb
enigma-0.0.4 spec/rack_test_client.rb
enigma-0.0.3 spec/rack_test_client.rb