From 7805ac207897a323d9e4d74b7664cbf3abde2dd0 Mon Sep 17 00:00:00 2001 From: Johan Carlsson Date: Sat, 24 Jan 2026 12:52:51 +0100 Subject: [PATCH] Make test infrastructure ports configurable via environment variables Add support for configuring Docker Compose and test ports via environment variables with defaults matching the existing hardcoded values: - TINYPROXY_PORT (default: 8888) - TINYPROXY_AUTH_PORT (default: 8889) - HTTPBIN_HTTP_PORT (default: 8080) - HTTPBIN_HTTPS_PORT (default: 8443) This allows running tests when default ports are already in use. --- Caddyfile | 8 ++++---- README.md | 9 +++++++++ docker-compose.yml | 11 +++++++---- test/mint/http1/integration_test.exs | 14 +++++++------- test/mint/integration_test.exs | 8 ++++---- test/mint/tunnel_proxy_test.exs | 16 ++++++++-------- test/mint/unsafe_proxy_test.exs | 12 ++++++------ test/support/mint/http_bin.ex | 19 +++++++++++++++++-- 8 files changed, 62 insertions(+), 35 deletions(-) diff --git a/Caddyfile b/Caddyfile index df7190ce..dbd52ff1 100644 --- a/Caddyfile +++ b/Caddyfile @@ -4,18 +4,18 @@ storage file_system /caddy_storage } -https://localhost:8443 { +https://localhost:{$HTTPBIN_HTTPS_PORT:8443} { reverse_proxy httpbin:80 } -http://localhost:8080 { +http://localhost:{$HTTPBIN_HTTP_PORT:8080} { reverse_proxy httpbin:80 } -https://caddyhttpbin:8443 { +https://caddyhttpbin:{$HTTPBIN_HTTPS_PORT:8443} { reverse_proxy httpbin:80 } -http://caddyhttpbin:8080 { +http://caddyhttpbin:{$HTTPBIN_HTTP_PORT:8080} { reverse_proxy httpbin:80 } diff --git a/README.md b/README.md index 81ebdc41..9a538b05 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,15 @@ DOCKER_USER="$UID:$GID" docker compose up --detach # or podman-compose up --deta mix test --include proxy ``` +If the default ports are already in use, you can configure them via environment variables: + +```sh +TINYPROXY_PORT=8887 docker compose up --detach +TINYPROXY_PORT=8887 mix test --include proxy +``` + +Available port variables: `TINYPROXY_PORT` (default 8888), `TINYPROXY_AUTH_PORT` (default 8889), `HTTPBIN_HTTP_PORT` (default 8080), `HTTPBIN_HTTPS_PORT` (default 8443). + ## License Copyright 2018 Eric Meadows-Jönsson and Andrea Leopardi diff --git a/docker-compose.yml b/docker-compose.yml index 29eddbc3..e602d3f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,18 +4,18 @@ services: proxy: build: ./test/docker/tinyproxy ports: - - "8888:8888" + - "${TINYPROXY_PORT:-8888}:8888" proxy-auth: build: ./test/docker/tinyproxy-auth ports: - - "8889:8888" + - "${TINYPROXY_AUTH_PORT:-8889}:8888" httpbin: image: docker.io/kennethreitz/httpbin:latest platform: linux/amd64 ports: - - "8080:80" + - "${HTTPBIN_HTTP_PORT:-8080}:80" caddyhttpbin: image: docker.io/caddy:2.8.4-alpine @@ -24,10 +24,13 @@ services: # because the container by default runs using the root user # and we are not root in the GH action so we cannot access them. user: "${DOCKER_USER}" + environment: + - HTTPBIN_HTTP_PORT=${HTTPBIN_HTTP_PORT:-8080} + - HTTPBIN_HTTPS_PORT=${HTTPBIN_HTTPS_PORT:-8443} volumes: # The :z mount option solves issues with SELinux. # See https://github.com/elixir-mint/mint/pull/406. - "./caddy_storage:/caddy_storage:z" - "./Caddyfile:/etc/caddy/Caddyfile:z" ports: - - "8443:8443" + - "${HTTPBIN_HTTPS_PORT:-8443}:${HTTPBIN_HTTPS_PORT:-8443}" diff --git a/test/mint/http1/integration_test.exs b/test/mint/http1/integration_test.exs index 17f6dfa7..6c740711 100644 --- a/test/mint/http1/integration_test.exs +++ b/test/mint/http1/integration_test.exs @@ -9,7 +9,7 @@ defmodule Mint.HTTP1.IntegrationTest do describe "local httpbin" do test "200 response" do - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/", [], nil) assert {:ok, conn, responses} = receive_stream(conn) @@ -22,7 +22,7 @@ defmodule Mint.HTTP1.IntegrationTest do end test "POST body" do - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request} = HTTP1.request(conn, "POST", "/post", [], "BODY") assert {:ok, conn, responses} = receive_stream(conn) @@ -35,7 +35,7 @@ defmodule Mint.HTTP1.IntegrationTest do test "POST body streaming" do headers = [{"content-length", "4"}] - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request} = HTTP1.request(conn, "POST", "/post", headers, :stream) assert {:ok, conn} = HTTP1.stream_request_body(conn, request, "BO") assert {:ok, conn} = HTTP1.stream_request_body(conn, request, "DY") @@ -50,7 +50,7 @@ defmodule Mint.HTTP1.IntegrationTest do end test "pipelining" do - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request1} = HTTP1.request(conn, "GET", "/", [], nil) assert {:ok, conn, request2} = HTTP1.request(conn, "GET", "/", [], nil) assert {:ok, conn, request3} = HTTP1.request(conn, "GET", "/", [], nil) @@ -71,7 +71,7 @@ defmodule Mint.HTTP1.IntegrationTest do end test "chunked with no chunks" do - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/stream-bytes/0", [], nil) assert {:ok, _conn, [_status, _headers | responses]} = receive_stream(conn) @@ -80,7 +80,7 @@ defmodule Mint.HTTP1.IntegrationTest do end test "chunked with single chunk" do - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/stream-bytes/1024?chunk_size=1024", [], nil) @@ -91,7 +91,7 @@ defmodule Mint.HTTP1.IntegrationTest do end test "chunked with multiple chunks" do - assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080) + assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port()) assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/stream-bytes/1024?chunk_size=100", [], nil) diff --git a/test/mint/integration_test.exs b/test/mint/integration_test.exs index 2343076d..619dfecf 100644 --- a/test/mint/integration_test.exs +++ b/test/mint/integration_test.exs @@ -157,7 +157,7 @@ defmodule Mint.IntegrationTest do test "200 response - http://httpbin.org" do assert {:ok, conn} = HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(), - proxy: {:http, "localhost", 8888, []} + proxy: {:http, "localhost", HttpBin.proxy_port(), []} ) assert conn.__struct__ == Mint.UnsafeProxy @@ -174,7 +174,7 @@ defmodule Mint.IntegrationTest do test "200 response - https://httpbin.org" do assert {:ok, conn} = HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(), - proxy: {:http, "localhost", 8888, []}, + proxy: {:http, "localhost", HttpBin.proxy_port(), []}, transport_opts: HttpBin.https_transport_opts() ) @@ -191,7 +191,7 @@ defmodule Mint.IntegrationTest do test "200 response with explicit http2 - https://httpbin.org" do assert {:ok, conn} = HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(), - proxy: {:http, "localhost", 8888, []}, + proxy: {:http, "localhost", HttpBin.proxy_port(), []}, protocols: [:http2], transport_opts: HttpBin.https_transport_opts() ) @@ -210,7 +210,7 @@ defmodule Mint.IntegrationTest do test "200 response without explicit http2 - https://httpbin.org" do assert {:ok, conn} = HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(), - proxy: {:http, "localhost", 8888, []}, + proxy: {:http, "localhost", HttpBin.proxy_port(), []}, protocols: [:http1, :http2], transport_opts: HttpBin.https_transport_opts() ) diff --git a/test/mint/tunnel_proxy_test.exs b/test/mint/tunnel_proxy_test.exs index a80bb15a..2d7b9864 100644 --- a/test/mint/tunnel_proxy_test.exs +++ b/test/mint/tunnel_proxy_test.exs @@ -15,7 +15,7 @@ defmodule Mint.TunnelProxyTest do assert {:ok, conn} = Mint.TunnelProxy.connect( - {:http, "localhost", 8888, []}, + {:http, "localhost", HttpBin.proxy_port(), []}, {:http, HttpBin.proxy_host(), HttpBin.http_port(), []} ) @@ -37,7 +37,7 @@ defmodule Mint.TunnelProxyTest do test "200 response - https://httpbin.org" do assert {:ok, conn} = Mint.TunnelProxy.connect( - {:http, "localhost", 8888, []}, + {:http, "localhost", HttpBin.proxy_port(), []}, {:https, HttpBin.proxy_host(), HttpBin.https_port(), transport_opts: HttpBin.https_transport_opts()} ) @@ -55,7 +55,7 @@ defmodule Mint.TunnelProxyTest do test "407 response - proxy with missing authentication" do assert {:error, %Mint.HTTPError{reason: {:proxy, {:unexpected_status, 407}}}} = Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(), - proxy: {:http, "localhost", 8889, []}, + proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []}, transport_opts: HttpBin.https_transport_opts() ) end @@ -65,7 +65,7 @@ defmodule Mint.TunnelProxyTest do assert {:error, %Mint.HTTPError{reason: {:proxy, {:unexpected_status, 401}}}} = Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(), - proxy: {:http, "localhost", 8889, []}, + proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []}, proxy_headers: [{"proxy-authorization", "basic #{invalid_auth64}"}], transport_opts: HttpBin.https_transport_opts() ) @@ -76,7 +76,7 @@ defmodule Mint.TunnelProxyTest do assert {:ok, conn} = Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(), - proxy: {:http, "localhost", 8889, []}, + proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []}, proxy_headers: [{"proxy-authorization", "basic #{auth64}"}], transport_opts: HttpBin.https_transport_opts() ) @@ -94,7 +94,7 @@ defmodule Mint.TunnelProxyTest do test "200 response with explicit http2 - https://httpbin.org" do assert {:ok, conn} = Mint.TunnelProxy.connect( - {:http, "localhost", 8888, []}, + {:http, "localhost", HttpBin.proxy_port(), []}, {:https, HttpBin.proxy_host(), HttpBin.https_port(), [protocols: [:http2], transport_opts: HttpBin.https_transport_opts()]} ) @@ -117,7 +117,7 @@ defmodule Mint.TunnelProxyTest do test "200 response without explicit http2 - https://httpbin.org" do assert {:ok, conn} = Mint.TunnelProxy.connect( - {:http, "localhost", 8888, []}, + {:http, "localhost", HttpBin.proxy_port(), []}, {:https, HttpBin.proxy_host(), HttpBin.https_port(), [protocols: [:http1, :http2], transport_opts: HttpBin.https_transport_opts()]} ) @@ -141,7 +141,7 @@ defmodule Mint.TunnelProxyTest do test "do not support nested HTTPS connections - https://httpbin.org" do assert {:ok, conn} = Mint.TunnelProxy.connect( - {:https, "localhost", 8888, []}, + {:https, "localhost", HttpBin.proxy_port(), []}, {:https, HttpBin.proxy_host(), HttpBin.https_port(), [transport_opts: HttpBin.https_transport_opts()]} ) diff --git a/test/mint/unsafe_proxy_test.exs b/test/mint/unsafe_proxy_test.exs index 9faf1c68..536034ce 100644 --- a/test/mint/unsafe_proxy_test.exs +++ b/test/mint/unsafe_proxy_test.exs @@ -11,7 +11,7 @@ defmodule Mint.UnsafeProxyTest do test "200 response - http://httpbin.org" do assert {:ok, conn} = UnsafeProxy.connect( - {:http, "localhost", 8888}, + {:http, "localhost", HttpBin.proxy_port()}, {:http, HttpBin.proxy_host(), HttpBin.http_port()} ) @@ -28,7 +28,7 @@ defmodule Mint.UnsafeProxyTest do test "407 response - proxy with missing authentication" do assert {:ok, conn} = HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(), - proxy: {:http, "localhost", 8889, []} + proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []} ) assert {:ok, conn, request} = HTTP.request(conn, "GET", "/", [], nil) @@ -42,7 +42,7 @@ defmodule Mint.UnsafeProxyTest do assert {:ok, conn} = HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(), - proxy: {:http, "localhost", 8889, []}, + proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []}, proxy_headers: [{"proxy-authorization", "basic #{invalid_auth64}"}] ) @@ -57,7 +57,7 @@ defmodule Mint.UnsafeProxyTest do assert {:ok, conn} = HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(), - proxy: {:http, "localhost", 8889, []}, + proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []}, proxy_headers: [{"proxy-authorization", "basic #{auth64}"}] ) @@ -73,7 +73,7 @@ defmodule Mint.UnsafeProxyTest do test "Mint.HTTP.protocol/1 on an unsafe proxy connection" do assert {:ok, %UnsafeProxy{} = conn} = UnsafeProxy.connect( - {:http, "localhost", 8888}, + {:http, "localhost", HttpBin.proxy_port()}, {:http, HttpBin.proxy_host(), HttpBin.http_port()} ) @@ -86,7 +86,7 @@ defmodule Mint.UnsafeProxyTest do assert {:ok, %UnsafeProxy{state: %{socket: socket}} = conn} = UnsafeProxy.connect( - {:http, "localhost", 8888}, + {:http, "localhost", HttpBin.proxy_port()}, {:http, HttpBin.proxy_host(), HttpBin.http_port()} ) diff --git a/test/support/mint/http_bin.ex b/test/support/mint/http_bin.ex index c8de293b..46468abe 100644 --- a/test/support/mint/http_bin.ex +++ b/test/support/mint/http_bin.ex @@ -10,14 +10,29 @@ defmodule Mint.HttpBin do end def http_port() do - 8080 + get_env_port("HTTPBIN_HTTP_PORT", 8080) end def https_port() do - 8443 + get_env_port("HTTPBIN_HTTPS_PORT", 8443) + end + + def proxy_port() do + get_env_port("TINYPROXY_PORT", 8888) + end + + def proxy_auth_port() do + get_env_port("TINYPROXY_AUTH_PORT", 8889) end def https_transport_opts() do [cacertfile: "caddy_storage/pki/authorities/local/root.crt"] end + + defp get_env_port(env_var, default) do + case System.get_env(env_var) do + nil -> default + value -> String.to_integer(value) + end + end end