Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,16 @@ Mint is a low-level client. If you need higher-level features such as connection

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.

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` (excluded by default) and require that you start local proxy instance through Docker Compose (or Podman Compose) from the Mint root directory in order to run.

If you encounter `{:error, %Mint.TransportError{reason: :nxdomain}}` error during the integration test, this suggests your DNS-based adblocker (self-hosted or VPN) might be blocking social media sites used in the integration test cases.
Tests are organized with tags. Proxy tests are tagged with `:proxy` (excluded by default) and require that you start local proxy instance through Docker Compose (or Podman Compose) from the Mint root directory in order to run.

Here are a few examples of running tests:

Run all default tests which including `:requires_internet_connection` except `:proxy`:
Run all default tests except `:proxy`:

```sh
mix test
```

Local tests, if you don't have an Internet connection available:

```sh
mix test --exclude requires_internet_connection
```

Run all tests:

```sh
Expand Down
64 changes: 64 additions & 0 deletions test/mint/cacerts_get_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule Mint.CacertsGetTest do
# :public_key.cacerts_load/1 replaces the CA store that
# :public_key.cacerts_get/0 returns for the whole VM, so these tests can't
# run concurrently with other tests.
use ExUnit.Case, async: false

alias Mint.{TransportError, HTTP, TestCertificates, TLSTestServer}

if List.to_integer(:erlang.system_info(:otp_release)) < 25 do
@moduletag :skip
end

setup do
cacertfile =
Path.join(
System.tmp_dir!(),
"mint_cacerts_get_test_#{System.unique_integer([:positive])}.pem"
)

on_exit(fn ->
apply(:public_key, :cacerts_load, [])
File.rm(cacertfile)
end)

%{cacertfile: cacertfile}
end

@tag :capture_log
test "using :public_key.cacerts_get/0", %{cacertfile: cacertfile} do
trusted = TestCertificates.pkix_test_chain()
untrusted = TestCertificates.pkix_test_chain()

pem_entries =
for der <- trusted.client_config[:cacerts], do: {:Certificate, der, :not_encrypted}

File.write!(cacertfile, :public_key.pem_encode(pem_entries))
:ok = apply(:public_key, :cacerts_load, [cacertfile])

cacerts = apply(:public_key, :cacerts_get, [])

{:ok, untrusted_port} = TLSTestServer.start(untrusted.server_config)
{:ok, trusted_port} = TLSTestServer.start(trusted.server_config)

assert {:error, %TransportError{}} =
HTTP.connect(
:https,
"localhost",
untrusted_port,
transport_opts: [
log_alert: false,
reuse_sessions: false,
cacerts: cacerts
]
)

assert {:ok, _conn} =
HTTP.connect(
:https,
"localhost",
trusted_port,
transport_opts: [reuse_sessions: false, cacerts: cacerts]
)
end
end
61 changes: 43 additions & 18 deletions test/mint/http1/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ defmodule Mint.HTTP1.IntegrationTest do

import Mint.HTTP1.TestHelpers

alias Mint.{TransportError, HTTP1, HttpBin}

@moduletag :requires_internet_connection
alias Mint.{TransportError, HTTP1, HttpBin, TestCertificates, TLSTestServer}

describe "local httpbin" do
test "200 response" do
Expand Down Expand Up @@ -102,15 +100,29 @@ defmodule Mint.HTTP1.IntegrationTest do
end
end

describe "twitter.com" do
describe "connect timeout" do
# A local connect can complete before a timeout of 0 fires. Linux drops the
# SYN for a listen socket whose accept queue is full, so the connect can't
# complete at all. Other systems reset the connection instead.
if :os.type() != {:unix, :linux} do
@tag skip: "needs Linux's handling of a full accept queue"
end

test "timeout with http" do
{:ok, listen_socket} = :gen_tcp.listen(0, backlog: 0)
{:ok, port} = :inet.port(listen_socket)
fill_accept_queue(port)

assert {:error, %TransportError{reason: :timeout}} =
HTTP1.connect(:http, "twitter.com", 80, transport_opts: [timeout: 0])
HTTP1.connect(:http, "localhost", port, transport_opts: [timeout: 0])
end

# The server never answers the ClientHello, so the handshake can't finish.
test "timeout with https" do
{:ok, port, _server_ref} = HTTP1.TestServer.start()

assert {:error, %TransportError{reason: :timeout}} =
HTTP1.connect(:https, "twitter.com", 443, transport_opts: [timeout: 0])
HTTP1.connect(:https, "localhost", port, transport_opts: [timeout: 0])
end
end

Expand Down Expand Up @@ -197,13 +209,14 @@ defmodule Mint.HTTP1.IntegrationTest do
end
end

describe "badssl.com" do
@describetag :requires_internet_connection

describe "certificate verification" do
@tag :capture_log
test "SSL with bad certificate" do
%{server_config: server_config} = TestCertificates.pkix_test_chain()
{:ok, port} = TLSTestServer.start(server_config)

assert {:error, %TransportError{reason: reason}} =
HTTP1.connect(:https, "untrusted-root.badssl.com", 443,
HTTP1.connect(:https, "localhost", port,
transport_opts: [log_alert: false, reuse_sessions: false]
)

Expand All @@ -212,16 +225,23 @@ defmodule Mint.HTTP1.IntegrationTest do
match?({:tls_alert, {:unknown_ca, _}}, reason)

assert {:ok, _conn} =
HTTP1.connect(:https, "untrusted-root.badssl.com", 443,
transport_opts: [verify: :verify_none]
)
HTTP1.connect(:https, "localhost", port, transport_opts: [verify: :verify_none])
end

@tag :capture_log
test "SSL with bad hostname" do
%{server_config: server_config, client_config: client_config} =
TestCertificates.pkix_test_chain(hostname: "wrong.host.example")

{:ok, port} = TLSTestServer.start(server_config)

assert {:error, %TransportError{reason: reason}} =
HTTP1.connect(:https, "wrong.host.badssl.com", 443,
transport_opts: [log_alert: false, reuse_sessions: false]
HTTP1.connect(:https, "localhost", port,
transport_opts: [
cacerts: client_config[:cacerts],
log_alert: false,
reuse_sessions: false
]
)

# OTP 21.3 changes the format of SSL errors. OTP 29 reports a hostname
Expand All @@ -232,9 +252,14 @@ defmodule Mint.HTTP1.IntegrationTest do
match?({:tls_alert, {:bad_certificate, _}}, reason)

assert {:ok, _conn} =
HTTP1.connect(:https, "wrong.host.badssl.com", 443,
transport_opts: [verify: :verify_none]
)
HTTP1.connect(:https, "localhost", port, transport_opts: [verify: :verify_none])
end
end

defp fill_accept_queue(port) do
case :gen_tcp.connect(~c"localhost", port, [], 100) do
{:ok, _socket} -> fill_accept_queue(port)
{:error, :timeout} -> :ok
end
end
end
14 changes: 14 additions & 0 deletions test/mint/http2/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,20 @@ defmodule Mint.HTTP2Test do

assert HTTP2.open?(conn)
end

# https://github.com/elixir-mint/mint/issues/171
test "request header names are lowercased before HPACK encoding", %{conn: conn} do
headers = [{"If-Modified-Since", "Wed, 26 May 2019 07:43:40 GMT"}]
assert {:ok, conn, _ref} = HTTP2.request(conn, "GET", "/", headers, nil)

assert_recv_frames [headers(hbf: hbf)]

decoded_headers = server_decode_headers(hbf)
assert {"if-modified-since", "Wed, 26 May 2019 07:43:40 GMT"} in decoded_headers
refute List.keymember?(decoded_headers, "If-Modified-Since", 0)

assert HTTP2.open?(conn)
end
end

describe "request header validation" do
Expand Down
130 changes: 0 additions & 130 deletions test/mint/http2/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ defmodule HTTP2.IntegrationTest do
alias Mint.HTTP2
alias Mint.HttpBin

@moduletag :requires_internet_connection

setup context do
transport_opts =
if Mint.Core.Transport.SSL.ssl_version() >= [10, 2] do
Expand Down Expand Up @@ -99,132 +97,4 @@ defmodule HTTP2.IntegrationTest do
assert HTTP2.open?(conn)
end
end

describe "twitter.com" do
@describetag connect: {"twitter.com", 443}
@browser_user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"

test "ping", %{conn: conn} do
assert {:ok, %HTTP2{} = conn, ref} = HTTP2.ping(conn)
assert {:ok, %HTTP2{} = conn, [{:pong, ^ref}]} = receive_stream(conn)
assert conn.buffer == ""
assert HTTP2.open?(conn)
end

test "GET /", %{conn: conn} do
assert {:ok, %HTTP2{} = conn, ref} =
HTTP2.request(conn, "GET", "/", [{"user-agent", @browser_user_agent}], nil)

assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn)

assert [{:status, ^ref, status}, {:headers, ^ref, headers} | rest] = responses
assert status in [200, 301, 302]

assert {_, [{:done, ^ref}]} = Enum.split_while(rest, &match?({:data, ^ref, _}, &1))

assert is_list(headers)

assert conn.buffer == ""
assert HTTP2.open?(conn)
end
end

describe "facebook.com" do
@describetag connect: {"facebook.com", 443}

test "ping", %{conn: conn} do
assert {:ok, %HTTP2{} = conn, ref} = HTTP2.ping(conn)
assert {:ok, %HTTP2{} = conn, [{:pong, ^ref}]} = receive_stream(conn)
assert conn.buffer == ""
assert HTTP2.open?(conn)
end

test "GET /", %{conn: conn} do
assert {:ok, %HTTP2{} = conn, ref} = HTTP2.request(conn, "GET", "/", [], nil)

assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn)

assert [{:status, ^ref, status}, {:headers, ^ref, headers} | rest] = responses
assert {_, [{:done, ^ref}]} = Enum.split_while(rest, &match?({:data, ^ref, _}, &1))

assert status == 301
assert is_list(headers)

assert conn.buffer == ""
assert HTTP2.open?(conn)
end
end

describe "robynthinks.wordpress.com" do
@describetag connect: {"robynthinks.wordpress.com", 443}

test "GET /feed/ - regression for #171", %{conn: conn} do
# Using non-downcased header meant that HPACK wouldn't find it in the
# static built-in headers table and so it wouldn't encode it correctly.
headers = [{"If-Modified-Since", "Wed, 26 May 2019 07:43:40 GMT"}]
assert {:ok, %HTTP2{} = conn, ref} = HTTP2.request(conn, "GET", "/feed/", headers, nil)

assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn)

assert [{:status, ^ref, status}, {:headers, ^ref, _headers} | rest] = responses
assert {_, [{:done, ^ref}]} = Enum.split_while(rest, &match?({:data, ^ref, _}, &1))

assert status in [200, 304]

assert conn.buffer == ""
assert HTTP2.open?(conn)
end
end

describe "www.shopify.com" do
@describetag connect: {"www.shopify.com", 443}

if List.to_integer(:erlang.system_info(:otp_release)) < 23 do
@tag :skip
end

# Informational responses were the issue.s
# https://github.com/elixir-mint/mint/issues/349
test "GET / with specific User-Agent header - regression for #349", %{conn: conn} do
assert %HTTP2{} = conn

assert {:ok, %HTTP2{} = conn, ref} =
HTTP2.request(conn, "GET", "/", [{"user-agent", "curl/7.68.0"}], nil)

assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn)

case responses do
[
{:status, ^ref, informational_status},
{:headers, ^ref, informational_headers},
{:status, ^ref, status},
{:headers, ^ref, headers}
| rest
] ->
assert informational_status == 103
assert {"link", _} = List.keyfind(informational_headers, "link", 0)
assert status == 200
assert is_list(headers) and length(headers) > 0

assert Enum.count(rest, &match?({:data, ^ref, _data}, &1)) >= 1
assert List.last(rest) == {:done, ref}

[{:status, ^ref, status}, {:headers, ^ref, headers} | rest] ->
assert status == 200
assert is_list(headers) and length(headers) > 0
assert Enum.count(rest, &match?({:data, ^ref, _data}, &1)) >= 1
assert List.last(rest) == {:done, ref}

_other ->
flunk(
"Unexpected responses. Expected status + headers + data, or informational " <>
"response + status + headers + data, got:\n#{inspect(responses, pretty: true)}"
)
end

assert HTTP2.open?(conn)
end
end

# TODO: certificate verification; badssl.com does not seem to support HTTP2
end
Loading
Loading