Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #227 from jnicklas/content_type_fix
Don't set content-type on responses without body
  • Loading branch information
rkh committed Aug 29, 2011
2 parents 2c460e3 + a25c014 commit 4425e37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rack/content_type.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/spec_content_type.rb
Expand Up @@ -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 4425e37

Please sign in to comment.