Permalink
Browse files

Fix body only caching headers

  • Loading branch information...
1 parent b44ff3d commit 87512b86120d75f829ac26e7d0b39c691075322e @josh josh committed May 23, 2011
Showing with 21 additions and 12 deletions.
  1. +5 −11 lib/sprockets/server.rb
  2. +16 −1 test/test_server.rb
View
@@ -169,24 +169,18 @@ def not_modified_response(asset, env)
# Returns a 200 OK response tuple
def ok_response(asset, env)
- if body_only?(env) && asset.dependencies?
- body = asset.body
- [ 206, {
- "Content-Range" => "bytes #{body.length-1}-#{asset.length-1}/#{asset.length}",
- "Content-Type" => asset.content_type,
- "Content-Length" => body.length.to_s,
- "Cache-Control" => "no-cache"
- }, [body] ]
+ if body_only?(env)
+ [ 200, headers(env, asset, asset.body.length), [asset.body] ]
else
- [ 200, headers(asset, env), asset ]
+ [ 200, headers(env, asset, asset.length), asset ]
end
end
- def headers(asset, env)
+ def headers(env, asset, length)
Hash.new.tap do |headers|
# Set content type and length headers
headers["Content-Type"] = asset.content_type
- headers["Content-Length"] = asset.length.to_s
+ headers["Content-Length"] = length.to_s
headers["Content-MD5"] = asset.digest
# Set caching headers
View
@@ -39,6 +39,7 @@ def app
get "/javascripts/foo.js?body=1"
assert_equal 200, last_response.status
assert_equal "var foo;\n", last_response.body
+ assert_equal "9", last_response.headers['Content-Length']
end
test "serve single source file from indexed environment" do
@@ -54,9 +55,10 @@ def app
test "serve source file body that has dependencies" do
get "/javascripts/application.js?body=true"
- assert_equal 206, last_response.status
+ assert_equal 200, last_response.status
assert_equal "\n(function() {\n application.boot();\n})();\n",
last_response.body
+ assert_equal "43", last_response.headers['Content-Length']
end
test "serve source with content type headers" do
@@ -131,6 +133,19 @@ def app
assert_equal nil, last_response.headers['Content-Length']
end
+ test "not modified partial response when etags match" do
+ get "/javascripts/application.js?body=1"
+ assert_equal 200, last_response.status
+ etag = last_response.headers['ETag']
+
+ get "/javascripts/application.js?body=1", {},
+ 'HTTP_IF_NONE_MATCH' => etag
+
+ assert_equal 304, last_response.status
+ assert_equal nil, last_response.headers['Content-Type']
+ assert_equal nil, last_response.headers['Content-Length']
+ end
+
test "if sources didnt change the server shouldnt rebundle" do
get "/javascripts/application.js"
asset_before = @env["application.js"]

0 comments on commit 87512b8

Please sign in to comment.