Permalink
Browse files

Don't set content-type on responses without body

304, 204, 1xx responses should not have a content type header. Just like the ContentLength middleware, the ContentType middleware should not set such a header.
  • Loading branch information...
1 parent 2c460e3 commit a25c014389c767c25e89e4bdab5fe9a9aa9de562 Jonas Nicklas and Kim Burgestrand committed Aug 29, 2011
Showing with 13 additions and 1 deletion.
  1. +7 −1 lib/rack/content_type.rb
  2. +6 −0 test/spec_content_type.rb
View
@@ -9,14 +9,20 @@ module Rack
#
# When no content type argument is provided, "text/html" is assumed.
class ContentType
+ include Rack::Utils
+
def initialize(app, content_type = "text/html")
@app, @content_type = app, content_type
end
def call(env)
status, headers, body = @app.call(env)
headers = Utils::HeaderHash.new(headers)
- headers['Content-Type'] ||= @content_type
+
+ unless STATUS_WITH_NO_ENTITY_BODY.include?(status)
+ headers['Content-Type'] ||= @content_type
+ end
+
[status, headers, body]
end
end
@@ -26,4 +26,10 @@
headers.to_a.select { |k,v| k.downcase == "content-type" }.
should.equal [["CONTENT-Type","foo/bar"]]
end
+
+ should "not set Content-Type on 304 responses" do
+ app = lambda { |env| [304, {}, []] }
+ response = Rack::ContentType.new(app, "text/html").call({})
+ response[1]['Content-Type'].should.equal nil
+ end
end

0 comments on commit a25c014

Please sign in to comment.