|
26 | 26 | from httmock import HTTMock # noqa
|
27 | 27 | from httmock import response # noqa
|
28 | 28 | from httmock import urlmatch # noqa
|
| 29 | +import six |
29 | 30 |
|
30 | 31 | import gitlab
|
31 | 32 | from gitlab import * # noqa
|
@@ -243,6 +244,75 @@ def resp_two(url, request):
|
243 | 244 | self.assertEqual(data[0].ref, "b")
|
244 | 245 | self.assertEqual(len(data), 2)
|
245 | 246 |
|
| 247 | + def test_list_recursion_limit_caught(self): |
| 248 | + @urlmatch(scheme="http", netloc="localhost", |
| 249 | + path='/api/v3/projects/1/repository/branches', method="get") |
| 250 | + def resp_one(url, request): |
| 251 | + """First request: |
| 252 | +
|
| 253 | + http://localhost/api/v3/projects/1/repository/branches?per_page=1 |
| 254 | + """ |
| 255 | + headers = { |
| 256 | + 'content-type': 'application/json', |
| 257 | + 'link': '<http://localhost/api/v3/projects/1/repository/branc' |
| 258 | + 'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3' |
| 259 | + '/projects/1/repository/branches?page=2&per_page=0>; rel="las' |
| 260 | + 't", <http://localhost/api/v3/projects/1/repository/branches?' |
| 261 | + 'page=1&per_page=0>; rel="first"' |
| 262 | + } |
| 263 | + content = ('[{"branch_name": "otherbranch", ' |
| 264 | + '"project_id": 1, "ref": "b"}]').encode("utf-8") |
| 265 | + resp = response(200, content, headers, None, 5, request) |
| 266 | + return resp |
| 267 | + |
| 268 | + @urlmatch(scheme="http", netloc="localhost", |
| 269 | + path='/api/v3/projects/1/repository/branches', method="get", |
| 270 | + query=r'.*page=2.*') |
| 271 | + def resp_two(url, request): |
| 272 | + # Mock a runtime error |
| 273 | + raise RuntimeError("maximum recursion depth exceeded") |
| 274 | + |
| 275 | + with HTTMock(resp_two, resp_one): |
| 276 | + data = self.gl.list(ProjectBranch, project_id=1, per_page=1, |
| 277 | + safe_all=True) |
| 278 | + self.assertEqual(data[0].branch_name, "otherbranch") |
| 279 | + self.assertEqual(data[0].project_id, 1) |
| 280 | + self.assertEqual(data[0].ref, "b") |
| 281 | + self.assertEqual(len(data), 1) |
| 282 | + |
| 283 | + def test_list_recursion_limit_not_caught(self): |
| 284 | + @urlmatch(scheme="http", netloc="localhost", |
| 285 | + path='/api/v3/projects/1/repository/branches', method="get") |
| 286 | + def resp_one(url, request): |
| 287 | + """First request: |
| 288 | +
|
| 289 | + http://localhost/api/v3/projects/1/repository/branches?per_page=1 |
| 290 | + """ |
| 291 | + headers = { |
| 292 | + 'content-type': 'application/json', |
| 293 | + 'link': '<http://localhost/api/v3/projects/1/repository/branc' |
| 294 | + 'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3' |
| 295 | + '/projects/1/repository/branches?page=2&per_page=0>; rel="las' |
| 296 | + 't", <http://localhost/api/v3/projects/1/repository/branches?' |
| 297 | + 'page=1&per_page=0>; rel="first"' |
| 298 | + } |
| 299 | + content = ('[{"branch_name": "otherbranch", ' |
| 300 | + '"project_id": 1, "ref": "b"}]').encode("utf-8") |
| 301 | + resp = response(200, content, headers, None, 5, request) |
| 302 | + return resp |
| 303 | + |
| 304 | + @urlmatch(scheme="http", netloc="localhost", |
| 305 | + path='/api/v3/projects/1/repository/branches', method="get", |
| 306 | + query=r'.*page=2.*') |
| 307 | + def resp_two(url, request): |
| 308 | + # Mock a runtime error |
| 309 | + raise RuntimeError("maximum recursion depth exceeded") |
| 310 | + |
| 311 | + with HTTMock(resp_two, resp_one): |
| 312 | + with six.assertRaisesRegex(self, GitlabError, |
| 313 | + "(maximum recursion depth exceeded)"): |
| 314 | + self.gl.list(ProjectBranch, project_id=1, per_page=1, all=True) |
| 315 | + |
246 | 316 | def test_list_401(self):
|
247 | 317 | @urlmatch(scheme="http", netloc="localhost",
|
248 | 318 | path="/api/v3/projects/1/repository/branches", method="get")
|
|
0 commit comments