test/spec_deflater.rb in rack-1.2.8 vs test/spec_deflater.rb in rack-1.3.0.beta
- old
+ new
@@ -1,9 +1,10 @@
require 'stringio'
require 'time' # for Time#httpdate
require 'rack/deflater'
require 'rack/mock'
+require 'zlib'
describe Rack::Deflater do
def build_response(status, body, accept_encoding, headers = {})
body = [body] if body.respond_to? :to_str
app = lambda { |env| [status, {}, body] }
@@ -11,10 +12,15 @@
response = Rack::Deflater.new(app).call(request)
return response
end
+ def inflate(buf)
+ inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
+ inflater.inflate(buf) << inflater.finish
+ 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")
@@ -24,13 +30,32 @@
"Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding"
})
buf = ''
response[2].each { |part| buf << part }
- buf.should.equal("K\313\317OJ,\002\000")
+ inflate(buf).should.equal("foobar")
end
+ should "flush deflated chunks to the client as they become ready" 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"
+ })
+ 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.should.equal(%w(foo bar))
+ 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)
@@ -38,11 +63,11 @@
"Content-Encoding" => "deflate",
"Vary" => "Accept-Encoding"
})
buf = ''
response[2].each { |part| buf << part }
- buf.should.equal("\363H\315\311\311W(\317/\312IQ\004\000")
+ inflate(buf).should.equal("Hello world!")
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
@@ -59,9 +84,28 @@
response[2].each { |part| buf << part }
io = StringIO.new(buf)
gz = Zlib::GzipReader.new(io)
gz.read.should.equal("foobar")
gz.close
+ end
+
+ should "flush gzipped chunks to the client as they become ready" 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"
+ })
+ 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.should.equal(%w(foo bar))
end
should "be able to fallback to no deflation" do
response = build_response(200, "Hello world!", "superzip")