@@ -1813,20 +1813,23 @@ def teardown_class(cls):
1813
1813
testutils .cleanup_apps ()
1814
1814
1815
1815
def _instrument_batch_messaging_service (self , app = None , status = 200 , payload = '' , exc = None ):
1816
- if not app :
1817
- app = firebase_admin .get_app ()
1816
+ def build_mock_transport (_ ):
1817
+ if exc :
1818
+ return _HttpMockException (exc )
1818
1819
1819
- fcm_service = messaging ._get_messaging_service (app )
1820
- if exc :
1821
- fcm_service ._transport = _HttpMockException (exc )
1822
- else :
1823
1820
if status == 200 :
1824
1821
content_type = 'multipart/mixed; boundary=boundary'
1825
1822
else :
1826
1823
content_type = 'application/json'
1827
- fcm_service . _transport = http .HttpMockSequence ([
1824
+ return http .HttpMockSequence ([
1828
1825
({'status' : str (status ), 'content-type' : content_type }, payload ),
1829
1826
])
1827
+
1828
+ if not app :
1829
+ app = firebase_admin .get_app ()
1830
+
1831
+ fcm_service = messaging ._get_messaging_service (app )
1832
+ fcm_service ._build_transport = build_mock_transport
1830
1833
return fcm_service
1831
1834
1832
1835
def _batch_payload (self , payloads ):
@@ -2053,6 +2056,29 @@ def test_send_all_runtime_exception(self):
2053
2056
assert excinfo .value .cause is exc
2054
2057
assert excinfo .value .http_response is None
2055
2058
2059
+ def test_send_transport_init (self ):
2060
+ def track_call_count (build_transport ):
2061
+ def wrapper (credential ):
2062
+ wrapper .calls += 1
2063
+ return build_transport (credential )
2064
+ wrapper .calls = 0
2065
+ return wrapper
2066
+
2067
+ payload = json .dumps ({'name' : 'message-id' })
2068
+ fcm_service = self ._instrument_batch_messaging_service (
2069
+ payload = self ._batch_payload ([(200 , payload ), (200 , payload )]))
2070
+ build_mock_transport = fcm_service ._build_transport
2071
+ fcm_service ._build_transport = track_call_count (build_mock_transport )
2072
+ msg = messaging .Message (topic = 'foo' )
2073
+
2074
+ batch_response = messaging .send_all ([msg , msg ], dry_run = True )
2075
+ assert batch_response .success_count == 2
2076
+ assert fcm_service ._build_transport .calls == 1
2077
+
2078
+ batch_response = messaging .send_all ([msg , msg ], dry_run = True )
2079
+ assert batch_response .success_count == 2
2080
+ assert fcm_service ._build_transport .calls == 2
2081
+
2056
2082
2057
2083
class TestSendMulticast (TestBatch ):
2058
2084
0 commit comments