Permalink
Browse files

Merge pull request #295 from lgierth/use-lint-in-tests

Use Rack::Lint in all tests
  • Loading branch information...
2 parents 73cd273 + 73945ff commit 6141192834f2a6c5a59ac84f8a7266319779a4b9 @raggi raggi committed May 4, 2012
View
@@ -29,7 +29,7 @@ def call(env)
unless headers['Cache-Control']
headers['Cache-Control'] =
- (digest ? @cache_control : @no_cache_control) || []
+ (digest ? @cache_control : @no_cache_control) || ""
end
[status, headers, body]
View
@@ -193,7 +193,7 @@ def config_file(name)
it "requires anything not ending in .ru" do
$: << File.dirname(__FILE__)
- app, options = Rack::Builder.parse_file 'builder/anything'
+ app, * = Rack::Builder.parse_file 'builder/anything'
Rack::MockRequest.new(app).get("/").body.to_s.should.equal 'OK'
$:.pop
end
View
@@ -3,15 +3,15 @@
require 'rack/mock'
describe Rack::Chunked do
- Enumerator = ::Enumerable::Enumerator unless defined?(Enumerator)
+ ::Enumerator = ::Enumerable::Enumerator unless Object.const_defined?(:Enumerator)
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
+ response = Rack::Lint.new(app).call(env)
+ # we want to use body like an array, but it only has #each
+ response[2] = Enumerator.new(response[2]).to_a
+ response
end
end
@@ -42,7 +42,7 @@ def chunked(app)
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.encoding.to_s.should.equal "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"
View
@@ -8,7 +8,6 @@
should "accept a block that modifies the environment" do
app = Rack::Builder.new do
use Rack::Lint
- use Rack::ContentLength
use Rack::Config do |env|
env['greeting'] = 'hello'
end
@@ -1,9 +1,22 @@
+require 'enumerator'
require 'rack/content_length'
+require 'rack/lint'
+require 'rack/mock'
describe Rack::ContentLength do
+ ::Enumerator = ::Enumerable::Enumerator unless Object.const_defined?(:Enumerator)
+
+ def content_length(app)
+ Rack::Lint.new Rack::ContentLength.new(app)
+ end
+
+ def request
+ Rack::MockRequest.env_for
+ end
+
should "set Content-Length on Array bodies if none is set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
- response = Rack::ContentLength.new(app).call({})
+ response = content_length(app).call(request)
response[1]['Content-Length'].should.equal '13'
end
@@ -12,25 +25,25 @@
def body.each ; yield call ; end
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
- response = Rack::ContentLength.new(app).call({})
+ response = content_length(app).call(request)
response[1]['Content-Length'].should.be.nil
end
should "not change Content-Length if it is already set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '1'}, "Hello, World!"] }
- response = Rack::ContentLength.new(app).call({})
+ response = content_length(app).call(request)
response[1]['Content-Length'].should.equal '1'
end
should "not set Content-Length on 304 responses" do
- app = lambda { |env| [304, {'Content-Type' => 'text/plain'}, []] }
- response = Rack::ContentLength.new(app).call({})
+ app = lambda { |env| [304, {}, []] }
+ response = content_length(app).call(request)
response[1]['Content-Length'].should.equal nil
end
should "not set Content-Length when Transfer-Encoding is chunked" do
- app = lambda { |env| [200, {'Transfer-Encoding' => 'chunked'}, []] }
- response = Rack::ContentLength.new(app).call({})
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Transfer-Encoding' => 'chunked'}, []] }
+ response = content_length(app).call(request)
response[1]['Content-Length'].should.equal nil
end
@@ -39,7 +52,7 @@ def body.each ; yield call ; end
#
# should "not force a Content-Length when Connection:close" do
# app = lambda { |env| [200, {'Connection' => 'close'}, []] }
- # response = Rack::ContentLength.new(app).call({})
+ # response = content_length(app).call({})
# response[1]['Content-Length'].should.equal nil
# end
@@ -51,8 +64,8 @@ def close; @closed = true; end
def to_ary; end
end.new(%w[one two three])
- app = lambda { |env| [200, {}, body] }
- Rack::ContentLength.new(app).call({})
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
+ content_length(app).call(request)
body.closed.should.equal true
end
@@ -64,10 +77,10 @@ def each
def to_ary; end
end.new(%w[one two three])
- app = lambda { |env| [200, {}, body] }
- response = Rack::ContentLength.new(app).call({})
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
+ response = content_length(app).call(request)
expected = %w[one two three]
response[1]['Content-Length'].should.equal expected.join.size.to_s
- response[2].should.equal expected
+ Enumerator.new(response[2]).to_a.should.equal expected
end
end
View
@@ -1,35 +1,45 @@
require 'rack/content_type'
+require 'rack/lint'
+require 'rack/mock'
describe Rack::ContentType do
+ def content_type(app, *args)
+ Rack::Lint.new Rack::ContentType.new(app, *args)
+ end
+
+ def request
+ Rack::MockRequest.env_for
+ end
+
should "set Content-Type to default text/html if none is set" do
app = lambda { |env| [200, {}, "Hello, World!"] }
- headers = Rack::ContentType.new(app).call({})[1]
+ headers = content_type(app).call(request)[1]
headers['Content-Type'].should.equal 'text/html'
end
should "set Content-Type to chosen default if none is set" do
app = lambda { |env| [200, {}, "Hello, World!"] }
headers =
- Rack::ContentType.new(app, 'application/octet-stream').call({})[1]
+ content_type(app, 'application/octet-stream').call(request)[1]
headers['Content-Type'].should.equal 'application/octet-stream'
end
should "not change Content-Type if it is already set" do
app = lambda { |env| [200, {'Content-Type' => 'foo/bar'}, "Hello, World!"] }
- headers = Rack::ContentType.new(app).call({})[1]
+ headers = content_type(app).call(request)[1]
headers['Content-Type'].should.equal 'foo/bar'
end
should "detect Content-Type case insensitive" do
app = lambda { |env| [200, {'CONTENT-Type' => 'foo/bar'}, "Hello, World!"] }
- headers = Rack::ContentType.new(app).call({})[1]
+ headers = content_type(app).call(request)[1]
headers.to_a.select { |k,v| k.downcase == "content-type" }.
should.equal [["CONTENT-Type","foo/bar"]]
end
should "not set Content-Type on 304 responses" do
app = lambda { |env| [304, {}, []] }
- response = Rack::ContentType.new(app, "text/html").call({})
+ response = content_type(app, "text/html").call(request)
response[1]['Content-Type'].should.equal nil
end
end
View
@@ -1,15 +1,27 @@
+require 'enumerator'
require 'stringio'
require 'time' # for Time#httpdate
require 'rack/deflater'
+require 'rack/lint'
require 'rack/mock'
require 'zlib'
describe Rack::Deflater do
+ ::Enumerator = ::Enumerable::Enumerator unless Object.const_defined?(:Enumerator)
+
+ def deflater(app)
+ Rack::Lint.new Rack::Deflater.new(app)
+ end
+
def build_response(status, body, accept_encoding, headers = {})
body = [body] if body.respond_to? :to_str
- app = lambda { |env| [status, {}, body] }
+ app = lambda do |env|
+ res = [status, {}, body]
+ res[1]["Content-Type"] = "text/plain" unless res[0] == 304
+ res
+ end
request = Rack::MockRequest.env_for("", headers.merge("HTTP_ACCEPT_ENCODING" => accept_encoding))
- response = Rack::Deflater.new(app).call(request)
+ response = deflater(app).call(request)
return response
end
@@ -28,7 +40,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200)
response[1].should.equal({
"Content-Encoding" => "deflate",
- "Vary" => "Accept-Encoding"
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
})
buf = ''
response[2].each { |part| buf << part }
@@ -44,7 +57,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200)
response[1].should.equal({
"Content-Encoding" => "deflate",
- "Vary" => "Accept-Encoding"
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
})
buf = []
inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
@@ -61,7 +75,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200)
response[1].should.equal({
"Content-Encoding" => "deflate",
- "Vary" => "Accept-Encoding"
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
})
buf = ''
response[2].each { |part| buf << part }
@@ -78,6 +93,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[1].should.equal({
"Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
})
buf = ''
@@ -97,7 +113,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200)
response[1].should.equal({
"Content-Encoding" => "gzip",
- "Vary" => "Accept-Encoding"
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
})
buf = []
inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
@@ -111,42 +128,43 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, "Hello world!", "superzip")
response[0].should.equal(200)
- response[1].should.equal({ "Vary" => "Accept-Encoding" })
- response[2].should.equal(["Hello world!"])
+ response[1].should.equal({ "Vary" => "Accept-Encoding", "Content-Type" => "text/plain" })
+ Enumerator.new(response[2]).to_a.should.equal(["Hello world!"])
end
should "be able to skip when there is no response entity body" do
response = build_response(304, [], "gzip")
response[0].should.equal(304)
response[1].should.equal({})
- response[2].should.equal([])
+ Enumerator.new(response[2]).to_a.should.equal([])
end
should "handle the lack of an acceptable encoding" do
response1 = build_response(200, "Hello world!", "identity;q=0", "PATH_INFO" => "/")
response1[0].should.equal(406)
response1[1].should.equal({"Content-Type" => "text/plain", "Content-Length" => "71"})
- response1[2].should.equal(["An acceptable encoding for the requested resource / could not be found."])
+ Enumerator.new(response1[2]).to_a.should.equal(["An acceptable encoding for the requested resource / could not be found."])
response2 = build_response(200, "Hello world!", "identity;q=0", "SCRIPT_NAME" => "/foo", "PATH_INFO" => "/bar")
response2[0].should.equal(406)
response2[1].should.equal({"Content-Type" => "text/plain", "Content-Length" => "78"})
- response2[2].should.equal(["An acceptable encoding for the requested resource /foo/bar could not be found."])
+ Enumerator.new(response2[2]).to_a.should.equal(["An acceptable encoding for the requested resource /foo/bar could not be found."])
end
should "handle gzip response with Last-Modified header" do
last_modified = Time.now.httpdate
- app = lambda { |env| [200, { "Last-Modified" => last_modified }, ["Hello World!"]] }
+ app = lambda { |env| [200, { "Content-Type" => "text/plain", "Last-Modified" => last_modified }, ["Hello World!"]] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
- response = Rack::Deflater.new(app).call(request)
+ response = deflater(app).call(request)
response[0].should.equal(200)
response[1].should.equal({
"Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding",
- "Last-Modified" => last_modified
+ "Last-Modified" => last_modified,
+ "Content-Type" => "text/plain"
})
buf = ''
@@ -158,28 +176,28 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end
should "do nothing when no-transform Cache-Control directive present" do
- app = lambda { |env| [200, {'Cache-Control' => 'no-transform'}, ['Hello World!']] }
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Cache-Control' => 'no-transform'}, ['Hello World!']] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
- response = Rack::Deflater.new(app).call(request)
+ response = deflater(app).call(request)
response[0].should.equal(200)
response[1].should.not.include "Content-Encoding"
- response[2].join.should.equal("Hello World!")
+ Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
end
should "do nothing when Content-Encoding already present" do
- app = lambda { |env| [200, {'Content-Encoding' => 'gzip'}, ['Hello World!']] }
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Encoding' => 'gzip'}, ['Hello World!']] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
- response = Rack::Deflater.new(app).call(request)
+ response = deflater(app).call(request)
response[0].should.equal(200)
- response[2].join.should.equal("Hello World!")
+ Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
end
should "deflate when Content-Encoding is identity" do
- app = lambda { |env| [200, {'Content-Encoding' => 'identity'}, ['Hello World!']] }
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Encoding' => 'identity'}, ['Hello World!']] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "deflate")
- response = Rack::Deflater.new(app).call(request)
+ response = deflater(app).call(request)
response[0].should.equal(200)
buf = ''
View
@@ -1,10 +1,11 @@
require 'rack/directory'
+require 'rack/lint'
require 'rack/mock'
describe Rack::Directory do
DOCROOT = File.expand_path(File.dirname(__FILE__)) unless defined? DOCROOT
FILE_CATCH = proc{|env| [200, {'Content-Type'=>'text/plain', "Content-Length" => "7"}, ['passed!']] }
- app = Rack::Directory.new DOCROOT, FILE_CATCH
+ app = Rack::Lint.new(Rack::Directory.new(DOCROOT, FILE_CATCH))
should "serve directory indices" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
Oops, something went wrong.

0 comments on commit 6141192

Please sign in to comment.