Module: Merb::Test::RequestHelper

Included in:
ControllerHelper, RouteHelper
Defined in:
lib/merb-core/test/helpers/request_helper.rb

Instance Method Summary collapse

Instance Method Details

#describe_input(input) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/merb-core/test/helpers/request_helper.rb', line 11

def describe_input(input)
  if input.respond_to?(:controller_name)
    "#{input.controller_name}##{input.action_name}"
  elsif input.respond_to?(:original_env)
    describe_request(input)
  else
    input
  end
end

#describe_request(rack) ⇒ Object



7
8
9
# File 'lib/merb-core/test/helpers/request_helper.rb', line 7

def describe_request(rack)
  "a #{rack.original_env["REQUEST_METHOD"] || "GET"} to '#{rack.url}'"
end

#request(uri, env = {}) ⇒ Object Also known as: requesting, response_for



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/merb-core/test/helpers/request_helper.rb', line 25

def request(uri, env = {})
  uri = url(uri) if uri.is_a?(Symbol)    

  if (env[:method] == "POST" || env["REQUEST_METHOD"] == "POST")
    params = env.delete(:body_params) if env.key?(:body_params)
    params = env.delete(:params) if env.key?(:params) && !env.key?(:input)
    
    unless env.key?(:input)
      env[:input] = Merb::Request.params_to_query_string(params)
      env["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
    end
  end

  if env[:params]
    uri << "&#{Merb::Request.params_to_query_string(env.delete(:body_params))}"
  end

  if @__cookie__
    env["HTTP_COOKIE"] = @__cookie__
  end

  app = Merb::Rack::Application.new
  rack = app.call(::Rack::MockRequest.env_for(uri, env))

  rack = Struct.new(:status, :headers, :body, :url, :original_env).
    new(rack[0], rack[1], rack[2], uri, env)

  @__cookie__ = rack.headers["Set-Cookie"] && rack.headers["Set-Cookie"].join

  rack
end

#status_code(input) ⇒ Object



21
22
23
# File 'lib/merb-core/test/helpers/request_helper.rb', line 21

def status_code(input)
  input.respond_to?(:status) ? input.status : input
end