2015-01-28 22:48:02 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-03-13 21:48:17 -04:00
|
|
|
require_relative '_lib'
|
2015-03-14 15:19:59 -04:00
|
|
|
require 'base64'
|
2010-01-20 16:51:24 -05:00
|
|
|
|
|
|
|
describe RestClient do
|
|
|
|
|
|
|
|
it "a simple request" do
|
|
|
|
body = 'abc'
|
|
|
|
stub_request(:get, "www.example.com").to_return(:body => body, :status => 200)
|
|
|
|
response = RestClient.get "www.example.com"
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.code).to eq 200
|
|
|
|
expect(response.body).to eq body
|
2010-01-20 16:51:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "a 404" do
|
|
|
|
body = "Ho hai ! I'm not here !"
|
|
|
|
stub_request(:get, "www.example.com").to_return(:body => body, :status => 404)
|
|
|
|
begin
|
|
|
|
RestClient.get "www.example.com"
|
|
|
|
raise
|
|
|
|
rescue RestClient::ResourceNotFound => e
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(e.http_code).to eq 404
|
|
|
|
expect(e.response.code).to eq 404
|
|
|
|
expect(e.response.body).to eq body
|
|
|
|
expect(e.http_body).to eq body
|
2010-01-20 16:51:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-28 22:48:02 -05:00
|
|
|
describe 'charset parsing' do
|
|
|
|
it 'handles utf-8' do
|
|
|
|
body = "λ".force_encoding('ASCII-8BIT')
|
|
|
|
stub_request(:get, "www.example.com").to_return(
|
|
|
|
:body => body, :status => 200, :headers => {
|
|
|
|
'Content-Type' => 'text/plain; charset=UTF-8'
|
|
|
|
})
|
|
|
|
response = RestClient.get "www.example.com"
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.encoding).to eq Encoding::UTF_8
|
|
|
|
expect(response.valid_encoding?).to eq true
|
2015-01-28 22:48:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles windows-1252' do
|
|
|
|
body = "\xff".force_encoding('ASCII-8BIT')
|
|
|
|
stub_request(:get, "www.example.com").to_return(
|
|
|
|
:body => body, :status => 200, :headers => {
|
|
|
|
'Content-Type' => 'text/plain; charset=windows-1252'
|
|
|
|
})
|
|
|
|
response = RestClient.get "www.example.com"
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.encoding).to eq Encoding::WINDOWS_1252
|
|
|
|
expect(response.encode('utf-8')).to eq "ÿ"
|
|
|
|
expect(response.valid_encoding?).to eq true
|
2015-01-28 22:48:02 -05:00
|
|
|
end
|
2010-01-20 16:51:24 -05:00
|
|
|
|
2015-01-28 22:48:02 -05:00
|
|
|
it 'handles binary' do
|
|
|
|
body = "\xfe".force_encoding('ASCII-8BIT')
|
|
|
|
stub_request(:get, "www.example.com").to_return(
|
|
|
|
:body => body, :status => 200, :headers => {
|
|
|
|
'Content-Type' => 'application/octet-stream; charset=binary'
|
|
|
|
})
|
|
|
|
response = RestClient.get "www.example.com"
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.encoding).to eq Encoding::BINARY
|
|
|
|
expect {
|
2015-01-28 22:48:02 -05:00
|
|
|
response.encode('utf-8')
|
2016-06-05 19:52:16 -04:00
|
|
|
}.to raise_error(Encoding::UndefinedConversionError)
|
|
|
|
expect(response.valid_encoding?).to eq true
|
2015-01-28 22:48:02 -05:00
|
|
|
end
|
2015-03-13 21:48:17 -04:00
|
|
|
|
|
|
|
it 'handles euc-jp' do
|
|
|
|
body = "\xA4\xA2\xA4\xA4\xA4\xA6\xA4\xA8\xA4\xAA".
|
|
|
|
force_encoding(Encoding::BINARY)
|
|
|
|
body_utf8 = 'あいうえお'
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(body_utf8.encoding).to eq Encoding::UTF_8
|
2015-03-13 21:48:17 -04:00
|
|
|
|
|
|
|
stub_request(:get, 'www.example.com').to_return(
|
|
|
|
:body => body, :status => 200, :headers => {
|
|
|
|
'Content-Type' => 'text/plain; charset=EUC-JP'
|
|
|
|
})
|
|
|
|
response = RestClient.get 'www.example.com'
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.encoding).to eq Encoding::EUC_JP
|
|
|
|
expect(response.valid_encoding?).to eq true
|
|
|
|
expect(response.length).to eq 5
|
|
|
|
expect(response.encode('utf-8')).to eq body_utf8
|
2015-03-13 21:48:17 -04:00
|
|
|
end
|
2015-03-14 15:19:59 -04:00
|
|
|
|
2016-06-19 14:47:58 -04:00
|
|
|
it 'defaults to the default encoding' do
|
2015-03-14 15:19:59 -04:00
|
|
|
stub_request(:get, 'www.example.com').to_return(
|
|
|
|
body: 'abc', status: 200, headers: {
|
|
|
|
'Content-Type' => 'text/plain'
|
|
|
|
})
|
|
|
|
|
|
|
|
response = RestClient.get 'www.example.com'
|
2016-06-19 14:47:58 -04:00
|
|
|
# expect(response.encoding).to eq Encoding.default_external
|
|
|
|
expect(response.encoding).to eq Encoding::UTF_8
|
2015-03-14 15:19:59 -04:00
|
|
|
end
|
|
|
|
|
2016-05-12 20:39:07 -04:00
|
|
|
it 'handles invalid encoding' do
|
|
|
|
stub_request(:get, 'www.example.com').to_return(
|
|
|
|
body: 'abc', status: 200, headers: {
|
|
|
|
'Content-Type' => 'text; charset=plain'
|
|
|
|
})
|
|
|
|
|
|
|
|
response = RestClient.get 'www.example.com'
|
2016-06-19 14:47:58 -04:00
|
|
|
# expect(response.encoding).to eq Encoding.default_external
|
|
|
|
expect(response.encoding).to eq Encoding::UTF_8
|
2016-05-12 20:39:07 -04:00
|
|
|
end
|
|
|
|
|
2015-03-14 15:19:59 -04:00
|
|
|
it 'leaves images as binary' do
|
|
|
|
gif = Base64.strict_decode64('R0lGODlhAQABAAAAADs=')
|
|
|
|
|
|
|
|
stub_request(:get, 'www.example.com').to_return(
|
|
|
|
body: gif, status: 200, headers: {
|
|
|
|
'Content-Type' => 'image/gif'
|
|
|
|
})
|
|
|
|
|
|
|
|
response = RestClient.get 'www.example.com'
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(response.encoding).to eq Encoding::BINARY
|
2015-03-14 15:19:59 -04:00
|
|
|
end
|
2015-01-28 22:48:02 -05:00
|
|
|
end
|
2013-07-31 12:58:25 -04:00
|
|
|
end
|