diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 96512b5fd..3f9b1a984 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -17,6 +17,13 @@ class ::ParameterError < StandardError; end private + def append_info_to_payload(payload) + super + + origin = request.origin + payload[:origin] = origin if origin.present? + end + def bad_request(exception) render_error_as_json(exception, :bad_request) end diff --git a/spec/requests/api_controller_spec.rb b/spec/requests/api_controller_spec.rb index acf6c8494..af6f24275 100644 --- a/spec/requests/api_controller_spec.rb +++ b/spec/requests/api_controller_spec.rb @@ -42,6 +42,34 @@ def index Rails.application.reload_routes! end + describe 'request logging' do + def completed_request_payload(headers: {}) + payload = nil + + callback = lambda do |event| + payload = event.payload + end + + ActiveSupport::Notifications.subscribed(callback, 'process_action.action_controller') do + get '/test', headers: + end + + payload + end + + it 'includes the request origin in the completed request payload' do + payload = completed_request_payload(headers: { 'Origin' => 'https://editor.raspberrypi.org' }) + + expect(payload).to include(origin: 'https://editor.raspberrypi.org') + end + + it 'omits the origin when the request does not include one' do + payload = completed_request_payload + + expect(payload).not_to have_key(:origin) + end + end + context 'when ActionController::ParameterMissing is raised' do before do test_controller.error = ActionController::ParameterMissing.new('foo')