Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove checks for encodings availability #4180

Merged
merged 1 commit into from
Dec 25, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions actionmailer/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def teardown
assert_equal(1, email.attachments.length)
assert_equal('invoice.jpg', email.attachments[0].filename)
expected = "\312\213\254\232)b"
expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding)
expected.force_encoding(Encoding::BINARY)
assert_equal expected, email.attachments['invoice.jpg'].decoded
end

Expand All @@ -116,7 +116,7 @@ def teardown
assert_equal(1, email.attachments.length)
assert_equal('invoice.jpg', email.attachments[0].filename)
expected = "\312\213\254\232)b"
expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding)
expected.force_encoding(Encoding::BINARY)
assert_equal expected, email.attachments['invoice.jpg'].decoded
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def raw_post
# variable is already set, wrap it in a StringIO.
def body
if raw_post = @env['RAW_POST_DATA']
raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
raw_post.force_encoding(Encoding::BINARY)
StringIO.new(raw_post)
else
@env['rack.input']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(app, options = {})

def generate_sid
sid = SecureRandom.hex(16)
sid.encode!('UTF-8') if sid.respond_to?(:encode!)
sid.encode!('UTF-8')
sid
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/capture_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def content_for?(name)
def with_output_buffer(buf = nil) #:nodoc:
unless buf
buf = ActionView::OutputBuffer.new
buf.force_encoding(output_buffer.encoding) if output_buffer.respond_to?(:encoding) && buf.respond_to?(:force_encoding)
buf.force_encoding(output_buffer.encoding) if output_buffer
end
self.output_buffer, old_buffer = buf, output_buffer
yield
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def test_route_with_text_default
assert_equal({ :controller => "content", :action => 'show_page', :id => 'foo' }, rs.recognize_path("/page/foo"))

token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in Russian
token.force_encoding(Encoding::BINARY) if token.respond_to?(:force_encoding)
token.force_encoding(Encoding::BINARY)
escaped_token = CGI::escape(token)

assert_equal '/page/' + escaped_token, url_for(rs, { :controller => 'content', :action => 'show_page', :id => token })
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/send_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_file_stream
require 'stringio'
output = StringIO.new
output.binmode
output.string.force_encoding(file_data.encoding) if output.string.respond_to?(:force_encoding)
output.string.force_encoding(file_data.encoding)
assert_nothing_raised { response.body_parts.each { |part| output << part.to_s } }
assert_equal file_data, output.string
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def test_test_uploaded_file
path = "#{FILES_DIR}/#{filename}"
content_type = 'image/png'
expected = File.read(path)
expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding)
expected.force_encoding(Encoding::BINARY)

file = Rack::Test::UploadedFile.new(path, content_type)
assert_equal filename, file.original_filename
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def teardown

# Rack doesn't handle multipart/mixed for us.
files = params['files']
files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
files.force_encoding('ASCII-8BIT')
assert_equal 19756, files.size
end

Expand Down
16 changes: 7 additions & 9 deletions actionpack/test/template/output_buffer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ def setup
assert_equal ['foo', 'bar'], body_parts
end

if '1.9'.respond_to?(:force_encoding)
test 'flushing preserves output buffer encoding' do
original_buffer = ' '.force_encoding(Encoding::EUC_JP)
@vc.output_buffer = original_buffer
@vc.flush_output_buffer
assert_equal ['foo', original_buffer], body_parts
assert_not_equal original_buffer, output_buffer
assert_equal Encoding::EUC_JP, output_buffer.encoding
end
test 'flushing preserves output buffer encoding' do
original_buffer = ' '.force_encoding(Encoding::EUC_JP)
@vc.output_buffer = original_buffer
@vc.flush_output_buffer
assert_equal ['foo', original_buffer], body_parts
assert_not_equal original_buffer, output_buffer
assert_equal Encoding::EUC_JP, output_buffer.encoding
end

protected
Expand Down
70 changes: 34 additions & 36 deletions actionpack/test/template/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,51 +410,49 @@ def teardown
GC.start
end

if '1.9'.respond_to?(:force_encoding)
def test_render_utf8_template_with_magic_comment
with_external_encoding Encoding::ASCII_8BIT do
result = @view.render(:file => "test/utf8_magic", :formats => [:html], :layouts => "layouts/yield")
assert_equal Encoding::UTF_8, result.encoding
assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
end
def test_render_utf8_template_with_magic_comment
with_external_encoding Encoding::ASCII_8BIT do
result = @view.render(:file => "test/utf8_magic", :formats => [:html], :layouts => "layouts/yield")
assert_equal Encoding::UTF_8, result.encoding
assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
end
end

def test_render_utf8_template_with_default_external_encoding
with_external_encoding Encoding::UTF_8 do
result = @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
assert_equal Encoding::UTF_8, result.encoding
assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
end
def test_render_utf8_template_with_default_external_encoding
with_external_encoding Encoding::UTF_8 do
result = @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
assert_equal Encoding::UTF_8, result.encoding
assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
end
end

def test_render_utf8_template_with_incompatible_external_encoding
with_external_encoding Encoding::SHIFT_JIS do
begin
@view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
flunk 'Should have raised incompatible encoding error'
rescue ActionView::Template::Error => error
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
end
def test_render_utf8_template_with_incompatible_external_encoding
with_external_encoding Encoding::SHIFT_JIS do
begin
@view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
flunk 'Should have raised incompatible encoding error'
rescue ActionView::Template::Error => error
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
end
end
end

def test_render_utf8_template_with_partial_with_incompatible_encoding
with_external_encoding Encoding::SHIFT_JIS do
begin
@view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield")
flunk 'Should have raised incompatible encoding error'
rescue ActionView::Template::Error => error
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
end
def test_render_utf8_template_with_partial_with_incompatible_encoding
with_external_encoding Encoding::SHIFT_JIS do
begin
@view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield")
flunk 'Should have raised incompatible encoding error'
rescue ActionView::Template::Error => error
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
end
end
end

def with_external_encoding(encoding)
old = Encoding.default_external
silence_warnings { Encoding.default_external = encoding }
yield
ensure
silence_warnings { Encoding.default_external = old }
end
def with_external_encoding(encoding)
old = Encoding.default_external
silence_warnings { Encoding.default_external = encoding }
yield
ensure
silence_warnings { Encoding.default_external = old }
end
end
88 changes: 42 additions & 46 deletions activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,52 +224,48 @@ def clear_cache!
@statements.clear
end

if "<3".respond_to?(:encode)
# Taken from here:
# https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
# Author: TOMITA Masahiro <tommy@tmtm.org>
ENCODINGS = {
"armscii8" => nil,
"ascii" => Encoding::US_ASCII,
"big5" => Encoding::Big5,
"binary" => Encoding::ASCII_8BIT,
"cp1250" => Encoding::Windows_1250,
"cp1251" => Encoding::Windows_1251,
"cp1256" => Encoding::Windows_1256,
"cp1257" => Encoding::Windows_1257,
"cp850" => Encoding::CP850,
"cp852" => Encoding::CP852,
"cp866" => Encoding::IBM866,
"cp932" => Encoding::Windows_31J,
"dec8" => nil,
"eucjpms" => Encoding::EucJP_ms,
"euckr" => Encoding::EUC_KR,
"gb2312" => Encoding::EUC_CN,
"gbk" => Encoding::GBK,
"geostd8" => nil,
"greek" => Encoding::ISO_8859_7,
"hebrew" => Encoding::ISO_8859_8,
"hp8" => nil,
"keybcs2" => nil,
"koi8r" => Encoding::KOI8_R,
"koi8u" => Encoding::KOI8_U,
"latin1" => Encoding::ISO_8859_1,
"latin2" => Encoding::ISO_8859_2,
"latin5" => Encoding::ISO_8859_9,
"latin7" => Encoding::ISO_8859_13,
"macce" => Encoding::MacCentEuro,
"macroman" => Encoding::MacRoman,
"sjis" => Encoding::SHIFT_JIS,
"swe7" => nil,
"tis620" => Encoding::TIS_620,
"ucs2" => Encoding::UTF_16BE,
"ujis" => Encoding::EucJP_ms,
"utf8" => Encoding::UTF_8,
"utf8mb4" => Encoding::UTF_8,
}
else
ENCODINGS = Hash.new { |h,k| h[k] = k }
end
# Taken from here:
# https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
# Author: TOMITA Masahiro <tommy@tmtm.org>
ENCODINGS = {
"armscii8" => nil,
"ascii" => Encoding::US_ASCII,
"big5" => Encoding::Big5,
"binary" => Encoding::ASCII_8BIT,
"cp1250" => Encoding::Windows_1250,
"cp1251" => Encoding::Windows_1251,
"cp1256" => Encoding::Windows_1256,
"cp1257" => Encoding::Windows_1257,
"cp850" => Encoding::CP850,
"cp852" => Encoding::CP852,
"cp866" => Encoding::IBM866,
"cp932" => Encoding::Windows_31J,
"dec8" => nil,
"eucjpms" => Encoding::EucJP_ms,
"euckr" => Encoding::EUC_KR,
"gb2312" => Encoding::EUC_CN,
"gbk" => Encoding::GBK,
"geostd8" => nil,
"greek" => Encoding::ISO_8859_7,
"hebrew" => Encoding::ISO_8859_8,
"hp8" => nil,
"keybcs2" => nil,
"koi8r" => Encoding::KOI8_R,
"koi8u" => Encoding::KOI8_U,
"latin1" => Encoding::ISO_8859_1,
"latin2" => Encoding::ISO_8859_2,
"latin5" => Encoding::ISO_8859_9,
"latin7" => Encoding::ISO_8859_13,
"macce" => Encoding::MacCentEuro,
"macroman" => Encoding::MacRoman,
"sjis" => Encoding::SHIFT_JIS,
"swe7" => nil,
"tis620" => Encoding::TIS_620,
"ucs2" => Encoding::UTF_16BE,
"ujis" => Encoding::EucJP_ms,
"utf8" => Encoding::UTF_8,
"utf8mb4" => Encoding::UTF_8,
}

# Get the client encoding for this database
def client_encoding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def string_to_binary(value)
end

def binary_to_string(value)
if value.respond_to?(:force_encoding) && value.encoding != Encoding::ASCII_8BIT
if value.encoding != Encoding::ASCII_8BIT
value = value.force_encoding(Encoding::ASCII_8BIT)
end

Expand Down
16 changes: 5 additions & 11 deletions activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ def setup
end

def test_client_encoding
if "<3".respond_to?(:encoding)
assert_equal Encoding::UTF_8, @conn.client_encoding
else
assert_equal 'utf8', @conn.client_encoding
end
assert_equal Encoding::UTF_8, @conn.client_encoding
end

def test_exec_insert_number
Expand All @@ -41,13 +37,11 @@ def test_exec_insert_string

value = result.rows.last.last

if "<3".respond_to?(:encoding)
# FIXME: this should probably be inside the mysql AR adapter?
value.force_encoding(@conn.client_encoding)
# FIXME: this should probably be inside the mysql AR adapter?
value.force_encoding(@conn.client_encoding)

# The strings in this file are utf-8, so transcode to utf-8
value.encode!(Encoding::UTF_8)
end
# The strings in this file are utf-8, so transcode to utf-8
value.encode!(Encoding::UTF_8)

assert_equal str, value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ def test_exec_query_typecasts_bind_vals
end

def test_quote_binary_column_escapes_it
return unless "<3".respond_to?(:encode)

DualEncoding.connection.execute(<<-eosql)
CREATE TABLE dual_encodings (
id integer PRIMARY KEY AUTOINCREMENT,
Expand All @@ -157,9 +155,7 @@ def test_quote_binary_column_escapes_it
assert_equal str, binary.data

ensure
if "<3".respond_to?(:encode)
DualEncoding.connection.drop_table('dual_encodings')
end
DualEncoding.connection.drop_table('dual_encodings')
end

def test_execute
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/binary_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BinaryTest < ActiveRecord::TestCase

def test_mixed_encoding
str = "\x80"
str.force_encoding('ASCII-8BIT') if str.respond_to?(:force_encoding)
str.force_encoding('ASCII-8BIT')

binary = Binary.new :name => 'いただきます!', :data => str
binary.save!
Expand All @@ -23,7 +23,7 @@ def test_mixed_encoding

# Mysql adapter doesn't properly encode things, so we have to do it
if current_adapter?(:MysqlAdapter)
name.force_encoding('UTF-8') if name.respond_to?(:force_encoding)
name.force_encoding('UTF-8')
end
assert_equal 'いただきます!', name
end
Expand All @@ -33,7 +33,7 @@ def test_load_save

FIXTURES.each do |filename|
data = File.read(ASSETS_ROOT + "/#{filename}")
data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
data.force_encoding('ASCII-8BIT')
data.freeze

bin = Binary.new(:data => data)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_subsubdir_file_with_arbitrary_name

def test_binary_in_fixtures
data = File.open(ASSETS_ROOT + "/flowers.jpg", 'rb') { |f| f.read }
data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
data.force_encoding('ASCII-8BIT')
data.freeze
assert_equal data, @flowers.data
end
Expand Down
Loading