diff options
author | Mircea Pricop <mircea.a.pricop@gmail.com> | 2012-07-06 17:19:20 +0200 |
---|---|---|
committer | mircea <mircea@mircea-X58L.(none)> | 2012-07-06 20:38:23 +0200 |
commit | 021f3d24f32dbad98c72cd5c36ff0b511be07a64 (patch) | |
tree | a68022e52b587335735c3625830befc2d2216330 /actionpack/test | |
parent | 07314e64fd62fb8e6165c8c539420160da9437e9 (diff) | |
download | rails-021f3d24f32dbad98c72cd5c36ff0b511be07a64.tar.gz rails-021f3d24f32dbad98c72cd5c36ff0b511be07a64.tar.bz2 rails-021f3d24f32dbad98c72cd5c36ff0b511be07a64.zip |
Prevent conflict between mime types and Object methods
Assuming the type ":touch", Collector.new was calling
send(:touch), which instead of triggering method_missing
and generating a new collector method, actually
invoked the private method `touch` inherited from
Object.
By generating the method for each mime type as it
is registered, the private methods on Object can
never be reached by `send`, because the `Collector`
will have them before `send` is called on it.
To do this, a callback mechanism was added to Mime::Type
This allows someone to add a callback for whenever
a new mime type is registered. The callback then
gets called with the new mime as a parameter.
This is then used in AbstractController::Collector
to generate new collector methods after each mime
is registered.
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 4 | ||||
-rw-r--r-- | actionpack/test/dispatch/mime_type_test.rb | 14 |
2 files changed, 17 insertions, 1 deletions
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index bdcd5561a8..c8e036b116 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -505,7 +505,7 @@ class RespondToControllerTest < ActionController::TestCase end class RespondWithController < ActionController::Base - respond_to :html, :json + respond_to :html, :json, :touch respond_to :xml, :except => :using_resource_with_block respond_to :js, :only => [ :using_resource_with_block, :using_resource, 'using_hash_resource' ] @@ -623,12 +623,14 @@ class RespondWithControllerTest < ActionController::TestCase super @request.host = "www.example.com" Mime::Type.register_alias('text/html', :iphone) + Mime::Type.register_alias('text/html', :touch) Mime::Type.register('text/x-mobile', :mobile) end def teardown super Mime::Type.unregister(:iphone) + Mime::Type.unregister(:touch) Mime::Type.unregister(:mobile) end diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index 9d77c3acc5..ed012093a7 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -118,6 +118,20 @@ class MimeTypeTest < ActiveSupport::TestCase end end + test "register callbacks" do + begin + registered_mimes = [] + Mime::Type.register_callback do |mime| + registered_mimes << mime + end + + Mime::Type.register("text/foo", :foo) + assert_equal registered_mimes, [Mime::FOO] + ensure + Mime::Type.unregister(:FOO) + end + end + test "custom type with extension aliases" do begin Mime::Type.register "text/foobar", :foobar, [], [:foo, "bar"] |