Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit a25c014389c767c25e89e4bdab5fe9a9aa9de562 1 parent 2c460e3
Jonas Nicklas and Kim Burgestrand authored
Showing with 13 additions and 1 deletion.
  1. +7 −1 lib/rack/content_type.rb
  2. +6 −0 test/spec_content_type.rb
View
8 lib/rack/content_type.rb
@@ -9,6 +9,8 @@ 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
@@ -16,7 +18,11 @@ def initialize(app, content_type = "text/html")
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
View
6 test/spec_content_type.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.