Permalink
Browse files

Refactored tests a bit more.

  • Loading branch information...
1 parent 2085a5f commit 8004fc13ce0a99bb70fa5f60c6744bbc132432b8 @jakubpawlowicz committed Dec 22, 2012
Showing with 67 additions and 44 deletions.
  1. +67 −44 test/spec_deflater.rb
View
@@ -9,10 +9,6 @@
describe Rack::Deflater do
::Enumerator = ::Enumerable::Enumerator unless Object.const_defined?(:Enumerator)
- def deflater(app, options = {})
- Rack::Lint.new Rack::Deflater.new(app, options)
- end
-
def build_response(status, body, accept_encoding, options = {})
body = [body] if body.respond_to? :to_str
app = lambda do |env|
@@ -21,12 +17,9 @@ def build_response(status, body, accept_encoding, options = {})
res
end
request = Rack::MockRequest.env_for("", (options['request_headers'] || {}).merge("HTTP_ACCEPT_ENCODING" => accept_encoding))
- deflater(app, options['deflater_options'] || {}).call(request)
- end
+ deflater = Rack::Lint.new Rack::Deflater.new(app, options['deflater_options'] || {})
- def inflate(buf)
- inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
- inflater.inflate(buf) << inflater.finish
+ deflater.call(request)
end
##
@@ -66,12 +59,15 @@ def verify(expected_status, expected_body, accept_encoding, options = {}, &block
yield(status, headers, body) if block_given?
# verify body
+ return if options['skip_body_verify']
+
body_text = ''
body.each { |part| body_text << part }
deflated_body = case expected_encoding
when 'deflate'
- inflate(body_text)
+ inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
+ inflater.inflate(body_text) << inflater.finish
when 'gzip'
io = StringIO.new(body_text)
gz = Zlib::GzipReader.new(io)
@@ -102,19 +98,20 @@ class << body; def each; yield("foo"); yield("bar"); end; end
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 = []
- inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
- response[2].each { |part| buf << inflater.inflate(part) }
- buf << inflater.finish
- buf.delete_if { |part| part.empty? }
- buf.join.should.equal("foobar")
+ verify(200, body, 'deflate', { 'skip_body_verify' => true }) do |status, headers, body|
+ headers.should.equal({
+ "Content-Encoding" => "deflate",
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
+ })
+
+ buf = []
+ inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
+ body.each { |part| buf << inflater.inflate(part) }
+ buf << inflater.finish
+
+ buf.delete_if { |part| part.empty? }.join.should.equal("foobar")
+ end
end
# TODO: This is really just a special case of the above...
@@ -145,20 +142,20 @@ class << body; def each; yield("foo"); yield("bar"); end; end
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 = []
- inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
- response[2].each { |part| buf << inflater.inflate(part) }
- buf << inflater.finish
- buf.delete_if { |part| part.empty? }
- buf.join.should.equal("foobar")
+ verify(200, body, 'gzip', { 'skip_body_verify' => true }) do |status, headers, body|
+ headers.should.equal({
+ "Content-Encoding" => "gzip",
+ "Vary" => "Accept-Encoding",
+ "Content-Type" => "text/plain"
+ })
+
+ buf = []
+ inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
+ body.each { |part| buf << inflater.inflate(part) }
+ buf << inflater.finish
+
+ buf.delete_if { |part| part.empty? }.join.should.equal("foobar")
+ end
end
should "be able to fallback to no deflation" do
@@ -180,20 +177,46 @@ class << body; def each; yield("foo"); yield("bar"); end; end
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."
+ options1 = {
+ 'app_status' => 200,
+ 'app_body' => body,
+ 'request_headers' => {
+ "PATH_INFO" => "/"
+ }
+ }
+ options2 = {
+ 'app_status' => 200,
+ 'app_body' => body,
+ 'request_headers' => {
+ "PATH_INFO" => "/foo/bar"
+ }
+ }
- verify(406, not_found_body1, 'identity;q=0', { 'app_status' => 200, 'app_body' => body, 'request_headers' => { "PATH_INFO" => "/" } }) do |status, headers, body|
- headers.should.equal({"Content-Type" => "text/plain", "Content-Length" => not_found_body1.length.to_s})
+ verify(406, not_found_body1, 'identity;q=0', options1) do |status, headers, body|
+ headers.should.equal({
+ "Content-Type" => "text/plain",
+ "Content-Length" => not_found_body1.length.to_s
+ })
end
- verify(406, not_found_body2, 'identity;q=0', { 'app_status' => 200, 'app_body' => body, 'request_headers' => { "PATH_INFO" => "/foo/bar" } }) do |status, headers, body|
- headers.should.equal({"Content-Type" => "text/plain", "Content-Length" => not_found_body2.length.to_s})
+ verify(406, not_found_body2, 'identity;q=0', options2) do |status, headers, body|
+ 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
last_modified = Time.now.httpdate
+ options = {
+ 'response_headers' => {
+ "Content-Type" => "text/plain",
+ "Last-Modified" => last_modified
+ }
+ }
- verify(200, 'Hello World!', 'gzip', { 'response_headers' => { "Content-Type" => "text/plain", "Last-Modified" => last_modified } }) do |status, headers, body|
+ verify(200, 'Hello World!', 'gzip', options) do |status, headers, body|
headers.should.equal({
"Content-Encoding" => "gzip",
"Vary" => "Accept-Encoding",
@@ -285,7 +308,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
verify(200, 'Hello World!', 'gzip', options)
end
- should "skip processing if path do not match :include" do
+ should "do nothing if path do not match :include" do
options = {
'deflater_options' => {
'include' => /^\/foo\/bar$/
@@ -294,7 +317,7 @@ class << body; def each; yield("foo"); yield("bar"); end; end
verify(200, 'Hello World!', { 'gzip' => nil }, options)
end
- should "skip processing if path matches :exclude" do
+ should "do nothing if path matches :exclude" do
options = {
'deflater_options' => {
'exclude' => /^\/$/

0 comments on commit 8004fc1

Please sign in to comment.