diff --git a/github/Requester.py b/github/Requester.py index 8970c9d1dd..48c7f00bea 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -386,7 +386,7 @@ def _refresh_token_if_needed(self) -> None: if not self.__installation_authorization: return if self._must_refresh_token(): - logging.debug("Refreshing access token") + self._logger.debug("Refreshing access token") self._refresh_token() def _refresh_token(self) -> None: @@ -617,7 +617,22 @@ def __requestRaw(self, cnx, verb, url, requestHeaders, input): return self.__requestRaw(original_cnx, verb, url, requestHeaders, input) if status == 301 and "location" in responseHeaders: - o = urllib.parse.urlparse(responseHeaders["location"]) + location = responseHeaders["location"] + o = urllib.parse.urlparse(location) + if o.scheme != self.__scheme: + raise RuntimeError(f"Github server redirected from {self.__scheme} protocol to {o.scheme}, " + f"please correct your Github server URL via base_url: Github(base_url=...)") + if o.hostname != self.__hostname: + raise RuntimeError(f"Github server redirected from host {self.__hostname} to {o.hostname}, " + f"please correct your Github server URL via base_url: Github(base_url=...)") + if o.path == url: + port = ":" + str(self.__port) if self.__port is not None else "" + requested_location = f"{self.__scheme}://{self.__hostname}{port}{url}" + raise RuntimeError(f"Requested {requested_location} but server redirected to {location}, " + f"you may need to correct your Github server URL " + f"via base_url: Github(base_url=...)") + if self._logger.isEnabledFor(logging.INFO): + self._logger.info(f"Following redirection from {url} to {o.path}") return self.__requestRaw(original_cnx, verb, o.path, requestHeaders, input) return status, responseHeaders, output @@ -671,10 +686,14 @@ def __createConnection(self): return self.__connection - def __log(self, verb, url, requestHeaders, input, status, responseHeaders, output): + @property + def _logger(self): if self.__logger is None: self.__logger = logging.getLogger(__name__) - if self.__logger.isEnabledFor(logging.DEBUG): + return self.__logger + + def __log(self, verb, url, requestHeaders, input, status, responseHeaders, output): + if self._logger.isEnabledFor(logging.DEBUG): headersForRequest = requestHeaders.copy() if "Authorization" in requestHeaders: if requestHeaders["Authorization"].startswith("Basic"): @@ -689,7 +708,7 @@ def __log(self, verb, url, requestHeaders, input, status, responseHeaders, outpu headersForRequest[ "Authorization" ] = "(unknown auth removed)" # pragma no cover (Cannot happen, but could if we add an authentication method => be prepared) - self.__logger.debug( + self._logger.debug( "%s %s://%s%s %s %s ==> %i %s %s", verb, self.__scheme, diff --git a/github/Requester.pyi b/github/Requester.pyi index be3c6aa00b..7c7f0877aa 100644 --- a/github/Requester.pyi +++ b/github/Requester.pyi @@ -1,5 +1,6 @@ from collections import OrderedDict from io import BufferedReader +from logging import Logger from typing import Any, Callable, Dict, Iterator, Optional, Tuple, Union from requests.models import Response @@ -51,6 +52,7 @@ class HTTPSRequestsConnectionClass: class Requester: __installation_authorization: Optional[InstallationAuthorization] = ... __app_auth: Optional[AppAuthentication] = ... + __logger: Logger def DEBUG_ON_RESPONSE( self, statusCode: int, responseHeader: Dict[str, str], data: str ) -> None: ... @@ -85,6 +87,8 @@ class Requester: headers: Dict[str, Any], output: str, ) -> Any: ... + @property + def _logger(self) -> Logger: ... def __log( self, verb: str, diff --git a/tests/ReplayData/Requester.testBaseUrlHostRedirection.txt b/tests/ReplayData/Requester.testBaseUrlHostRedirection.txt new file mode 100644 index 0000000000..c93c448fed --- /dev/null +++ b/tests/ReplayData/Requester.testBaseUrlHostRedirection.txt @@ -0,0 +1,11 @@ +https +GET +www.github.com +None +/repos/PyGithub/PyGithub +{'User-Agent': 'PyGithub/Python'} +None +301 +[('Content-Length', '0'), ('Location', 'https://github.com/repos/PyGithub/PyGithub')] + + diff --git a/tests/ReplayData/Requester.testBaseUrlPortRedirection.txt b/tests/ReplayData/Requester.testBaseUrlPortRedirection.txt new file mode 100644 index 0000000000..bee76296de --- /dev/null +++ b/tests/ReplayData/Requester.testBaseUrlPortRedirection.txt @@ -0,0 +1,11 @@ +https +GET +api.github.com +None +/repos/PyGithub/PyGithub +{'User-Agent': 'PyGithub/Python'} +None +301 +[('Content-Length', '0'), ('Location', 'https://api.github.com:443/repos/PyGithub/PyGithub')] + + diff --git a/tests/ReplayData/Requester.testBaseUrlPrefixRedirection.txt b/tests/ReplayData/Requester.testBaseUrlPrefixRedirection.txt new file mode 100644 index 0000000000..7a2ff05b54 --- /dev/null +++ b/tests/ReplayData/Requester.testBaseUrlPrefixRedirection.txt @@ -0,0 +1,22 @@ +https +GET +api.github.com +None +/api/v3/repos/PyGithub/PyGithub +{'User-Agent': 'PyGithub/Python'} +None +301 +[('Content-Length', '0'), ('Location', 'https://api.github.com/repos/PyGithub/PyGithub')] + + +https +GET +api.github.com +None +/repos/PyGithub/PyGithub +{'User-Agent': 'PyGithub/Python'} +None +200 +[('Server', 'GitHub.com'), ('Date', 'Tue, 09 May 2023 08:03:55 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Cache-Control', 'public, max-age=60, s-maxage=60'), ('Vary', 'Accept, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"c4bbbf57b6ff21caac0b59dec0ae83b3b9e66a234db40e22ee60c1b26b771a3f"'), ('Last-Modified', 'Tue, 09 May 2023 07:44:21 GMT'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-RateLimit-Limit', '60'), ('X-RateLimit-Remaining', '55'), ('X-RateLimit-Reset', '1683622021'), ('X-RateLimit-Resource', 'core'), ('X-RateLimit-Used', '5'), ('Accept-Ranges', 'bytes'), ('Transfer-Encoding', 'chunked'), ('X-GitHub-Request-Id', 'C22C:5529:6AEA2F:6BEA9A:6459FE6B')] +{"id":3544490,"node_id":"MDEwOlJlcG9zaXRvcnkzNTQ0NDkw","name":"PyGithub","full_name":"PyGithub/PyGithub","private":false,"owner":{"login":"PyGithub","id":11288996,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExMjg4OTk2","avatar_url":"https://avatars.githubusercontent.com/u/11288996?v=4","gravatar_id":"","url":"https://api.github.com/users/PyGithub","html_url":"https://github.com/PyGithub","followers_url":"https://api.github.com/users/PyGithub/followers","following_url":"https://api.github.com/users/PyGithub/following{/other_user}","gists_url":"https://api.github.com/users/PyGithub/gists{/gist_id}","starred_url":"https://api.github.com/users/PyGithub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyGithub/subscriptions","organizations_url":"https://api.github.com/users/PyGithub/orgs","repos_url":"https://api.github.com/users/PyGithub/repos","events_url":"https://api.github.com/users/PyGithub/events{/privacy}","received_events_url":"https://api.github.com/users/PyGithub/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/PyGithub/PyGithub","description":"Typed interactions with the GitHub API v3","fork":false,"url":"https://api.github.com/repos/PyGithub/PyGithub","forks_url":"https://api.github.com/repos/PyGithub/PyGithub/forks","keys_url":"https://api.github.com/repos/PyGithub/PyGithub/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PyGithub/PyGithub/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PyGithub/PyGithub/teams","hooks_url":"https://api.github.com/repos/PyGithub/PyGithub/hooks","issue_events_url":"https://api.github.com/repos/PyGithub/PyGithub/issues/events{/number}","events_url":"https://api.github.com/repos/PyGithub/PyGithub/events","assignees_url":"https://api.github.com/repos/PyGithub/PyGithub/assignees{/user}","branches_url":"https://api.github.com/repos/PyGithub/PyGithub/branches{/branch}","tags_url":"https://api.github.com/repos/PyGithub/PyGithub/tags","blobs_url":"https://api.github.com/repos/PyGithub/PyGithub/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PyGithub/PyGithub/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PyGithub/PyGithub/git/refs{/sha}","trees_url":"https://api.github.com/repos/PyGithub/PyGithub/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PyGithub/PyGithub/statuses/{sha}","languages_url":"https://api.github.com/repos/PyGithub/PyGithub/languages","stargazers_url":"https://api.github.com/repos/PyGithub/PyGithub/stargazers","contributors_url":"https://api.github.com/repos/PyGithub/PyGithub/contributors","subscribers_url":"https://api.github.com/repos/PyGithub/PyGithub/subscribers","subscription_url":"https://api.github.com/repos/PyGithub/PyGithub/subscription","commits_url":"https://api.github.com/repos/PyGithub/PyGithub/commits{/sha}","git_commits_url":"https://api.github.com/repos/PyGithub/PyGithub/git/commits{/sha}","comments_url":"https://api.github.com/repos/PyGithub/PyGithub/comments{/number}","issue_comment_url":"https://api.github.com/repos/PyGithub/PyGithub/issues/comments{/number}","contents_url":"https://api.github.com/repos/PyGithub/PyGithub/contents/{+path}","compare_url":"https://api.github.com/repos/PyGithub/PyGithub/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PyGithub/PyGithub/merges","archive_url":"https://api.github.com/repos/PyGithub/PyGithub/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PyGithub/PyGithub/downloads","issues_url":"https://api.github.com/repos/PyGithub/PyGithub/issues{/number}","pulls_url":"https://api.github.com/repos/PyGithub/PyGithub/pulls{/number}","milestones_url":"https://api.github.com/repos/PyGithub/PyGithub/milestones{/number}","notifications_url":"https://api.github.com/repos/PyGithub/PyGithub/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PyGithub/PyGithub/labels{/name}","releases_url":"https://api.github.com/repos/PyGithub/PyGithub/releases{/id}","deployments_url":"https://api.github.com/repos/PyGithub/PyGithub/deployments","created_at":"2012-02-25T12:53:47Z","updated_at":"2023-05-09T07:44:21Z","pushed_at":"2023-05-09T07:33:55Z","git_url":"git://github.com/PyGithub/PyGithub.git","ssh_url":"git@github.com:PyGithub/PyGithub.git","clone_url":"https://github.com/PyGithub/PyGithub.git","svn_url":"https://github.com/PyGithub/PyGithub","homepage":"https://pygithub.readthedocs.io/","size":13824,"stargazers_count":5996,"watchers_count":5996,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"has_discussions":true,"forks_count":1628,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":258,"license":{"key":"lgpl-3.0","name":"GNU Lesser General Public License v3.0","spdx_id":"LGPL-3.0","url":"https://api.github.com/licenses/lgpl-3.0","node_id":"MDc6TGljZW5zZTEy"},"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":["github","github-api","pygithub","python"],"visibility":"public","forks":1628,"open_issues":258,"watchers":5996,"default_branch":"master","temp_clone_token":null,"organization":{"login":"PyGithub","id":11288996,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExMjg4OTk2","avatar_url":"https://avatars.githubusercontent.com/u/11288996?v=4","gravatar_id":"","url":"https://api.github.com/users/PyGithub","html_url":"https://github.com/PyGithub","followers_url":"https://api.github.com/users/PyGithub/followers","following_url":"https://api.github.com/users/PyGithub/following{/other_user}","gists_url":"https://api.github.com/users/PyGithub/gists{/gist_id}","starred_url":"https://api.github.com/users/PyGithub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyGithub/subscriptions","organizations_url":"https://api.github.com/users/PyGithub/orgs","repos_url":"https://api.github.com/users/PyGithub/repos","events_url":"https://api.github.com/users/PyGithub/events{/privacy}","received_events_url":"https://api.github.com/users/PyGithub/received_events","type":"Organization","site_admin":false},"network_count":1628,"subscribers_count":115} + diff --git a/tests/ReplayData/Requester.testBaseUrlSchemeRedirection.txt b/tests/ReplayData/Requester.testBaseUrlSchemeRedirection.txt new file mode 100644 index 0000000000..cec965dfd8 --- /dev/null +++ b/tests/ReplayData/Requester.testBaseUrlSchemeRedirection.txt @@ -0,0 +1,11 @@ +http +GET +api.github.com +None +/repos/PyGithub/PyGithub +{'User-Agent': 'PyGithub/Python'} +None +301 +[('Content-Length', '0'), ('Location', 'https://api.github.com/repos/PyGithub/PyGithub')] + + diff --git a/tests/ReplayData/Requester.testLoggingRedirection.txt b/tests/ReplayData/Requester.testLoggingRedirection.txt new file mode 100644 index 0000000000..fea24909c1 --- /dev/null +++ b/tests/ReplayData/Requester.testLoggingRedirection.txt @@ -0,0 +1,22 @@ +https +GET +api.github.com +None +/repos/EnricoMi/test +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +None +301 +[('Server', 'GitHub.com'), ('Date', 'Tue, 09 May 2023 06:52:11 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Content-Length', '150'), ('X-OAuth-Scopes', 'repo'), ('X-Accepted-OAuth-Scopes', 'admin:repo_hook, delete_repo, read:repo_hook, repo, repo:status, repo_deployment, security_events, write:repo_hook'), ('github-authentication-token-expiration', '2023-06-08 06:12:06 UTC'), ('Location', 'https://api.github.com/repositories/638123443'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4988'), ('X-RateLimit-Reset', '1683617929'), ('X-RateLimit-Used', '12'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('X-GitHub-Request-Id', 'C484:49F3:317517:31F05B:6459ED9B')] +{"message":"Moved Permanently","url":"https://api.github.com/repositories/638123443","documentation_url":"https://docs.github.com/v3/#http-redirects"} + +https +GET +api.github.com +None +/repositories/638123443 +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +None +200 +[('Server', 'GitHub.com'), ('Date', 'Tue, 09 May 2023 06:52:11 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"851839f10d426070ef6e5fb497f190a4fdc29bcfc6b77ec6dbeaa7bedafbd759"'), ('Last-Modified', 'Tue, 09 May 2023 06:14:05 GMT'), ('X-OAuth-Scopes', 'repo'), ('X-Accepted-OAuth-Scopes', 'repo'), ('github-authentication-token-expiration', '2023-06-08 06:12:06 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4987'), ('X-RateLimit-Reset', '1683617929'), ('X-RateLimit-Used', '13'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'C48E:F5B6:2DB7D8:2E32F1:6459ED9B')] +{"id":638123443,"node_id":"R_kgDOJgj9sw","name":"test-renamed","full_name":"EnricoMi/test-renamed","private":true,"owner":{"login":"EnricoMi","id":44700269,"node_id":"MDQ6VXNlcjQ0NzAwMjY5","avatar_url":"https://avatars.githubusercontent.com/u/44700269?v=4","gravatar_id":"","url":"https://api.github.com/users/EnricoMi","html_url":"https://github.com/EnricoMi","followers_url":"https://api.github.com/users/EnricoMi/followers","following_url":"https://api.github.com/users/EnricoMi/following{/other_user}","gists_url":"https://api.github.com/users/EnricoMi/gists{/gist_id}","starred_url":"https://api.github.com/users/EnricoMi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/EnricoMi/subscriptions","organizations_url":"https://api.github.com/users/EnricoMi/orgs","repos_url":"https://api.github.com/users/EnricoMi/repos","events_url":"https://api.github.com/users/EnricoMi/events{/privacy}","received_events_url":"https://api.github.com/users/EnricoMi/received_events","type":"User","site_admin":false},"html_url":"https://github.com/EnricoMi/test-renamed","description":null,"fork":false,"url":"https://api.github.com/repos/EnricoMi/test-renamed","forks_url":"https://api.github.com/repos/EnricoMi/test-renamed/forks","keys_url":"https://api.github.com/repos/EnricoMi/test-renamed/keys{/key_id}","collaborators_url":"https://api.github.com/repos/EnricoMi/test-renamed/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/EnricoMi/test-renamed/teams","hooks_url":"https://api.github.com/repos/EnricoMi/test-renamed/hooks","issue_events_url":"https://api.github.com/repos/EnricoMi/test-renamed/issues/events{/number}","events_url":"https://api.github.com/repos/EnricoMi/test-renamed/events","assignees_url":"https://api.github.com/repos/EnricoMi/test-renamed/assignees{/user}","branches_url":"https://api.github.com/repos/EnricoMi/test-renamed/branches{/branch}","tags_url":"https://api.github.com/repos/EnricoMi/test-renamed/tags","blobs_url":"https://api.github.com/repos/EnricoMi/test-renamed/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/EnricoMi/test-renamed/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/EnricoMi/test-renamed/git/refs{/sha}","trees_url":"https://api.github.com/repos/EnricoMi/test-renamed/git/trees{/sha}","statuses_url":"https://api.github.com/repos/EnricoMi/test-renamed/statuses/{sha}","languages_url":"https://api.github.com/repos/EnricoMi/test-renamed/languages","stargazers_url":"https://api.github.com/repos/EnricoMi/test-renamed/stargazers","contributors_url":"https://api.github.com/repos/EnricoMi/test-renamed/contributors","subscribers_url":"https://api.github.com/repos/EnricoMi/test-renamed/subscribers","subscription_url":"https://api.github.com/repos/EnricoMi/test-renamed/subscription","commits_url":"https://api.github.com/repos/EnricoMi/test-renamed/commits{/sha}","git_commits_url":"https://api.github.com/repos/EnricoMi/test-renamed/git/commits{/sha}","comments_url":"https://api.github.com/repos/EnricoMi/test-renamed/comments{/number}","issue_comment_url":"https://api.github.com/repos/EnricoMi/test-renamed/issues/comments{/number}","contents_url":"https://api.github.com/repos/EnricoMi/test-renamed/contents/{+path}","compare_url":"https://api.github.com/repos/EnricoMi/test-renamed/compare/{base}...{head}","merges_url":"https://api.github.com/repos/EnricoMi/test-renamed/merges","archive_url":"https://api.github.com/repos/EnricoMi/test-renamed/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/EnricoMi/test-renamed/downloads","issues_url":"https://api.github.com/repos/EnricoMi/test-renamed/issues{/number}","pulls_url":"https://api.github.com/repos/EnricoMi/test-renamed/pulls{/number}","milestones_url":"https://api.github.com/repos/EnricoMi/test-renamed/milestones{/number}","notifications_url":"https://api.github.com/repos/EnricoMi/test-renamed/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/EnricoMi/test-renamed/labels{/name}","releases_url":"https://api.github.com/repos/EnricoMi/test-renamed/releases{/id}","deployments_url":"https://api.github.com/repos/EnricoMi/test-renamed/deployments","created_at":"2023-05-09T06:13:40Z","updated_at":"2023-05-09T06:14:05Z","pushed_at":"2023-05-09T06:13:40Z","git_url":"git://github.com/EnricoMi/test-renamed.git","ssh_url":"git@github.com:EnricoMi/test-renamed.git","clone_url":"https://github.com/EnricoMi/test-renamed.git","svn_url":"https://github.com/EnricoMi/test-renamed","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"has_discussions":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":0,"license":null,"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":[],"visibility":"private","forks":0,"open_issues":0,"watchers":0,"default_branch":"main","permissions":{"admin":true,"maintain":true,"push":true,"triage":true,"pull":true},"temp_clone_token":"AKVBE3OMGQYHDOJWMHTJI3DELHXMO","allow_squash_merge":true,"allow_merge_commit":true,"allow_rebase_merge":true,"allow_auto_merge":false,"delete_branch_on_merge":false,"allow_update_branch":false,"use_squash_pr_title_as_default":false,"squash_merge_commit_message":"COMMIT_MESSAGES","squash_merge_commit_title":"COMMIT_OR_PR_TITLE","merge_commit_message":"PR_TITLE","merge_commit_title":"MERGE_MESSAGE","network_count":0,"subscribers_count":0} + diff --git a/tests/Requester.py b/tests/Requester.py new file mode 100644 index 0000000000..685ed4ee9f --- /dev/null +++ b/tests/Requester.py @@ -0,0 +1,91 @@ +############################ 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 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 redirection from /api/v3/repos/PyGithub/PyGithub to /repos/PyGithub/PyGithub' + ) + + # test protocol change + # test host name change + # test appi path change \ No newline at end of file