diff --git a/README.rdoc b/README.rdoc index 4e27acd1..b79e4a7f 100644 --- a/README.rdoc +++ b/README.rdoc @@ -100,7 +100,7 @@ This will render ./views/index.haml === Sass get '/stylesheet.css' do - header 'Content-Type' => 'text/css; charset=utf-8' + content_type 'text/css', :charset => 'utf-8' sass :stylesheet end @@ -174,7 +174,7 @@ This works like Haml except you use erb instead of haml === Sass -This works like Haml except you use sass instead of haml. It's also a good idea to add header 'Content-Type' => 'text/css; charset=utf-8' before your call to sass so Sinatra returns the proper content type header with the file. +This works like Haml except you use sass instead of haml. It's also a good idea to add content_type 'text/css', :charset => 'utf-8' before your call to sass so Sinatra returns the proper content type header with the file. === Builder diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 4a4cf583..e43b18b9 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -405,7 +405,7 @@ module Sinatra # underlying headers Hash. With a Hash argument, add or overwrite # existing response headers with the values provided: # - # headers 'Content-Type' => "text/html; charset=utf-8", + # headers 'Content-Type' => "text/html;charset=utf-8", # 'Last-Modified' => Time.now.httpdate, # 'X-UA-Compatible' => 'IE=edge' # @@ -416,8 +416,32 @@ module Sinatra end alias :header :headers + # Set the content type of the response body (HTTP 'Content-Type' header). + # + # The +type+ argument may be an internet media type (e.g., 'text/html', + # 'application/xml+atom', 'image/png') or a Symbol key into the + # Rack::File::MIME_TYPES table. + # + # Media type parameters, such as "charset", may also be specified using the + # optional hash argument: + # + # get '/foo.html' do + # content_type 'text/html', :charset => 'utf-8' + # "

Hello World

" + # end + # + def content_type(type, params={}) + type = Rack::File::MIME_TYPES[type.to_s] if type.kind_of?(Symbol) + fail "Invalid or undefined media_type: #{type}" if type.nil? + if params.any? + params = params.collect { |kv| "%s=%s" % kv }.join(', ') + type = [ type, params ].join(";") + end + response.header['Content-Type'] = type + end + end - + module RenderingHelpers def render(renderer, template, options={}) diff --git a/test/app_test.rb b/test/app_test.rb index 8a109538..618e1ed4 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -113,6 +113,28 @@ context "Sinatra" do end + specify "should easily set response Content-Type" do + get '/foo.html' do + content_type 'text/html', :charset => 'utf-8' + "

Hello, World

" + end + + get_it '/foo.html' + should.be.ok + headers['Content-Type'].should.equal 'text/html;charset=utf-8' + body.should.equal '

Hello, World

' + + get '/foo.xml' do + content_type :xml + "" + end + + get_it '/foo.xml' + should.be.ok + headers['Content-Type'].should.equal 'application/xml' + body.should.equal '' + end + specify "delegates HEAD requests to GET handlers" do get '/invisible' do "I am invisible to the world"