@@ -535,6 +535,49 @@ def callback(_):
535535 finally :
536536 testutils .cleanup_apps ()
537537
538+ @pytest .mark .parametrize (
539+ 'url,emulator_host,expected_base_url,expected_namespace' ,
540+ [
541+ # Production URLs with no override:
542+ ('https://test.firebaseio.com' , None , 'https://test.firebaseio.com/.json' , None ),
543+ ('https://test.firebaseio.com/' , None , 'https://test.firebaseio.com/.json' , None ),
544+
545+ # Production URLs with emulator_host override:
546+ ('https://test.firebaseio.com' , 'localhost:9000' , 'http://localhost:9000/.json' ,
547+ 'test' ),
548+ ('https://test.firebaseio.com/' , 'localhost:9000' , 'http://localhost:9000/.json' ,
549+ 'test' ),
550+
551+ # Emulator URL with no override.
552+ ('http://localhost:8000/?ns=test' , None , 'http://localhost:8000/.json' , 'test' ),
553+
554+ # emulator_host is ignored when the original URL is already emulator.
555+ ('http://localhost:8000/?ns=test' , 'localhost:9999' , 'http://localhost:8000/.json' ,
556+ 'test' ),
557+ ]
558+ )
559+ def test_listen_sse_client (self , url , emulator_host , expected_base_url , expected_namespace ,
560+ mocker ):
561+ if emulator_host :
562+ os .environ [_EMULATOR_HOST_ENV_VAR ] = emulator_host
563+
564+ try :
565+ firebase_admin .initialize_app (testutils .MockCredential (), {'databaseURL' : url })
566+ ref = db .reference ()
567+ mock_sse_client = mocker .patch ('firebase_admin._sseclient.SSEClient' )
568+ mock_callback = mocker .Mock ()
569+ ref .listen (mock_callback )
570+ args , kwargs = mock_sse_client .call_args
571+ assert args [0 ] == expected_base_url
572+ if expected_namespace :
573+ assert kwargs .get ('params' ) == {'ns' : expected_namespace }
574+ else :
575+ assert kwargs .get ('params' ) == {}
576+ finally :
577+ if _EMULATOR_HOST_ENV_VAR in os .environ :
578+ del os .environ [_EMULATOR_HOST_ENV_VAR ]
579+ testutils .cleanup_apps ()
580+
538581 def test_listener_session (self ):
539582 firebase_admin .initialize_app (testutils .MockCredential (), {
540583 'databaseURL' : 'https://test.firebaseio.com' ,
0 commit comments