diff --git a/app/emu_svc.py b/app/emu_svc.py index b1b8c61..10736a9 100644 --- a/app/emu_svc.py +++ b/app/emu_svc.py @@ -59,7 +59,7 @@ async def handle_forwarded_beacon(self, request): except Exception as e: error_msg = 'Server error when processing forwarded beacon: %s' % e self.log.error(error_msg) - raise web.HTTPBadRequest(error_msg) + raise web.HTTPBadRequest(reason=error_msg) async def clone_repo(self, repo_url=None): """ diff --git a/tests/test_emu_svc.py b/tests/test_emu_svc.py index d02bb2c..a2fd725 100644 --- a/tests/test_emu_svc.py +++ b/tests/test_emu_svc.py @@ -6,7 +6,9 @@ import pytest from pathlib import Path, PosixPath -from unittest.mock import patch, call +from unittest.mock import patch, call, AsyncMock + +from aiohttp import web from app.utility.base_world import BaseWorld from plugins.emu.app.emu_svc import EmuService @@ -242,3 +244,15 @@ def test_register_required_payloads(self, emu_svc): want = {'payload1', 'payload2', 'payload3'} emu_svc._register_required_payloads(payloads) assert emu_svc.required_payloads == want + + async def test_handle_forwarded_beacon_bad_json_raises_http_bad_request(self, emu_svc): + """Regression test: handle_forwarded_beacon must raise HTTPBadRequest with a reason= + keyword argument when the request body is not valid JSON.""" + mock_request = AsyncMock() + mock_request.read.return_value = b'not-valid-json' + + with pytest.raises(web.HTTPBadRequest) as exc_info: + await emu_svc.handle_forwarded_beacon(mock_request) + + assert exc_info.value.reason is not None + assert len(exc_info.value.reason) > 0