Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #227 from jnicklas/content_type_fix

Don't set content-type on responses without body
  • Loading branch information...
commit 4425e374c4d1aec2aa13cc16556b577aa5df1b09 2 parents 2c460e3 + a25c014
@rkh rkh 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.