Sha256: bfdd738bf2e7f9a9b964378f0f4fc8c55717ce9b20ed4599b46a180c9652d98d

Contents?: true

Size: 936 Bytes

Versions: 13

Compression:

Stored size: 936 Bytes

Contents

module Rack::App::Test::Utils

  extend self

  def format_properties(properties)
    raise('use hash format such as params: {"key" => "value"} or headers with the same concept') unless properties.is_a?(Hash)
    properties[:params] ||= {}
    properties[:headers]||= {}

    properties
  end

  def env_by(properties)

    properties = format_properties(properties)
    env = properties[:headers].reduce({}) { |m, (k, v)| m.merge("HTTP_#{k.to_s.gsub('-', '_').upcase}" => v.to_s) }
    payload = properties.delete(:payload)
    env["rack.input"]= ::Rack::Lint::InputWrapper.new(string_io_for(payload))
    env[::Rack::QUERY_STRING]= Rack::App::Utils.encode_www_form(properties[:params].to_a)
    env.merge!(properties[:env] || {})

    env
  end

  def string_io_for(payload)
    case payload

      when IO
        payload

      when String
        StringIO.new(payload.to_s)

      else
        StringIO.new('')

    end
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rack-app-5.10.1 lib/rack/app/test/utils.rb
rack-app-5.10.0 lib/rack/app/test/utils.rb
rack-app-5.9.0 lib/rack/app/test/utils.rb
rack-app-5.8.0 lib/rack/app/test/utils.rb
rack-app-5.7.0 lib/rack/app/test/utils.rb
rack-app-5.7.0.rc lib/rack/app/test/utils.rb
rack-app-5.6.0 lib/rack/app/test/utils.rb
rack-app-5.5.1 lib/rack/app/test/utils.rb
rack-app-5.4.0 lib/rack/app/test/utils.rb
rack-app-5.3.2 lib/rack/app/test/utils.rb
rack-app-5.3.1 lib/rack/app/test/utils.rb
rack-app-5.3.0 lib/rack/app/test/utils.rb
rack-app-5.2.0 lib/rack/app/test/utils.rb