Skip to content

Commit a5bd807

Browse files
author
Vidas P
committed
Fix Post agent to correctly decode server response
1 parent f699bcb commit a5bd807

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [current]
9+
### Fixed
10+
- Post agent will now interpret server responses as UTF-8 with an option
11+
to override.
912

1013
## [0.9.14.2] - 2021-04-18
1114
### Fixed

app/models/agents/post_agent.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class PostAgent < Agent
4949
* `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
5050
* `disable_ssl_verification` - Set to `true` to disable ssl verification.
5151
* `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
52+
* `force_response_encoding` - Interpret server response in this encoding. Default is UTF-8.
5253
5354
#{receiving_file_handling_agent_description}
5455
@@ -114,6 +115,14 @@ def validate_options
114115
errors.add(:base, 'when content_type is a form, if provided, payload must be a hash')
115116
end
116117

118+
if options['force_response_encoding'].present?
119+
begin
120+
encoding = ::Encoding.find(options['force_response_encoding'])
121+
rescue ArgumentError
122+
errors.add(:base, "force_response_encoding '#{options['force_response_encoding']}' is invalid")
123+
end
124+
end
125+
117126
if options.has_key?('emit_messages') && boolify(options['emit_messages']).nil?
118127
errors.add(:base, 'if provided, emit_messages must be true or false')
119128
end
@@ -223,8 +232,12 @@ def handle(data, message = Message.new, headers)
223232

224233
if boolify(interpolated['emit_messages'])
225234
new_message = interpolated['output_mode'].to_s == 'merge' ? message.payload.dup : {}
235+
236+
encoding = ::Encoding.find(options['force_response_encoding'] || 'UTF-8')
237+
body = response.body.force_encoding(encoding).encode('UTF-8')
238+
226239
create_message payload: new_message.merge(
227-
body: response.body,
240+
body: body,
228241
headers: normalize_response_headers(response.headers),
229242
status: response.status
230243
)

spec/models/agents/post_agent_spec.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'ostruct'
33

44
describe Agents::PostAgent do
5+
let(:response_body) { '<html>an über webpage!</html>' }
56
before do
67
@valid_options = {
78
'post_url' => 'http://www.example.com',
@@ -52,7 +53,7 @@
5253
raise "unexpected Content-Type: #{content_type}"
5354
end
5455
end
55-
{ status: 200, body: '<html>a webpage!</html>', headers: { 'Content-type' => 'text/html' } }
56+
{ status: 200, body: response_body, headers: { 'Content-type' => 'text/html' } }
5657
}
5758
end
5859

@@ -268,7 +269,26 @@
268269

269270
it 'emits the body' do
270271
@checker.check
271-
expect(@checker.messages.last.payload['body']).to eq '<html>a webpage!</html>'
272+
expect(@checker.messages.last.payload['body']).to eq '<html>an über webpage!</html>'
273+
end
274+
275+
context 'when response is has no encoding (binary)' do
276+
let!(:response_body) { "an \xC3\xBCber webpage!".force_encoding('binary') }
277+
278+
it 'emits the body decoded as UTF-8' do
279+
@checker.check
280+
expect(@checker.messages.last.payload['body']).to eq 'an über webpage!'
281+
end
282+
end
283+
284+
context 'when response encoding is given explicitly' do
285+
let!(:response_body) { "a\xE8i\xFB!".force_encoding('binary') }
286+
287+
it 'emits the body decoded according to force_response_encoding' do
288+
@checker.options['force_response_encoding'] = 'windows-1257'
289+
@checker.check
290+
expect(@checker.messages.last.payload['body']).to eq 'ačiū!'
291+
end
272292
end
273293

274294
it 'emits the response headers capitalized by default' do
@@ -435,6 +455,14 @@
435455
expect(@checker).to be_valid
436456
end
437457

458+
it 'requires force_response_encoding to be supported encoding' do
459+
@checker.options['force_response_encoding'] = 'utf-19'
460+
expect(@checker).not_to be_valid
461+
462+
@checker.options['force_response_encoding'] = 'windows-1257'
463+
expect(@checker).to be_valid
464+
end
465+
438466
it "requires output_mode to be 'clean' or 'merge', if present" do
439467
@checker.options['output_mode'] = 'what?'
440468
expect(@checker).not_to be_valid

0 commit comments

Comments
 (0)