Permalink
Browse files

Wrap test apps in Rack::Lint (first pass)

Update tests for Auth::Basic, Auth::Digest, Builder, Casecase, Handler::CGI,
Chunked, CommonLogger and ConditionalGet.
  • Loading branch information...
1 parent 39582c2 commit 484bcfd4931e3cbdb0b3759aa0e5e8c8113cc575 @lgierth lgierth committed with raggi Dec 18, 2011
View
@@ -4,7 +4,7 @@ module Rack
# status codes).
class Cascade
- NotFound = [404, {}, []]
+ NotFound = [404, {"Content-Type" => "text/plain"}, []]
attr_reader :apps
@@ -2,4 +2,4 @@
require '../testrequest'
-run TestRequest.new
+run Rack::Lint.new(TestRequest.new)
View
@@ -2,4 +2,4 @@
# -*- ruby -*-
require '../testrequest'
-run TestRequest.new
+run Rack::Lint.new(TestRequest.new)
View
@@ -1,4 +1,5 @@
require 'rack/auth/basic'
+require 'rack/lint'
require 'rack/mock'
describe Rack::Auth::Basic do
@@ -7,7 +8,9 @@ def realm
end
def unprotected_app
- lambda { |env| [ 200, {'Content-Type' => 'text/plain'}, ["Hi #{env['REMOTE_USER']}"] ] }
+ Rack::Lint.new lambda { |env|
+ [ 200, {'Content-Type' => 'text/plain'}, ["Hi #{env['REMOTE_USER']}"] ]
+ }
end
def protected_app
View
@@ -1,4 +1,5 @@
require 'rack/auth/digest/md5'
+require 'rack/lint'
require 'rack/mock'
describe Rack::Auth::Digest::MD5 do
@@ -7,10 +8,10 @@ def realm
end
def unprotected_app
- lambda do |env|
+ Rack::Lint.new lambda { |env|
friend = Rack::Utils.parse_query(env["QUERY_STRING"])["friend"]
[ 200, {'Content-Type' => 'text/plain'}, ["Hi #{env['REMOTE_USER']}#{friend ? " and #{friend}" : ''}"] ]
- end
+ }
end
def protected_app
View
@@ -1,4 +1,5 @@
require 'rack/builder'
+require 'rack/lint'
require 'rack/mock'
require 'rack/showexceptions'
require 'rack/urlmap'
@@ -18,46 +19,54 @@ def self.env
end
describe Rack::Builder do
+ def builder(&block)
+ Rack::Lint.new Rack::Builder.new(&block)
+ end
+
+ def builder_to_app(&block)
+ Rack::Lint.new Rack::Builder.new(&block).to_app
+ end
+
it "supports mapping" do
- app = Rack::Builder.new do
+ app = builder_to_app do
map '/' do |outer_env|
- run lambda { |inner_env| [200, {}, ['root']] }
+ run lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, ['root']] }
end
map '/sub' do
- run lambda { |inner_env| [200, {}, ['sub']] }
+ run lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, ['sub']] }
end
- end.to_app
+ end
Rack::MockRequest.new(app).get("/").body.to_s.should.equal 'root'
Rack::MockRequest.new(app).get("/sub").body.to_s.should.equal 'sub'
end
it "doesn't dupe env even when mapping" do
- app = Rack::Builder.new do
+ app = builder_to_app do
use NothingMiddleware
map '/' do |outer_env|
run lambda { |inner_env|
inner_env['new_key'] = 'new_value'
- [200, {}, ['root']]
+ [200, {"Content-Type" => "text/plain"}, ['root']]
}
end
- end.to_app
+ end
Rack::MockRequest.new(app).get("/").body.to_s.should.equal 'root'
NothingMiddleware.env['new_key'].should.equal 'new_value'
end
it "chains apps by default" do
- app = Rack::Builder.new do
+ app = builder_to_app do
use Rack::ShowExceptions
run lambda { |env| raise "bzzzt" }
- end.to_app
+ end
Rack::MockRequest.new(app).get("/").should.be.server_error
Rack::MockRequest.new(app).get("/").should.be.server_error
Rack::MockRequest.new(app).get("/").should.be.server_error
end
it "has implicit #to_app" do
- app = Rack::Builder.new do
+ app = builder do
use Rack::ShowExceptions
run lambda { |env| raise "bzzzt" }
end
@@ -68,13 +77,13 @@ def self.env
end
it "supports blocks on use" do
- app = Rack::Builder.new do
+ app = builder do
use Rack::ShowExceptions
use Rack::Auth::Basic do |username, password|
'secret' == password
end
- run lambda { |env| [200, {}, ['Hi Boss']] }
+ run lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hi Boss']] }
end
response = Rack::MockRequest.new(app).get("/")
@@ -89,7 +98,7 @@ def self.env
end
it "has explicit #to_app" do
- app = Rack::Builder.app do
+ app = builder do
use Rack::ShowExceptions
run lambda { |env| raise "bzzzt" }
end
@@ -100,17 +109,19 @@ def self.env
end
it "can mix map and run for endpoints" do
- app = Rack::Builder.app do
- map('/sub') { run lambda { |inner_env| [200, {}, ['sub']] }}
- run lambda { |inner_env| [200, {}, ['root']] }
+ app = builder do
+ map '/sub' do
+ run lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, ['sub']] }
+ end
+ run lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, ['root']] }
end
Rack::MockRequest.new(app).get("/").body.to_s.should.equal 'root'
Rack::MockRequest.new(app).get("/sub").body.to_s.should.equal 'sub'
end
it "accepts middleware-only map blocks" do
- app = Rack::Builder.app do
+ app = builder do
map('/foo') { use Rack::ShowExceptions }
run lambda { |env| raise "bzzzt" }
end
@@ -120,7 +131,7 @@ def self.env
end
should "initialize apps once" do
- app = Rack::Builder.new do
+ app = builder do
class AppClass
def initialize
@called = 0
@@ -141,7 +152,7 @@ def call(env)
end
it "allows use after run" do
- app = Rack::Builder.app do
+ app = builder do
run lambda { |env| raise "bzzzt" }
use Rack::ShowExceptions
end
@@ -153,7 +164,7 @@ def call(env)
it 'complains about a missing run' do
proc do
- Rack::Builder.app { use Rack::ShowExceptions }
+ Rack::Lint.new Rack::Builder.app { use Rack::ShowExceptions }
end.should.raise(RuntimeError)
end
View
@@ -1,9 +1,14 @@
require 'rack/cascade'
require 'rack/file'
+require 'rack/lint'
require 'rack/urlmap'
require 'rack/mock'
describe Rack::Cascade do
+ def cascade(*args)
+ Rack::Lint.new Rack::Cascade.new(*args)
+ end
+
docroot = File.expand_path(File.dirname(__FILE__))
app1 = Rack::File.new(docroot)
@@ -13,20 +18,20 @@
[200, { "Content-Type" => "text/plain"}, [""]]})
should "dispatch onward on 404 by default" do
- cascade = Rack::Cascade.new([app1, app2, app3])
+ cascade = cascade([app1, app2, app3])
Rack::MockRequest.new(cascade).get("/cgi/test").should.be.ok
Rack::MockRequest.new(cascade).get("/foo").should.be.ok
Rack::MockRequest.new(cascade).get("/toobad").should.be.not_found
Rack::MockRequest.new(cascade).get("/cgi/../..").should.be.forbidden
end
should "dispatch onward on whatever is passed" do
- cascade = Rack::Cascade.new([app1, app2, app3], [404, 403])
+ cascade = cascade([app1, app2, app3], [404, 403])
Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.not_found
end
should "return 404 if empty" do
- Rack::MockRequest.new(Rack::Cascade.new([])).get('/').should.be.not_found
+ Rack::MockRequest.new(cascade([])).get('/').should.be.not_found
end
should "append new app" do
View
@@ -1,59 +1,74 @@
require 'rack/chunked'
+require 'rack/lint'
require 'rack/mock'
describe Rack::Chunked do
+ def chunked(app)
+ proc do |env|
+ app = Rack::Chunked.new(app)
+ Rack::Lint.new(app).call(env).tap do |response|
+ # we want to use body like an array, but it only has #each
+ response[2] = Enumerator.new(response[2]).to_a
+ end
+ end
+ end
+
before do
@env = Rack::MockRequest.
env_for('/', 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET')
end
should 'chunk responses with no Content-Length' do
- app = lambda { |env| [200, {}, ['Hello', ' ', 'World!']] }
- response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
+ app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hello', ' ', 'World!']] }
+ response = Rack::MockResponse.new(*chunked(app).call(@env))
response.headers.should.not.include 'Content-Length'
response.headers['Transfer-Encoding'].should.equal 'chunked'
response.body.should.equal "5\r\nHello\r\n1\r\n \r\n6\r\nWorld!\r\n0\r\n\r\n"
end
should 'chunks empty bodies properly' do
- app = lambda { |env| [200, {}, []] }
- response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
+ app = lambda { |env| [200, {"Content-Type" => "text/plain"}, []] }
+ response = Rack::MockResponse.new(*chunked(app).call(@env))
response.headers.should.not.include 'Content-Length'
response.headers['Transfer-Encoding'].should.equal 'chunked'
response.body.should.equal "0\r\n\r\n"
end
should 'chunks encoded bodies properly' do
body = ["\uFFFEHello", " ", "World"].map {|t| t.encode("UTF-16LE") }
- app = lambda { |env| [200, {}, body] }
- response = Rack::MockResponse.new(*Rack::Chunked.new(app).call(@env))
+ app = lambda { |env| [200, {"Content-Type" => "text/plain"}, body] }
+ response = Rack::MockResponse.new(*chunked(app).call(@env))
response.headers.should.not.include 'Content-Length'
response.headers['Transfer-Encoding'].should.equal 'chunked'
response.body.encoding.to_s.should == "ASCII-8BIT"
response.body.should.equal "c\r\n\xFE\xFFH\x00e\x00l\x00l\x00o\x00\r\n2\r\n \x00\r\na\r\nW\x00o\x00r\x00l\x00d\x00\r\n0\r\n\r\n"
end if RUBY_VERSION >= "1.9"
should 'not modify response when Content-Length header present' do
- app = lambda { |env| [200, {'Content-Length'=>'12'}, ['Hello', ' ', 'World!']] }
- status, headers, body = Rack::Chunked.new(app).call(@env)
+ app = lambda { |env|
+ [200, {"Content-Type" => "text/plain", 'Content-Length'=>'12'}, ['Hello', ' ', 'World!']]
+ }
+ status, headers, body = chunked(app).call(@env)
status.should.equal 200
headers.should.not.include 'Transfer-Encoding'
headers.should.include 'Content-Length'
body.join.should.equal 'Hello World!'
end
should 'not modify response when client is HTTP/1.0' do
- app = lambda { |env| [200, {}, ['Hello', ' ', 'World!']] }
+ app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ['Hello', ' ', 'World!']] }
@env['HTTP_VERSION'] = 'HTTP/1.0'
- status, headers, body = Rack::Chunked.new(app).call(@env)
+ status, headers, body = chunked(app).call(@env)
status.should.equal 200
headers.should.not.include 'Transfer-Encoding'
body.join.should.equal 'Hello World!'
end
should 'not modify response when Transfer-Encoding header already present' do
- app = lambda { |env| [200, {'Transfer-Encoding' => 'identity'}, ['Hello', ' ', 'World!']] }
- status, headers, body = Rack::Chunked.new(app).call(@env)
+ app = lambda { |env|
+ [200, {"Content-Type" => "text/plain", 'Transfer-Encoding' => 'identity'}, ['Hello', ' ', 'World!']]
+ }
+ status, headers, body = chunked(app).call(@env)
status.should.equal 200
headers['Transfer-Encoding'].should.equal 'identity'
body.join.should.equal 'Hello World!'
@@ -62,7 +77,7 @@
[100, 204, 205, 304].each do |status_code|
should "not modify response when status code is #{status_code}" do
app = lambda { |env| [status_code, {}, []] }
- status, headers, _ = Rack::Chunked.new(app).call(@env)
+ status, headers, _ = chunked(app).call(@env)
status.should.equal status_code
headers.should.not.include 'Transfer-Encoding'
end
@@ -1,19 +1,20 @@
require 'rack/commonlogger'
+require 'rack/lint'
require 'rack/mock'
describe Rack::CommonLogger do
obj = 'foobar'
length = obj.size
- app = lambda { |env|
+ app = Rack::Lint.new lambda { |env|
[200,
{"Content-Type" => "text/html", "Content-Length" => length.to_s},
[obj]]}
- app_without_length = lambda { |env|
+ app_without_length = Rack::Lint.new lambda { |env|
[200,
{"Content-Type" => "text/html"},
[]]}
- app_with_zero_length = lambda { |env|
+ app_with_zero_length = Rack::Lint.new lambda { |env|
[200,
{"Content-Type" => "text/html", "Content-Length" => "0"},
[]]}
Oops, something went wrong.

0 comments on commit 484bcfd

Please sign in to comment.