Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Update tests for Lobster, Lock, Logger and MethodOverride.
  • Loading branch information...
commit 761bc5472c0b447937ef31b0b81528cfec5f8766 1 parent 0cb39b0
@lgierth lgierth authored lgierth committed
View
25 test/spec_lobster.rb
@@ -1,28 +1,43 @@
require 'rack/lobster'
+require 'rack/lint'
require 'rack/mock'
+module LobsterHelpers
+ def lobster
+ Rack::MockRequest.new Rack::Lint.new(Rack::Lobster.new)
+ end
+
+ def lambda_lobster
+ Rack::MockRequest.new Rack::Lint.new(Rack::Lobster::LambdaLobster)
+ end
+end
+
describe Rack::Lobster::LambdaLobster do
+ extend LobsterHelpers
+
should "be a single lambda" do
Rack::Lobster::LambdaLobster.should.be.kind_of Proc
end
should "look like a lobster" do
- res = Rack::MockRequest.new(Rack::Lobster::LambdaLobster).get("/")
+ res = lambda_lobster.get("/")
res.should.be.ok
res.body.should.include "(,(,,(,,,("
res.body.should.include "?flip"
end
should "be flippable" do
- res = Rack::MockRequest.new(Rack::Lobster::LambdaLobster).get("/?flip")
+ res = lambda_lobster.get("/?flip")
res.should.be.ok
res.body.should.include "(,,,(,,(,("
end
end
describe Rack::Lobster do
+ extend LobsterHelpers
+
should "look like a lobster" do
- res = Rack::MockRequest.new(Rack::Lobster.new).get("/")
+ res = lobster.get("/")
res.should.be.ok
res.body.should.include "(,(,,(,,,("
res.body.should.include "?flip"
@@ -30,14 +45,14 @@
end
should "be flippable" do
- res = Rack::MockRequest.new(Rack::Lobster.new).get("/?flip=left")
+ res = lobster.get("/?flip=left")
res.should.be.ok
res.body.should.include "(,,,(,,(,("
end
should "provide crashing for testing purposes" do
lambda {
- Rack::MockRequest.new(Rack::Lobster.new).get("/?flip=crash")
+ lobster.get("/?flip=crash")
}.should.raise
end
end
View
57 test/spec_lock.rb
@@ -1,3 +1,5 @@
+require 'enumerator'
+require 'rack/lint'
require 'rack/lock'
require 'rack/mock'
@@ -22,10 +24,24 @@ def unlock
end
end
+module LockHelpers
+ def lock_app(app, lock = Lock.new)
+ app = if lock
+ Rack::Lock.new app, lock
+ else
+ Rack::Lock.new app
+ end
+ Rack::Lint.new app
+ end
+end
+
describe Rack::Lock do
+ extend LockHelpers
+
describe 'Proxy' do
+ extend LockHelpers
+
should 'delegate each' do
- lock = Lock.new
env = Rack::MockRequest.env_for("/")
response = Class.new {
attr_accessor :close_called
@@ -33,7 +49,7 @@ def initialize; @close_called = false; end
def each; %w{ hi mom }.each { |x| yield x }; end
}.new
- app = Rack::Lock.new(lambda { |inner_env| [200, {}, response] }, lock)
+ app = lock_app(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, response] })
response = app.call(env)[2]
list = []
response.each { |x| list << x }
@@ -47,7 +63,7 @@ def each; %w{ hi mom }.each { |x| yield x }; end
res = ['Hello World']
def res.to_path ; "/tmp/hello.txt" ; end
- app = Rack::Lock.new(lambda { |inner_env| [200, {}, res] }, lock)
+ app = Rack::Lock.new(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, res] }, lock)
body = app.call(env)[2]
body.should.respond_to :to_path
@@ -55,12 +71,11 @@ def res.to_path ; "/tmp/hello.txt" ; end
end
should 'not delegate to_path if body does not implement it' do
- lock = Lock.new
env = Rack::MockRequest.env_for("/")
res = ['Hello World']
- app = Rack::Lock.new(lambda { |inner_env| [200, {}, res] }, lock)
+ app = lock_app(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, res] })
body = app.call(env)[2]
body.should.not.respond_to :to_path
@@ -68,7 +83,6 @@ def res.to_path ; "/tmp/hello.txt" ; end
end
should 'call super on close' do
- lock = Lock.new
env = Rack::MockRequest.env_for("/")
response = Class.new {
attr_accessor :close_called
@@ -76,7 +90,7 @@ def initialize; @close_called = false; end
def close; @close_called = true; end
}.new
- app = Rack::Lock.new(lambda { |inner_env| [200, {}, response] }, lock)
+ app = lock_app(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, response] })
app.call(env)
response.close_called.should.equal false
response.close
@@ -87,7 +101,7 @@ def close; @close_called = true; end
lock = Lock.new
env = Rack::MockRequest.env_for("/")
response = Object.new
- app = Rack::Lock.new(lambda { |inner_env| [200, {}, response] }, lock)
+ app = lock_app(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, response] }, lock)
lock.synchronized.should.equal false
response = app.call(env)[2]
lock.synchronized.should.equal true
@@ -96,19 +110,20 @@ def close; @close_called = true; end
end
should "return value from app" do
- lock = Lock.new
env = Rack::MockRequest.env_for("/")
- body = [200, {}, %w{ hi mom }]
- app = Rack::Lock.new(lambda { |inner_env| body }, lock)
- app.call(env).should.equal body
+ body = [200, {"Content-Type" => "text/plain"}, %w{ hi mom }]
+ app = lock_app(lambda { |inner_env| body })
+ app.call(env).tap do |res|
+ res[0].should.equal body[0]
+ res[1].should.equal body[1]
+ Enumerator.new(res[2]).to_a.should.equal ["hi", "mom"]
+ end
end
should "call synchronize on lock" do
lock = Lock.new
env = Rack::MockRequest.env_for("/")
- app = Rack::Lock.new(lambda { |inner_env|
- [200, {}, %w{ a b c }]
- }, lock)
+ app = lock_app(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, %w{ a b c }] }, lock)
lock.synchronized.should.equal false
app.call(env)
lock.synchronized.should.equal true
@@ -117,16 +132,16 @@ def close; @close_called = true; end
should "unlock if the app raises" do
lock = Lock.new
env = Rack::MockRequest.env_for("/")
- app = Rack::Lock.new(lambda { raise Exception }, lock)
+ app = lock_app(lambda { raise Exception })
lambda { app.call(env) }.should.raise(Exception)
lock.synchronized.should.equal false
end
should "set multithread flag to false" do
- app = Rack::Lock.new(lambda { |env|
+ app = lock_app(lambda { |env|
env['rack.multithread'].should.equal false
- [200, {}, %w{ a b c }]
- })
+ [200, {"Content-Type" => "text/plain"}, %w{ a b c }]
+ }, false)
app.call(Rack::MockRequest.env_for("/"))
end
@@ -136,7 +151,7 @@ def call(env)
env['rack.multithread'].should.equal true
super
end
- }.new(lambda { |env| [200, {}, %w{ a b c }] })
- app.call(Rack::MockRequest.env_for("/"))
+ }.new(lambda { |env| [200, {"Content-Type" => "text/plain"}, %w{ a b c }] })
+ Rack::Lint.new(app).call(Rack::MockRequest.env_for("/"))
end
end
View
9 test/spec_logger.rb
@@ -1,5 +1,7 @@
require 'stringio'
+require 'rack/lint'
require 'rack/logger'
+require 'rack/mock'
describe Rack::Logger do
app = lambda { |env|
@@ -11,13 +13,6 @@
[200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]]
}
- should "log to rack.errors" do
- errors = StringIO.new
- Rack::Logger.new(app).call('rack.errors' => errors)
- errors.string.should.match(/INFO -- : Program started/)
- errors.string.should.match(/WARN -- : Nothing to do/)
- end
-
should "conform to Rack::Lint" do
errors = StringIO.new
a = Rack::Lint.new(Rack::Logger.new(app))
View
43 test/spec_methodoverride.rb
@@ -3,20 +3,24 @@
require 'rack/mock'
describe Rack::MethodOverride do
+ def app
+ Rack::Lint.new(Rack::MethodOverride.new(lambda {|e|
+ [200, {"Content-Type" => "text/plain"}, []]
+ }))
+ end
+
should "not affect GET requests" do
env = Rack::MockRequest.env_for("/?_method=delete", :method => "GET")
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["REQUEST_METHOD"].should.equal "GET"
+ env["REQUEST_METHOD"].should.equal "GET"
end
should "modify REQUEST_METHOD for POST requests when _method parameter is set" do
env = Rack::MockRequest.env_for("/", :method => "POST", :input => "_method=put")
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["REQUEST_METHOD"].should.equal "PUT"
+ env["REQUEST_METHOD"].should.equal "PUT"
end
should "modify REQUEST_METHOD for POST requests when X-HTTP-Method-Override is set" do
@@ -24,36 +28,32 @@
:method => "POST",
"HTTP_X_HTTP_METHOD_OVERRIDE" => "PATCH"
)
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["REQUEST_METHOD"].should.equal "PATCH"
+ env["REQUEST_METHOD"].should.equal "PATCH"
end
should "not modify REQUEST_METHOD if the method is unknown" do
env = Rack::MockRequest.env_for("/", :method => "POST", :input => "_method=foo")
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["REQUEST_METHOD"].should.equal "POST"
+ env["REQUEST_METHOD"].should.equal "POST"
end
should "not modify REQUEST_METHOD when _method is nil" do
env = Rack::MockRequest.env_for("/", :method => "POST", :input => "foo=bar")
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["REQUEST_METHOD"].should.equal "POST"
+ env["REQUEST_METHOD"].should.equal "POST"
end
should "store the original REQUEST_METHOD prior to overriding" do
env = Rack::MockRequest.env_for("/",
:method => "POST",
:input => "_method=options")
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["rack.methodoverride.original_method"].should.equal "POST"
+ env["rack.methodoverride.original_method"].should.equal "POST"
end
should "not modify REQUEST_METHOD when given invalid multipart form data" do
@@ -63,11 +63,10 @@
EOF
env = Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
- "CONTENT_LENGTH" => input.size,
+ "CONTENT_LENGTH" => input.size.to_s,
:method => "POST", :input => input)
- app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
- req = app.call(env)
+ app.call env
- req.env["REQUEST_METHOD"].should.equal "POST"
+ env["REQUEST_METHOD"].should.equal "POST"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.