@@ -1216,14 +1216,59 @@ def test_flagsmith_posts_analytics_to_analytics_url_when_set(
12161216
12171217 # Then exactly one analytics POST landed on the override host (with the
12181218 # trailing slash applied), carried the tracked feature payload, and the
1219- # Edge Proxy never received an analytics request.
1220- analytics_calls = [
1221- call for call in responses .calls if call .request .method == "POST"
1219+ # Edge Proxy never received an analytics request. We filter by the exact
1220+ # analytics URL rather than counting all POSTs, because the module-global
1221+ # analytics session can leak background requests from other tests.
1222+ analytics_posts = [
1223+ call
1224+ for call in responses .calls
1225+ if call .request .method == "POST" and call .request .url == expected_analytics_url
12221226 ]
1223- assert len (analytics_calls ) == 1
1224- request = analytics_calls [0 ].request
1225- assert request .url == expected_analytics_url
1226- assert "edge-proxy" not in request .url
1227+ assert len (analytics_posts ) == 1
1228+ request = analytics_posts [0 ].request
12271229 assert request .body is not None
12281230 assert json .loads (request .body ) == {"some_feature" : 1 }
12291231 assert request .headers ["X-Environment-Key" ] == api_key
1232+ assert not [
1233+ call
1234+ for call in responses .calls
1235+ if call .request .method == "POST" and "edge-proxy" in call .request .url
1236+ ]
1237+
1238+
1239+ @responses .activate ()
1240+ def test_flagsmith_posts_analytics_to_api_url_when_analytics_url_unset (
1241+ api_key : str , flags_json : str , mocker : MockerFixture
1242+ ) -> None :
1243+ # Given a Flagsmith client with analytics enabled but no analytics_url
1244+ # override. This is the backwards-compatible default: analytics must keep
1245+ # posting to <api_url>/analytics/flags/, derived from api_url alone.
1246+ #
1247+ # As above, swap the FuturesSession for a synchronous requests.Session so
1248+ # the analytics POST is observable on the test thread.
1249+ mocker .patch ("flagsmith.analytics.session" , requests .Session ())
1250+ flagsmith = Flagsmith (
1251+ environment_key = api_key ,
1252+ api_url = "http://core-api.flagsmith.com/api/v1/" ,
1253+ enable_analytics = True ,
1254+ )
1255+
1256+ expected_analytics_url = "http://core-api.flagsmith.com/api/v1/analytics/flags/"
1257+ responses .add (method = "GET" , url = flagsmith .environment_flags_url , body = flags_json )
1258+ responses .add (method = "POST" , url = expected_analytics_url , status = 200 )
1259+
1260+ # When the evaluation API is exercised and analytics flushed deterministically.
1261+ flags = flagsmith .get_environment_flags ()
1262+ assert flags .is_feature_enabled ("some_feature" ) is True
1263+ assert flagsmith ._analytics_processor is not None
1264+ flagsmith ._analytics_processor .flush ()
1265+
1266+ # Then exactly one analytics POST landed on the api_url-derived endpoint.
1267+ # Filter by the exact URL to stay robust against background requests leaked
1268+ # by the module-global analytics session in other tests.
1269+ analytics_posts = [
1270+ call
1271+ for call in responses .calls
1272+ if call .request .method == "POST" and call .request .url == expected_analytics_url
1273+ ]
1274+ assert len (analytics_posts ) == 1
0 commit comments