Skip to content
Browse files

Wrap test apps in Rack::Lint and fix uncovered errors (fifth pass)

Update tests for MockRequest, MockResponse, NullLogger, Recursive and Runtime.
  • Loading branch information...
1 parent 761bc54 commit 8f39d3312d77d418ef456c21000ef12108193f12 @lgierth lgierth committed with lgierth Dec 28, 2011
Showing with 46 additions and 23 deletions.
  1. +8 −7 test/spec_mock.rb
  2. +11 −2 test/spec_nulllogger.rb
  3. +12 −9 test/spec_recursive.rb
  4. +15 −5 test/spec_runtime.rb
View
15 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
@@ -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
@@ -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"
View
13 test/spec_nulllogger.rb
@@ -1,3 +1,6 @@
+require 'enumerator'
+require 'rack/lint'
+require 'rack/mock'
require 'rack/nulllogger'
describe Rack::NullLogger do
@@ -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
View
21 test/spec_recursive.rb
@@ -1,3 +1,4 @@
+require 'rack/lint'
require 'rack/recursive'
require 'rack/mock'
@@ -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
@@ -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")
@@ -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
View
20 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\.]+/

0 comments on commit 8f39d33

Please sign in to comment.
Something went wrong with that request. Please try again.