Merge branch '35815-webhook-log-encoding-error' into 'master'

Fix encoding error for WebHook logging

Closes #35815

See merge request !13230
This commit is contained in:
Dmitriy Zaporozhets 2017-08-02 08:53:49 +00:00
commit 6e8703440e
3 changed files with 28 additions and 1 deletions

View file

@ -101,7 +101,7 @@ class WebHookService
request_headers: build_headers(hook_name),
request_data: request_data,
response_headers: format_response_headers(response),
response_body: response.body,
response_body: safe_response_body(response),
response_status: response.code,
internal_error_message: error_message
)
@ -124,4 +124,10 @@ class WebHookService
def format_response_headers(response)
response.headers.each_capitalized.to_h
end
def safe_response_body(response)
return '' unless response.body
response.body.encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
end
end

View file

@ -0,0 +1,4 @@
---
title: Fix encoding error for WebHook logging
merge_request: 13230
author: Alexander Randa (@randaalex)

View file

@ -112,6 +112,23 @@ describe WebHookService do
end
end
context 'with unsafe response body' do
before do
WebMock.stub_request(:post, project_hook.url).to_return(status: 200, body: "\xBB")
service_instance.execute
end
it 'log successful execution' do
expect(hook_log.trigger).to eq('push_hooks')
expect(hook_log.url).to eq(project_hook.url)
expect(hook_log.request_headers).to eq(headers)
expect(hook_log.response_body).to eq('')
expect(hook_log.response_status).to eq('200')
expect(hook_log.execution_duration).to be > 0
expect(hook_log.internal_error_message).to be_nil
end
end
context 'should not log ServiceHooks' do
let(:service_hook) { create(:service_hook) }
let(:service_instance) { described_class.new(service_hook, data, 'service_hook') }