Skip to content

Commit c136fa8

Browse files
committed
Common: Add HTTPRequestEvent
1 parent d43bb67 commit c136fa8

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

monkey/common/agent_events/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
from .register import register_common_agent_events
1414
from .cpu_consumption_event import CPUConsumptionEvent
1515
from .ram_consumption_event import RAMConsumptionEvent
16+
from .http_request_event import HTTPRequestEvent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from http import HTTPMethod
2+
3+
from pydantic import AnyHttpUrl
4+
5+
from . import AbstractAgentEvent
6+
7+
8+
class HTTPRequestEvent(AbstractAgentEvent):
9+
"""
10+
An event that occurs when the Agent consumes significant RAM for its own purposes.
11+
12+
Attributes:
13+
:param method: The HTTP method used to make the request
14+
:param url: The URL to which the request was sent
15+
"""
16+
17+
method: HTTPMethod
18+
url: AnyHttpUrl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from http import HTTPMethod
2+
from uuid import UUID
3+
4+
import pytest
5+
6+
from common.agent_events import HTTPRequestEvent
7+
8+
AGENT_ID = UUID("012e7238-7b81-4108-8c7f-0787bc3f3c10")
9+
TIMESTAMP = 1664371327.4067292
10+
HTTP_URL = "http://www.test.org/RESTFUL/test?filter=my_filter"
11+
HTTPS_URL = "https://www.test.org/RESTFUL/test?filter=my_filter"
12+
METHOD = HTTPMethod.GET
13+
14+
15+
@pytest.mark.parametrize("url", (HTTP_URL, HTTPS_URL))
16+
def test_constructor(url: str):
17+
event = HTTPRequestEvent(source=AGENT_ID, timestamp=TIMESTAMP, method=METHOD, url=url)
18+
19+
assert event.source == AGENT_ID
20+
assert event.timestamp == TIMESTAMP
21+
assert event.target is None
22+
assert len(event.tags) == 0
23+
assert event.method == METHOD
24+
assert event.url == url
25+
26+
27+
@pytest.mark.parametrize(
28+
"invalid_url", ("www.missing-schema.org", -1, None, "ftp://wrong.schema.org")
29+
)
30+
def test_invalid_url(invalid_url):
31+
with pytest.raises((ValueError, TypeError)):
32+
HTTPRequestEvent(
33+
source=AGENT_ID,
34+
timestamp=TIMESTAMP,
35+
method=METHOD,
36+
url=invalid_url,
37+
)
38+
39+
40+
@pytest.mark.parametrize("invalid_method", ("not-a-method", "POST/GET", None, 999))
41+
def test_invalid_method(invalid_method):
42+
with pytest.raises((ValueError, TypeError)):
43+
HTTPRequestEvent(source=AGENT_ID, timestamp=TIMESTAMP, method=invalid_method, url=HTTP_URL)

vulture_allowlist.py

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
AbstractAgentEvent,
1818
CPUConsumptionEvent,
1919
FileEncryptionEvent,
20+
HTTPRequestEvent,
2021
RAMConsumptionEvent,
2122
)
2223
from common.agent_plugins import (
@@ -203,3 +204,6 @@
203204
RAMConsumptionEvent
204205
RAMConsumptionEvent.utilization
205206
RAMConsumptionEvent.bytes
207+
HTTPRequestEvent
208+
HTTPRequestEvent.method
209+
HTTPRequestEvent.url

0 commit comments

Comments
 (0)