Module: Rack::App::Test::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/rack/app/test/utils.rb

Instance Method Summary collapse

Instance Method Details

#env_by(uri, properties) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/app/test/utils.rb', line 13

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

#format_properties(properties) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/rack/app/test/utils.rb', line 5

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

#query_string_by(uri, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rack/app/test/utils.rb', line 25

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

#string_io_for(payload) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/app/test/utils.rb', line 34

def string_io_for(payload)
  case payload

    when IO
      payload

    when String
      StringIO.new(payload.to_s)

    else
      StringIO.new('')

  end
end