Skip to content

Commit

Permalink
Wrap test apps in Rack::Lint and fix uncovered errors (fifth pass)
Browse files Browse the repository at this point in the history
Update tests for MockRequest, MockResponse, NullLogger, Recursive and Runtime.
  • Loading branch information
Lars Gierth authored and Lars Gierth committed Mar 31, 2012
1 parent 761bc54 commit 8f39d33
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
15 changes: 8 additions & 7 deletions test/spec_mock.rb
@@ -1,8 +1,9 @@
require 'yaml'
require 'rack/lint'
require 'rack/mock'
require 'stringio'

app = lambda { |env|
app = Rack::Lint.new(lambda { |env|
req = Rack::Request.new(env)

env["mock.postdata"] = env["rack.input"].read
Expand All @@ -11,10 +12,11 @@
env["rack.errors"].flush
end

Rack::Response.new(env.to_yaml,
body = req.head? ? "" : env.to_yaml
Rack::Response.new(body,
req.GET["status"] || 200,
"Content-Type" => "text/yaml").finish
}
})

describe Rack::MockRequest do
should "return a MockResponse" do
Expand Down Expand Up @@ -62,10 +64,9 @@
res = Rack::MockRequest.new(app).delete("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.equal "DELETE"

res = Rack::MockRequest.new(app).head("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.equal "HEAD"

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

Rack::MockRequest.env_for("/", :method => "OPTIONS")["REQUEST_METHOD"].
should.equal "OPTIONS"
Expand Down
13 changes: 11 additions & 2 deletions test/spec_nulllogger.rb
@@ -1,3 +1,6 @@
require 'enumerator'
require 'rack/lint'
require 'rack/mock'
require 'rack/nulllogger'

describe Rack::NullLogger do
Expand All @@ -6,7 +9,13 @@
env['rack.logger'].warn "b00m"
[200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]]
}
logger = Rack::NullLogger.new(app)
lambda{ logger.call({}) }.should.not.raise

logger = Rack::Lint.new(Rack::NullLogger.new(app))

res = logger.call(Rack::MockRequest.env_for)
res[0..1].should.equal [
200, {'Content-Type' => 'text/plain'}
]
Enumerator.new(res[2]).to_a.should.equal ["Hello, World!"]
end
end
21 changes: 12 additions & 9 deletions test/spec_recursive.rb
@@ -1,3 +1,4 @@
require 'rack/lint'
require 'rack/recursive'
require 'rack/mock'

Expand Down Expand Up @@ -28,11 +29,14 @@
@app4 = lambda { |env|
raise Rack::ForwardRequest.new("http://example.org/app1/quux?meh")
}

def recursive(map)
Rack::Lint.new Rack::Recursive.new(Rack::URLMap.new(map))
end

should "allow for subrequests" do
res = Rack::MockRequest.new(Rack::Recursive.new(
Rack::URLMap.new("/app1" => @app1,
"/app2" => @app2))).
res = Rack::MockRequest.new(recursive("/app1" => @app1,
"/app2" => @app2)).
get("/app2")

res.should.be.ok
Expand All @@ -41,9 +45,8 @@

should "raise error on requests not below the app" do
app = Rack::URLMap.new("/app1" => @app1,
"/app" => Rack::Recursive.new(
Rack::URLMap.new("/1" => @app1,
"/2" => @app2)))
"/app" => recursive("/1" => @app1,
"/2" => @app2))

lambda {
Rack::MockRequest.new(app).get("/app/2")
Expand All @@ -52,9 +55,9 @@
end

should "support forwarding" do
app = Rack::Recursive.new(Rack::URLMap.new("/app1" => @app1,
"/app3" => @app3,
"/app4" => @app4))
app = recursive("/app1" => @app1,
"/app3" => @app3,
"/app4" => @app4)

res = Rack::MockRequest.new(app).get("/app3")
res.should.be.ok
Expand Down
20 changes: 15 additions & 5 deletions test/spec_runtime.rb
@@ -1,35 +1,45 @@
require 'rack/lint'
require 'rack/mock'
require 'rack/runtime'

describe Rack::Runtime do
def runtime_app(app, *args)
Rack::Lint.new Rack::Runtime.new(app, *args)
end

def request
Rack::MockRequest.env_for
end

it "sets X-Runtime is none is set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
response = Rack::Runtime.new(app).call({})
response = runtime_app(app).call(request)
response[1]['X-Runtime'].should =~ /[\d\.]+/
end

it "doesn't set the X-Runtime if it is already set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain', "X-Runtime" => "foobar"}, "Hello, World!"] }
response = Rack::Runtime.new(app).call({})
response = runtime_app(app).call(request)
response[1]['X-Runtime'].should == "foobar"
end

should "allow a suffix to be set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
response = Rack::Runtime.new(app, "Test").call({})
response = runtime_app(app, "Test").call(request)
response[1]['X-Runtime-Test'].should =~ /[\d\.]+/
end

should "allow multiple timers to be set" do
app = lambda { |env| sleep 0.1; [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
runtime = Rack::Runtime.new(app, "App")
runtime = runtime_app(app, "App")

# wrap many times to guarantee a measurable difference
100.times do |i|
runtime = Rack::Runtime.new(runtime, i.to_s)
end
runtime = Rack::Runtime.new(runtime, "All")

response = runtime.call({})
response = runtime.call(request)

response[1]['X-Runtime-App'].should =~ /[\d\.]+/
response[1]['X-Runtime-All'].should =~ /[\d\.]+/
Expand Down

0 comments on commit 8f39d33

Please sign in to comment.