Skip to content

Commit

Permalink
Deprecate Mint adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Aug 26, 2023
1 parent 752ded5 commit a1022a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.2.6 (TBA)

* Added `Assent.HTTPAdapter.Finch`
* Deprecated `Assent.HTTPAdapter.Mint`

## v0.2.5 (2023-08-21)

Expand Down
28 changes: 2 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def application do
end
```

This is not necessary if you'll use another HTTP adapter, such as Finch or Mint.
This is not necessary if you'll use another HTTP adapter like Finch.

Assent requires Erlang OTP 22.1 or greater.

Expand Down Expand Up @@ -197,7 +197,7 @@ end

By default Erlangs built-in `:httpc` is used for requests. SSL verification is automatically enabled when `:certifi` and `:ssl_verify_fun` packages are available. `:httpc` only supports HTTP/1.1.

If you would like HTTP/2 support, you should consider adding [`Finch`](https://github.com/sneako/finch) or [`Mint`](https://github.com/ericmj/mint) to your project.
If you would like HTTP/2 support, you should consider adding [`Finch`](https://github.com/sneako/finch) to your project.

### Finch

Expand All @@ -222,30 +222,6 @@ config = [
]
```

### Mint

Update `mix.exs`:

```elixir
defp deps do
[
# ...
{:mint, "~> 1.0"},
{:castore, "~> 1.0"} # Required for SSL validation
]
end
```

Pass the `:http_adapter` with your provider configuration:

```elixir
config = [
client_id: "REPLACE_WITH_CLIENT_ID",
client_secret: "REPLACE_WITH_CLIENT_SECRET",
http_adapter: Assent.HTTPAdapter.Mint
]
```

## JWT Adapter

By default the built-in `Assent.JWTAdapter.AssentJWT` is used for JWT parsing, but you can change it to any third-party library with a custom `Assent.JWTAdapter`. A [JOSE](https://github.com/potatosalad/erlang-jose) adapter `Assent.JWTAdapter.JOSE` is included.
Expand Down
2 changes: 2 additions & 0 deletions lib/assent/http_adapter/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Assent.HTTPAdapter.Mint do

@impl HTTPAdapter
def request(method, url, body, headers, mint_opts \\ nil) do
IO.warn("#{inspect __MODULE__} is deprecated, consider use #{inspect Assent.HTTPAdapter.Finch} instead")

headers = headers ++ [HTTPAdapter.user_agent_header()]

%{scheme: scheme, port: port, host: host, path: path, query: query} = URI.parse(url)
Expand Down
29 changes: 22 additions & 7 deletions test/assent/http_adapter/mint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Assent.HTTPAdapter.MintTest do
use ExUnit.Case
doctest Assent.HTTPAdapter.Mint

alias ExUnit.CaptureIO
alias Mint.TransportError
alias Assent.HTTPAdapter.{HTTPResponse, Mint}

Expand All @@ -12,7 +13,9 @@ defmodule Assent.HTTPAdapter.MintTest do

mint_opts = [transport_opts: [cacerts: TestServer.x509_suite().cacerts], protocols: [:http1]]

assert {:ok, %HTTPResponse{status: 200, body: "HTTP/1.1"}} = Mint.request(:get, TestServer.url(), nil, [], mint_opts)
assert CaptureIO.capture_io(:stderr, fn ->
assert {:ok, %HTTPResponse{status: 200, body: "HTTP/1.1"}} = Mint.request(:get, TestServer.url(), nil, [], mint_opts)
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end

test "handles SSL with bad certificate" do
Expand All @@ -21,7 +24,9 @@ defmodule Assent.HTTPAdapter.MintTest do
bad_host_url = TestServer.url(host: "bad-host.localhost")
mint_opts = [transport_opts: [cacerts: TestServer.x509_suite().cacerts]]

assert {:error, %TransportError{reason: {:tls_alert, {:handshake_failure, _error}}}} = Mint.request(:get, bad_host_url, nil, [], mint_opts)
assert CaptureIO.capture_io(:stderr, fn ->
assert {:error, %TransportError{reason: {:tls_alert, {:handshake_failure, _error}}}} = Mint.request(:get, bad_host_url, nil, [], mint_opts)
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end

test "handles SSL with bad certificate and no verification" do
Expand All @@ -31,7 +36,9 @@ defmodule Assent.HTTPAdapter.MintTest do
bad_host_url = TestServer.url(host: "bad-host.localhost")
mint_opts = [transport_opts: [cacerts: TestServer.x509_suite().cacerts, verify: :verify_none]]

assert {:ok, %HTTPResponse{status: 200}} = Mint.request(:get, bad_host_url, nil, [], mint_opts)
assert CaptureIO.capture_io(:stderr, fn ->
assert {:ok, %HTTPResponse{status: 200}} = Mint.request(:get, bad_host_url, nil, [], mint_opts)
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end

if :crypto.supports()[:curves] do
Expand All @@ -41,7 +48,9 @@ defmodule Assent.HTTPAdapter.MintTest do

mint_opts = [transport_opts: [cacerts: TestServer.x509_suite().cacerts]]

assert {:ok, %HTTPResponse{status: 200, body: "HTTP/2"}} = Mint.request(:get, TestServer.url(), nil, [], mint_opts)
assert CaptureIO.capture_io(:stderr, fn ->
assert {:ok, %HTTPResponse{status: 200, body: "HTTP/2"}} = Mint.request(:get, TestServer.url(), nil, [], mint_opts)
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end
else
IO.warn("No support curve algorithms, can't test in #{__MODULE__}")
Expand All @@ -52,7 +61,9 @@ defmodule Assent.HTTPAdapter.MintTest do
url = TestServer.url()
TestServer.stop()

assert {:error, %TransportError{reason: :econnrefused}} = Mint.request(:get, url, nil, [])
assert CaptureIO.capture_io(:stderr, fn ->
assert {:error, %TransportError{reason: :econnrefused}} = Mint.request(:get, url, nil, [])
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end

test "handles query in URL" do
Expand All @@ -62,7 +73,9 @@ defmodule Assent.HTTPAdapter.MintTest do
Plug.Conn.send_resp(conn, 200, "")
end)

assert {:ok, %HTTPResponse{status: 200}} = Mint.request(:get, TestServer.url("/get?a=1"), nil, [])
assert CaptureIO.capture_io(:stderr, fn ->
assert {:ok, %HTTPResponse{status: 200}} = Mint.request(:get, TestServer.url("/get?a=1"), nil, [])
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end

test "handles POST" do
Expand All @@ -77,7 +90,9 @@ defmodule Assent.HTTPAdapter.MintTest do
Plug.Conn.send_resp(conn, 200, "")
end)

assert {:ok, %HTTPResponse{status: 200}} = Mint.request(:post, TestServer.url("/post"), "a=1&b=2", [{"content-type", "application/x-www-form-urlencoded"}])
assert CaptureIO.capture_io(:stderr, fn ->
assert {:ok, %HTTPResponse{status: 200}} = Mint.request(:post, TestServer.url("/post"), "a=1&b=2", [{"content-type", "application/x-www-form-urlencoded"}])
end) =~ "Assent.HTTPAdapter.Mint is deprecated, consider use Assent.HTTPAdapter.Finch instead"
end
end
end

0 comments on commit a1022a4

Please sign in to comment.