Skip to content

Commit 0855277

Browse files
Replace requests to httpbin.org with local requests (#401)
Adds Caddy to the docker-compose.yml file in order to serve HTTPS requests for the local httpbin.
1 parent 371f4d7 commit 0855277

File tree

12 files changed

+142
-40
lines changed

12 files changed

+142
-40
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
run: mix dialyzer --plt
6969

7070
- name: Start docker
71-
run: docker-compose up --detach
71+
run: DOCKER_USER="$UID:$GID" docker-compose up --detach
7272

7373
- name: Check for unused dependencies
7474
run: mix do deps.get, deps.unlock --check-unused

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ erl_crash.dump
2222
# Ignore package tarball (built via "mix hex.build").
2323
mint-*.tar
2424

25+
/caddy_storage
26+

Caddyfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
local_certs
3+
skip_install_trust
4+
storage file_system /caddy_storage
5+
}
6+
7+
https://localhost:8443 {
8+
reverse_proxy httpbin:80
9+
}
10+
11+
http://localhost:8080 {
12+
reverse_proxy httpbin:80
13+
}
14+
15+
https://caddyhttpbin:8443 {
16+
reverse_proxy httpbin:80
17+
}
18+
19+
http://caddyhttpbin:8080 {
20+
reverse_proxy httpbin:80
21+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Mint is a low-level client. If you need higher-level features such as connection
107107

108108
If you wish to contribute, check out the [issue list][issues] and let us know what you want to work on, so that we can discuss it and reduce duplicate work.
109109

110-
Tests are organized with tags. Integration tests that hit real websites over the internet are tagged with `:requires_internet_connection`. Proxy tests are tagged with `:proxy` and require that you run `docker-compose up` from the Mint root directory in order to run (they are excluded by default when you run `$ mix test`). A few examples of running tests:
110+
Tests are organized with tags. Integration tests that hit real websites over the internet are tagged with `:requires_internet_connection`. Proxy tests are tagged with `:proxy` and require that you run `DOCKER_USER="$UID:$GID" docker-compose up` from the Mint root directory in order to run (they are excluded by default when you run `$ mix test`). A few examples of running tests:
111111

112112
* `$ mix test` to run the test suite without caring about Docker and `docker-compose up`.
113113

caddy_storage/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

docker-compose.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,16 @@ services:
1515
image: kennethreitz/httpbin:latest
1616
ports:
1717
- "8080:80"
18-
- "8443:443"
18+
19+
caddyhttpbin:
20+
image: caddy:2.6.4-alpine
21+
# in github actions we want to access the files created
22+
# by caddy. however, in linux these end up being owned by root
23+
# because the container by defaults runs using the root user
24+
# and we are not root in the gh action so cannot access them
25+
user: "${DOCKER_USER}"
26+
volumes:
27+
- "./caddy_storage:/caddy_storage"
28+
- "./Caddyfile:/etc/caddy/Caddyfile"
29+
ports:
30+
- "8443:8443"

test/mint/http1/integration_test.exs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Mint.HTTP1.IntegrationTest do
33

44
import Mint.HTTP1.TestHelpers
55

6-
alias Mint.{TransportError, HTTP1}
6+
alias Mint.{TransportError, HTTP1, HttpBin}
77

88
describe "local httpbin" do
99
test "200 response" do
@@ -100,21 +100,24 @@ defmodule Mint.HTTP1.IntegrationTest do
100100
end
101101
end
102102

103-
describe "httpbin.org" do
104-
@describetag :requires_internet_connection
105-
103+
describe "twitter.com" do
106104
test "timeout with http" do
107105
assert {:error, %TransportError{reason: :timeout}} =
108-
HTTP1.connect(:http, "httpbin.org", 80, transport_opts: [timeout: 0])
106+
HTTP1.connect(:http, "twitter.com", 80, transport_opts: [timeout: 0])
109107
end
110108

111109
test "timeout with https" do
112110
assert {:error, %TransportError{reason: :timeout}} =
113-
HTTP1.connect(:https, "httpbin.org", 443, transport_opts: [timeout: 0])
111+
HTTP1.connect(:https, "twitter.com", 443, transport_opts: [timeout: 0])
114112
end
113+
end
115114

115+
describe "httpbin.org" do
116116
test "keep alive" do
117-
assert {:ok, conn} = HTTP1.connect(:https, "httpbin.org", 443)
117+
assert {:ok, conn} =
118+
HTTP1.connect(:https, HttpBin.host(), HttpBin.https_port(),
119+
transport_opts: HttpBin.https_transport_opts()
120+
)
118121

119122
assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/", [], nil)
120123
assert {:ok, conn, responses} = receive_stream(conn)
@@ -125,7 +128,10 @@ defmodule Mint.HTTP1.IntegrationTest do
125128
assert {:headers, ^request, _} = headers
126129
assert merge_body(responses, request) =~ "Other Utilities"
127130

128-
assert {:ok, conn} = HTTP1.connect(:https, "httpbin.org", 443)
131+
assert {:ok, conn} =
132+
HTTP1.connect(:https, HttpBin.host(), HttpBin.https_port(),
133+
transport_opts: HttpBin.https_transport_opts()
134+
)
129135

130136
assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/", [], nil)
131137
assert {:ok, conn, responses} = receive_stream(conn)
@@ -141,8 +147,8 @@ defmodule Mint.HTTP1.IntegrationTest do
141147
assert {:error, %TransportError{reason: reason}} =
142148
HTTP1.connect(
143149
:https,
144-
"httpbin.org",
145-
443,
150+
HttpBin.host(),
151+
HttpBin.https_port(),
146152
transport_opts: [
147153
cacertfile: "test/support/empty_cacerts.pem",
148154
log_alert: false,
@@ -157,7 +163,10 @@ defmodule Mint.HTTP1.IntegrationTest do
157163
end
158164

159165
test "SSL, path, long body" do
160-
assert {:ok, conn} = HTTP1.connect(:https, "httpbin.org", 443)
166+
assert {:ok, conn} =
167+
HTTP1.connect(:https, HttpBin.host(), HttpBin.https_port(),
168+
transport_opts: HttpBin.https_transport_opts()
169+
)
161170

162171
assert {:ok, conn, request} = HTTP1.request(conn, "GET", "/bytes/50000", [], nil)
163172
assert {:ok, conn, responses} = receive_stream(conn)

test/mint/http2/integration_test.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule HTTP2.IntegrationTest do
44
import Mint.HTTP2.TestHelpers
55

66
alias Mint.HTTP2
7+
alias Mint.HttpBin
78

89
@moduletag :requires_internet_connection
910

@@ -17,8 +18,12 @@ defmodule HTTP2.IntegrationTest do
1718

1819
case Map.fetch(context, :connect) do
1920
{:ok, {host, port}} ->
21+
extra_transport_opts = Map.get(context, :transport_opts, [])
22+
2023
assert {:ok, %HTTP2{} = conn} =
21-
HTTP2.connect(:https, host, port, transport_opts: transport_opts)
24+
HTTP2.connect(:https, host, port,
25+
transport_opts: transport_opts ++ extra_transport_opts
26+
)
2227

2328
[conn: conn]
2429

@@ -45,7 +50,8 @@ defmodule HTTP2.IntegrationTest do
4550
end
4651

4752
describe "httpbin.org" do
48-
@describetag connect: {"httpbin.org", 443}
53+
@describetag connect: {HttpBin.host(), HttpBin.https_port()},
54+
transport_opts: HttpBin.https_transport_opts()
4955

5056
test "GET /user-agent", %{conn: conn} do
5157
assert {:ok, %HTTP2{} = conn, req_id} = HTTP2.request(conn, "GET", "/user-agent", [], nil)
@@ -56,7 +62,6 @@ defmodule HTTP2.IntegrationTest do
5662
{:status, ^req_id, 200},
5763
{:headers, ^req_id, headers},
5864
{:data, ^req_id, data},
59-
{:data, ^req_id, ""},
6065
{:done, ^req_id}
6166
] = responses
6267

test/mint/integration_test.exs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Mint.IntegrationTest do
33

44
import Mint.HTTP1.TestHelpers
55

6-
alias Mint.{TransportError, HTTP}
6+
alias Mint.{TransportError, HTTP, HttpBin}
77

88
@moduletag :requires_internet_connection
99

@@ -149,7 +149,9 @@ defmodule Mint.IntegrationTest do
149149

150150
test "200 response - http://httpbin.org" do
151151
assert {:ok, conn} =
152-
HTTP.connect(:http, "httpbin.org", 80, proxy: {:http, "localhost", 8888, []})
152+
HTTP.connect(:http, HttpBin.proxy_host(), HttpBin.http_port(),
153+
proxy: {:http, "localhost", 8888, []}
154+
)
153155

154156
assert conn.__struct__ == Mint.UnsafeProxy
155157
assert {:ok, conn, request} = HTTP.request(conn, "GET", "/", [], nil)
@@ -164,7 +166,10 @@ defmodule Mint.IntegrationTest do
164166

165167
test "200 response - https://httpbin.org" do
166168
assert {:ok, conn} =
167-
HTTP.connect(:https, "httpbin.org", 443, proxy: {:http, "localhost", 8888, []})
169+
HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
170+
proxy: {:http, "localhost", 8888, []},
171+
transport_opts: HttpBin.https_transport_opts()
172+
)
168173

169174
assert {:ok, conn, request} = HTTP.request(conn, "GET", "/", [], nil)
170175
assert {:ok, _conn, responses} = receive_stream(conn)
@@ -178,9 +183,10 @@ defmodule Mint.IntegrationTest do
178183

179184
test "200 response with explicit http2 - https://httpbin.org" do
180185
assert {:ok, conn} =
181-
HTTP.connect(:https, "httpbin.org", 443,
186+
HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
182187
proxy: {:http, "localhost", 8888, []},
183-
protocols: [:http2]
188+
protocols: [:http2],
189+
transport_opts: HttpBin.https_transport_opts()
184190
)
185191

186192
assert conn.__struct__ == Mint.HTTP2
@@ -196,9 +202,10 @@ defmodule Mint.IntegrationTest do
196202

197203
test "200 response without explicit http2 - https://httpbin.org" do
198204
assert {:ok, conn} =
199-
HTTP.connect(:https, "httpbin.org", 443,
205+
HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
200206
proxy: {:http, "localhost", 8888, []},
201-
protocols: [:http1, :http2]
207+
protocols: [:http1, :http2],
208+
transport_opts: HttpBin.https_transport_opts()
202209
)
203210

204211
assert conn.__struct__ == Mint.HTTP2

test/mint/tunnel_proxy_test.exs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule Mint.TunnelProxyTest do
44
import Mint.HTTP1.TestHelpers
55

66
alias Mint.HTTP
7+
alias Mint.HttpBin
78

89
@moduletag :proxy
910
@moduletag :requires_internet_connection
@@ -15,7 +16,7 @@ defmodule Mint.TunnelProxyTest do
1516
assert {:ok, conn} =
1617
Mint.TunnelProxy.connect(
1718
{:http, "localhost", 8888, []},
18-
{:http, "httpbin.org", 80, []}
19+
{:http, HttpBin.proxy_host(), HttpBin.http_port(), []}
1920
)
2021

2122
assert conn.__struct__ == Mint.HTTP1
@@ -37,7 +38,8 @@ defmodule Mint.TunnelProxyTest do
3738
assert {:ok, conn} =
3839
Mint.TunnelProxy.connect(
3940
{:http, "localhost", 8888, []},
40-
{:https, "httpbin.org", 443, []}
41+
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
42+
transport_opts: HttpBin.https_transport_opts()}
4143
)
4244

4345
assert {:ok, conn, request} = HTTP.request(conn, "GET", "/", [], nil)
@@ -52,26 +54,31 @@ defmodule Mint.TunnelProxyTest do
5254

5355
test "407 response - proxy with missing authentication" do
5456
assert {:error, %Mint.HTTPError{reason: {:proxy, {:unexpected_status, 407}}}} =
55-
Mint.HTTP.connect(:https, "httpbin.org", 443, proxy: {:http, "localhost", 8889, []})
57+
Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
58+
proxy: {:http, "localhost", 8889, []},
59+
transport_opts: HttpBin.https_transport_opts()
60+
)
5661
end
5762

5863
test "401 response - proxy with invalid authentication" do
5964
invalid_auth64 = Base.encode64("test:wrong_password")
6065

6166
assert {:error, %Mint.HTTPError{reason: {:proxy, {:unexpected_status, 401}}}} =
62-
Mint.HTTP.connect(:https, "httpbin.org", 443,
67+
Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
6368
proxy: {:http, "localhost", 8889, []},
64-
proxy_headers: [{"proxy-authorization", "basic #{invalid_auth64}"}]
69+
proxy_headers: [{"proxy-authorization", "basic #{invalid_auth64}"}],
70+
transport_opts: HttpBin.https_transport_opts()
6571
)
6672
end
6773

6874
test "200 response - proxy with valid authentication" do
6975
auth64 = Base.encode64("test:password")
7076

7177
assert {:ok, conn} =
72-
Mint.HTTP.connect(:https, "httpbin.org", 443,
78+
Mint.HTTP.connect(:https, HttpBin.proxy_host(), HttpBin.https_port(),
7379
proxy: {:http, "localhost", 8889, []},
74-
proxy_headers: [{"proxy-authorization", "basic #{auth64}"}]
80+
proxy_headers: [{"proxy-authorization", "basic #{auth64}"}],
81+
transport_opts: HttpBin.https_transport_opts()
7582
)
7683

7784
assert {:ok, conn, request} = HTTP.request(conn, "GET", "/", [], nil)
@@ -88,7 +95,8 @@ defmodule Mint.TunnelProxyTest do
8895
assert {:ok, conn} =
8996
Mint.TunnelProxy.connect(
9097
{:http, "localhost", 8888, []},
91-
{:https, "httpbin.org", 443, [protocols: [:http2]]}
98+
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
99+
[protocols: [:http2], transport_opts: HttpBin.https_transport_opts()]}
92100
)
93101

94102
assert conn.__struct__ == Mint.HTTP2
@@ -110,7 +118,8 @@ defmodule Mint.TunnelProxyTest do
110118
assert {:ok, conn} =
111119
Mint.TunnelProxy.connect(
112120
{:http, "localhost", 8888, []},
113-
{:https, "httpbin.org", 443, [protocols: [:http1, :http2]]}
121+
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
122+
[protocols: [:http1, :http2], transport_opts: HttpBin.https_transport_opts()]}
114123
)
115124

116125
assert conn.__struct__ == Mint.HTTP2
@@ -133,7 +142,8 @@ defmodule Mint.TunnelProxyTest do
133142
assert {:ok, conn} =
134143
Mint.TunnelProxy.connect(
135144
{:https, "localhost", 8888, []},
136-
{:https, "httpbin.org", 443, []}
145+
{:https, HttpBin.proxy_host(), HttpBin.https_port(),
146+
[transport_opts: HttpBin.https_transport_opts()]}
137147
)
138148

139149
assert conn.__struct__ == Mint.HTTP1

0 commit comments

Comments
 (0)