@@ -106,6 +106,42 @@ def test_get_stats_error(self, dynamic_links_test):
106106 dynamic_links .get_link_stats (MOCK_SHORT_URL , options , app = dynamic_links_test .app )
107107 assert excinfo .value .code == 500
108108
109+ @pytest .mark .parametrize ('error_code' , [400 , 401 , 500 ])
110+ def test_server_error (self , dynamic_links_test , error_code ):
111+ options = dynamic_links .StatOptions (duration_days = 9 )
112+ dynamic_links_test ._instrument_dynamic_links (
113+ payload = json .dumps ({'error' : {
114+ 'status' : 'INTERNAL_ERROR' ,
115+ 'message' : 'json_test_error' ,
116+ 'code' : error_code }}),
117+ status = error_code )
118+ with pytest .raises (dynamic_links .ApiCallError ) as excinfo :
119+ dynamic_links .get_link_stats (
120+ MOCK_SHORT_URL , options , app = dynamic_links_test .app )
121+ assert 'json_test_error' in str (excinfo .value )
122+
123+ @pytest .mark .parametrize ('error_code' , [400 , 401 , 500 ])
124+ def test_server_unformatted_error (self , dynamic_links_test , error_code ):
125+ options = dynamic_links .StatOptions (duration_days = 9 )
126+ dynamic_links_test ._instrument_dynamic_links (
127+ payload = 'custom error message' ,
128+ status = error_code )
129+ with pytest .raises (dynamic_links .ApiCallError ) as excinfo :
130+ dynamic_links .get_link_stats (
131+ MOCK_SHORT_URL , options , app = dynamic_links_test .app )
132+ assert 'custom error message' in str (excinfo .value )
133+
134+ def test_server_none_error (self , dynamic_links_test ):
135+ options = dynamic_links .StatOptions (duration_days = 9 )
136+ dynamic_links_test ._instrument_dynamic_links (
137+ payload = '' ,
138+ status = 400 )
139+ with pytest .raises (dynamic_links .ApiCallError ) as excinfo :
140+ dynamic_links .get_link_stats (
141+ MOCK_SHORT_URL , options , app = dynamic_links_test .app )
142+ assert 'Unexpected HTTP response' in str (excinfo .value )
143+
144+
109145 @pytest .mark .parametrize ('invalid_url' , ['google.com' ] + INVALID_STRINGS )
110146 def test_get_stats_invalid_url (self , dynamic_links_test , invalid_url ):
111147 options = dynamic_links .StatOptions (duration_days = 9 )
0 commit comments