Skip to content

Commit

Permalink
fix: 修复 APIEndpoint 默认端口对于 http 服务设置错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
shabbywu committed Dec 30, 2021
1 parent 4bfbd21 commit fdf6810
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion moby_distribution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion moby_distribution/spec/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "Apache-2.0"
Expand Down
17 changes: 17 additions & 0 deletions tests/spec/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ssl
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler
from unittest import mock

import pytest
from cryptography import x509
Expand Down Expand Up @@ -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

0 comments on commit fdf6810

Please sign in to comment.