From 9c8663ff54a94747634ec09323b40490eacb8de0 Mon Sep 17 00:00:00 2001 From: JP-Ellis Date: Thu, 21 Mar 2024 17:17:05 +1100 Subject: [PATCH] chore: replace localhost with ipv4 loopback The `localhost` may resolve to either the IPv4 or IPv6 loopback device; so to avoid ambiguity, we use the IPv4 loopback address. Signed-off-by: JP-Ellis --- .github/workflows/test.yml | 4 ++-- examples/conftest.py | 2 +- examples/docker-compose.yml | 2 +- examples/tests/test_00_consumer.py | 2 +- examples/tests/test_01_provider_fastapi.py | 4 ++-- examples/tests/test_01_provider_flask.py | 2 +- src/pact/cli/verify.py | 2 +- .../v3/interaction/sync_message_interaction.py | 6 +++--- src/pact/v3/pact.py | 6 +++--- src/pact/v3/verifier.py | 4 ++-- tests/v3/compatibility_suite/test_v1_provider.py | 2 +- .../v3/compatibility_suite/util/pact-broker.yml | 2 +- tests/v3/compatibility_suite/util/provider.py | 2 +- tests/v3/test_pact.py | 8 ++++---- tests/v3/test_verifier.py | 16 ++++++++-------- 15 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0695b7ff27..1d8fc0176b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -105,7 +105,7 @@ jobs: - name: Ensure broker is live run: | i=0 - until curl -sSf http://localhost:9292/diagnostic/status/heartbeat; do + until curl -sSf http://127.0.0.1:9292/diagnostic/status/heartbeat; do i=$((i+1)) if [ $i -gt 120 ]; then echo "Broker failed to start" @@ -116,7 +116,7 @@ jobs: - name: Examples run: > - hatch run example --broker-url=http://pactbroker:pactbroker@localhost:9292 + hatch run example --broker-url=http://pactbroker:pactbroker@127.0.0.1:9292 lint: name: Lint diff --git a/examples/conftest.py b/examples/conftest.py index 64b4ed7889..abba4e0a8d 100644 --- a/examples/conftest.py +++ b/examples/conftest.py @@ -47,7 +47,7 @@ def broker(request: pytest.FixtureRequest) -> Generator[URL, Any, None]: compose_file_name=["docker-compose.yml"], pull=True, ) as _: - yield URL("http://pactbroker:pactbroker@localhost:9292") + yield URL("http://pactbroker:pactbroker@127.0.0.1:9292") return diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml index 2405a49a9a..eb0ad424b4 100644 --- a/examples/docker-compose.yml +++ b/examples/docker-compose.yml @@ -36,7 +36,7 @@ services: "--silent", "--show-error", "--fail", - "http://pactbroker:pactbroker@localhost:9292/diagnostic/status/heartbeat", + "http://pactbroker:pactbroker@127.0.0.1:9292/diagnostic/status/heartbeat", ] interval: 1s timeout: 2s diff --git a/examples/tests/test_00_consumer.py b/examples/tests/test_00_consumer.py index a49f9664a8..aa0b84bb8c 100644 --- a/examples/tests/test_00_consumer.py +++ b/examples/tests/test_00_consumer.py @@ -33,7 +33,7 @@ log = logging.getLogger(__name__) -MOCK_URL = URL("http://localhost:8080") +MOCK_URL = URL("http://127.0.0.1:8080") @pytest.fixture() diff --git a/examples/tests/test_01_provider_fastapi.py b/examples/tests/test_01_provider_fastapi.py index 7ccd3128aa..2c134f723b 100644 --- a/examples/tests/test_01_provider_fastapi.py +++ b/examples/tests/test_01_provider_fastapi.py @@ -36,7 +36,7 @@ from examples.src.fastapi import app from pact import Verifier -PROVIDER_URL = URL("http://localhost:8080") +PROVIDER_URL = URL("http://127.0.0.1:8080") class ProviderState(BaseModel): @@ -79,7 +79,7 @@ def run_server() -> None: lambda cannot be used as the target of a `multiprocessing.Process` as it cannot be pickled. """ - host = PROVIDER_URL.host if PROVIDER_URL.host else "localhost" + host = PROVIDER_URL.host if PROVIDER_URL.host else "127.0.0.1" port = PROVIDER_URL.port if PROVIDER_URL.port else 8080 uvicorn.run(app, host=host, port=port) diff --git a/examples/tests/test_01_provider_flask.py b/examples/tests/test_01_provider_flask.py index ba5c39d432..bc3d820136 100644 --- a/examples/tests/test_01_provider_flask.py +++ b/examples/tests/test_01_provider_flask.py @@ -35,7 +35,7 @@ from examples.src.flask import app from pact import Verifier -PROVIDER_URL = URL("http://localhost:8080") +PROVIDER_URL = URL("http://127.0.0.1:8080") @app.route("/_pact/provider_states", methods=["POST"]) diff --git a/src/pact/cli/verify.py b/src/pact/cli/verify.py index b388a53001..e0f658cd3c 100644 --- a/src/pact/cli/verify.py +++ b/src/pact/cli/verify.py @@ -134,7 +134,7 @@ def main(pacts, base_url, pact_url, pact_urls, states_url, states_setup_url, Verify one or more contracts against a provider service. Minimal example: - pact-verifier --provider-base-url=http://localhost:8080 ./pacts + pact-verifier --provider-base-url=http://127.0.0.1:8080 ./pacts """ # NOQA error = click.style('Error:', fg='red') warning = click.style('Warning:', fg='yellow') diff --git a/src/pact/v3/interaction/sync_message_interaction.py b/src/pact/v3/interaction/sync_message_interaction.py index 59e32487ef..f7e7c291a0 100644 --- a/src/pact/v3/interaction/sync_message_interaction.py +++ b/src/pact/v3/interaction/sync_message_interaction.py @@ -274,7 +274,7 @@ def upon_receiving( def serve( # noqa: PLR0913 self, - addr: str = "localhost", + addr: str = "127.0.0.1", port: int = 0, transport: str = "http", transport_config: str | None = None, @@ -295,7 +295,7 @@ def serve( # noqa: PLR0913 Args: addr: - Address to bind the mock server to. Defaults to `localhost`. + Address to bind the mock server to. Defaults to `127.0.0.1`. port: Port to bind the mock server to. Defaults to `0`, which will @@ -457,7 +457,7 @@ class PactServer: def __init__( # noqa: PLR0913 self, pact_handle: pact.v3.ffi.PactHandle, - host: str = "localhost", + host: str = "127.0.0.1", port: int = 0, transport: str = "HTTP", transport_config: str | None = None, diff --git a/src/pact/v3/pact.py b/src/pact/v3/pact.py index 18bccac0bd..8ce9137873 100644 --- a/src/pact/v3/pact.py +++ b/src/pact/v3/pact.py @@ -234,7 +234,7 @@ def upon_receiving( def serve( # noqa: PLR0913 self, - addr: str = "localhost", + addr: str = "127.0.0.1", port: int = 0, transport: str = "http", transport_config: str | None = None, @@ -256,7 +256,7 @@ def serve( # noqa: PLR0913 Args: addr: - Address to bind the mock server to. Defaults to `localhost`. + Address to bind the mock server to. Defaults to `127.0.0.1`. port: Port to bind the mock server to. Defaults to `0`, which will @@ -424,7 +424,7 @@ class PactServer: def __init__( # noqa: PLR0913 self, pact_handle: pact.v3.ffi.PactHandle, - host: str = "localhost", + host: str = "127.0.0.1", port: int = 0, transport: str = "HTTP", transport_config: str | None = None, diff --git a/src/pact/v3/verifier.py b/src/pact/v3/verifier.py index 2e8ce85b58..5dbec2ca76 100644 --- a/src/pact/v3/verifier.py +++ b/src/pact/v3/verifier.py @@ -99,7 +99,7 @@ def set_info( # noqa: PLR0913 host: The provider hostname or IP address. If the provider is running - on the same machine as the verifier, `localhost` can be used. + on the same machine as the verifier, `127.0.0.1` can be used. port: The provider port. If not specified, the default port for the @@ -141,7 +141,7 @@ def set_info( # noqa: PLR0913 url = URL.build( scheme=scheme or "http", - host=host or "localhost", + host=host or "127.0.0.1", port=port, path=path or "", ) diff --git a/tests/v3/compatibility_suite/test_v1_provider.py b/tests/v3/compatibility_suite/test_v1_provider.py index 9b216962d4..c32ed493f9 100644 --- a/tests/v3/compatibility_suite/test_v1_provider.py +++ b/tests/v3/compatibility_suite/test_v1_provider.py @@ -499,7 +499,7 @@ def a_pact_file_for_interaction_is_to_be_verified_from_a_pact_broker( compose_file_name="pact-broker.yml", pull=True, ) as _: - pact_broker = PactBroker(URL("http://pactbroker:pactbroker@localhost:9292")) + pact_broker = PactBroker(URL("http://pactbroker:pactbroker@127.0.0.1:9292")) pact_broker.publish(pacts_dir) verifier.broker_source(pact_broker.url) yield pact_broker diff --git a/tests/v3/compatibility_suite/util/pact-broker.yml b/tests/v3/compatibility_suite/util/pact-broker.yml index 53b3f6d726..1e0d6b0e95 100644 --- a/tests/v3/compatibility_suite/util/pact-broker.yml +++ b/tests/v3/compatibility_suite/util/pact-broker.yml @@ -36,7 +36,7 @@ services: "--silent", "--show-error", "--fail", - "http://pactbroker:pactbroker@localhost:9292/diagnostic/status/heartbeat", + "http://pactbroker:pactbroker@127.0.0.1:9292/diagnostic/status/heartbeat", ] interval: 1s timeout: 2s diff --git a/tests/v3/compatibility_suite/util/provider.py b/tests/v3/compatibility_suite/util/provider.py index f90c359028..1277ee60d7 100644 --- a/tests/v3/compatibility_suite/util/provider.py +++ b/tests/v3/compatibility_suite/util/provider.py @@ -244,7 +244,7 @@ def run(self) -> None: """ Start the provider. """ - url = URL(f"http://localhost:{_find_free_port()}") + url = URL(f"http://127.0.0.1:{_find_free_port()}") self.app.run( host=url.host, port=url.port, diff --git a/tests/v3/test_pact.py b/tests/v3/test_pact.py index 5f32e7d801..0074b1dd6e 100644 --- a/tests/v3/test_pact.py +++ b/tests/v3/test_pact.py @@ -45,13 +45,13 @@ def test_empty_provider() -> None: def test_serve(pact: Pact) -> None: with pact.serve() as srv: assert srv.port > 0 - assert srv.host == "localhost" - assert str(srv).startswith("http://localhost") + assert srv.host == "127.0.0.1" + assert str(srv).startswith("http://127.0.0.1") assert srv.url.scheme == "http" - assert srv.url.host == "localhost" + assert srv.url.host == "127.0.0.1" assert srv.url.path == "/" assert srv / "foo" == srv.url / "foo" - assert str(srv / "foo") == f"http://localhost:{srv.port}/foo" + assert str(srv / "foo") == f"http://127.0.0.1:{srv.port}/foo" @pytest.mark.skip(reason="TODO: implement") diff --git a/tests/v3/test_verifier.py b/tests/v3/test_verifier.py index cedb665119..09bc0d3ac6 100644 --- a/tests/v3/test_verifier.py +++ b/tests/v3/test_verifier.py @@ -28,11 +28,11 @@ def test_str_repr(verifier: Verifier) -> None: def test_set_provider_info(verifier: Verifier) -> None: name = "test_provider" - url = "http://localhost:8888/api" + url = "http://127.0.0.1:8888/api" verifier.set_info(name, url=url) scheme = "http" - host = "localhost" + host = "127.0.0.1" port = 8888 path = "/api" verifier.set_info( @@ -106,7 +106,7 @@ def test_set_error_on_empty_pact(verifier: Verifier) -> None: def test_set_publish_options(verifier: Verifier) -> None: verifier.set_publish_options( version="1.0.0", - url="http://localhost:8080/build/1234", + url="http://127.0.0.1:8080/build/1234", branch="main", tags=["main", "test", "prod"], ) @@ -130,7 +130,7 @@ def test_add_custom_headers(verifier: Verifier) -> None: def test_add_source(verifier: Verifier) -> None: # URL - verifier.add_source("http://localhost:8080/pact.json") + verifier.add_source("http://127.0.0.1:8080/pact.json") # File verifier.add_source(ASSETS_DIR / "pacts" / "basic.json") @@ -140,21 +140,21 @@ def test_add_source(verifier: Verifier) -> None: def test_broker_source(verifier: Verifier) -> None: - verifier.broker_source("http://localhost:8080") + verifier.broker_source("http://127.0.0.1:8080") verifier.broker_source( - "http://localhost:8080", + "http://127.0.0.1:8080", username="user", password="password", # noqa: S106 ) verifier.broker_source( - "http://localhost:8080", + "http://127.0.0.1:8080", token="1234", # noqa: S106 ) def test_broker_source_selector(verifier: Verifier) -> None: ( - verifier.broker_source("http://localhost:8080", selector=True) + verifier.broker_source("http://127.0.0.1:8080", selector=True) .consumer_tags("main", "test") .provider_tags("main", "test") .consumer_versions("1.2.3")