content_type response helper with mime type lookup and parameter support.
ResponseHelpers#content_type takes a media type and parameters and sets the Content-Type response header accordingly.
This commit is contained in:
parent
d343d27d87
commit
ccc19b0436
|
@ -100,7 +100,7 @@ This will render <tt>./views/index.haml</tt>
|
||||||
|
|
||||||
=== Sass
|
=== Sass
|
||||||
get '/stylesheet.css' do
|
get '/stylesheet.css' do
|
||||||
header 'Content-Type' => 'text/css; charset=utf-8'
|
content_type 'text/css', :charset => 'utf-8'
|
||||||
sass :stylesheet
|
sass :stylesheet
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ This works like Haml except you use <tt>erb</tt> instead of <tt>haml</tt>
|
||||||
|
|
||||||
=== Sass
|
=== 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
|
=== Builder
|
||||||
|
|
||||||
|
|
|
@ -405,7 +405,7 @@ module Sinatra
|
||||||
# underlying headers Hash. With a Hash argument, add or overwrite
|
# underlying headers Hash. With a Hash argument, add or overwrite
|
||||||
# existing response headers with the values provided:
|
# 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,
|
# 'Last-Modified' => Time.now.httpdate,
|
||||||
# 'X-UA-Compatible' => 'IE=edge'
|
# 'X-UA-Compatible' => 'IE=edge'
|
||||||
#
|
#
|
||||||
|
@ -416,6 +416,30 @@ module Sinatra
|
||||||
end
|
end
|
||||||
alias :header :headers
|
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
|
end
|
||||||
|
|
||||||
module RenderingHelpers
|
module RenderingHelpers
|
||||||
|
|
|
@ -113,6 +113,28 @@ context "Sinatra" do
|
||||||
|
|
||||||
end
|
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
|
specify "delegates HEAD requests to GET handlers" do
|
||||||
get '/invisible' do
|
get '/invisible' do
|
||||||
"I am invisible to the world"
|
"I am invisible to the world"
|
||||||
|
|
Loading…
Reference in New Issue