Skip to content

Commit

Permalink
MockRequest can now only create the Rack environment
Browse files Browse the repository at this point in the history
darcs-hash:20070228182155-4fc50-a86eb71770e1748ed7555f419b292bb8150c9161.gz
  • Loading branch information
leahneukirchen committed Feb 28, 2007
1 parent 0a8bb68 commit f428736
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/rack/mock.rb
Expand Up @@ -40,22 +40,35 @@ def put(uri, opts={}) request("PUT", uri, opts) end
def delete(uri, opts={}) request("DELETE", uri, opts) end

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

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

env["REQUEST_METHOD"] = method
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.empty? ? "/" : uri.path
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"] = errors = StringIO.new
env["rack.errors"] = StringIO.new
end

opts[:input] ||= ""
Expand All @@ -65,13 +78,11 @@ def request(method="GET", uri="", opts={})
env["rack.input"] = opts[:input]
end

if opts[:lint]
app = Rack::Lint.new(@app)
else
app = @app
end
opts.each { |field, value|
env[field] = value if String === field
}

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

Expand Down
9 changes: 9 additions & 0 deletions test/spec_rack_mock.rb
Expand Up @@ -23,6 +23,12 @@
res.should.be.kind_of Rack::MockResponse
end

specify "should be able to only return the environment" do
env = Rack::MockRequest.env_for("")
env.should.be.kind_of Hash
env.should.include "rack.version"
end

specify "should provide sensible defaults" do
res = Rack::MockRequest.new(App).request

Expand Down Expand Up @@ -53,6 +59,9 @@
res = Rack::MockRequest.new(App).delete("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.equal "DELETE"

Rack::MockRequest.env_for("/", :method => "OPTIONS")["REQUEST_METHOD"].
should.equal "OPTIONS"
end

specify "should allow posting" do
Expand Down

0 comments on commit f428736

Please sign in to comment.