Skip to content

Commit 2245192

Browse files
Make test infrastructure ports configurable via environment variables (#475)
1 parent e28c85a commit 2245192

File tree

8 files changed

+62
-35
lines changed

8 files changed

+62
-35
lines changed

Caddyfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
storage file_system /caddy_storage
55
}
66

7-
https://localhost:8443 {
7+
https://localhost:{$HTTPBIN_HTTPS_PORT:8443} {
88
reverse_proxy httpbin:80
99
}
1010

11-
http://localhost:8080 {
11+
http://localhost:{$HTTPBIN_HTTP_PORT:8080} {
1212
reverse_proxy httpbin:80
1313
}
1414

15-
https://caddyhttpbin:8443 {
15+
https://caddyhttpbin:{$HTTPBIN_HTTPS_PORT:8443} {
1616
reverse_proxy httpbin:80
1717
}
1818

19-
http://caddyhttpbin:8080 {
19+
http://caddyhttpbin:{$HTTPBIN_HTTP_PORT:8080} {
2020
reverse_proxy httpbin:80
2121
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ DOCKER_USER="$UID:$GID" docker compose up --detach # or podman-compose up --deta
134134
mix test --include proxy
135135
```
136136

137+
If the default ports are already in use, you can configure them via environment variables:
138+
139+
```sh
140+
TINYPROXY_PORT=8887 docker compose up --detach
141+
TINYPROXY_PORT=8887 mix test --include proxy
142+
```
143+
144+
Available port variables: `TINYPROXY_PORT` (default 8888), `TINYPROXY_AUTH_PORT` (default 8889), `HTTPBIN_HTTP_PORT` (default 8080), `HTTPBIN_HTTPS_PORT` (default 8443).
145+
137146
## License
138147

139148
Copyright 2018 Eric Meadows-Jönsson and Andrea Leopardi

docker-compose.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ services:
44
proxy:
55
build: ./test/docker/tinyproxy
66
ports:
7-
- "8888:8888"
7+
- "${TINYPROXY_PORT:-8888}:8888"
88

99
proxy-auth:
1010
build: ./test/docker/tinyproxy-auth
1111
ports:
12-
- "8889:8888"
12+
- "${TINYPROXY_AUTH_PORT:-8889}:8888"
1313

1414
httpbin:
1515
image: docker.io/kennethreitz/httpbin:latest
1616
platform: linux/amd64
1717
ports:
18-
- "8080:80"
18+
- "${HTTPBIN_HTTP_PORT:-8080}:80"
1919

2020
caddyhttpbin:
2121
image: docker.io/caddy:2.8.4-alpine
@@ -24,10 +24,13 @@ services:
2424
# because the container by default runs using the root user
2525
# and we are not root in the GH action so we cannot access them.
2626
user: "${DOCKER_USER}"
27+
environment:
28+
- HTTPBIN_HTTP_PORT=${HTTPBIN_HTTP_PORT:-8080}
29+
- HTTPBIN_HTTPS_PORT=${HTTPBIN_HTTPS_PORT:-8443}
2730
volumes:
2831
# The :z mount option solves issues with SELinux.
2932
# See https://github.com/elixir-mint/mint/pull/406.
3033
- "./caddy_storage:/caddy_storage:z"
3134
- "./Caddyfile:/etc/caddy/Caddyfile:z"
3235
ports:
33-
- "8443:8443"
36+
- "${HTTPBIN_HTTPS_PORT:-8443}:${HTTPBIN_HTTPS_PORT:-8443}"

test/mint/http1/integration_test.exs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Mint.HTTP1.IntegrationTest do
99

1010
describe "local httpbin" do
1111
test "200 response" do
12-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
12+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
1313
assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/", [], nil)
1414
assert {:ok, conn, responses} = receive_stream(conn)
1515

@@ -22,7 +22,7 @@ defmodule Mint.HTTP1.IntegrationTest do
2222
end
2323

2424
test "POST body" do
25-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
25+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
2626
assert {:ok, conn, request} = HTTP1.request(conn, "POST", "/post", [], "BODY")
2727
assert {:ok, conn, responses} = receive_stream(conn)
2828

@@ -35,7 +35,7 @@ defmodule Mint.HTTP1.IntegrationTest do
3535

3636
test "POST body streaming" do
3737
headers = [{"content-length", "4"}]
38-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
38+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
3939
assert {:ok, conn, request} = HTTP1.request(conn, "POST", "/post", headers, :stream)
4040
assert {:ok, conn} = HTTP1.stream_request_body(conn, request, "BO")
4141
assert {:ok, conn} = HTTP1.stream_request_body(conn, request, "DY")
@@ -50,7 +50,7 @@ defmodule Mint.HTTP1.IntegrationTest do
5050
end
5151

5252
test "pipelining" do
53-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
53+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
5454
assert {:ok, conn, request1} = HTTP1.request(conn, "GET", "/", [], nil)
5555
assert {:ok, conn, request2} = HTTP1.request(conn, "GET", "/", [], nil)
5656
assert {:ok, conn, request3} = HTTP1.request(conn, "GET", "/", [], nil)
@@ -71,7 +71,7 @@ defmodule Mint.HTTP1.IntegrationTest do
7171
end
7272

7373
test "chunked with no chunks" do
74-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
74+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
7575
assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/stream-bytes/0", [], nil)
7676

7777
assert {:ok, _conn, [_status, _headers | responses]} = receive_stream(conn)
@@ -80,7 +80,7 @@ defmodule Mint.HTTP1.IntegrationTest do
8080
end
8181

8282
test "chunked with single chunk" do
83-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
83+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
8484

8585
assert {:ok, conn, request} =
8686
HTTP1.request(conn, "GET", "/stream-bytes/1024?chunk_size=1024", [], nil)
@@ -91,7 +91,7 @@ defmodule Mint.HTTP1.IntegrationTest do
9191
end
9292

9393
test "chunked with multiple chunks" do
94-
assert {:ok, conn} = HTTP1.connect(:http, "localhost", 8080)
94+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", HttpBin.http_port())
9595

9696
assert {:ok, conn, request} =
9797
HTTP1.request(conn, "GET", "/stream-bytes/1024?chunk_size=100", [], nil)

test/mint/integration_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ defmodule Mint.IntegrationTest do
157157
test "200 response - http://httpbin.org" do
158158
assert {:ok, conn} =
159159
HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(),
160-
proxy: {:http, "localhost", 8888, []}
160+
proxy: {:http, "localhost", HttpBin.proxy_port(), []}
161161
)
162162

163163
assert conn.__struct__ == Mint.UnsafeProxy
@@ -174,7 +174,7 @@ defmodule Mint.IntegrationTest do
174174
test "200 response - https://httpbin.org" do
175175
assert {:ok, conn} =
176176
HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
177-
proxy: {:http, "localhost", 8888, []},
177+
proxy: {:http, "localhost", HttpBin.proxy_port(), []},
178178
transport_opts: HttpBin.https_transport_opts()
179179
)
180180

@@ -191,7 +191,7 @@ defmodule Mint.IntegrationTest do
191191
test "200 response with explicit http2 - https://httpbin.org" do
192192
assert {:ok, conn} =
193193
HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
194-
proxy: {:http, "localhost", 8888, []},
194+
proxy: {:http, "localhost", HttpBin.proxy_port(), []},
195195
protocols: [:http2],
196196
transport_opts: HttpBin.https_transport_opts()
197197
)
@@ -210,7 +210,7 @@ defmodule Mint.IntegrationTest do
210210
test "200 response without explicit http2 - https://httpbin.org" do
211211
assert {:ok, conn} =
212212
HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
213-
proxy: {:http, "localhost", 8888, []},
213+
proxy: {:http, "localhost", HttpBin.proxy_port(), []},
214214
protocols: [:http1, :http2],
215215
transport_opts: HttpBin.https_transport_opts()
216216
)

test/mint/tunnel_proxy_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule Mint.TunnelProxyTest do
1515

1616
assert {:ok, conn} =
1717
Mint.TunnelProxy.connect(
18-
{:http, "localhost", 8888, []},
18+
{:http, "localhost", HttpBin.proxy_port(), []},
1919
{:http, HttpBin.proxy_host(), HttpBin.http_port(), []}
2020
)
2121

@@ -37,7 +37,7 @@ defmodule Mint.TunnelProxyTest do
3737
test "200 response - https://httpbin.org" do
3838
assert {:ok, conn} =
3939
Mint.TunnelProxy.connect(
40-
{:http, "localhost", 8888, []},
40+
{:http, "localhost", HttpBin.proxy_port(), []},
4141
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
4242
transport_opts: HttpBin.https_transport_opts()}
4343
)
@@ -55,7 +55,7 @@ defmodule Mint.TunnelProxyTest do
5555
test "407 response - proxy with missing authentication" do
5656
assert {:error, %Mint.HTTPError{reason: {:proxy, {:unexpected_status, 407}}}} =
5757
Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
58-
proxy: {:http, "localhost", 8889, []},
58+
proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []},
5959
transport_opts: HttpBin.https_transport_opts()
6060
)
6161
end
@@ -65,7 +65,7 @@ defmodule Mint.TunnelProxyTest do
6565

6666
assert {:error, %Mint.HTTPError{reason: {:proxy, {:unexpected_status, 401}}}} =
6767
Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
68-
proxy: {:http, "localhost", 8889, []},
68+
proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []},
6969
proxy_headers: [{"proxy-authorization", "basic #{invalid_auth64}"}],
7070
transport_opts: HttpBin.https_transport_opts()
7171
)
@@ -76,7 +76,7 @@ defmodule Mint.TunnelProxyTest do
7676

7777
assert {:ok, conn} =
7878
Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
79-
proxy: {:http, "localhost", 8889, []},
79+
proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []},
8080
proxy_headers: [{"proxy-authorization", "basic #{auth64}"}],
8181
transport_opts: HttpBin.https_transport_opts()
8282
)
@@ -94,7 +94,7 @@ defmodule Mint.TunnelProxyTest do
9494
test "200 response with explicit http2 - https://httpbin.org" do
9595
assert {:ok, conn} =
9696
Mint.TunnelProxy.connect(
97-
{:http, "localhost", 8888, []},
97+
{:http, "localhost", HttpBin.proxy_port(), []},
9898
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
9999
[protocols: [:http2], transport_opts: HttpBin.https_transport_opts()]}
100100
)
@@ -117,7 +117,7 @@ defmodule Mint.TunnelProxyTest do
117117
test "200 response without explicit http2 - https://httpbin.org" do
118118
assert {:ok, conn} =
119119
Mint.TunnelProxy.connect(
120-
{:http, "localhost", 8888, []},
120+
{:http, "localhost", HttpBin.proxy_port(), []},
121121
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
122122
[protocols: [:http1, :http2], transport_opts: HttpBin.https_transport_opts()]}
123123
)
@@ -141,7 +141,7 @@ defmodule Mint.TunnelProxyTest do
141141
test "do not support nested HTTPS connections - https://httpbin.org" do
142142
assert {:ok, conn} =
143143
Mint.TunnelProxy.connect(
144-
{:https, "localhost", 8888, []},
144+
{:https, "localhost", HttpBin.proxy_port(), []},
145145
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
146146
[transport_opts: HttpBin.https_transport_opts()]}
147147
)

test/mint/unsafe_proxy_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Mint.UnsafeProxyTest do
1111
test "200 response - http://httpbin.org" do
1212
assert {:ok, conn} =
1313
UnsafeProxy.connect(
14-
{:http, "localhost", 8888},
14+
{:http, "localhost", HttpBin.proxy_port()},
1515
{:http, HttpBin.proxy_host(), HttpBin.http_port()}
1616
)
1717

@@ -28,7 +28,7 @@ defmodule Mint.UnsafeProxyTest do
2828
test "407 response - proxy with missing authentication" do
2929
assert {:ok, conn} =
3030
HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(),
31-
proxy: {:http, "localhost", 8889, []}
31+
proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []}
3232
)
3333

3434
assert {:ok, conn, request} = HTTP.request(conn, "GET", "/", [], nil)
@@ -42,7 +42,7 @@ defmodule Mint.UnsafeProxyTest do
4242

4343
assert {:ok, conn} =
4444
HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(),
45-
proxy: {:http, "localhost", 8889, []},
45+
proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []},
4646
proxy_headers: [{"proxy-authorization", "basic #{invalid_auth64}"}]
4747
)
4848

@@ -57,7 +57,7 @@ defmodule Mint.UnsafeProxyTest do
5757

5858
assert {:ok, conn} =
5959
HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(),
60-
proxy: {:http, "localhost", 8889, []},
60+
proxy: {:http, "localhost", HttpBin.proxy_auth_port(), []},
6161
proxy_headers: [{"proxy-authorization", "basic #{auth64}"}]
6262
)
6363

@@ -73,7 +73,7 @@ defmodule Mint.UnsafeProxyTest do
7373
test "Mint.HTTP.protocol/1 on an unsafe proxy connection" do
7474
assert {:ok, %UnsafeProxy{} = conn} =
7575
UnsafeProxy.connect(
76-
{:http, "localhost", 8888},
76+
{:http, "localhost", HttpBin.proxy_port()},
7777
{:http, HttpBin.proxy_host(), HttpBin.http_port()}
7878
)
7979

@@ -86,7 +86,7 @@ defmodule Mint.UnsafeProxyTest do
8686

8787
assert {:ok, %UnsafeProxy{state: %{socket: socket}} = conn} =
8888
UnsafeProxy.connect(
89-
{:http, "localhost", 8888},
89+
{:http, "localhost", HttpBin.proxy_port()},
9090
{:http, HttpBin.proxy_host(), HttpBin.http_port()}
9191
)
9292

test/support/mint/http_bin.ex

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,29 @@ defmodule Mint.HttpBin do
1010
end
1111

1212
def http_port() do
13-
8080
13+
get_env_port("HTTPBIN_HTTP_PORT", 8080)
1414
end
1515

1616
def https_port() do
17-
8443
17+
get_env_port("HTTPBIN_HTTPS_PORT", 8443)
18+
end
19+
20+
def proxy_port() do
21+
get_env_port("TINYPROXY_PORT", 8888)
22+
end
23+
24+
def proxy_auth_port() do
25+
get_env_port("TINYPROXY_AUTH_PORT", 8889)
1826
end
1927

2028
def https_transport_opts() do
2129
[cacertfile: "caddy_storage/pki/authorities/local/root.crt"]
2230
end
31+
32+
defp get_env_port(env_var, default) do
33+
case System.get_env(env_var) do
34+
nil -> default
35+
value -> String.to_integer(value)
36+
end
37+
end
2338
end

0 commit comments

Comments
 (0)