Skip to content

Commit fd81a73

Browse files
authored
Convert strings to UTF-8 (#1624)
1 parent bc27144 commit fd81a73

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/faraday/logging/formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def dump_headers(headers)
6363

6464
def dump_body(body)
6565
if body.respond_to?(:to_str)
66-
body.to_str
66+
body.to_str.encode(Encoding::UTF_8, undef: :replace, invalid: :replace)
6767
else
6868
pretty_inspect(body)
6969
end

spec/faraday/response/logger_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
stubs.post('/ohai') { [200, { 'Content-Type' => 'text/html' }, 'fred'] }
2222
stubs.post('/ohyes') { [200, { 'Content-Type' => 'text/html' }, 'pebbles'] }
2323
stubs.get('/rubbles') { [200, { 'Content-Type' => 'application/json' }, rubbles] }
24+
stubs.get('/8bit') { [200, { 'Content-Type' => 'text/html' }, (+'café!').force_encoding(Encoding::ASCII_8BIT)] }
2425
stubs.get('/filtered_body') { [200, { 'Content-Type' => 'text/html' }, 'soylent green is people'] }
2526
stubs.get('/filtered_headers') { [200, { 'Content-Type' => 'text/html' }, 'headers response'] }
2627
stubs.get('/filtered_params') { [200, { 'Content-Type' => 'text/html' }, 'params response'] }
@@ -238,6 +239,11 @@ def response(_env)
238239
expect(string_io.string).to match(%(fred))
239240
end
240241

242+
it 'converts to UTF-8' do
243+
conn.get '/8bit'
244+
expect(string_io.string).to match(%(caf��!))
245+
end
246+
241247
after do
242248
described_class.default_options = { bodies: false }
243249
end

0 commit comments

Comments
 (0)