Skip to content
Browse files

content_type response helper with mime type lookup and parameter supp…

…ort.

ResponseHelpers#content_type takes a media type and parameters and sets
the Content-Type response header accordingly.
  • Loading branch information...
1 parent d343d27 commit ccc19b0436add4fedea1c009b80a35c573a8a558 @rtomayko rtomayko committed Apr 13, 2008
Showing with 50 additions and 4 deletions.
  1. +2 −2 README.rdoc
  2. +26 −2 lib/sinatra.rb
  3. +22 −0 test/app_test.rb
View
4 README.rdoc
@@ -100,7 +100,7 @@ This will render <tt>./views/index.haml</tt>
=== 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 <tt>erb</tt> instead of <tt>haml</tt>
=== Sass
-This works like Haml except you use <tt>sass</tt> instead of <tt>haml</tt>. It's also a good idea to add <tt>header 'Content-Type' => 'text/css; charset=utf-8'</tt> before your call to <tt>sass</tt> so Sinatra returns the proper content type header with the file.
+This works like Haml except you use <tt>sass</tt> instead of <tt>haml</tt>. It's also a good idea to add <tt>content_type 'text/css', :charset => 'utf-8'</tt> before your call to <tt>sass</tt> so Sinatra returns the proper content type header with the file.
=== Builder
View
28 lib/sinatra.rb
@@ -405,7 +405,7 @@ def redirect(path, *args)
# 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 @@ def headers(header = nil)
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'
+ # "<h1>Hello World</h1>"
+ # 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={})
View
22 test/app_test.rb
@@ -113,6 +113,28 @@
end
+ specify "should easily set response Content-Type" do
+ get '/foo.html' do
+ content_type 'text/html', :charset => 'utf-8'
+ "<h1>Hello, World</h1>"
+ end
+
+ get_it '/foo.html'
+ should.be.ok
+ headers['Content-Type'].should.equal 'text/html;charset=utf-8'
+ body.should.equal '<h1>Hello, World</h1>'
+
+ get '/foo.xml' do
+ content_type :xml
+ "<feed></feed>"
+ end
+
+ get_it '/foo.xml'
+ should.be.ok
+ headers['Content-Type'].should.equal 'application/xml'
+ body.should.equal '<feed></feed>'
+ end
+
specify "delegates HEAD requests to GET handlers" do
get '/invisible' do
"I am invisible to the world"

0 comments on commit ccc19b0

Please sign in to comment.
Something went wrong with that request. Please try again.