mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00

Use the (undocumented) URI.get_encoding method introduced in Ruby 2.1 to look up encodings by the aliases specified in HTML5. This means that the behavior will differ slightly between versions of Ruby, but the encodings selected are largely compatible. For example, `ISO-8859-1` is an alias for `Windows-1252` per the HTML5 specification, while in ruby versions < 2.1 it will be used as is. These two encodings are largely compatible, and the alias exists due to servers that return a `charset=ISO-8859-1` when they actually are using `Windows-1252`. Other aliases that differ include `shift_jis` (rendered as `Windows-31J`) and `euc-jp` (rendered as `CP51932`).
117 lines
4 KiB
Ruby
117 lines
4 KiB
Ruby
require_relative '_lib'
|
|
|
|
describe RestClient::Utils do
|
|
describe '.get_encoding_from_headers' do
|
|
it 'assumes no encoding by default for text' do
|
|
headers = {:content_type => 'text/plain'}
|
|
RestClient::Utils.get_encoding_from_headers(headers).
|
|
should eq nil
|
|
end
|
|
|
|
it 'returns nil on failures' do
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{:content_type => 'blah'}).should eq nil
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{}).should eq nil
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{:content_type => 'foo; bar=baz'}).should eq nil
|
|
end
|
|
|
|
it 'handles various charsets' do
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{:content_type => 'text/plain; charset=UTF-8'}).should eq 'UTF-8'
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{:content_type => 'application/json; charset=ISO-8859-1'}).
|
|
should eq 'ISO-8859-1'
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{:content_type => 'text/html; charset=windows-1251'}).
|
|
should eq 'windows-1251'
|
|
|
|
RestClient::Utils.get_encoding_from_headers(
|
|
{:content_type => 'text/html; charset="UTF-16"'}).
|
|
should eq 'UTF-16'
|
|
end
|
|
end
|
|
|
|
describe '.find_encoding' do
|
|
it 'finds various normal encoding names' do
|
|
{
|
|
'utf-8' => Encoding::UTF_8,
|
|
'big5' => Encoding::Big5,
|
|
'euc-kr' => Encoding::EUC_KR,
|
|
'WINDOWS-1252' => Encoding::Windows_1252,
|
|
'windows-31j' => Encoding::Windows_31J,
|
|
}.each_pair do |name, enc|
|
|
RestClient::Utils.find_encoding(name).should eq enc
|
|
end
|
|
end
|
|
|
|
it 'returns nil on failures' do
|
|
%w{nonexistent utf-99}.each do |name|
|
|
RestClient::Utils.find_encoding(name).should be_nil
|
|
end
|
|
end
|
|
|
|
it 'uses URI.get_encoding if available', if: RUBY_VERSION >= '2.1' do
|
|
{
|
|
'utf8' => Encoding::UTF_8,
|
|
'utf-16' => Encoding::UTF_16LE,
|
|
'latin1' => Encoding::Windows_1252,
|
|
'iso-8859-1' => Encoding::Windows_1252,
|
|
'shift_jis' => Encoding::Windows_31J,
|
|
'euc-jp' => Encoding::CP51932,
|
|
}.each_pair do |name, enc|
|
|
RestClient::Utils.find_encoding(name).should eq enc
|
|
end
|
|
end
|
|
|
|
it 'uses Encoding.find if URI.get_encoding unavailable', if: RUBY_VERSION < '2.1' do
|
|
{
|
|
'utf8' => nil,
|
|
'utf-16' => Encoding::UTF_16,
|
|
'latin1' => nil,
|
|
'iso-8859-1' => Encoding::ISO_8859_1,
|
|
'shift_jis' => Encoding::Shift_JIS,
|
|
'euc-jp' => Encoding::EUC_JP,
|
|
}.each_pair do |name, enc|
|
|
RestClient::Utils.find_encoding(name).should eq enc
|
|
end
|
|
end
|
|
end
|
|
|
|
describe '.cgi_parse_header' do
|
|
it 'parses headers' do
|
|
RestClient::Utils.cgi_parse_header('text/plain').
|
|
should eq ['text/plain', {}]
|
|
|
|
RestClient::Utils.cgi_parse_header('text/vnd.just.made.this.up ; ').
|
|
should eq ['text/vnd.just.made.this.up', {}]
|
|
|
|
RestClient::Utils.cgi_parse_header('text/plain;charset=us-ascii').
|
|
should eq ['text/plain', {'charset' => 'us-ascii'}]
|
|
|
|
RestClient::Utils.cgi_parse_header('text/plain ; charset="us-ascii"').
|
|
should eq ['text/plain', {'charset' => 'us-ascii'}]
|
|
|
|
RestClient::Utils.cgi_parse_header(
|
|
'text/plain ; charset="us-ascii"; another=opt').
|
|
should eq ['text/plain', {'charset' => 'us-ascii', 'another' => 'opt'}]
|
|
|
|
RestClient::Utils.cgi_parse_header(
|
|
'attachment; filename="silly.txt"').
|
|
should eq ['attachment', {'filename' => 'silly.txt'}]
|
|
|
|
RestClient::Utils.cgi_parse_header(
|
|
'attachment; filename="strange;name"').
|
|
should eq ['attachment', {'filename' => 'strange;name'}]
|
|
|
|
RestClient::Utils.cgi_parse_header(
|
|
'attachment; filename="strange;name";size=123;').should eq \
|
|
['attachment', {'filename' => 'strange;name', 'size' => '123'}]
|
|
|
|
RestClient::Utils.cgi_parse_header(
|
|
'form-data; name="files"; filename="fo\\"o;bar"').should eq \
|
|
['form-data', {'name' => 'files', 'filename' => 'fo"o;bar'}]
|
|
end
|
|
end
|
|
end
|