Class: Rack::MockRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mock.rb

Overview

Rack::MockRequest helps testing your Rack application without actually using HTTP.

After performing a request on a URL with get/post/put/delete, it returns a MockResponse with useful helper methods for effective testing.

You can pass a hash with additional configuration to the get/post/put/delete.

:input

A String or IO-like to be used as rack.input.

:fatal

Raise a FatalWarning if the app writes to rack.errors.

:lint

If true, wrap the application in a Rack::Lint.

Defined Under Namespace

Classes: FatalWarner, FatalWarning

Constant Summary collapse

DEFAULT_ENV =
{
  "rack.version" => [0,1],
  "rack.input" => StringIO.new,
  "rack.errors" => StringIO.new,
  "rack.multithread" => true,
  "rack.multiprocess" => true,
  "rack.run_once" => false,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MockRequest

Returns a new instance of MockRequest.



51
52
53
# File 'lib/rack/mock.rb', line 51

def initialize(app)
  @app = app
end

Class Method Details

.env_for(uri = "", opts = {}) ⇒ Object

Return the Rack environment used for a request to uri.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rack/mock.rb', line 74

def self.env_for(uri="", opts={})
  uri = URI(uri)
  env = DEFAULT_ENV.dup

  env["REQUEST_METHOD"] = opts[:method] || "GET"
  env["SERVER_NAME"] = uri.host || "example.org"
  env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
  env["QUERY_STRING"] = uri.query.to_s
  env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path
  env["rack.url_scheme"] = uri.scheme || "http"

  env["SCRIPT_NAME"] = opts[:script_name] || ""

  if opts[:fatal]
    env["rack.errors"] = FatalWarner.new
  else
    env["rack.errors"] = StringIO.new
  end

  opts[:input] ||= ""
  if String === opts[:input]
    env["rack.input"] = StringIO.new(opts[:input])
  else
    env["rack.input"] = opts[:input]
  end

  opts.each { |field, value|
    env[field] = value  if String === field
  }

  env
end

Instance Method Details

#delete(uri, opts = {}) ⇒ Object



58
# File 'lib/rack/mock.rb', line 58

def delete(uri, opts={}) request("DELETE", uri, opts) end

#get(uri, opts = {}) ⇒ Object



55
# File 'lib/rack/mock.rb', line 55

def get(uri, opts={})    request("GET", uri, opts)    end

#post(uri, opts = {}) ⇒ Object



56
# File 'lib/rack/mock.rb', line 56

def post(uri, opts={})   request("POST", uri, opts)   end

#put(uri, opts = {}) ⇒ Object



57
# File 'lib/rack/mock.rb', line 57

def put(uri, opts={})    request("PUT", uri, opts)    end

#request(method = "GET", uri = "", opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rack/mock.rb', line 60

def request(method="GET", uri="", opts={})
  env = self.class.env_for(uri, opts.merge(:method => method))

  if opts[:lint]
    app = Rack::Lint.new(@app)
  else
    app = @app
  end

  errors = env["rack.errors"]
  MockResponse.new(*(app.call(env) + [errors]))
end