mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Start adding Content-Type charset handling.
This commit is contained in:
parent
29b56a33f3
commit
b860da40d5
2 changed files with 57 additions and 0 deletions
|
@ -17,8 +17,27 @@ module RestClient
|
|||
result.extend Response
|
||||
result.net_http_res = net_http_res
|
||||
result.args = args
|
||||
fix_encoding(result)
|
||||
result
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.fix_encoding(response)
|
||||
charset = RestClient::Utils.get_encoding_from_headers(response.headers)
|
||||
encoding = nil
|
||||
|
||||
begin
|
||||
encoding = Encoding.find(charset) if charset
|
||||
rescue ArgumentError
|
||||
RestClient.log "No such encoding: #{charset.inspect}"
|
||||
end
|
||||
|
||||
return unless encoding
|
||||
|
||||
response.body.force_encoding(encoding)
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
require 'spec_helper'
|
||||
|
||||
describe RestClient do
|
||||
|
@ -31,5 +32,42 @@ describe RestClient do
|
|||
end
|
||||
end
|
||||
|
||||
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"
|
||||
response.encoding.should eq Encoding::UTF_8
|
||||
response.valid_encoding?.should eq true
|
||||
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"
|
||||
response.encoding.should eq Encoding::WINDOWS_1252
|
||||
response.encode('utf-8').should eq "ÿ"
|
||||
response.valid_encoding?.should eq true
|
||||
end
|
||||
|
||||
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"
|
||||
response.encoding.should eq Encoding::BINARY
|
||||
lambda {
|
||||
response.encode('utf-8')
|
||||
}.should_raise(Encoding::UndefinedConversionError)
|
||||
response.valid_encoding?.should eq true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue