From 11c6537a5bfec8a37be7a44595a8a966bfed4011 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sat, 17 Oct 2026 01:35:58 -0600 Subject: [PATCH] Test the constructor's trimming and report's tag cleaning The constructor strips surrounding whitespace from base_url, application and key, and report() drops None tag keys/values and stringifies the rest before merging in the program version, but no test exercised either. Lock in that a padded base URL, application and key still yield /api/metrics, "Bearer " and the bare application name, and that a None "version" tag does not suppress the program's own version. Co-Authored-By: Claude Opus 5.5 --- tests/test_trace_client.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_trace_client.py b/tests/test_trace_client.py index 2f13bd3..48b12f8 100644 --- a/tests/test_trace_client.py +++ b/tests/test_trace_client.py @@ -89,6 +89,17 @@ def test_report_posts_the_event_to_the_metrics_endpoint_with_the_key(self): json.loads(request["body"])) client.close() + def test_constructor_trims_the_base_url_application_and_key(self): + client = TraceClient(" " + self.base_url + "/ ", " MyGame ", "1.2.3", key=" k-123 ") + client.report("startup") + self.assertTrue(self.capture.arrived.wait(5), "a padded base URL should still reach the server") + request = self.capture.requests[0] + self.assertEqual("/api/metrics", request["path"]) + self.assertEqual("Bearer k-123", request["authorization"], "the key is sent without its padding") + self.assertEqual("MyGame", json.loads(request["body"])["application"]) + self.assertTrue(request["user_agent"].endswith("(MyGame)"), request["user_agent"]) + client.close() + def test_report_carries_value_and_tags_when_given(self): client = TraceClient(self.base_url, "MyGame", "1.2.3", key="k") client.report("world-load", 2.5, {"seed": "42", "size": 'the "big" one'}) @@ -354,6 +365,14 @@ def test_report_never_modifies_the_callers_tags(self): self.assertEqual({"name": "home", "version": "1.2.3"}, json.loads(self.capture.requests[0]["body"])["tags"]) + def test_report_drops_none_tags_and_stringifies_the_rest_before_adding_the_version(self): + client = TraceClient(self.base_url, "MyGame", "1.2.3", key="k") + client.report("command", tags={"version": None, None: "orphan", "count": 7}) + self.assertTrue(self.capture.arrived.wait(5)) + self.assertEqual({"count": "7", "version": "1.2.3"}, json.loads(self.capture.requests[0]["body"])["tags"], + "a None version tag does not suppress the program's own version") + client.close() + def test_with_version_never_modifies_the_callers_mapping(self): from trace_client.trace_client import _with_version tags = {"name": "home"}