async def test_client_401_raise_ratelimit( httpserver: httpserver.HTTPServer) -> None: owner = github_types.GitHubLogin("owner") repo = "repo" httpserver.expect_request("/users/owner/installation").respond_with_json({ "id": 12345, "target_type": "User", "permissions": { "checks": "write", "contents": "write", "pull_requests": "write", }, "account": { "login": "******", "id": 12345 }, }) httpserver.expect_request( "/app/installations/12345/access_tokens").respond_with_json( { "token": "<token>", "expires_at": "2100-12-31T23:59:59Z" }, headers={ "X-RateLimit-Remaining": 5000, "X-RateLimit-Reset": 1234567890 }, ) httpserver.expect_oneshot_request( "/repos/owner/repo/pull/1").respond_with_json( {"message": "quota !"}, status=403, headers={ "X-RateLimit-Remaining": 0, "X-RateLimit-Reset": 1234567890 }, ) with mock.patch( "mergify_engine.config.GITHUB_API_URL", httpserver.url_for("/")[:-1], ): async with github.aget_client(owner) as client: with pytest.raises(exceptions.RateLimited): await client.item(f"/repos/{owner}/{repo}/pull/1") httpserver.check_assertions()
async def test_client_HTTP_400(httpserver: httpserver.HTTPServer) -> None: httpserver.expect_oneshot_request("/").respond_with_json( {"message": "This is an 4XX error"}, status=400) async with http.AsyncClient() as client: with pytest.raises(http.HTTPClientSideError) as exc_info: await client.get(httpserver.url_for("/")) assert exc_info.value.message == "This is an 4XX error" assert exc_info.value.status_code == 400 assert exc_info.value.response.status_code == 400 assert str(exc_info.value.request.url) == httpserver.url_for("/") httpserver.check_assertions()
async def test_client_abuse_403_no_header( httpserver: httpserver.HTTPServer) -> None: abuse_message = ("You have triggered an abuse detection mechanism. " "Please wait a few minutes before you try again.") httpserver.expect_request("/users/owner/installation").respond_with_json({ "id": 12345, "target_type": "User", "permissions": { "checks": "write", "contents": "write", "pull_requests": "write", }, "account": { "login": "******", "id": 12345 }, }) httpserver.expect_request( "/app/installations/12345/access_tokens").respond_with_json({ "token": "<token>", "expires_at": "2100-12-31T23:59:59Z" }) httpserver.expect_oneshot_request("/").respond_with_json( {"message": abuse_message}, status=403, ) with mock.patch( "mergify_engine.config.GITHUB_API_URL", httpserver.url_for("/")[:-1], ): async with github.AsyncGithubInstallationClient( github.get_auth(github_types.GitHubLogin("owner"))) as client: with pytest.raises(http.HTTPClientSideError) as exc_info: await client.get(httpserver.url_for("/")) assert exc_info.value.message == abuse_message assert exc_info.value.status_code == 403 assert exc_info.value.response.status_code == 403 assert str(exc_info.value.request.url) == httpserver.url_for("/") assert len(httpserver.log) == 3 httpserver.check_assertions()
async def _do_test_client_retry_429( httpserver: httpserver.HTTPServer, retry_after: str, expected_seconds: int ) -> None: records = [] def record_date(_): records.append(datetime.datetime.utcnow()) return Response("It works now !", 200) httpserver.expect_oneshot_request("/").respond_with_data( "This is an 429 error", status=429, headers={"Retry-After": retry_after} ) httpserver.expect_request("/").respond_with_handler(record_date) async with http.AsyncClient() as client: now = datetime.datetime.utcnow() await client.get(httpserver.url_for("/")) assert len(httpserver.log) == 2 elapsed_seconds = (records[0] - now).total_seconds() assert expected_seconds - 1 < elapsed_seconds <= expected_seconds + 1 httpserver.check_assertions()
def test_client_temporary_HTTP_500(httpserver: httpserver.HTTPServer) -> None: httpserver.expect_oneshot_request("/").respond_with_data( "This is an 5XX error", status=500) httpserver.expect_oneshot_request("/").respond_with_data( "This is an 5XX error", status=500) httpserver.expect_oneshot_request("/").respond_with_data( "This is an 5XX error", status=500) httpserver.expect_request("/").respond_with_data("It works now !", status=200) with http.Client() as client: client.get(httpserver.url_for("/")) # 4 retries assert len(httpserver.log) == 4 httpserver.check_assertions()