From e33a96de63b16f0751141532db866a2af84c0d74 Mon Sep 17 00:00:00 2001 From: Dustin Byrne Date: Tue, 18 Aug 2026 16:42:45 -0400 Subject: [PATCH] test: harden snapshot User-Agent normalization --- posthog/test/test_server_payload_snapshots.py | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/posthog/test/test_server_payload_snapshots.py b/posthog/test/test_server_payload_snapshots.py index 792bca16..2874e076 100644 --- a/posthog/test/test_server_payload_snapshots.py +++ b/posthog/test/test_server_payload_snapshots.py @@ -1,16 +1,18 @@ import json +import re from pathlib import Path from unittest import mock +import pytest import requests from freezegun import freeze_time from posthog.client import Client - _SNAPSHOT_DIRECTORY = Path(__file__).with_name("snapshots") _TEST_FILE_SUFFIX = "posthog/test/test_server_payload_snapshots.py" _FIXED_TIME = "2026-01-02T03:04:05+00:00" +_USER_AGENT_PATTERN = re.compile(r"posthog-python/[!#$%&'*+\-.^_`|~0-9A-Za-z]+") _RUNTIME_CONTEXT = { "$os": "", "$os_distro": "", @@ -41,7 +43,11 @@ def _normalize_snapshot_value(value): for key, item in value.items(): if key == "$lib_version": normalized[key] = "" - elif key == "User-Agent" and isinstance(item, str): + elif ( + key == "User-Agent" + and isinstance(item, str) + and _USER_AGENT_PATTERN.fullmatch(item) + ): normalized[key] = "posthog-python/" elif ( key == "abs_path" and isinstance(item, str) and _has_test_file_suffix(item) @@ -228,6 +234,29 @@ def _flags_request(): return _transport_request(session.post.call_args) +def test_normalizes_release_user_agent_version(): + assert _normalize_snapshot_value({"User-Agent": "posthog-python/7.39.1"}) == { + "User-Agent": "posthog-python/" + } + + +@pytest.mark.parametrize( + "user_agent", + [ + "posthog-pythons/7.39.1", + "posthog_python/7.39.1", + "posthog-python/", + "posthog-python/7.39.1;", + "posthog-python/(7.39.1)", + "Mozilla/5.0", + ], +) +def test_does_not_normalize_unexpected_user_agent(user_agent): + assert _normalize_snapshot_value({"User-Agent": user_agent}) == { + "User-Agent": user_agent + } + + def test_legacy_capture_identify_alias_and_group_identify_request_snapshot(): _assert_json_snapshot("legacy_event_family", _legacy_event_family_request())