@@ -30,14 +30,81 @@ 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+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
45+ def resp_cont (url , request ):
46+ nonlocal call_count
47+ call_count += 1
48+ return response (status_code , {"Here is why it failed" }, {}, None , 5 , request )
49+
50+ with HTTMock (resp_cont ):
51+ with pytest .raises (GitlabHttpError ):
52+ gl .http_request ("get" , "/projects" )
53+
54+ assert call_count == 1
55+
56+
57+ def test_http_request_with_retry_on_method_for_transient_failures (gl ):
58+ call_count = 0
59+ calls_before_success = 3
60+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
61+ def resp_cont (url , request ):
62+ nonlocal call_count
63+ call_count += 1
64+ status_code = 200 if call_count == calls_before_success else 500
65+ return response (status_code , {"Failure is the stepping stone to success" }, {}, None , 5 , request )
66+
67+ with HTTMock (resp_cont ):
68+ http_r = gl .http_request ("get" , "/projects" , retry_transient_errors = True )
69+
70+ assert http_r .status_code == 200
71+ assert call_count == calls_before_success
72+
73+
74+ def test_http_request_with_retry_on_class_for_transient_failures (gl_retry ):
75+ call_count = 0
76+ calls_before_success = 3
77+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
78+ def resp_cont (url , request ):
79+ nonlocal call_count
80+ call_count += 1
81+ status_code = 200 if call_count == calls_before_success else 500
82+ return response (status_code , {"Failure is the stepping stone to success" }, {}, None , 5 , request )
83+
84+ with HTTMock (resp_cont ):
85+ http_r = gl_retry .http_request ("get" , "/projects" )
86+
87+ assert http_r .status_code == 200
88+ assert call_count == calls_before_success
89+
90+
91+ def test_http_request_with_retry_on_class_and_method_for_transient_failures (gl_retry ):
92+ call_count = 0
93+ calls_before_success = 3
94+ @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
95+ def resp_cont (url , request ):
96+ nonlocal call_count
97+ call_count += 1
98+ status_code = 200 if call_count == calls_before_success else 500
99+ return response (status_code , {"Here is why it failed" }, {}, None , 5 , request )
100+
101+ with HTTMock (resp_cont ):
102+ with pytest .raises (GitlabHttpError ):
103+ gl_retry .http_request ("get" , "/projects" , retry_transient_errors = False )
104+
105+ assert call_count == 1
106+
107+
41108def test_get_request (gl ):
42109 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
43110 def resp_cont (url , request ):
@@ -66,7 +133,7 @@ def resp_cont(url, request):
66133def test_get_request_404 (gl ):
67134 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "get" )
68135 def resp_cont (url , request ):
69- content = {"Here is wh it failed" }
136+ content = {"Here is why it failed" }
70137 return response (404 , content , {}, None , 5 , request )
71138
72139 with HTTMock (resp_cont ):
@@ -150,7 +217,7 @@ def test_post_request_404(gl):
150217 scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "post"
151218 )
152219 def resp_cont (url , request ):
153- content = {"Here is wh it failed" }
220+ content = {"Here is why it failed" }
154221 return response (404 , content , {}, None , 5 , request )
155222
156223 with HTTMock (resp_cont ):
@@ -186,7 +253,7 @@ def resp_cont(url, request):
186253def test_put_request_404 (gl ):
187254 @urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "put" )
188255 def resp_cont (url , request ):
189- content = {"Here is wh it failed" }
256+ content = {"Here is why it failed" }
190257 return response (404 , content , {}, None , 5 , request )
191258
192259 with HTTMock (resp_cont ):
@@ -226,7 +293,7 @@ def test_delete_request_404(gl):
226293 scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "delete"
227294 )
228295 def resp_cont (url , request ):
229- content = {"Here is wh it failed" }
296+ content = {"Here is why it failed" }
230297 return response (404 , content , {}, None , 5 , request )
231298
232299 with HTTMock (resp_cont ):
0 commit comments