describe Rack::Contrib::ContentHash do let (:response) { [200, {'Location' => 'lolwhat'}, 'foobar-body'] } let (:response_clone) { [200, {'Location' => 'lolwhat'}, 'foobar-body'] } describe "#initialize" do it "accepts an app" do expect { ware = Rack::Contrib::ContentHash.new nil }.not_to raise_error end it "requires an app" do expect { ware = Rack::Contrib::ContentHash.new }.to raise_error end it "accepts a hash of enabled algorithms" do expect { ware = Rack::Contrib::ContentHash.new nil, md5: true }.not_to raise_error end end describe "#call" do it "requires an env" do ware = Rack::Contrib::ContentHash.new nil expect { ware.call }.to raise_error end it "calls the app's .call method" do app = double(nil, call: response) env = {'foo' => 'bar'} ware = Rack::Contrib::ContentHash.new app ware.call env app.should have_received(:call).once.with(env) end context "all hashing is disabled" do it "returns the same data from the app" do app = double(nil, call: response) env = {} ware = Rack::Contrib::ContentHash.new app ware.call(env).should eq(response_clone) end end context "MD5 hashing is enabled" do it "hashes the body, and adds a Content-MD5 header" do app = double(nil, call: response) env = {} ware = Rack::Contrib::ContentHash.new app, md5: true response_clone[1]['Content-MD5'] = '58110b6c0b6880db960c1f19e73feb9a' ware.call(env).should eq(response_clone) end end context "SHA1 hashing is enabled" do it "hashes the body, and adds a Content-SHA1 header" do app = double(nil, call: response) env = {} ware = Rack::Contrib::ContentHash.new app, sha1: true response_clone[1]['Content-SHA1'] = 'e04ce9c63d277c7714f71d916461959710b761f2' ware.call(env).should eq(response_clone) end end context "SHA1 and MD5 hashing are both enabled" do it "hashes the body, and adds a Content-SHA1 and Content-MD5 header" do app = double(nil, call: response) env = {} ware = Rack::Contrib::ContentHash.new app, sha1: true, md5: true response_clone[1]['Content-SHA1'] = 'e04ce9c63d277c7714f71d916461959710b761f2' response_clone[1]['Content-MD5'] = '58110b6c0b6880db960c1f19e73feb9a' ware.call(env).should eq(response_clone) end end end end