diff options
author | Lars Gierth <lars.gierth@gmail.com> | 2011-12-28 01:59:13 +0100 |
---|---|---|
committer | Lars Gierth <lars@soundcloud.com> | 2012-03-31 20:23:55 +0200 |
commit | bd43405d79dfc3c750e64fc16a0398ca6c07850a (patch) | |
tree | 943d4b8f9e8e337a3b919239a0042f873161a695 /test/spec_deflater.rb | |
parent | 284000f6769c12d68ab737d766c26fefe3bd91b6 (diff) | |
download | rack-bd43405d79dfc3c750e64fc16a0398ca6c07850a.tar.gz |
Wrap test apps in Rack::Lint and fix uncovered errors (second pass)
Update tests for Config, ContentLength, ContentType and Deflater.
Diffstat (limited to 'test/spec_deflater.rb')
-rw-r--r-- | test/spec_deflater.rb | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/test/spec_deflater.rb b/test/spec_deflater.rb index c125ccf7..46526669 100644 --- a/test/spec_deflater.rb +++ b/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 @@ describe Rack::Deflater do 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 @@ describe Rack::Deflater do 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 @@ describe Rack::Deflater do 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 @@ describe Rack::Deflater do response[1].should.equal({ "Content-Encoding" => "gzip", "Vary" => "Accept-Encoding", + "Content-Type" => "text/plain" }) buf = '' @@ -97,7 +111,8 @@ describe Rack::Deflater do 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 @@ describe Rack::Deflater do 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 @@ describe Rack::Deflater do 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 @@ describe Rack::Deflater do 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 |