Skip to content

Commit fe6f5f9

Browse files
committed
Upgrade straight to 3.x; add headers in a test that was missing one
1 parent 61be1e5 commit fe6f5f9

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

lib/tesla/adapter/hackney.ex

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ if Code.ensure_loaded?(:hackney) do
2929
3030
- `:max_body` - Max response body size in bytes. Actual response may be bigger because hackney stops after the last chunk that surpasses `:max_body`.
3131
"""
32+
@hackney_version Application.spec(:hackney, :vsn)
33+
|> to_string()
34+
|> Version.parse!()
3235
@behaviour Tesla.Adapter
3336
alias Tesla.Multipart
3437

@@ -108,9 +111,15 @@ if Code.ensure_loaded?(:hackney) do
108111
handle_async_response({handle, %{status: nil, headers: nil}})
109112
end
110113

111-
defp handle({:ok, status, headers, handle}, opts) when is_hackney_connection_handle(handle) do
112-
with {:ok, body} <- :hackney.body(handle, Keyword.get(opts, :max_body, :infinity)) do
113-
{:ok, status, headers, body}
114+
if Version.match?(@hackney_version, "~> 1.0") do
115+
# this is only for hackney 1.x, hackney 3.x returns body in get request {:ok, status, headers, body}
116+
# we can't live it in 3.x because `:hackney.body is not there anymore and it would cause compilation warnings
117+
# hence, we remove the clause at compile time
118+
defp handle({:ok, status, headers, handle}, opts)
119+
when is_hackney_connection_handle(handle) do
120+
with {:ok, body} <- :hackney.body(handle, Keyword.get(opts, :max_body, :infinity)) do
121+
{:ok, status, headers, body}
122+
end
114123
end
115124
end
116125

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule Tesla.Mixfile do
5656

5757
# http clients
5858
{:ibrowse, "4.4.2", optional: true},
59-
{:hackney, "~> 1.21 or ~> 2.0", optional: true},
59+
{:hackney, "~> 1.21 or ~> 3.0", optional: true},
6060
{:gun, ">= 1.0.0", optional: true},
6161
{:finch, "~> 0.13", optional: true},
6262
{:castore, "~> 0.1 or ~> 1.0", optional: true},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"},
1717
"fuse": {:hex, :fuse, "2.5.0", "71afa90be21da4e64f94abba9d36472faa2d799c67fedc3bd1752a88ea4c4753", [:rebar3], [], "hexpm", "7f52a1c84571731ad3c91d569e03131cc220ebaa7e2a11034405f0bac46a4fef"},
1818
"gun": {:hex, :gun, "2.2.0", "b8f6b7d417e277d4c2b0dc3c07dfdf892447b087f1cc1caff9c0f556b884e33d", [:make, :rebar3], [{:cowlib, ">= 2.15.0 and < 3.0.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "76022700c64287feb4df93a1795cff6741b83fb37415c40c34c38d2a4645261a"},
19-
"hackney": {:hex, :hackney, "2.0.1", "f9df03ab6af220baed85ac7656ef7b7c819e531746cc5100704266543e046bb7", [:rebar3], [{:certifi, "~> 2.15.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 7.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.4", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "6d9cf39525c01af345c8596a71905226fe735651fa289d42898c3b1d97a99fd0"},
19+
"hackney": {:hex, :hackney, "3.0.1", "adfd227bf9ccf4bc40db641a11a4573970d90c80596e27d1dc7a71f4f1cf7e38", [:rebar3], [{:certifi, "~> 2.15.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 7.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.4", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "505d3628faba463ee9a2524aa7f53f558372f05c66614337242f3cd04fec7e93"},
2020
"hpax": {:hex, :hpax, "1.0.2", "762df951b0c399ff67cc57c3995ec3cf46d696e41f0bba17da0518d94acd4aac", [:mix], [], "hexpm", "2f09b4c1074e0abd846747329eaa26d535be0eb3d189fa69d812bfb8bfefd32f"},
2121
"httparrot": {:hex, :httparrot, "1.4.0", "ba4b9ff1199b409349a887a683b7d4c1a5f1357d654efde9b6f6a07984efc1f9", [:mix], [{:con_cache, "~> 1.1", [hex: :con_cache, repo: "hexpm", optional: false]}, {:cowboy, "~> 2.12", [hex: :cowboy, repo: "hexpm", optional: false]}, {:exjsx, "~> 3.0 or ~> 4.0", [hex: :exjsx, repo: "hexpm", optional: false]}], "hexpm", "6ebdcf36353424ada74743fe3180cb5f5ed5424349fc41a425521b2bf5678081"},
2222
"ibrowse": {:hex, :ibrowse, "4.4.2", "7fe943ba6cb88514dca631c7408c4a7897ce69fb0905244e2bfbc839d42f8d45", [:rebar3], [], "hexpm", "f088cee1faf6514b18c7783e8bc64c628d140a239786dc1f58fe9766e9584f41"},

test/tesla/adapter/hackney_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ defmodule Tesla.Adapter.HackneyTest do
4040
request = %Env{
4141
method: :post,
4242
url: "#{@http}/post",
43-
body: String.duplicate("long response", 1000)
43+
body: String.duplicate("long response", 1000),
44+
# TODO: check in hackney if no default headers is intentional
45+
headers: [{"content-type", "application/octet-stream"}]
4446
}
4547

4648
assert {:ok, %Env{} = response} = call(request, with_body: true, max_body: 100)

0 commit comments

Comments
 (0)