Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rewrote deflater tests to be more concise.

  • Loading branch information...
commit 649629c555bdde4a767d181ab39e9064b953a989 1 parent dc5927f
@jakubpawlowicz authored
Showing with 97 additions and 112 deletions.
  1. +97 −112 test/spec_deflater.rb
View
209 test/spec_deflater.rb
@@ -31,21 +31,42 @@ def inflate(buf)
inflater.inflate(buf) << inflater.finish
end
+ def verify(response, body_text, options = {}, &block)
+ response[0].should.equal(options['status'] || 200)
+
+ text_body = ''
+ response[2].each { |part| text_body << part }
+
+ deflated_body = case options['deflate']
+ when 'deflate'
+ inflate(text_body)
+ when 'gzip'
+ io = StringIO.new(text_body)
+ gz = Zlib::GzipReader.new(io)
+ temp_body = gz.read
+ gz.close
+ temp_body
+ else
+ text_body
+ end
+
+ deflated_body.should.equal(body_text)
+
+ block.yield(response[1]) if block_given?
+ end
+
should "be able to deflate bodies that respond to each" do
body = Object.new
class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, body, "deflate")
-
- response[0].should.equal(200)
- response[1].should.equal({
- "Content-Encoding" => "deflate",
- "Vary" => "Accept-Encoding",
- "Content-Type" => "text/plain"
- })
- buf = ''
- response[2].each { |part| buf << part }
- inflate(buf).should.equal("foobar")
+ verify(response, 'foobar', { 'deflate' => 'deflate' }) do |headers|
+ headers.should.equal({
+ "Content-Encoding" => "deflate",
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
+ })
+ end
end
should "flush deflated chunks to the client as they become ready" do
@@ -53,7 +74,6 @@ class << body; def each; yield("foo"); yield("bar"); end; end
class << body; def each; yield("foo"); yield("bar"); end; end
response = build_response(200, body, "deflate")
-
response[0].should.equal(200)
response[1].should.equal({
"Content-Encoding" => "deflate",
@@ -70,38 +90,29 @@ class << body; def each; yield("foo"); yield("bar"); end; end
# TODO: This is really just a special case of the above...
should "be able to deflate String bodies" do
- response = build_response(200, "Hello world!", "deflate")
-
- response[0].should.equal(200)
- response[1].should.equal({
- "Content-Encoding" => "deflate",
- "Vary" => "Accept-Encoding",
- "Content-Type" => "text/plain"
- })
- buf = ''
- response[2].each { |part| buf << part }
- inflate(buf).should.equal("Hello world!")
+ body = "Hello world!"
+ response = build_response(200, body, "deflate")
+ verify(response, body, { 'deflate' => 'deflate' }) do |headers|
+ headers.should.equal({
+ "Content-Encoding" => "deflate",
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
+ })
+ end
end
should "be able to gzip bodies that respond to each" do
body = Object.new
class << body; def each; yield("foo"); yield("bar"); end; end
- response = build_response(200, body, "gzip")
-
- response[0].should.equal(200)
- response[1].should.equal({
- "Content-Encoding" => "gzip",
- "Vary" => "Accept-Encoding",
- "Content-Type" => "text/plain"
- })
-
- buf = ''
- response[2].each { |part| buf << part }
- io = StringIO.new(buf)
- gz = Zlib::GzipReader.new(io)
- gz.read.should.equal("foobar")
- gz.close
+ response = build_response(200, 'foobar', "gzip")
+ verify(response, 'foobar', { 'deflate' => 'gzip' }) do |headers|
+ headers.should.equal({
+ "Content-Encoding" => "gzip",
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
+ })
+ end
end
should "flush gzipped chunks to the client as they become ready" do
@@ -125,84 +136,82 @@ class << body; def each; yield("foo"); yield("bar"); end; end
end
should "be able to fallback to no deflation" do
- response = build_response(200, "Hello world!", "superzip")
-
- response[0].should.equal(200)
- response[1].should.equal({ "Vary" => "Accept-Encoding", "Content-Type" => "text/plain" })
- Enumerator.new(response[2]).to_a.should.equal(["Hello world!"])
+ body = "Hello world!"
+ response = build_response(200, body, "superzip")
+ verify(response, body) do |headers|
+ headers.should.equal({ "Vary" => "Accept-Encoding", "Content-Type" => "text/plain" })
+ end
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({})
- Enumerator.new(response[2]).to_a.should.equal([])
+ verify(response, '', { 'status' => 304 }) do |headers|
+ headers.should.equal({})
+ end
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"})
- Enumerator.new(response1[2]).to_a.should.equal(["An acceptable encoding for the requested resource / could not be found."])
+ body = "Hello world!"
+ not_found_body1 = "An acceptable encoding for the requested resource / could not be found."
+ not_found_body2 = "An acceptable encoding for the requested resource /foo/bar could not be found."
+
+ response1 = build_response(200, body, "identity;q=0", "PATH_INFO" => "/")
+ verify(response1, not_found_body1, { 'status' => 406 }) do |headers|
+ headers.should.equal({"Content-Type" => "text/plain", "Content-Length" => not_found_body1.length.to_s})
+ end
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"})
- Enumerator.new(response2[2]).to_a.should.equal(["An acceptable encoding for the requested resource /foo/bar could not be found."])
+ verify(response2, not_found_body2, { 'status' => 406 }) do |headers|
+ headers.should.equal({"Content-Type" => "text/plain", "Content-Length" => not_found_body2.length.to_s})
+ end
end
should "handle gzip response with Last-Modified header" do
+ body = 'Hello World!'
last_modified = Time.now.httpdate
- app = lambda { |env| [200, { "Content-Type" => "text/plain", "Last-Modified" => last_modified }, ["Hello World!"]] }
+ app = lambda { |env| [200, { "Content-Type" => "text/plain", "Last-Modified" => last_modified }, [body]] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app).call(request)
- response[0].should.equal(200)
- response[1].should.equal({
- "Content-Encoding" => "gzip",
- "Vary" => "Accept-Encoding",
- "Last-Modified" => last_modified,
- "Content-Type" => "text/plain"
- })
-
- buf = ''
- response[2].each { |part| buf << part }
- io = StringIO.new(buf)
- gz = Zlib::GzipReader.new(io)
- gz.read.should.equal("Hello World!")
- gz.close
+ verify(response, body, { 'deflate' => 'gzip' }) do |headers|
+ headers.should.equal({
+ "Content-Encoding" => "gzip",
+ "Vary" => "Accept-Encoding",
+ "Last-Modified" => last_modified,
+ "Content-Type" => "text/plain"
+ })
+ end
end
should "do nothing when no-transform Cache-Control directive present" do
- app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Cache-Control' => 'no-transform'}, ['Hello World!']] }
+ body = 'Hello World!'
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Cache-Control' => 'no-transform'}, [body]] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app).call(request)
- response[0].should.equal(200)
- response[1].should.not.include "Content-Encoding"
- Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ verify(response, body) do |headers|
+ headers.should.not.include "Content-Encoding"
+ end
end
should "do nothing when Content-Encoding already present" do
- app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Encoding' => 'gzip'}, ['Hello World!']] }
+ body = 'Hello World!'
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Encoding' => 'gzip'}, [body]] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app).call(request)
- response[0].should.equal(200)
- Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ verify(response, body)
end
should "deflate when Content-Encoding is identity" do
- app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Encoding' => 'identity'}, ['Hello World!']] }
+ body = 'Hello World!'
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Encoding' => 'identity'}, [body]] }
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "deflate")
response = deflater(app).call(request)
- response[0].should.equal(200)
- buf = ''
- response[2].each { |part| buf << part }
- inflate(buf).should.equal("Hello World!")
+ verify(response, body, { 'deflate' => 'deflate' })
end
should "do nothing if body length is less than a given threshold" do
@@ -213,8 +222,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "min_content_length" => body.length + 1 }).call(request)
- response[0].should.equal(200)
- Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ verify(response, body)
end
should "do nothing if Content-Length is not given" do
@@ -225,8 +233,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "min_content_length" => body.length + 1 }).call(request)
- response[0].should.equal(200)
- Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ verify(response, body)
end
should "gzip response if body length is equal or longer than a given threshold" do
@@ -237,13 +244,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "min_content_length" => body.length }).call(request)
- response[0].should.equal(200)
- buf = ''
- response[2].each { |part| buf << part }
- io = StringIO.new(buf)
- gz = Zlib::GzipReader.new(io)
- gz.read.should.equal(body)
- gz.close
+ verify(response, body, { 'deflate' => 'gzip' })
end
should "process if path matches :include" do
@@ -254,13 +255,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "include" => /^\/$/ }).call(request)
- response[0].should.equal(200)
- buf = ''
- response[2].each { |part| buf << part }
- io = StringIO.new(buf)
- gz = Zlib::GzipReader.new(io)
- gz.read.should.equal(body)
- gz.close
+ verify(response, body, { 'deflate' => 'gzip' })
end
should "skip processing if path do not match :include" do
@@ -271,8 +266,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "include" => /^\/something$/ }).call(request)
- response[0].should.equal(200)
- Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ verify(response, body)
end
should "skip processing if path matches :exclude" do
@@ -283,8 +277,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "exclude" => /^\/$/ }).call(request)
- response[0].should.equal(200)
- Enumerator.new(response[2]).to_a.join.should.equal("Hello World!")
+ verify(response, body)
end
should "gzip response if path do not match :exclude" do
@@ -295,13 +288,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "exclude" => /^sth$/ }).call(request)
- response[0].should.equal(200)
- buf = ''
- response[2].each { |part| buf << part }
- io = StringIO.new(buf)
- gz = Zlib::GzipReader.new(io)
- gz.read.should.equal(body)
- gz.close
+ verify(response, body, { 'deflate' => 'gzip' })
end
should "do nothing if :skip_if lambda evaluates to true" do
@@ -315,8 +302,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "gzip")
response = deflater(app, { "skip_if" => skip_if }).call(request)
- response[0].should.equal(200)
- Enumerator.new(response[2]).to_a.join.should.equal(body_text)
+ verify(response, body_text)
end
should "deflate response if :skip_if lambda evaluates to false" do
@@ -330,7 +316,6 @@ class << body; def each; yield("foo"); yield("bar"); end; end
request = Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => "deflate")
response = deflater(app, { "skip_if" => skip_if }).call(request)
- response[0].should.equal(200)
- inflate(Enumerator.new(response[2]).to_a.join).should.equal(body_text)
+ verify(response, body_text, { 'deflate' => 'deflate' })
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.