Skip to content

Commit

Permalink
chore: replace localhost with ipv4 loopback
Browse files Browse the repository at this point in the history
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 <josh@jpellis.me>
  • Loading branch information
JP-Ellis committed Mar 21, 2024
1 parent 4a7fea8 commit 9c8663f
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/test_00_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/test_01_provider_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion examples/tests/test_01_provider_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion src/pact/cli/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions src/pact/v3/interaction/sync_message_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/pact/v3/pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/pact/v3/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "",
)
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/compatibility_suite/test_v1_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/compatibility_suite/util/pact-broker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/compatibility_suite/util/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/v3/test_pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 8 additions & 8 deletions tests/v3/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"],
)
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 9c8663f

Please sign in to comment.