Skip to content
This repository
Browse code

Removed all ignore_headers before writing to the cache

By default, this will strip the Set-Cookie response header before storing a
cacheable response.
  • Loading branch information...
commit d668c883e93c556a1ef02f019ebfcb78df3fe75e 1 parent 566f5d5
Ryan McGeary authored February 10, 2012
8  lib/rack/cache/context.rb
@@ -260,6 +260,7 @@ def fetch
260 260
 
261 261
     # Write the response to the cache.
262 262
     def store(response)
  263
+      strip_ignore_headers(response)
263 264
       metastore.store(@request, response, entitystore)
264 265
       response.headers['Age'] = response.age.to_s
265 266
     rescue Exception => e
@@ -269,6 +270,13 @@ def store(response)
269 270
       record :store
270 271
     end
271 272
 
  273
+    # Remove all ignored response headers before writing to the cache.
  274
+    def strip_ignore_headers(response)
  275
+      ignore_headers.each do |name|
  276
+        response.headers.delete(name)
  277
+      end
  278
+    end
  279
+
272 280
     def log_error(exception)
273 281
       @env['rack.errors'].write("cache error: #{exception.message}\n#{exception.backtrace.join("\n")}\n")
274 282
     end
20  test/context_test.rb
@@ -85,6 +85,26 @@
85 85
     response.headers['Cache-Control'].should.equal 'private'
86 86
   end
87 87
 
  88
+  it 'does remove Set-Cookie response header from a cacheable response' do
  89
+    respond_with 200, 'Cache-Control' => 'public', 'ETag' => '"FOO"', 'Set-Cookie' => 'TestCookie=OK'
  90
+    get '/'
  91
+
  92
+    app.should.be.called
  93
+    response.should.be.ok
  94
+    cache.trace.should.include :store
  95
+    response.headers['Set-Cookie'].should.be.nil
  96
+  end
  97
+
  98
+  it 'does not remove Set-Cookie response header from a private response' do
  99
+    respond_with 200, 'Cache-Control' => 'private', 'Set-Cookie' => 'TestCookie=OK'
  100
+    get '/'
  101
+
  102
+    app.should.be.called
  103
+    response.should.be.ok
  104
+    cache.trace.should.not.include :store
  105
+    response.headers['Set-Cookie'].should.equal 'TestCookie=OK'
  106
+  end
  107
+
88 108
   it 'responds with 304 when If-Modified-Since matches Last-Modified' do
89 109
     timestamp = Time.now.httpdate
90 110
     respond_with do |req,res|

0 notes on commit d668c88

Please sign in to comment.
Something went wrong with that request. Please try again.