Page MenuHomePhorge

No OneTemporary

Size
18 KB
Referenced Files
None
Subscribers
None
diff --git a/test/tesla/adapter/gun_test.exs b/test/tesla/adapter/gun_test.exs
index 00771df..c1fdf08 100644
--- a/test/tesla/adapter/gun_test.exs
+++ b/test/tesla/adapter/gun_test.exs
@@ -1,235 +1,235 @@
defmodule Tesla.Adapter.GunTest do
use ExUnit.Case
use Tesla.AdapterCase, adapter: Tesla.Adapter.Gun
use Tesla.AdapterCase.Basic
use Tesla.AdapterCase.Multipart
use Tesla.AdapterCase.StreamRequestBody
use Tesla.AdapterCase.SSL
alias Tesla.Adapter.Gun
setup do
on_exit(fn -> assert Supervisor.which_children(:gun_sup) == [] end)
end
test "fallback adapter timeout option" do
request = %Env{
method: :get,
url: "#{@http}/delay/2"
}
assert {:error, :recv_response_timeout} = call(request, timeout: 1_000)
end
test "max_body option" do
request = %Env{
method: :get,
url: "#{@http}/stream-bytes/100"
}
assert {:error, :body_too_large} = call(request, max_body: 5)
end
test "url without path" do
request = %Env{
method: :get,
url: "#{@http}"
}
assert {:ok, %Env{status: 200}} = call(request)
end
test "url without path, but with query" do
request = %Env{
method: :get,
url: "#{@http}",
query: [
param: "value"
]
}
- assert {:ok, %Env{status: 200} = response} = call(request)
+ assert {:ok, %Env{status: 200} = _response} = call(request)
end
test "ipv4 request" do
request = %Env{
method: :get,
url: "http://127.0.0.1:#{Application.get_env(:httparrot, :http_port)}/stream-bytes/10"
}
assert {:ok, %Env{status: 200, body: body}} = call(request)
assert byte_size(body) == 16
end
test "response stream" do
request = %Env{
method: :get,
url: "#{@http}/stream-bytes/10"
}
assert {:ok, %Env{status: 200, body: body}} = call(request)
assert byte_size(body) == 16
end
test "response body as stream" do
request = %Env{
method: :get,
url: "#{@http}/stream-bytes/10"
}
assert {:ok, %Env{status: 200, body: stream}} = call(request, body_as: :stream)
assert is_function(stream)
assert stream |> Enum.join() |> byte_size() == 16
end
test "response body as chunks with closing connection" do
request = %Env{
method: :get,
url: "#{@http}/stream-bytes/10"
}
assert {:ok, %Env{status: 200, body: %{pid: pid, stream: stream, opts: opts}}} =
call(request, body_as: :chunks)
assert is_pid(pid)
assert is_reference(stream)
assert read_body(pid, stream, opts) |> byte_size() == 16
refute Process.alive?(pid)
end
test "certificates_verification option" do
request = %Env{
method: :get,
url: "#{@https}"
}
- assert {:ok, %Env{} = response} =
+ assert {:ok, %Env{} = _response} =
call(request,
certificates_verification: true,
transport_opts: [
cacertfile: "./deps/httparrot/priv/ssl/server-ca.crt"
]
)
end
describe "reusing connection" do
setup do
uri = URI.parse(@http)
{:ok, conn} = :gun.open(to_charlist(uri.host), uri.port)
request = %Env{
method: :get,
url: "#{@http}/stream-bytes/10"
}
on_exit(fn -> Gun.close(conn) end)
{:ok, request: request, conn: conn}
end
test "response body as plain", %{request: request, conn: conn} do
assert {:ok, %Env{status: 200, body: body}} = call(request, conn: conn, close_conn: false)
assert byte_size(body) == 16
assert Process.alive?(conn)
end
test "response body as chunks", %{request: request, conn: conn} do
opts = [body_as: :chunks, conn: conn, close_conn: false]
assert {:ok, %Env{status: 200, body: %{pid: pid, stream: stream}}} = call(request, opts)
assert is_pid(pid)
assert is_reference(stream)
assert conn == pid
assert read_body(pid, stream, opts) |> byte_size() == 16
assert Process.alive?(pid)
end
test "response body as stream without closing connection", %{request: request, conn: conn} do
assert {:ok, %Env{status: 200, body: stream}} =
call(request, body_as: :stream, conn: conn, close_conn: false)
assert is_function(stream)
assert stream |> Enum.join() |> byte_size() == 16
assert Process.alive?(conn)
end
test "response body as stream with closing connection", %{request: request, conn: conn} do
assert {:ok, %Env{status: 200, body: stream}} = call(request, body_as: :stream, conn: conn)
assert is_function(stream)
assert stream |> Enum.join() |> byte_size() == 16
refute Process.alive?(conn)
end
test "opened to another domain", %{request: request, conn: conn} do
new_url = "http://127.0.0.1:#{Application.get_env(:httparrot, :http_port)}/stream-bytes/10"
assert {:error, :invalid_conn} = call(Map.put(request, :url, new_url), conn: conn)
end
end
test "error response" do
request = %Env{
method: :get,
url: "#{@http}/status/500"
}
assert {:ok, %Env{} = response} = call(request, timeout: 1_000)
assert response.status == 500
end
test "error on socks proxy" do
request = %Env{
method: :get,
url: "#{@http}/status/500"
}
assert {:error, "socks protocol is not supported"} ==
call(request, proxy: {:socks5, 'localhost', 1234})
end
test "receive gun_up message when receive is false" do
request = %Env{
method: :get,
url: "#{@http}"
}
assert {:ok, %Env{} = response} = call(request, receive: false)
assert response.status == 200
assert_receive {:gun_up, pid, :http}
assert is_pid(pid)
end
test "on TLS errors get timeout error from await_up method" do
request = %Env{
method: :get,
url: "#{@https}"
}
{time, resp} =
:timer.tc(fn ->
call(request,
timeout: 60_000,
certificates_verification: true
)
end)
assert resp == {:error, :timeout}
assert time / 1_000_000 < 6
end
defp read_body(pid, stream, opts, acc \\ "") do
case Gun.read_chunk(pid, stream, opts) do
{:fin, body} ->
acc <> body
{:nofin, part} ->
read_body(pid, stream, opts, acc <> part)
end
end
end
diff --git a/test/tesla/middleware/follow_redirects_test.exs b/test/tesla/middleware/follow_redirects_test.exs
index edfbe6c..167a9d9 100644
--- a/test/tesla/middleware/follow_redirects_test.exs
+++ b/test/tesla/middleware/follow_redirects_test.exs
@@ -1,301 +1,301 @@
defmodule Tesla.Middleware.FollowRedirectsTest do
use ExUnit.Case
defmodule Client do
use Tesla
plug Tesla.Middleware.FollowRedirects
adapter fn env ->
{status, headers, body} =
case env.url do
"http://example.com/0" ->
assert env.query == []
{200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
{301, [{"location", "http://example.com/#{next}"}], ""}
end
{:ok, %{env | status: status, headers: headers, body: body}}
end
end
test "redirects if default max redirects isn't exceeded" do
assert {:ok, env} = Client.get("http://example.com/5")
assert env.status == 200
end
test "raise error when redirect default max redirects is exceeded" do
assert {:error, {Tesla.Middleware.FollowRedirects, :too_many_redirects}} ==
Client.get("http://example.com/6")
end
test "drop the query" do
assert {:ok, env} = Client.get("http://example.com/5", some_query: "params")
assert env.query == []
end
defmodule CustomMaxRedirectsClient do
use Tesla
plug Tesla.Middleware.FollowRedirects, max_redirects: 1
adapter fn env ->
{status, headers, body} =
case env.url do
"http://example.com/0" ->
assert env.query == []
{200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
{301, [{"location", "http://example.com/#{next}"}], ""}
end
{:ok, %{env | status: status, headers: headers, body: body}}
end
end
alias CustomMaxRedirectsClient, as: CMRClient
test "redirects if custom max redirects isn't exceeded" do
assert {:ok, env} = CMRClient.get("http://example.com/1")
assert env.status == 200
end
test "raise error when custom max redirects is exceeded" do
assert {:error, {Tesla.Middleware.FollowRedirects, :too_many_redirects}} ==
CMRClient.get("http://example.com/2")
end
defmodule RelativeLocationClient do
use Tesla
plug Tesla.Middleware.FollowRedirects
adapter fn env ->
{status, headers, body} =
case env.url do
"https://example.com/pl" ->
{200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com" ->
{301, [{"location", "https://example.com"}], ""}
"https://example.com" ->
{301, [{"location", "/pl"}], ""}
"https://example.com/" ->
{301, [{"location", "/pl"}], ""}
"https://example.com/article" ->
{301, [{"location", "/pl"}], ""}
"https://example.com/one/two" ->
{301, [{"location", "three"}], ""}
"https://example.com/one/three" ->
{200, [{"content-type", "text/plain"}], "foo bar baz"}
end
{:ok, %{env | status: status, headers: headers, body: body}}
end
end
alias RelativeLocationClient, as: RLClient
test "supports relative address in location header" do
assert {:ok, env} = RLClient.get("http://example.com")
assert env.status == 200
end
test "doesn't create double slashes inside new url" do
assert {:ok, env} = RLClient.get("https://example.com/")
assert env.url == "https://example.com/pl"
end
test "rewrites URLs to their root" do
assert {:ok, env} = RLClient.get("https://example.com/article")
assert env.url == "https://example.com/pl"
end
test "rewrites URLs relative to the original URL" do
assert {:ok, env} = RLClient.get("https://example.com/one/two")
assert env.url == "https://example.com/one/three"
end
defmodule CustomRewriteMethodClient do
use Tesla
plug Tesla.Middleware.FollowRedirects
adapter fn env ->
{status, headers, body} =
case env.url do
"http://example.com/0" ->
{200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
{303, [{"location", "http://example.com/#{next}"}], ""}
end
{:ok, %{env | status: status, headers: headers, body: body}}
end
end
alias CustomRewriteMethodClient, as: CRMClient
test "rewrites method to get for 303 requests" do
assert {:ok, env} = CRMClient.post("http://example.com/1", "")
assert env.method == :get
end
defmodule CustomPreservesRequestClient do
use Tesla
plug Tesla.Middleware.FollowRedirects
adapter fn env ->
{status, headers, body} =
case env.url do
"http://example.com/0" ->
{200, env.headers, env.body}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
{307, [{"location", "http://example.com/#{next}"}], ""}
end
{:ok, %{env | status: status, headers: headers, body: body}}
end
end
alias CustomPreservesRequestClient, as: CPRClient
test "Preserves original request for 307" do
assert {:ok, env} =
CPRClient.post(
"http://example.com/1",
"Body data",
headers: [{"X-Custom-Header", "custom value"}]
)
assert env.method == :post
assert env.body == "Body data"
assert env.headers == [{"X-Custom-Header", "custom value"}]
end
describe "headers" do
defp setup_client(_) do
middleware = [Tesla.Middleware.FollowRedirects]
adapter = fn
%{url: "http://example.com/keep", headers: headers} = env ->
send(self(), headers)
{:ok,
%{env | status: 301, headers: [{"location", "http://example.com/next"}], body: ""}}
%{url: "http://example.com/drop", headers: headers} = env ->
send(self(), headers)
{:ok,
%{env | status: 301, headers: [{"location", "http://example.net/next"}], body: ""}}
%{url: "http://example.com/next", headers: headers} = env ->
send(self(), headers)
{:ok, %{env | status: 200, headers: [], body: "ok com"}}
%{url: "http://example.net/next", headers: headers} = env ->
send(self(), headers)
{:ok, %{env | status: 200, headers: [], body: "ok net"}}
end
{:ok, client: Tesla.client(middleware, adapter)}
end
setup :setup_client
test "Keep authorization header on redirect to the same domain", %{client: client} do
- assert {:ok, env} =
+ assert {:ok, _env} =
Tesla.post(client, "http://example.com/keep", "",
headers: [
{"content-type", "text/plain"},
{"authorization", "Basic Zm9vOmJhcg=="}
]
)
# Initial request receives all headers
assert_receive [
{"content-type", "text/plain"},
{"authorization", "Basic Zm9vOmJhcg=="}
]
# Next request also receives all headers
assert_receive [
{"content-type", "text/plain"},
{"authorization", "Basic Zm9vOmJhcg=="}
]
end
test "Strip authorization header on redirect to a different domain", %{client: client} do
- assert {:ok, env} =
+ assert {:ok, _env} =
Tesla.post(client, "http://example.com/drop", "",
headers: [
{"content-type", "text/plain"},
{"authorization", "Basic Zm9vOmJhcg=="}
]
)
# Initial request receives all headers
assert_receive [
{"content-type", "text/plain"},
{"authorization", "Basic Zm9vOmJhcg=="}
]
# Next request does not receive authorization header
assert_receive [
{"content-type", "text/plain"}
]
end
test "Keep custom host header on redirect to a different domain", %{client: client} do
- assert {:ok, env} =
+ assert {:ok, _env} =
Tesla.post(client, "http://example.com/keep", "",
headers: [
{"host", "example.xyz"}
]
)
# Initial request receives host header
assert_receive [
{"host", "example.xyz"}
]
# Next request does not receive host header
assert_receive [
{"host", "example.xyz"}
]
end
test "Strip custom host header on redirect to a different domain", %{client: client} do
- assert {:ok, env} =
+ assert {:ok, _env} =
Tesla.post(client, "http://example.com/drop", "",
headers: [
{"host", "example.xyz"}
]
)
# Initial request receives host header
assert_receive [
{"host", "example.xyz"}
]
# Next request does not receive host header
assert_receive []
end
end
end
diff --git a/test/tesla/middleware/telemetry_test.exs b/test/tesla/middleware/telemetry_test.exs
index 6986333..eab30a5 100644
--- a/test/tesla/middleware/telemetry_test.exs
+++ b/test/tesla/middleware/telemetry_test.exs
@@ -1,75 +1,75 @@
defmodule Tesla.Middleware.TelemetryTest do
use ExUnit.Case, async: true
defmodule Client do
use Tesla
plug Tesla.Middleware.Telemetry
adapter fn env ->
case env.url do
"/telemetry" -> {:ok, env}
"/telemetry_error" -> {:error, :econnrefused}
"/telemetry_exception" -> raise "some exception"
end
end
end
setup do
Application.ensure_all_started(:telemetry)
on_exit(fn ->
:telemetry.list_handlers([])
|> Enum.each(&:telemetry.detach(&1.id))
end)
:ok
end
test "events are all emitted properly" do
Enum.each(["/telemetry", "/telemetry_error"], fn path ->
:telemetry.attach("start event", [:tesla, :request, :start], &echo_event/4, %{
caller: self()
})
:telemetry.attach("stop event", [:tesla, :request, :stop], &echo_event/4, %{
caller: self()
})
:telemetry.attach("legacy event", [:tesla, :request], &echo_event/4, %{
caller: self()
})
Client.get(path)
- assert_receive {:event, [:tesla, :request, :start], %{system_time: time},
- %{env: %Tesla.Env{url: path, method: :get}}}
+ assert_receive {:event, [:tesla, :request, :start], %{system_time: _time},
+ %{env: %Tesla.Env{url: ^path, method: :get}}}
- assert_receive {:event, [:tesla, :request, :stop], %{duration: time},
- %{env: %Tesla.Env{url: path, method: :get}}}
+ assert_receive {:event, [:tesla, :request, :stop], %{duration: _time},
+ %{env: %Tesla.Env{url: ^path, method: :get}}}
- assert_receive {:event, [:tesla, :request], %{request_time: time}, %{result: result}}
+ assert_receive {:event, [:tesla, :request], %{request_time: _time}, %{result: _result}}
end)
end
test "with an exception raised" do
:telemetry.attach("with_exception", [:tesla, :request, :exception], &echo_event/4, %{
caller: self()
})
assert_raise RuntimeError, fn ->
Client.get("/telemetry_exception")
end
- assert_receive {:event, [:tesla, :request, :exception], %{duration: time},
+ assert_receive {:event, [:tesla, :request, :exception], %{duration: _time},
%{
- kind: kind,
- reason: reason,
- stacktrace: stacktrace
+ kind: _kind,
+ reason: _reason,
+ stacktrace: _stacktrace
}}
end
def echo_event(event, measurements, metadata, config) do
send(config.caller, {:event, event, measurements, metadata})
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 23, 9:07 AM (19 h, 11 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38929
Default Alt Text
(18 KB)

Event Timeline