@@ -30,14 +30,99 @@ def resp_cont(url, request):
30
30
def test_http_request_404 (gl ):
31
31
@urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "get" )
32
32
def resp_cont (url , request ):
33
- content = {"Here is wh it failed" }
33
+ content = {"Here is why it failed" }
34
34
return response (404 , content , {}, None , 5 , request )
35
35
36
36
with HTTMock (resp_cont ):
37
37
with pytest .raises (GitlabHttpError ):
38
38
gl .http_request ("get" , "/not_there" )
39
39
40
40
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
+
41
126
def test_get_request (gl ):
42
127
@urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/projects" , method = "get" )
43
128
def resp_cont (url , request ):
@@ -66,7 +151,7 @@ def resp_cont(url, request):
66
151
def test_get_request_404 (gl ):
67
152
@urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "get" )
68
153
def resp_cont (url , request ):
69
- content = {"Here is wh it failed" }
154
+ content = {"Here is why it failed" }
70
155
return response (404 , content , {}, None , 5 , request )
71
156
72
157
with HTTMock (resp_cont ):
@@ -150,7 +235,7 @@ def test_post_request_404(gl):
150
235
scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "post"
151
236
)
152
237
def resp_cont (url , request ):
153
- content = {"Here is wh it failed" }
238
+ content = {"Here is why it failed" }
154
239
return response (404 , content , {}, None , 5 , request )
155
240
156
241
with HTTMock (resp_cont ):
@@ -186,7 +271,7 @@ def resp_cont(url, request):
186
271
def test_put_request_404 (gl ):
187
272
@urlmatch (scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "put" )
188
273
def resp_cont (url , request ):
189
- content = {"Here is wh it failed" }
274
+ content = {"Here is why it failed" }
190
275
return response (404 , content , {}, None , 5 , request )
191
276
192
277
with HTTMock (resp_cont ):
@@ -226,7 +311,7 @@ def test_delete_request_404(gl):
226
311
scheme = "http" , netloc = "localhost" , path = "/api/v4/not_there" , method = "delete"
227
312
)
228
313
def resp_cont (url , request ):
229
- content = {"Here is wh it failed" }
314
+ content = {"Here is why it failed" }
230
315
return response (404 , content , {}, None , 5 , request )
231
316
232
317
with HTTMock (resp_cont ):
0 commit comments