Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140768
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/adapter/httpc.ex b/lib/tesla/adapter/httpc.ex
index a94c3a4..664248d 100644
--- a/lib/tesla/adapter/httpc.ex
+++ b/lib/tesla/adapter/httpc.ex
@@ -1,32 +1,35 @@
defmodule Tesla.Adapter.Httpc do
def call(env, _opts) do
with {:ok, {status, headers, body}} <- request(env) do
format_response(env, status, headers, body)
end
end
defp format_response(env, {_, status, _}, headers, body) do
%{env | status: status,
headers: headers,
body: body}
end
defp request(env) do
content_type = to_char_list(env.headers["content-type"] || "")
- request(
- env.method,
+ handle request(
+ env.method || :get,
Tesla.build_url(env.url, env.query) |> to_char_list,
Enum.into(env.headers, [], fn {k,v} -> {to_char_list(k), to_char_list(v)} end),
content_type,
env.body
)
end
defp request(method, url, headers, _content_type, nil) do
:httpc.request(method, {url, headers}, [], [])
end
defp request(method, url, headers, content_type, body) do
:httpc.request(method, {url, headers, content_type, body}, [], [])
end
+
+ defp handle({:error, {:failed_connect, _}}), do: {:error, :econnrefused}
+ defp handle(response), do: response
end
diff --git a/lib/tesla/adapter/ibrowse.ex b/lib/tesla/adapter/ibrowse.ex
index 280b714..023dde2 100644
--- a/lib/tesla/adapter/ibrowse.ex
+++ b/lib/tesla/adapter/ibrowse.ex
@@ -1,22 +1,25 @@
if Code.ensure_loaded?(:ibrowse) do
defmodule Tesla.Adapter.Ibrowse do
def call(env, opts) do
with {:ok, status, headers, body} <- request(env, opts || []) do
%{env | status: status,
headers: headers,
body: body}
end
end
defp request(env, opts) do
body = env.body || []
- :ibrowse.send_req(
+ handle :ibrowse.send_req(
Tesla.build_url(env.url, env.query) |> to_char_list,
Enum.into(env.headers, []),
env.method,
body,
opts
)
end
+
+ defp handle({:error, {:conn_failed, error}}), do: error
+ defp handle(response), do: response
end
end
diff --git a/test/tesla/adapter/test_case.ex b/test/tesla/adapter/test_case.ex
index 82ac73d..9b651c1 100644
--- a/test/tesla/adapter/test_case.ex
+++ b/test/tesla/adapter/test_case.ex
@@ -1,90 +1,99 @@
defmodule Tesla.Adapter.TestCase do
@httpbin_url "http://localhost:#{Application.get_env(:httparrot, :http_port)}"
def httpbin_url, do: @httpbin_url
end
defmodule Tesla.Adapter.TestCase.Basic do
defmacro __using__([adapter: adapter]) do
quote do
defmodule B.Client do
use Tesla
adapter unquote(adapter)
end
import Tesla.Adapter.TestCase, only: [httpbin_url: 0]
require Tesla
+ setup do
+ {adapter, _} = B.Client.__adapter__
+ {:ok, adapter: adapter}
+ end
+
test "basic head request" do
response = B.Client.head("#{httpbin_url}/ip")
assert response.status == 200
end
test "basic get request" do
response = B.Client.get("#{httpbin_url}/ip")
assert response.status == 200
end
test "basic post request" do
response = B.Client.post("#{httpbin_url}/post", "some-post-data", headers: %{"Content-Type" => "text/plain"})
assert response.status == 200
assert response.headers["content-type"] == "application/json"
assert Regex.match?(~r/some-post-data/, response.body)
end
test "passing query params" do
client = Tesla.build_client([{Tesla.Middleware.JSON, nil}])
response = client |> B.Client.get("#{httpbin_url}/get", query: [
page: 1, sort: "desc",
status: ["a", "b", "c"],
user: [name: "Jon", age: 20]
])
args = response.body["args"]
assert args["page"] == "1"
assert args["sort"] == "desc"
assert args["status[]"] == ["a", "b", "c"]
assert args["user[name]"] == "Jon"
assert args["user[age]"] == "20"
end
+ test "error: normalized connection refused error", %{adapter: adapter} do
+ assert {:error, :econnrefused} == adapter.call(%Tesla.Env{url: "http://localhost:1234"}, [])
+ end
+
test "error: connection refused" do
assert_raise Tesla.Error, fn ->
response = B.Client.get("http://localhost:1234")
end
end
end
end
end
defmodule Tesla.Adapter.TestCase.StreamRequestBody do
defmacro __using__([adapter: adapter]) do
quote do
defmodule S.Client do
use Tesla
adapter unquote(adapter)
end
import Tesla.Adapter.TestCase, only: [httpbin_url: 0]
test "stream request body: Stream.map" do
body = (1..5) |> Stream.map(&to_string/1)
response = S.Client.post("#{httpbin_url}/post", body, headers: %{"Content-Type" => "text/plain"})
assert response.status == 200
assert Regex.match?(~r/12345/, to_string(response.body))
end
test "stream request body: Stream.unfold" do
body = Stream.unfold(5, fn 0 -> nil; n -> {n,n-1} end)
|> Stream.map(&to_string/1)
response = S.Client.post("#{httpbin_url}/post", body, headers: %{"Content-Type" => "text/plain"})
assert response.status == 200
assert Regex.match?(~r/54321/, to_string(response.body))
end
end
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jan 21, 12:17 PM (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55713
Default Alt Text
(5 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment