Skip to content

Commit

Permalink
Wrap test apps in Rack::Lint and fix uncovered errors (second pass)
Browse files Browse the repository at this point in the history
Update tests for Config, ContentLength, ContentType and Deflater.
  • Loading branch information
Lars Gierth authored and Lars Gierth committed Mar 31, 2012
1 parent 284000f commit bd43405
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 36 deletions.
1 change: 0 additions & 1 deletion test/spec_config.rb
Expand Up @@ -8,7 +8,6 @@
should "accept a block that modifies the environment" do should "accept a block that modifies the environment" do
app = Rack::Builder.new do app = Rack::Builder.new do
use Rack::Lint use Rack::Lint
use Rack::ContentLength
use Rack::Config do |env| use Rack::Config do |env|
env['greeting'] = 'hello' env['greeting'] = 'hello'
end end
Expand Down
37 changes: 24 additions & 13 deletions test/spec_content_length.rb
@@ -1,9 +1,20 @@
require 'enumerator'
require 'rack/content_length' require 'rack/content_length'
require 'rack/lint'
require 'rack/mock'


describe Rack::ContentLength do 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 should "set Content-Length on Array bodies if none is set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] } 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' response[1]['Content-Length'].should.equal '13'
end end


Expand All @@ -12,25 +23,25 @@
def body.each ; yield call ; end def body.each ; yield call ; end


app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] } 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 response[1]['Content-Length'].should.be.nil
end end


should "not change Content-Length if it is already set" do should "not change Content-Length if it is already set" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '1'}, "Hello, World!"] } 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' response[1]['Content-Length'].should.equal '1'
end end


should "not set Content-Length on 304 responses" do should "not set Content-Length on 304 responses" do
app = lambda { |env| [304, {'Content-Type' => 'text/plain'}, []] } app = lambda { |env| [304, {}, []] }
response = Rack::ContentLength.new(app).call({}) response = content_length(app).call(request)
response[1]['Content-Length'].should.equal nil response[1]['Content-Length'].should.equal nil
end end


should "not set Content-Length when Transfer-Encoding is chunked" do should "not set Content-Length when Transfer-Encoding is chunked" do
app = lambda { |env| [200, {'Transfer-Encoding' => 'chunked'}, []] } app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Transfer-Encoding' => 'chunked'}, []] }
response = Rack::ContentLength.new(app).call({}) response = content_length(app).call(request)
response[1]['Content-Length'].should.equal nil response[1]['Content-Length'].should.equal nil
end end


Expand All @@ -39,7 +50,7 @@ def body.each ; yield call ; end
# #
# should "not force a Content-Length when Connection:close" do # should "not force a Content-Length when Connection:close" do
# app = lambda { |env| [200, {'Connection' => 'close'}, []] } # app = lambda { |env| [200, {'Connection' => 'close'}, []] }
# response = Rack::ContentLength.new(app).call({}) # response = content_length(app).call({})
# response[1]['Content-Length'].should.equal nil # response[1]['Content-Length'].should.equal nil
# end # end


Expand All @@ -51,8 +62,8 @@ def close; @closed = true; end
def to_ary; end def to_ary; end
end.new(%w[one two three]) end.new(%w[one two three])


app = lambda { |env| [200, {}, body] } app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
Rack::ContentLength.new(app).call({}) content_length(app).call(request)
body.closed.should.equal true body.closed.should.equal true
end end


Expand All @@ -64,10 +75,10 @@ def each
def to_ary; end def to_ary; end
end.new(%w[one two three]) end.new(%w[one two three])


app = lambda { |env| [200, {}, body] } app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
response = Rack::ContentLength.new(app).call({}) response = content_length(app).call(request)
expected = %w[one two three] expected = %w[one two three]
response[1]['Content-Length'].should.equal expected.join.size.to_s 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
end end
20 changes: 15 additions & 5 deletions test/spec_content_type.rb
@@ -1,35 +1,45 @@
require 'rack/content_type' require 'rack/content_type'
require 'rack/lint'
require 'rack/mock'


describe Rack::ContentType do 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 should "set Content-Type to default text/html if none is set" do
app = lambda { |env| [200, {}, "Hello, World!"] } 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' headers['Content-Type'].should.equal 'text/html'
end end


should "set Content-Type to chosen default if none is set" do should "set Content-Type to chosen default if none is set" do
app = lambda { |env| [200, {}, "Hello, World!"] } app = lambda { |env| [200, {}, "Hello, World!"] }
headers = 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' headers['Content-Type'].should.equal 'application/octet-stream'
end end


should "not change Content-Type if it is already set" do should "not change Content-Type if it is already set" do
app = lambda { |env| [200, {'Content-Type' => 'foo/bar'}, "Hello, World!"] } 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' headers['Content-Type'].should.equal 'foo/bar'
end end


should "detect Content-Type case insensitive" do should "detect Content-Type case insensitive" do
app = lambda { |env| [200, {'CONTENT-Type' => 'foo/bar'}, "Hello, World!"] } 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" }. headers.to_a.select { |k,v| k.downcase == "content-type" }.
should.equal [["CONTENT-Type","foo/bar"]] should.equal [["CONTENT-Type","foo/bar"]]
end end


should "not set Content-Type on 304 responses" do should "not set Content-Type on 304 responses" do
app = lambda { |env| [304, {}, []] } 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 response[1]['Content-Type'].should.equal nil
end end
end end
50 changes: 33 additions & 17 deletions test/spec_deflater.rb
@@ -1,15 +1,25 @@
require 'enumerator'
require 'stringio' require 'stringio'
require 'time' # for Time#httpdate require 'time' # for Time#httpdate
require 'rack/deflater' require 'rack/deflater'
require 'rack/lint'
require 'rack/mock' require 'rack/mock'
require 'zlib' require 'zlib'


describe Rack::Deflater do describe Rack::Deflater do
def deflater(app)
Rack::Lint.new Rack::Deflater.new(app)
end

def build_response(status, body, accept_encoding, headers = {}) def build_response(status, body, accept_encoding, headers = {})
body = [body] if body.respond_to? :to_str 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)) 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 return response
end end
Expand All @@ -28,7 +38,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200) response[0].should.equal(200)
response[1].should.equal({ response[1].should.equal({
"Content-Encoding" => "deflate", "Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding" "Vary" => "Accept-Encoding",
"Content-Type" => "text/plain"
}) })
buf = '' buf = ''
response[2].each { |part| buf << part } response[2].each { |part| buf << part }
Expand All @@ -44,7 +55,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200) response[0].should.equal(200)
response[1].should.equal({ response[1].should.equal({
"Content-Encoding" => "deflate", "Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding" "Vary" => "Accept-Encoding",
"Content-Type" => "text/plain"
}) })
buf = [] buf = []
inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS) inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
Expand All @@ -61,7 +73,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200) response[0].should.equal(200)
response[1].should.equal({ response[1].should.equal({
"Content-Encoding" => "deflate", "Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding" "Vary" => "Accept-Encoding",
"Content-Type" => "text/plain"
}) })
buf = '' buf = ''
response[2].each { |part| buf << part } response[2].each { |part| buf << part }
Expand All @@ -78,6 +91,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[1].should.equal({ response[1].should.equal({
"Content-Encoding" => "gzip", "Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding", "Vary" => "Accept-Encoding",
"Content-Type" => "text/plain"
}) })


buf = '' buf = ''
Expand All @@ -97,7 +111,8 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response[0].should.equal(200) response[0].should.equal(200)
response[1].should.equal({ response[1].should.equal({
"Content-Encoding" => "gzip", "Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding" "Vary" => "Accept-Encoding",
"Content-Type" => "text/plain"
}) })
buf = [] buf = []
inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 32) inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
Expand All @@ -111,42 +126,43 @@ class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, "Hello world!", "superzip") response = build_response(200, "Hello world!", "superzip")


response[0].should.equal(200) response[0].should.equal(200)
response[1].should.equal({ "Vary" => "Accept-Encoding" }) response[1].should.equal({ "Vary" => "Accept-Encoding", "Content-Type" => "text/plain" })
response[2].should.equal(["Hello world!"]) Enumerator.new(response[2]).to_a.should.equal(["Hello world!"])
end end


should "be able to skip when there is no response entity body" do should "be able to skip when there is no response entity body" do
response = build_response(304, [], "gzip") response = build_response(304, [], "gzip")


response[0].should.equal(304) response[0].should.equal(304)
response[1].should.equal({}) response[1].should.equal({})
response[2].should.equal([]) Enumerator.new(response[2]).to_a.should.equal([])
end end


should "handle the lack of an acceptable encoding" do should "handle the lack of an acceptable encoding" do
response1 = build_response(200, "Hello world!", "identity;q=0", "PATH_INFO" => "/") response1 = build_response(200, "Hello world!", "identity;q=0", "PATH_INFO" => "/")
response1[0].should.equal(406) response1[0].should.equal(406)
response1[1].should.equal({"Content-Type" => "text/plain", "Content-Length" => "71"}) 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 = build_response(200, "Hello world!", "identity;q=0", "SCRIPT_NAME" => "/foo", "PATH_INFO" => "/bar")
response2[0].should.equal(406) response2[0].should.equal(406)
response2[1].should.equal({"Content-Type" => "text/plain", "Content-Length" => "78"}) 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 end


should "handle gzip response with Last-Modified header" do should "handle gzip response with Last-Modified header" do
last_modified = Time.now.httpdate 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") 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[0].should.equal(200)
response[1].should.equal({ response[1].should.equal({
"Content-Encoding" => "gzip", "Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding", "Vary" => "Accept-Encoding",
"Last-Modified" => last_modified "Last-Modified" => last_modified,
"Content-Type" => "text/plain"
}) })


buf = '' buf = ''
Expand All @@ -158,13 +174,13 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end end


should "do nothing when no-transform Cache-Control directive present" do 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") 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[0].should.equal(200)
response[1].should.not.include "Content-Encoding" 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 end


should "do nothing when Content-Encoding already present" do should "do nothing when Content-Encoding already present" do
Expand Down

0 comments on commit bd43405

Please sign in to comment.