提交 e4ba720c 编写于 作者: A Aaron Patterson

stop calling deprecated methods

We should be asking the mime type method for the mime objects rather
than via const lookup
上级 efc6dd55
...@@ -7,7 +7,7 @@ def self.generate_method_for_mime(mime) ...@@ -7,7 +7,7 @@ def self.generate_method_for_mime(mime)
const = sym.upcase const = sym.upcase
class_eval <<-RUBY, __FILE__, __LINE__ + 1 class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{sym}(*args, &block) # def html(*args, &block) def #{sym}(*args, &block) # def html(*args, &block)
custom(Mime::#{const}, *args, &block) # custom(Mime::HTML, *args, &block) custom(Mime::Type[:#{const}], *args, &block) # custom(Mime::Type[:HTML], *args, &block)
end # end end # end
RUBY RUBY
end end
...@@ -25,7 +25,7 @@ def #{sym}(*args, &block) # def html(*args, &block) ...@@ -25,7 +25,7 @@ def #{sym}(*args, &block) # def html(*args, &block)
def method_missing(symbol, &block) def method_missing(symbol, &block)
const_name = symbol.upcase const_name = symbol.upcase
unless Mime.const_defined?(const_name) unless Mime::Type.registered?(const_name)
raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \ raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \
"http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads. " \ "http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads. " \
"If you meant to respond to a variant like :tablet or :phone, not a custom format, " \ "If you meant to respond to a variant like :tablet or :phone, not a custom format, " \
...@@ -33,7 +33,7 @@ def method_missing(symbol, &block) ...@@ -33,7 +33,7 @@ def method_missing(symbol, &block)
"format.html { |html| html.tablet { ... } }" "format.html { |html| html.tablet { ... } }"
end end
mime_constant = Mime.const_get(const_name) mime_constant = Mime::Type[const_name]
if Mime::SET.include?(mime_constant) if Mime::SET.include?(mime_constant)
AbstractController::Collector.generate_method_for_mime(mime_constant) AbstractController::Collector.generate_method_for_mime(mime_constant)
......
...@@ -55,7 +55,7 @@ def render_to_body(options = {}) ...@@ -55,7 +55,7 @@ def render_to_body(options = {})
# Returns Content-Type of rendered content # Returns Content-Type of rendered content
# :api: public # :api: public
def rendered_format def rendered_format
Mime::TEXT Mime::Type[:TEXT]
end end
DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %i( DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %i(
......
...@@ -229,14 +229,14 @@ def initialize(mimes, variant = nil) ...@@ -229,14 +229,14 @@ def initialize(mimes, variant = nil)
@responses = {} @responses = {}
@variant = variant @variant = variant
mimes.each { |mime| @responses["Mime::#{mime.upcase}".constantize] = nil } mimes.each { |mime| @responses[Mime::Type[mime.to_sym.upcase]] = nil }
end end
def any(*args, &block) def any(*args, &block)
if args.any? if args.any?
args.each { |type| send(type, &block) } args.each { |type| send(type, &block) }
else else
custom(Mime::ALL, &block) custom(Mime::Type[:ALL], &block)
end end
end end
alias :all :any alias :all :any
...@@ -251,7 +251,7 @@ def custom(mime_type, &block) ...@@ -251,7 +251,7 @@ def custom(mime_type, &block)
end end
def response def response
response = @responses.fetch(format, @responses[Mime::ALL]) response = @responses.fetch(format, @responses[Mime::Type[:ALL]])
if response.is_a?(VariantCollector) # `format.html.phone` - variant inline syntax if response.is_a?(VariantCollector) # `format.html.phone` - variant inline syntax
response.variant response.variant
elsif response.nil? || response.arity == 0 # `format.html` - just a format, call its block elsif response.nil? || response.arity == 0 # `format.html` - just a format, call its block
......
...@@ -68,11 +68,11 @@ def self._render_with_renderer_method_name(key) ...@@ -68,11 +68,11 @@ def self._render_with_renderer_method_name(key)
# ActionController::Renderers.add :csv do |obj, options| # ActionController::Renderers.add :csv do |obj, options|
# filename = options[:filename] || 'data' # filename = options[:filename] || 'data'
# str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s # str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
# send_data str, type: Mime::CSV, # send_data str, type: Mime::Type[:CSV],
# disposition: "attachment; filename=#{filename}.csv" # disposition: "attachment; filename=#{filename}.csv"
# end # end
# #
# Note that we used Mime::CSV for the csv mime type as it comes with Rails. # Note that we used Mime::Type[:CSV] for the csv mime type as it comes with Rails.
# For a custom renderer, you'll need to register a mime type with # For a custom renderer, you'll need to register a mime type with
# <tt>Mime::Type.register</tt>. # <tt>Mime::Type.register</tt>.
# #
...@@ -116,24 +116,24 @@ module All ...@@ -116,24 +116,24 @@ module All
json = json.to_json(options) unless json.kind_of?(String) json = json.to_json(options) unless json.kind_of?(String)
if options[:callback].present? if options[:callback].present?
if content_type.nil? || content_type == Mime::JSON if content_type.nil? || content_type == Mime::Type[:JSON]
self.content_type = Mime::JS self.content_type = Mime::Type[:JS]
end end
"/**/#{options[:callback]}(#{json})" "/**/#{options[:callback]}(#{json})"
else else
self.content_type ||= Mime::JSON self.content_type ||= Mime::Type[:JSON]
json json
end end
end end
add :js do |js, options| add :js do |js, options|
self.content_type ||= Mime::JS self.content_type ||= Mime::Type[:JS]
js.respond_to?(:to_js) ? js.to_js(options) : js js.respond_to?(:to_js) ? js.to_js(options) : js
end end
add :xml do |xml, options| add :xml do |xml, options|
self.content_type ||= Mime::XML self.content_type ||= Mime::Type[:XML]
xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml
end end
end end
......
...@@ -64,7 +64,7 @@ def _render_in_priorities(options) ...@@ -64,7 +64,7 @@ def _render_in_priorities(options)
end end
def _set_html_content_type def _set_html_content_type
self.content_type = Mime::HTML.to_s self.content_type = Mime::Type[:HTML].to_s
end end
def _set_rendered_content_type(format) def _set_rendered_content_type(format)
......
...@@ -34,7 +34,7 @@ def initialize(env, session) ...@@ -34,7 +34,7 @@ def initialize(env, session)
self.session = session self.session = session
self.session_options = TestSession::DEFAULT_OPTIONS self.session_options = TestSession::DEFAULT_OPTIONS
@custom_param_parsers = { @custom_param_parsers = {
Mime::XML => lambda { |raw_post| Hash.from_xml(raw_post)['hash'] } Mime::Type[:XML] => lambda { |raw_post| Hash.from_xml(raw_post)['hash'] }
} }
end end
...@@ -402,7 +402,7 @@ def xml_http_request(*args) ...@@ -402,7 +402,7 @@ def xml_http_request(*args)
MSG MSG
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
@request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ') @request.env['HTTP_ACCEPT'] ||= [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], 'text/xml', Mime::Type[:ALL]].join(', ')
__send__(*args).tap do __send__(*args).tap do
@request.env.delete 'HTTP_X_REQUESTED_WITH' @request.env.delete 'HTTP_X_REQUESTED_WITH'
@request.env.delete 'HTTP_ACCEPT' @request.env.delete 'HTTP_ACCEPT'
...@@ -505,7 +505,7 @@ def process(action, *args) ...@@ -505,7 +505,7 @@ def process(action, *args)
if xhr if xhr
@request.set_header 'HTTP_X_REQUESTED_WITH', 'XMLHttpRequest' @request.set_header 'HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'
@request.fetch_header('HTTP_ACCEPT') do |k| @request.fetch_header('HTTP_ACCEPT') do |k|
@request.set_header k, [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ') @request.set_header k, [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], 'text/xml', Mime::Type[:ALL]].join(', ')
end end
end end
......
...@@ -66,9 +66,9 @@ def formats ...@@ -66,9 +66,9 @@ def formats
elsif use_accept_header && valid_accept_header elsif use_accept_header && valid_accept_header
accepts accepts
elsif xhr? elsif xhr?
[Mime::JS] [Mime::Type[:JS]]
else else
[Mime::HTML] [Mime::Type[:HTML]]
end end
set_header k, v set_header k, v
end end
...@@ -134,14 +134,14 @@ def formats=(extensions) ...@@ -134,14 +134,14 @@ def formats=(extensions)
# #
def negotiate_mime(order) def negotiate_mime(order)
formats.each do |priority| formats.each do |priority|
if priority == Mime::ALL if priority == Mime::Type[:ALL]
return order.first return order.first
elsif order.include?(priority) elsif order.include?(priority)
return priority return priority
end end
end end
order.include?(Mime::ALL) ? format : nil order.include?(Mime::Type[:ALL]) ? format : nil
end end
protected protected
......
...@@ -59,11 +59,11 @@ def const_defined?(sym, inherit = true) ...@@ -59,11 +59,11 @@ def const_defined?(sym, inherit = true)
ActiveSupport::Deprecation.warn <<-eow ActiveSupport::Deprecation.warn <<-eow
Accessing mime types via constants is deprecated. Please change: Accessing mime types via constants is deprecated. Please change:
`Mime::#{sym}` `Mime.const_defined?(#{sym})`
to: to:
`Mime::Type[:#{sym}]` `Mime::Type.registered?(:#{sym})`
eow eow
true true
else else
......
...@@ -361,7 +361,7 @@ def assign_default_content_type_and_charset! ...@@ -361,7 +361,7 @@ def assign_default_content_type_and_charset!
return if content_type return if content_type
ct = parse_content_type ct = parse_content_type
set_content_type(ct.mime_type || Mime::HTML.to_s, set_content_type(ct.mime_type || Mime::Type[:HTML].to_s,
ct.charset || self.class.default_charset) ct.charset || self.class.default_charset)
end end
......
...@@ -12,7 +12,7 @@ module Assertions ...@@ -12,7 +12,7 @@ module Assertions
include Rails::Dom::Testing::Assertions include Rails::Dom::Testing::Assertions
def html_document def html_document
@html_document ||= if @response.content_type === Mime::XML @html_document ||= if @response.content_type === Mime::Type[:XML]
Nokogiri::XML::Document.parse(@response.body) Nokogiri::XML::Document.parse(@response.body)
else else
Nokogiri::HTML::Document.parse(@response.body) Nokogiri::HTML::Document.parse(@response.body)
......
...@@ -354,7 +354,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false) ...@@ -354,7 +354,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false)
if xhr if xhr
headers ||= {} headers ||= {}
headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
headers['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ') headers['HTTP_ACCEPT'] ||= [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], 'text/xml', Mime::Type[:ALL]].join(', ')
end end
# this modifies the passed request_env directly # this modifies the passed request_env directly
......
...@@ -53,9 +53,9 @@ class TestCollector < ActiveSupport::TestCase ...@@ -53,9 +53,9 @@ class TestCollector < ActiveSupport::TestCase
collector.html collector.html
collector.text(:foo) collector.text(:foo)
collector.js(:bar) { :baz } collector.js(:bar) { :baz }
assert_equal [Mime::HTML, [], nil], collector.responses[0] assert_equal [Mime::Type[:HTML], [], nil], collector.responses[0]
assert_equal [Mime::TEXT, [:foo], nil], collector.responses[1] assert_equal [Mime::Type[:TEXT], [:foo], nil], collector.responses[1]
assert_equal [Mime::JS, [:bar]], collector.responses[2][0,2] assert_equal [Mime::Type[:JS], [:bar]], collector.responses[2][0,2]
assert_equal :baz, collector.responses[2][2].call assert_equal :baz, collector.responses[2][2].call
end end
end end
......
...@@ -65,7 +65,7 @@ def render_url ...@@ -65,7 +65,7 @@ def render_url
end end
def render_text_with_custom_content_type def render_text_with_custom_content_type
render body: "Hello!", content_type: Mime::RSS render body: "Hello!", content_type: Mime::Type[:RSS]
end end
def session_stuffing def session_stuffing
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
class OldContentTypeController < ActionController::Base class OldContentTypeController < ActionController::Base
# :ported: # :ported:
def render_content_type_from_body def render_content_type_from_body
response.content_type = Mime::RSS response.content_type = Mime::Type[:RSS]
render body: "hello world!" render body: "hello world!"
end end
...@@ -14,7 +14,7 @@ def render_defaults ...@@ -14,7 +14,7 @@ def render_defaults
# :ported: # :ported:
def render_content_type_from_render def render_content_type_from_render
render body: "hello world!", :content_type => Mime::RSS render body: "hello world!", :content_type => Mime::Type[:RSS]
end end
# :ported: # :ported:
...@@ -36,7 +36,7 @@ def render_default_for_builder ...@@ -36,7 +36,7 @@ def render_default_for_builder
end end
def render_change_for_builder def render_change_for_builder
response.content_type = Mime::HTML response.content_type = Mime::Type[:HTML]
render :action => "render_default_for_builder" render :action => "render_default_for_builder"
end end
...@@ -45,7 +45,7 @@ def render_default_content_types_for_respond_to ...@@ -45,7 +45,7 @@ def render_default_content_types_for_respond_to
format.html { render body: "hello world!" } format.html { render body: "hello world!" }
format.xml { render action: "render_default_content_types_for_respond_to" } format.xml { render action: "render_default_content_types_for_respond_to" }
format.js { render body: "hello world!" } format.js { render body: "hello world!" }
format.rss { render body: "hello world!", content_type: Mime::XML } format.rss { render body: "hello world!", content_type: Mime::Type[:XML] }
end end
end end
end end
...@@ -64,68 +64,68 @@ def setup ...@@ -64,68 +64,68 @@ def setup
def test_render_defaults def test_render_defaults
get :render_defaults get :render_defaults
assert_equal "utf-8", @response.charset assert_equal "utf-8", @response.charset
assert_equal Mime::TEXT, @response.content_type assert_equal Mime::Type[:TEXT], @response.content_type
end end
def test_render_changed_charset_default def test_render_changed_charset_default
with_default_charset "utf-16" do with_default_charset "utf-16" do
get :render_defaults get :render_defaults
assert_equal "utf-16", @response.charset assert_equal "utf-16", @response.charset
assert_equal Mime::TEXT, @response.content_type assert_equal Mime::Type[:TEXT], @response.content_type
end end
end end
# :ported: # :ported:
def test_content_type_from_body def test_content_type_from_body
get :render_content_type_from_body get :render_content_type_from_body
assert_equal Mime::RSS, @response.content_type assert_equal Mime::Type[:RSS], @response.content_type
assert_equal "utf-8", @response.charset assert_equal "utf-8", @response.charset
end end
# :ported: # :ported:
def test_content_type_from_render def test_content_type_from_render
get :render_content_type_from_render get :render_content_type_from_render
assert_equal Mime::RSS, @response.content_type assert_equal Mime::Type[:RSS], @response.content_type
assert_equal "utf-8", @response.charset assert_equal "utf-8", @response.charset
end end
# :ported: # :ported:
def test_charset_from_body def test_charset_from_body
get :render_charset_from_body get :render_charset_from_body
assert_equal Mime::TEXT, @response.content_type assert_equal Mime::Type[:TEXT], @response.content_type
assert_equal "utf-16", @response.charset assert_equal "utf-16", @response.charset
end end
# :ported: # :ported:
def test_nil_charset_from_body def test_nil_charset_from_body
get :render_nil_charset_from_body get :render_nil_charset_from_body
assert_equal Mime::TEXT, @response.content_type assert_equal Mime::Type[:TEXT], @response.content_type
assert_equal "utf-8", @response.charset, @response.headers.inspect assert_equal "utf-8", @response.charset, @response.headers.inspect
end end
def test_nil_default_for_erb def test_nil_default_for_erb
with_default_charset nil do with_default_charset nil do
get :render_default_for_erb get :render_default_for_erb
assert_equal Mime::HTML, @response.content_type assert_equal Mime::Type[:HTML], @response.content_type
assert_nil @response.charset, @response.headers.inspect assert_nil @response.charset, @response.headers.inspect
end end
end end
def test_default_for_erb def test_default_for_erb
get :render_default_for_erb get :render_default_for_erb
assert_equal Mime::HTML, @response.content_type assert_equal Mime::Type[:HTML], @response.content_type
assert_equal "utf-8", @response.charset assert_equal "utf-8", @response.charset
end end
def test_default_for_builder def test_default_for_builder
get :render_default_for_builder get :render_default_for_builder
assert_equal Mime::XML, @response.content_type assert_equal Mime::Type[:XML], @response.content_type
assert_equal "utf-8", @response.charset assert_equal "utf-8", @response.charset
end end
def test_change_for_builder def test_change_for_builder
get :render_change_for_builder get :render_change_for_builder
assert_equal Mime::HTML, @response.content_type assert_equal Mime::Type[:HTML], @response.content_type
assert_equal "utf-8", @response.charset assert_equal "utf-8", @response.charset
end end
...@@ -144,24 +144,24 @@ class AcceptBasedContentTypeTest < ActionController::TestCase ...@@ -144,24 +144,24 @@ class AcceptBasedContentTypeTest < ActionController::TestCase
tests OldContentTypeController tests OldContentTypeController
def test_render_default_content_types_for_respond_to def test_render_default_content_types_for_respond_to
@request.accept = Mime::HTML.to_s @request.accept = Mime::Type[:HTML].to_s
get :render_default_content_types_for_respond_to get :render_default_content_types_for_respond_to
assert_equal Mime::HTML, @response.content_type assert_equal Mime::Type[:HTML], @response.content_type
@request.accept = Mime::JS.to_s @request.accept = Mime::Type[:JS].to_s
get :render_default_content_types_for_respond_to get :render_default_content_types_for_respond_to
assert_equal Mime::JS, @response.content_type assert_equal Mime::Type[:JS], @response.content_type
end end
def test_render_default_content_types_for_respond_to_with_template def test_render_default_content_types_for_respond_to_with_template
@request.accept = Mime::XML.to_s @request.accept = Mime::Type[:XML].to_s
get :render_default_content_types_for_respond_to get :render_default_content_types_for_respond_to
assert_equal Mime::XML, @response.content_type assert_equal Mime::Type[:XML], @response.content_type
end end
def test_render_default_content_types_for_respond_to_with_overwrite def test_render_default_content_types_for_respond_to_with_overwrite
@request.accept = Mime::RSS.to_s @request.accept = Mime::Type[:RSS].to_s
get :render_default_content_types_for_respond_to get :render_default_content_types_for_respond_to
assert_equal Mime::XML, @response.content_type assert_equal Mime::Type[:XML], @response.content_type
end end
end end
...@@ -7,12 +7,12 @@ def index ...@@ -7,12 +7,12 @@ def index
end end
def set_on_response_obj def set_on_response_obj
response.content_type = Mime::RSS response.content_type = Mime::Type[:RSS]
render body: "Hello world!" render body: "Hello world!"
end end
def set_on_render def set_on_render
render body: "Hello world!", content_type: Mime::RSS render body: "Hello world!", content_type: Mime::Type[:RSS]
end end
end end
......
...@@ -12,7 +12,7 @@ def render_simon_says ...@@ -12,7 +12,7 @@ def render_simon_says
def test_using_custom_render_option def test_using_custom_render_option
ActionController.add_renderer :simon do |says, options| ActionController.add_renderer :simon do |says, options|
self.content_type = Mime::TEXT self.content_type = Mime::Type[:TEXT]
self.response_body = "Simon says: #{says}" self.response_body = "Simon says: #{says}"
end end
......
...@@ -92,6 +92,6 @@ def test_should_render_xml_but_keep_custom_content_type ...@@ -92,6 +92,6 @@ def test_should_render_xml_but_keep_custom_content_type
def test_should_use_implicit_content_type def test_should_use_implicit_content_type
get :implicit_content_type, format: 'atom' get :implicit_content_type, format: 'atom'
assert_equal Mime::ATOM, @response.content_type assert_equal Mime::Type[:ATOM], @response.content_type
end end
end end
...@@ -89,7 +89,7 @@ def test_headers_after_send_shouldnt_include_charset ...@@ -89,7 +89,7 @@ def test_headers_after_send_shouldnt_include_charset
# Test that send_file_headers! is setting the correct HTTP headers. # Test that send_file_headers! is setting the correct HTTP headers.
def test_send_file_headers_bang def test_send_file_headers_bang
options = { options = {
:type => Mime::PNG, :type => Mime::Type[:PNG],
:disposition => 'disposition', :disposition => 'disposition',
:filename => 'filename' :filename => 'filename'
} }
...@@ -115,7 +115,7 @@ def test_send_file_headers_bang ...@@ -115,7 +115,7 @@ def test_send_file_headers_bang
def test_send_file_headers_with_disposition_as_a_symbol def test_send_file_headers_with_disposition_as_a_symbol
options = { options = {
:type => Mime::PNG, :type => Mime::Type[:PNG],
:disposition => :disposition, :disposition => :disposition,
:filename => 'filename' :filename => 'filename'
} }
......
...@@ -65,7 +65,7 @@ def test_put_json ...@@ -65,7 +65,7 @@ def test_put_json
def test_register_and_use_json_simple def test_register_and_use_json_simple
with_test_route_set do with_test_route_set do
with_params_parsers Mime::JSON => Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do with_params_parsers Mime::Type[:JSON] => Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
post "/", post "/",
params: '{"request":{"summary":"content...","title":"JSON"}}', params: '{"request":{"summary":"content...","title":"JSON"}}',
headers: { 'CONTENT_TYPE' => 'application/json' } headers: { 'CONTENT_TYPE' => 'application/json' }
...@@ -99,7 +99,7 @@ def test_dasherized_keys_as_json ...@@ -99,7 +99,7 @@ def test_dasherized_keys_as_json
def test_parsing_json_doesnot_rescue_exception def test_parsing_json_doesnot_rescue_exception
req = Class.new(ActionDispatch::Request) do req = Class.new(ActionDispatch::Request) do
def params_parsers def params_parsers
{ Mime::JSON => Proc.new { |data| raise Interrupt } } { Mime::Type[:JSON] => Proc.new { |data| raise Interrupt } }
end end
def content_length; get_header('rack.input').length; end def content_length; get_header('rack.input').length; end
......
...@@ -13,7 +13,7 @@ class MimeTypeTest < ActiveSupport::TestCase ...@@ -13,7 +13,7 @@ class MimeTypeTest < ActiveSupport::TestCase
test "unregister" do test "unregister" do
begin begin
Mime::Type.register("text/x-mobile", :mobile) Mime::Type.register("text/x-mobile", :mobile)
assert Mime.const_defined?(:MOBILE) assert Mime::Type.registered?(:MOBILE)
assert_equal Mime::Type[:MOBILE], Mime::LOOKUP['text/x-mobile'] assert_equal Mime::Type[:MOBILE], Mime::LOOKUP['text/x-mobile']
assert_equal Mime::Type[:MOBILE], Mime::EXTENSION_LOOKUP['mobile'] assert_equal Mime::Type[:MOBILE], Mime::EXTENSION_LOOKUP['mobile']
...@@ -157,7 +157,7 @@ class MimeTypeTest < ActiveSupport::TestCase ...@@ -157,7 +157,7 @@ class MimeTypeTest < ActiveSupport::TestCase
types = Mime::SET.symbols.uniq - [:all, :iphone] types = Mime::SET.symbols.uniq - [:all, :iphone]
# Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
types.delete_if { |type| !Mime.const_defined?(type.upcase) } types.delete_if { |type| !Mime::Type.registered?(type.upcase) }
types.each do |type| types.each do |type|
mime = Mime::Type[type.upcase] mime = Mime::Type[type.upcase]
...@@ -184,7 +184,7 @@ class MimeTypeTest < ActiveSupport::TestCase ...@@ -184,7 +184,7 @@ class MimeTypeTest < ActiveSupport::TestCase
all_types = Mime::SET.symbols all_types = Mime::SET.symbols
all_types.uniq! all_types.uniq!
# Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
all_types.delete_if { |type| !Mime.const_defined?(type.upcase) } all_types.delete_if { |type| !Mime::Type.registered?(type.upcase) }
end end
test "references gives preference to symbols before strings" do test "references gives preference to symbols before strings" do
......
...@@ -750,33 +750,33 @@ class RequestFormat < BaseRequestTest ...@@ -750,33 +750,33 @@ class RequestFormat < BaseRequestTest
test "xml format" do test "xml format" do
request = stub_request request = stub_request
assert_called(request, :parameters, times: 2, returns: {format: :xml}) do assert_called(request, :parameters, times: 2, returns: {format: :xml}) do
assert_equal Mime::XML, request.format assert_equal Mime::Type[:XML], request.format
end end
end end
test "xhtml format" do test "xhtml format" do
request = stub_request request = stub_request
assert_called(request, :parameters, times: 2, returns: {format: :xhtml}) do assert_called(request, :parameters, times: 2, returns: {format: :xhtml}) do
assert_equal Mime::HTML, request.format assert_equal Mime::Type[:HTML], request.format
end end
end end
test "txt format" do test "txt format" do
request = stub_request request = stub_request
assert_called(request, :parameters, times: 2, returns: {format: :txt}) do assert_called(request, :parameters, times: 2, returns: {format: :txt}) do
assert_equal Mime::TEXT, request.format assert_equal Mime::Type[:TEXT], request.format
end end
end end
test "XMLHttpRequest" do test "XMLHttpRequest" do
request = stub_request( request = stub_request(
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
'HTTP_ACCEPT' => [Mime::JS, Mime::HTML, Mime::XML, "text/xml", Mime::ALL].join(",") 'HTTP_ACCEPT' => [Mime::Type[:JS], Mime::Type[:HTML], Mime::Type[:XML], "text/xml", Mime::Type[:ALL]].join(",")
) )
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert request.xhr? assert request.xhr?
assert_equal Mime::JS, request.format assert_equal Mime::Type[:JS], request.format
end end
end end
...@@ -796,29 +796,29 @@ class RequestFormat < BaseRequestTest ...@@ -796,29 +796,29 @@ class RequestFormat < BaseRequestTest
test "formats text/html with accept header" do test "formats text/html with accept header" do
request = stub_request 'HTTP_ACCEPT' => 'text/html' request = stub_request 'HTTP_ACCEPT' => 'text/html'
assert_equal [Mime::HTML], request.formats assert_equal [Mime::Type[:HTML]], request.formats
end end
test "formats blank with accept header" do test "formats blank with accept header" do
request = stub_request 'HTTP_ACCEPT' => '' request = stub_request 'HTTP_ACCEPT' => ''
assert_equal [Mime::HTML], request.formats assert_equal [Mime::Type[:HTML]], request.formats
end end
test "formats XMLHttpRequest with accept header" do test "formats XMLHttpRequest with accept header" do
request = stub_request 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" request = stub_request 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
assert_equal [Mime::JS], request.formats assert_equal [Mime::Type[:JS]], request.formats
end end
test "formats application/xml with accept header" do test "formats application/xml with accept header" do
request = stub_request('CONTENT_TYPE' => 'application/xml; charset=UTF-8', request = stub_request('CONTENT_TYPE' => 'application/xml; charset=UTF-8',
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest") 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest")
assert_equal [Mime::XML], request.formats assert_equal [Mime::Type[:XML]], request.formats
end end
test "formats format:text with accept header" do test "formats format:text with accept header" do
request = stub_request request = stub_request
assert_called(request, :parameters, times: 2, returns: {format: :txt}) do assert_called(request, :parameters, times: 2, returns: {format: :txt}) do
assert_equal [Mime::TEXT], request.formats assert_equal [Mime::Type[:TEXT]], request.formats
end end
end end
...@@ -848,7 +848,7 @@ class RequestFormat < BaseRequestTest ...@@ -848,7 +848,7 @@ class RequestFormat < BaseRequestTest
test "formats with xhr request" do test "formats with xhr request" do
request = stub_request 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" request = stub_request 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [Mime::JS], request.formats assert_equal [Mime::Type[:JS]], request.formats
end end
end end
...@@ -859,35 +859,35 @@ class RequestFormat < BaseRequestTest ...@@ -859,35 +859,35 @@ class RequestFormat < BaseRequestTest
begin begin
request = stub_request 'HTTP_ACCEPT' => 'application/xml' request = stub_request 'HTTP_ACCEPT' => 'application/xml'
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats assert_equal [ Mime::Type[:HTML] ], request.formats
end end
request = stub_request 'HTTP_ACCEPT' => 'koz-asked/something-crazy' request = stub_request 'HTTP_ACCEPT' => 'koz-asked/something-crazy'
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats assert_equal [ Mime::Type[:HTML] ], request.formats
end end
request = stub_request 'HTTP_ACCEPT' => '*/*;q=0.1' request = stub_request 'HTTP_ACCEPT' => '*/*;q=0.1'
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats assert_equal [ Mime::Type[:HTML] ], request.formats
end end
request = stub_request 'HTTP_ACCEPT' => 'application/jxw' request = stub_request 'HTTP_ACCEPT' => 'application/jxw'
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats assert_equal [ Mime::Type[:HTML] ], request.formats
end end
request = stub_request 'HTTP_ACCEPT' => 'application/xml', request = stub_request 'HTTP_ACCEPT' => 'application/xml',
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
assert_called(request, :parameters, times: 1, returns: {}) do assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::JS ], request.formats assert_equal [ Mime::Type[:JS] ], request.formats
end end
request = stub_request 'HTTP_ACCEPT' => 'application/xml', request = stub_request 'HTTP_ACCEPT' => 'application/xml',
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
assert_called(request, :parameters, times: 2, returns: {format: :json}) do assert_called(request, :parameters, times: 2, returns: {format: :json}) do
assert_equal [ Mime::JSON ], request.formats assert_equal [ Mime::Type[:JSON] ], request.formats
end end
ensure ensure
ActionDispatch::Request.ignore_accept_header = old_ignore_accept_header ActionDispatch::Request.ignore_accept_header = old_ignore_accept_header
...@@ -897,7 +897,7 @@ class RequestFormat < BaseRequestTest ...@@ -897,7 +897,7 @@ class RequestFormat < BaseRequestTest
class RequestMimeType < BaseRequestTest class RequestMimeType < BaseRequestTest
test "content type" do test "content type" do
assert_equal Mime::HTML, stub_request('CONTENT_TYPE' => 'text/html').content_mime_type assert_equal Mime::Type[:HTML], stub_request('CONTENT_TYPE' => 'text/html').content_mime_type
end end
test "no content type" do test "no content type" do
...@@ -905,11 +905,11 @@ class RequestMimeType < BaseRequestTest ...@@ -905,11 +905,11 @@ class RequestMimeType < BaseRequestTest
end end
test "content type is XML" do test "content type is XML" do
assert_equal Mime::XML, stub_request('CONTENT_TYPE' => 'application/xml').content_mime_type assert_equal Mime::Type[:XML], stub_request('CONTENT_TYPE' => 'application/xml').content_mime_type
end end
test "content type with charset" do test "content type with charset" do
assert_equal Mime::XML, stub_request('CONTENT_TYPE' => 'application/xml; charset=UTF-8').content_mime_type assert_equal Mime::Type[:XML], stub_request('CONTENT_TYPE' => 'application/xml; charset=UTF-8').content_mime_type
end end
test "user agent" do test "user agent" do
...@@ -922,9 +922,9 @@ class RequestMimeType < BaseRequestTest ...@@ -922,9 +922,9 @@ class RequestMimeType < BaseRequestTest
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
) )
assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON]) assert_equal nil, request.negotiate_mime([Mime::Type[:XML], Mime::Type[:JSON]])
assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML]) assert_equal Mime::Type[:HTML], request.negotiate_mime([Mime::Type[:XML], Mime::Type[:HTML]])
assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL]) assert_equal Mime::Type[:HTML], request.negotiate_mime([Mime::Type[:XML], Mime::Type[:ALL]])
end end
test "negotiate_mime with content_type" do test "negotiate_mime with content_type" do
...@@ -933,7 +933,7 @@ class RequestMimeType < BaseRequestTest ...@@ -933,7 +933,7 @@ class RequestMimeType < BaseRequestTest
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
) )
assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV]) assert_equal Mime::Type[:XML], request.negotiate_mime([Mime::Type[:XML], Mime::Type[:CSV]])
end end
end end
......
...@@ -178,13 +178,13 @@ def test_only_set_charset_still_defaults_to_text_html ...@@ -178,13 +178,13 @@ def test_only_set_charset_still_defaults_to_text_html
test "read charset and content type" do test "read charset and content type" do
resp = ActionDispatch::Response.new.tap { |response| resp = ActionDispatch::Response.new.tap { |response|
response.charset = 'utf-16' response.charset = 'utf-16'
response.content_type = Mime::XML response.content_type = Mime::Type[:XML]
response.body = 'Hello' response.body = 'Hello'
} }
resp.to_a resp.to_a
assert_equal('utf-16', resp.charset) assert_equal('utf-16', resp.charset)
assert_equal(Mime::XML, resp.content_type) assert_equal(Mime::Type[:XML], resp.content_type)
assert_equal('application/xml; charset=utf-16', resp.headers['Content-Type']) assert_equal('application/xml; charset=utf-16', resp.headers['Content-Type'])
end end
...@@ -317,7 +317,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest ...@@ -317,7 +317,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
@app = lambda { |env| @app = lambda { |env|
ActionDispatch::Response.new.tap { |resp| ActionDispatch::Response.new.tap { |resp|
resp.charset = 'utf-16' resp.charset = 'utf-16'
resp.content_type = Mime::XML resp.content_type = Mime::Type[:XML]
resp.body = 'Hello' resp.body = 'Hello'
}.to_a }.to_a
} }
...@@ -326,7 +326,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest ...@@ -326,7 +326,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
assert_equal('utf-16', @response.charset) assert_equal('utf-16', @response.charset)
assert_equal(Mime::XML, @response.content_type) assert_equal(Mime::Type[:XML], @response.content_type)
assert_equal('application/xml; charset=utf-16', @response.headers['Content-Type']) assert_equal('application/xml; charset=utf-16', @response.headers['Content-Type'])
end end
...@@ -342,7 +342,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest ...@@ -342,7 +342,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
assert_equal('utf-16', @response.charset) assert_equal('utf-16', @response.charset)
assert_equal(Mime::XML, @response.content_type) assert_equal(Mime::Type[:XML], @response.content_type)
assert_equal('application/xml; charset=utf-16', @response.headers['Content-Type']) assert_equal('application/xml; charset=utf-16', @response.headers['Content-Type'])
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册