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 (second pass)

Update tests for Config, ContentLength, ContentType and Deflater.
  • Loading branch information...
commit bd43405d79dfc3c750e64fc16a0398ca6c07850a 1 parent 284000f
@lgierth lgierth authored lgierth committed
View
1  test/spec_config.rb
@@ -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
View
37 test/spec_content_length.rb
@@ -1,9 +1,20 @@
+require 'enumerator'
require 'rack/content_length'
+require 'rack/lint'
+require 'rack/mock'
describe Rack::ContentLength do
+ 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 +23,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 +50,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 +62,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 +75,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
20 test/spec_content_type.rb
@@ -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
50 test/spec_deflater.rb
@@ -1,15 +1,25 @@
+require 'enumerator'
require 'stringio'
require 'time' # for Time#httpdate
require 'rack/deflater'
+require 'rack/lint'
require 'rack/mock'
require 'zlib'
describe Rack::Deflater do
+ 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|
+ [status, {}, body].tap do |res|
+ res[1]["Content-Type"] = "text/plain" unless res[0] == 304
+ end
+ 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 +38,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 +55,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 +73,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 +91,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 +111,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,8 +126,8 @@ 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
@@ -120,33 +135,34 @@ class << body; def each; yield("foo"); yield("bar"); end; end
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,13 +174,13 @@ 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
Please sign in to comment.
Something went wrong with that request. Please try again.