############################ Copyrights and license ############################ # # # Copyright 2023 Enrico Minack # # # # This file is part of PyGithub. # # http://pygithub.readthedocs.io/ # # # # PyGithub is free software: you can redistribute it and/or modify it under # # the terms of the GNU Lesser General Public License as published by the Free # # Software Foundation, either version 3 of the License, or (at your option) # # any later version. # # # # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # # details. # # # # You should have received a copy of the GNU Lesser General Public License # # along with PyGithub. If not, see . # # # ################################################################################ from unittest import mock import github from . import Framework class Requester(Framework.TestCase): logger = None def setUp(self): super().setUp() self.logger = mock.MagicMock() github.Requester.Requester.injectLogger(self.logger) def tearDown(self): github.Requester.Requester.resetLogger() super().tearDown() def testLoggingRedirection(self): self.assertEqual(self.g.get_repo("EnricoMi/test").name, "test-renamed") self.logger.info.assert_called_once_with( "Following Github server redirection from /repos/EnricoMi/test to /repositories/638123443" ) def testBaseUrlSchemeRedirection(self): gh = github.Github(base_url="http://api.github.com") with self.assertRaises(RuntimeError) as exc: gh.get_repo("PyGithub/PyGithub") self.assertEqual( exc.exception.args, ( "Github server redirected from http protocol to https, please correct your " "Github server URL via base_url: Github(base_url=...)", ), ) def testBaseUrlHostRedirection(self): gh = github.Github(base_url="https://www.github.com") with self.assertRaises(RuntimeError) as exc: gh.get_repo("PyGithub/PyGithub") self.assertEqual( exc.exception.args, ( "Github server redirected from host www.github.com to github.com, " "please correct your Github server URL via base_url: Github(base_url=...)", ), ) def testBaseUrlPortRedirection(self): # replay data forged gh = github.Github(base_url="https://api.github.com") with self.assertRaises(RuntimeError) as exc: gh.get_repo("PyGithub/PyGithub") self.assertEqual( exc.exception.args, ( "Requested https://api.github.com/repos/PyGithub/PyGithub but server " "redirected to https://api.github.com:443/repos/PyGithub/PyGithub, " "you may need to correct your Github server URL " "via base_url: Github(base_url=...)", ), ) def testBaseUrlPrefixRedirection(self): # replay data forged gh = github.Github(base_url="https://api.github.com/api/v3") self.assertEqual(gh.get_repo("PyGithub/PyGithub").name, "PyGithub") self.logger.info.assert_called_once_with( "Following Github server redirection from /api/v3/repos/PyGithub/PyGithub to /repos/PyGithub/PyGithub" )