Skip to content

Commit

Permalink
Add specs for frozen stubbed body
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSmartnik committed Mar 20, 2019
1 parent 413df85 commit d90a62f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
12 changes: 6 additions & 6 deletions lib/httparty/text_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TextEncoder
attr_reader :text, :content_type, :assume_utf16_is_big_endian

def initialize(text, assume_utf16_is_big_endian: true, content_type: nil)
@text = text
@text = text.dup
@content_type = content_type
@assume_utf16_is_big_endian = assume_utf16_is_big_endian
end
Expand Down Expand Up @@ -33,24 +33,24 @@ def encoded_text
def encode_utf_16
if text.bytesize >= 2
if text.getbyte(0) == 0xFF && text.getbyte(1) == 0xFE
return text.dup.force_encoding("UTF-16LE")
return text.force_encoding("UTF-16LE")
elsif text.getbyte(0) == 0xFE && text.getbyte(1) == 0xFF
return text.dup.force_encoding("UTF-16BE")
return text.force_encoding("UTF-16BE")
end
end

if assume_utf16_is_big_endian # option
text.dup.force_encoding("UTF-16BE")
text.force_encoding("UTF-16BE")
else
text.dup.force_encoding("UTF-16LE")
text.force_encoding("UTF-16LE")
end
end

def encode_with_ruby_encoding
# NOTE: This will raise an argument error if the
# charset does not exist
encoding = Encoding.find(charset)
text.dup.force_encoding(encoding.to_s)
text.force_encoding(encoding.to_s)
rescue ArgumentError
text
end
Expand Down
72 changes: 49 additions & 23 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,28 +505,42 @@
end

if "".respond_to?(:encoding)
it "should process charset in content type properly" do
response = stub_response "Content".force_encoding('ascii-8bit')
response.initialize_http_header("Content-Type" => "text/plain;charset = utf-8")
resp = @request.perform
context 'when body has ascii-8bit encoding' do
let(:response) { stub_response "Content".force_encoding('ascii-8bit') }

expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
end
it "processes charset in content type properly" do
response.initialize_http_header("Content-Type" => "text/plain;charset = utf-8")
resp = @request.perform

it "should process charset in content type properly if it has a different case" do
response = stub_response "Content".force_encoding('ascii-8bit')
response.initialize_http_header("Content-Type" => "text/plain;CHARSET = utf-8")
resp = @request.perform
expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
end

expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
end
it "processes charset in content type properly if it has a different case" do
response.initialize_http_header("Content-Type" => "text/plain;CHARSET = utf-8")
resp = @request.perform

it "should process quoted charset in content type properly" do
response = stub_response "Content".force_encoding('ascii-8bit')
response.initialize_http_header("Content-Type" => "text/plain;charset = \"utf-8\"")
resp = @request.perform
expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
end

it "processes quoted charset in content type properly" do
response.initialize_http_header("Content-Type" => "text/plain;charset = \"utf-8\"")
resp = @request.perform

expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
end

context 'when stubed body is frozen' do
let(:response) do
stub_response "Content".force_encoding('ascii-8bit').freeze
end

expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
it 'processes frozen body correctly' do
response.initialize_http_header("Content-Type" => "text/plain;charset = utf-8")
resp = @request.perform

expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))
end
end
end

it "should process response with a nil body" do
Expand All @@ -537,14 +551,26 @@
expect(resp.body).to be_nil
end

it "should process utf-16 charset with little endian bom correctly" do
@request.options[:assume_utf16_is_big_endian] = true
context 'when assume_utf16_is_big_endian is true' do
before { @request.options[:assume_utf16_is_big_endian] = true }

response = stub_response "\xFF\xFEC\x00o\x00n\x00t\x00e\x00n\x00t\x00"
response.initialize_http_header("Content-Type" => "text/plain;charset = utf-16")
resp = @request.perform
it "should process utf-16 charset with little endian bom correctly" do
response = stub_response "\xFF\xFEC\x00o\x00n\x00t\x00e\x00n\x00t\x00"

expect(resp.body.encoding).to eq(Encoding.find("UTF-16LE"))
response.initialize_http_header("Content-Type" => "text/plain;charset = utf-16")
resp = @request.perform

expect(resp.body.encoding).to eq(Encoding.find("UTF-16LE"))
end

it 'processes stubbed frozen body correctly' do
response = stub_response "\xFF\xFEC\x00o\x00n\x00t\x00e\x00n\x00t\x00".freeze

response.initialize_http_header("Content-Type" => "text/plain;charset = utf-16")
resp = @request.perform

expect(resp.body.encoding).to eq(Encoding.find("UTF-16LE"))
end
end

it "should process utf-16 charset with big endian bom correctly" do
Expand Down

0 comments on commit d90a62f

Please sign in to comment.