From fdf68103bcdb47010d27dde375b08fbb8151fb73 Mon Sep 17 00:00:00 2001 From: shabbywu Date: Thu, 30 Dec 2021 11:47:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20APIEndpoint=20?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=AB=AF=E5=8F=A3=E5=AF=B9=E4=BA=8E=20http?= =?UTF-8?q?=20=E6=9C=8D=E5=8A=A1=E8=AE=BE=E7=BD=AE=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- moby_distribution/__init__.py | 2 +- moby_distribution/spec/endpoint.py | 6 +++++- pyproject.toml | 2 +- tests/spec/test_endpoint.py | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/moby_distribution/__init__.py b/moby_distribution/__init__.py index 2aa427a..bea96f2 100644 --- a/moby_distribution/__init__.py +++ b/moby_distribution/__init__.py @@ -7,7 +7,7 @@ from moby_distribution.spec.image_json import ImageJSON from moby_distribution.spec.manifest import ManifestSchema1, ManifestSchema2, OCIManifestSchema1 -__version__ = "0.3.4" +__version__ = "0.3.5" __ALL__ = [ "DockerRegistryV2Client", "Blob", diff --git a/moby_distribution/spec/endpoint.py b/moby_distribution/spec/endpoint.py index 796bd81..a6bf113 100644 --- a/moby_distribution/spec/endpoint.py +++ b/moby_distribution/spec/endpoint.py @@ -46,7 +46,11 @@ def api_base_url(self) -> str: parts = match.groupdict() hostname = parts["domain"] or parts["ipv4"] or parts["ipv6"] - port = int(parts["port"] or 443) + + port = parts["port"] + if not port: + port = "443" if self.is_secure_repository()[0] else "80" + path = parts["path"] or "" return f"{hostname}:{port}{path}" diff --git a/pyproject.toml b/pyproject.toml index 1734b20..44af292 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "moby-distribution" -version = "0.3.4" +version = "0.3.5" description = "Yet another moby(docker) distribution implement by python." authors = ["shabbywu "] license = "Apache-2.0" diff --git a/tests/spec/test_endpoint.py b/tests/spec/test_endpoint.py index 0b91137..fa19a10 100644 --- a/tests/spec/test_endpoint.py +++ b/tests/spec/test_endpoint.py @@ -5,6 +5,7 @@ import ssl import threading from http.server import HTTPServer, SimpleHTTPRequestHandler +from unittest import mock import pytest from cryptography import x509 @@ -115,3 +116,19 @@ def server(request): ) def test_is_secure_repository(server, expected): assert APIEndpoint(url=f"{server[0]}:{server[1]}").is_secure_repository() == expected + + +@pytest.mark.parametrize( + "url, support_https, expected", + [ + ("127.0.0.1", False, "127.0.0.1:80"), + ("127.0.0.1", True, "127.0.0.1:443"), + ("127.0.0.1:5000", False, "127.0.0.1:5000"), + ("127.0.0.1:5000", True, "127.0.0.1:5000"), + ], +) +def test_api_base_url(url, support_https, expected): + with mock.patch( + "moby_distribution.spec.endpoint.APIEndpoint.is_secure_repository", return_value=[support_https, False] + ): + assert APIEndpoint(url=url).api_base_url == expected