module Ruter::Test::InstanceMethods

Internal: This module provides the Ruter::Test API methods. If you don't like the stand-alone version, you can integrate this module to your preferred testing environment.

The following example uses Minitest:

class HomeTest < Minitest::Test
  include Ruter::Test::InstanceMethods

  def app
    return Ruter
  end

  def test_home
    get("/")

    assert_equal 200, res.status
  end
end

Public Instance Methods

delete(path, params = {}, env = {}) click to toggle source

Public: Issues a DELETE request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.delete("/users/1")
# File lib/ruter/test.rb, line 161
def delete(path, params = {}, env = {})
  request(path, env.merge(method: "DELETE".freeze, params: params))
end
get(path, params = {}, env = {}) click to toggle source

Public: Issues a GET request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.get("/search", name: "alice")
# File lib/ruter/test.rb, line 113
def get(path, params = {}, env = {})
  request(path, env.merge(method: Rack::GET, params: params))
end
head(path, params = {}, env = {}) click to toggle source

Public: Issues a HEAD request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.head("/users/1")
# File lib/ruter/test.rb, line 173
def head(path, params = {}, env = {})
  request(path, env.merge(method: Rack::HEAD, params: params))
end
options(path, params = {}, env = {}) click to toggle source

Public: Issues a OPTIONS request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.options("/users")
# File lib/ruter/test.rb, line 185
def options(path, params = {}, env = {})
  request(path, env.merge(method: "OPTIONS".freeze, params: params))
end
patch(path, params = {}, env = {}) click to toggle source

Public: Issues a PATCH request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.patch("/users/1", username: "alice")
# File lib/ruter/test.rb, line 149
def patch(path, params = {}, env = {})
  request(path, env.merge(method: "PATCH".freeze, params: params))
end
post(path, params = {}, env = {}) click to toggle source

Public: Issues a POST request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.post("/signup", username: "alice", password: "secret")
# File lib/ruter/test.rb, line 125
def post(path, params = {}, env = {})
  request(path, env.merge(method: "POST".freeze, params: params))
end
put(path, params = {}, env = {}) click to toggle source

Public: Issues a PUT request for the given path with the given params and Rack environment.

Examples

app = Ruter::Test.new
app.put("/users/1", username: "bob", name: "Bob")
# File lib/ruter/test.rb, line 137
def put(path, params = {}, env = {})
  request(path, env.merge(method: "PUT".freeze, params: params))
end
req() click to toggle source

Public: Returns the current request object or nil if no requests have been issued yet.

Examples

app = Ruter::Test.new
app.get("/", {}, { "HTTP_USER_AGENT" => "Ruter::Test" })

app.req.get?
# => true

app.req.env["HTTP_USER_AGENT"]
# => "Ruter::Test"

The returned object is an instance of Rack::Request.

# File lib/ruter/test.rb, line 80
def req
  @__req
end
res() click to toggle source

Public: Returns the current response object or nil if no requests have been issued yet.

Examples

app = Ruter::Test.new
app.get("/", name: "alice")

app.res.status
# => 200

app.res.body
# => "Hello alice!"

The returned object is an instance of Rack::MockResponse

# File lib/ruter/test.rb, line 101
def res
  @__res
end

Private Instance Methods

request(path, opts = {}) click to toggle source
# File lib/ruter/test.rb, line 191
def request(path, opts = {})
  @__req = Rack::Request.new(Rack::MockRequest.env_for(path, opts))
  @__res = Rack::MockResponse.new(*app.call(@__req.env))
end