lib/open-uri.rb: accept :encoding option as well as encoding in mode string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e6484a1530
commit
a2831955b2
@ -108,6 +108,7 @@ module OpenURI
|
|||||||
:ssl_verify_mode => nil,
|
:ssl_verify_mode => nil,
|
||||||
:ftp_active_mode => false,
|
:ftp_active_mode => false,
|
||||||
:redirect => true,
|
:redirect => true,
|
||||||
|
:encoding => nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
def OpenURI.check_options(options) # :nodoc:
|
def OpenURI.check_options(options) # :nodoc:
|
||||||
@ -141,6 +142,12 @@ module OpenURI
|
|||||||
encoding, = $1,Encoding.find($1) if $1
|
encoding, = $1,Encoding.find($1) if $1
|
||||||
mode = nil
|
mode = nil
|
||||||
end
|
end
|
||||||
|
if options.has_key? :encoding
|
||||||
|
if !encoding.nil?
|
||||||
|
raise ArgumentError, "encoding specified twice"
|
||||||
|
end
|
||||||
|
encoding = Encoding.find(options[:encoding])
|
||||||
|
end
|
||||||
|
|
||||||
unless mode == nil ||
|
unless mode == nil ||
|
||||||
mode == 'r' || mode == 'rb' ||
|
mode == 'r' || mode == 'rb' ||
|
||||||
|
@ -604,6 +604,24 @@ class TestOpenURI < Test::Unit::TestCase
|
|||||||
assert_equal(content_ej, f.read)
|
assert_equal(content_ej, f.read)
|
||||||
assert_equal("text/plain", f.content_type)
|
assert_equal("text/plain", f.content_type)
|
||||||
assert_equal("euc-jp", f.charset)
|
assert_equal("euc-jp", f.charset)
|
||||||
|
assert_equal(Encoding::EUC_JP, f.read.encoding)
|
||||||
|
}
|
||||||
|
open("#{url}/ej/", 'r:utf-8') {|f|
|
||||||
|
# override charset with encoding option
|
||||||
|
assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
|
||||||
|
assert_equal("text/plain", f.content_type)
|
||||||
|
assert_equal("euc-jp", f.charset)
|
||||||
|
assert_equal(Encoding::UTF_8, f.read.encoding)
|
||||||
|
}
|
||||||
|
open("#{url}/ej/", :encoding=>'utf-8') {|f|
|
||||||
|
# override charset with encoding option
|
||||||
|
assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
|
||||||
|
assert_equal("text/plain", f.content_type)
|
||||||
|
assert_equal("euc-jp", f.charset)
|
||||||
|
assert_equal(Encoding::UTF_8, f.read.encoding)
|
||||||
|
}
|
||||||
|
assert_raise(ArgumentError) {
|
||||||
|
open("#{url}/ej/", 'r:utf-8', :encoding=>'utf-8') {|f| }
|
||||||
}
|
}
|
||||||
open("#{url}/nc/") {|f|
|
open("#{url}/nc/") {|f|
|
||||||
assert_equal("aa", f.read)
|
assert_equal("aa", f.read)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user