Skip to content

Commit

Permalink
Implement Mime::Type.=~ to match all synonyms against arg [#1573 stat…
Browse files Browse the repository at this point in the history
…e:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
mojodna authored and josh committed Dec 15, 2008
1 parent 7c18518 commit f36dafa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Expand Up @@ -587,7 +587,7 @@ def assert_select_email(&block)
def response_from_page_or_rjs()
content_type = @response.content_type

if content_type && content_type =~ /text\/javascript/
if content_type && Mime::JS =~ content_type
body = @response.body.dup
root = HTML::Node.new(nil)

Expand Down
8 changes: 8 additions & 0 deletions actionpack/lib/action_controller/mime_type.rb
Expand Up @@ -176,6 +176,14 @@ def ==(mime_type)
end
end

def =~(mime_type)
return false if mime_type.blank?
regexp = Regexp.new(mime_type.to_s)
(@synonyms + [ self ]).any? do |synonym|
synonym.to_s =~ regexp
end
end

# Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See
# ActionController::RequestForgeryProtection.
def verify_request?
Expand Down
8 changes: 8 additions & 0 deletions actionpack/test/controller/mime_type_test.rb
Expand Up @@ -81,4 +81,12 @@ def test_verifiable_mime_types
assert verified.each { |type| assert Mime.const_get(type.to_s.upcase).verify_request?, "Verifiable Mime Type is not verified: #{type.inspect}" }
assert unverified.each { |type| assert !Mime.const_get(type.to_s.upcase).verify_request?, "Nonverifiable Mime Type is verified: #{type.inspect}" }
end

def test_regexp_matcher
assert Mime::JS =~ "text/javascript"
assert Mime::JS =~ "application/javascript"
assert Mime::JS !~ "text/html"
assert !(Mime::JS !~ "text/javascript")
assert !(Mime::JS !~ "application/javascript")
end
end

0 comments on commit f36dafa

Please sign in to comment.