Sha256: fb80990e842e94bc41527c1fffdeaf7cd512c5f14a59f14cba580f7b83c5bbb2

Contents?: true

Size: 1.12 KB

Versions: 23

Compression:

Stored size: 1.12 KB

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(uri, properties)

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

    env
  end

  def query_string_by(uri, params={})
     uri_based = URI.parse(uri).query.to_s
     prop_based = Rack::Utils.build_nested_query(params)

     parts = [uri_based, prop_based]
     parts.delete("")
     parts.join("&")
  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

23 entries across 23 versions & 2 rubygems

Version Path
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/test/utils.rb
rack-app-7.7.0 lib/rack/app/test/utils.rb
rack-app-7.6.5 lib/rack/app/test/utils.rb
rack-app-7.6.4 lib/rack/app/test/utils.rb
rack-app-7.6.3 lib/rack/app/test/utils.rb
rack-app-7.6.2 lib/rack/app/test/utils.rb
rack-app-7.6.1 lib/rack/app/test/utils.rb
rack-app-7.6.0 lib/rack/app/test/utils.rb
rack-app-7.5.2 lib/rack/app/test/utils.rb
rack-app-7.5.1 lib/rack/app/test/utils.rb
rack-app-7.5.0 lib/rack/app/test/utils.rb
rack-app-7.4.0 lib/rack/app/test/utils.rb
rack-app-7.3.2 lib/rack/app/test/utils.rb
rack-app-7.3.1 lib/rack/app/test/utils.rb
rack-app-7.3.0 lib/rack/app/test/utils.rb
rack-app-7.2.2 lib/rack/app/test/utils.rb
rack-app-7.2.1 lib/rack/app/test/utils.rb
rack-app-7.2.0 lib/rack/app/test/utils.rb
rack-app-7.1.0 lib/rack/app/test/utils.rb
rack-app-7.0.0 lib/rack/app/test/utils.rb