@@ -30,14 +30,99 @@ def resp_cont(url, request):
3030def test_http_request_404 (gl ):
3131 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "get" )
3232 def resp_cont (url , request ):
33- content = {"Here is wh it failed" }
33+ content = {"Here is why it failed" }
3434 return response (404 , content , {}, None , 5 , request )
3535
3636 with HTTMock (resp_cont ):
3737 with pytest .raises (GitlabHttpError ):
3838 gl .http_request ("get" , "/not_there" )
3939
4040
41+ @pytest .mark .parametrize ("status_code" , [500 , 502 , 503 , 504 ])
42+ def test_http_request_with_only_failures (gl , status_code ):
43+ call_count = 0
44+
45+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
46+ def resp_cont (url , request ):
47+ nonlocal call_count
48+ call_count += 1
49+ return response (status_code , {"Here is why it failed" }, {}, None , 5 , request )
50+
51+ with HTTMock (resp_cont ):
52+ with pytest .raises (GitlabHttpError ):
53+ gl .http_request ("get" , "/projects" )
54+
55+ assert call_count == 1
56+
57+
58+ def test_http_request_with_retry_on_method_for_transient_failures (gl ):
59+ call_count = 0
60+ calls_before_success = 3
61+
62+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
63+ def resp_cont (url , request ):
64+ nonlocal call_count
65+ call_count += 1
66+ status_code = 200 if call_count == calls_before_success else 500
67+ return response (
68+ status_code ,
69+ {"Failure is the stepping stone to success" },
70+ {},
71+ None ,
72+ 5 ,
73+ request ,
74+ )
75+
76+ with HTTMock (resp_cont ):
77+ http_r = gl .http_request ("get" , "/projects" , retry_transient_errors = True )
78+
79+ assert http_r .status_code == 200
80+ assert call_count == calls_before_success
81+
82+
83+ def test_http_request_with_retry_on_class_for_transient_failures (gl_retry ):
84+ call_count = 0
85+ calls_before_success = 3
86+
87+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
88+ def resp_cont (url , request ):
89+ nonlocal call_count
90+ call_count += 1
91+ status_code = 200 if call_count == calls_before_success else 500
92+ return response (
93+ status_code ,
94+ {"Failure is the stepping stone to success" },
95+ {},
96+ None ,
97+ 5 ,
98+ request ,
99+ )
100+
101+ with HTTMock (resp_cont ):
102+ http_r = gl_retry .http_request ("get" , "/projects" )
103+
104+ assert http_r .status_code == 200
105+ assert call_count == calls_before_success
106+
107+
108+ def test_http_request_with_retry_on_class_and_method_for_transient_failures (gl_retry ):
109+ call_count = 0
110+ calls_before_success = 3
111+
112+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
113+ def resp_cont (url , request ):
114+ nonlocal call_count
115+ call_count += 1
116+ status_code = 200 if call_count == calls_before_success else 500
117+ return response (status_code , {"Here is why it failed" }, {}, None , 5 , request )
118+
119+ with HTTMock (resp_cont ):
120+ with pytest .raises (GitlabHttpError ):
121+ gl_retry .http_request ("get" , "/projects" , retry_transient_errors = False )
122+
123+ assert call_count == 1
124+
125+
41126def test_get_request (gl ):
42127 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
43128 def resp_cont (url , request ):
@@ -66,7 +151,7 @@ def resp_cont(url, request):
66151def test_get_request_404 (gl ):
67152 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "get" )
68153 def resp_cont (url , request ):
69- content = {"Here is wh it failed" }
154+ content = {"Here is why it failed" }
70155 return response (404 , content , {}, None , 5 , request )
71156
72157 with HTTMock (resp_cont ):
@@ -150,7 +235,7 @@ def test_post_request_404(gl):
150235 scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "post"
151236 )
152237 def resp_cont (url , request ):
153- content = {"Here is wh it failed" }
238+ content = {"Here is why it failed" }
154239 return response (404 , content , {}, None , 5 , request )
155240
156241 with HTTMock (resp_cont ):
@@ -186,7 +271,7 @@ def resp_cont(url, request):
186271def test_put_request_404 (gl ):
187272 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "put" )
188273 def resp_cont (url , request ):
189- content = {"Here is wh it failed" }
274+ content = {"Here is why it failed" }
190275 return response (404 , content , {}, None , 5 , request )
191276
192277 with HTTMock (resp_cont ):
@@ -226,7 +311,7 @@ def test_delete_request_404(gl):
226311 scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "delete"
227312 )
228313 def resp_cont (url , request ):
229- content = {"Here is wh it failed" }
314+ content = {"Here is why it failed" }
230315 return response (404 , content , {}, None , 5 , request )
231316
232317 with HTTMock (resp_cont ):
0 commit comments