Page MenuHomePhorge

No OneTemporary

Size
17 KB
Referenced Files
None
Subscribers
None
diff --git a/test/tesla/middleware/compression_test.exs b/test/tesla/middleware/compression_test.exs
index 741d916..69ca55a 100644
--- a/test/tesla/middleware/compression_test.exs
+++ b/test/tesla/middleware/compression_test.exs
@@ -1,112 +1,102 @@
defmodule Tesla.Middleware.CompressionTest do
use ExUnit.Case
defmodule CompressionGzipRequestClient do
use Tesla
plug Tesla.Middleware.Compression
adapter fn env ->
{status, headers, body} =
case env.url do
"/" ->
- {200, %{'Content-Type' => 'text/plain'}, :zlib.gunzip(env.body)}
+ {200, [{"content-type", "text/plain"}], :zlib.gunzip(env.body)}
end
%{env | status: status, headers: headers, body: body}
end
end
test "compress request body (gzip)" do
assert CompressionGzipRequestClient.post("/", "compress request").body == "compress request"
end
defmodule CompressionDeflateRequestClient do
use Tesla
plug Tesla.Middleware.Compression, format: "deflate"
adapter fn env ->
{status, headers, body} =
case env.url do
"/" ->
- {200, %{'Content-Type' => 'text/plain'}, :zlib.unzip(env.body)}
+ {200, [{"content-type", "text/plain"}], :zlib.unzip(env.body)}
end
%{env | status: status, headers: headers, body: body}
end
end
test "compress request body (deflate)" do
assert CompressionDeflateRequestClient.post("/", "compress request").body ==
"compress request"
end
defmodule CompressionResponseClient do
use Tesla
plug Tesla.Middleware.Compression
adapter fn env ->
{status, headers, body} =
case env.url do
"/response-gzip" ->
- {
- 200,
- %{'Content-Type' => 'text/plain', 'Content-Encoding' => 'gzip'},
- :zlib.gzip("decompressed gzip")
- }
+ {200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}],
+ :zlib.gzip("decompressed gzip")}
"/response-deflate" ->
- {
- 200,
- %{'Content-Type' => 'text/plain', 'Content-Encoding' => 'deflate'},
- :zlib.zip("decompressed deflate")
- }
+ {200, [{"content-type", "text/plain"}, {"content-encoding", "deflate"}],
+ :zlib.zip("decompressed deflate")}
"/response-identity" ->
- {
- 200,
- %{'Content-Type' => 'text/plain', 'Content-Encoding' => 'identity'},
- "unchanged"
- }
+ {200, [{"content-type", "text/plain"}, {"content-encoding", "identity"}], "unchanged"}
end
%{env | status: status, headers: headers, body: body}
end
end
test "decompress response body (gzip)" do
assert CompressionResponseClient.get("/response-gzip").body == "decompressed gzip"
end
test "decompress response body (deflate)" do
assert CompressionResponseClient.get("/response-deflate").body == "decompressed deflate"
end
test "return unchanged response for unsupported content-encoding" do
assert CompressionResponseClient.get("/response-identity").body == "unchanged"
end
defmodule CompressRequestDecompressResponseClient do
use Tesla
plug Tesla.Middleware.CompressRequest
plug Tesla.Middleware.DecompressResponse
adapter fn env ->
{status, headers, body} =
case env.url do
"/" ->
- {200, %{'Content-Type' => 'text/plain', 'Content-Encoding' => 'gzip'}, env.body}
+ {200, [{"content-type", "text/plain"}, {"content-encoding", "gzip"}], env.body}
end
%{env | status: status, headers: headers, body: body}
end
end
test "CompressRequest / DecompressResponse work without options" do
alias CompressRequestDecompressResponseClient, as: CRDRClient
assert CRDRClient.post("/", "foo bar").body == "foo bar"
end
end
diff --git a/test/tesla/middleware/follow_redirects_test.exs b/test/tesla/middleware/follow_redirects_test.exs
index 1e04c79..9185c00 100644
--- a/test/tesla/middleware/follow_redirects_test.exs
+++ b/test/tesla/middleware/follow_redirects_test.exs
@@ -1,105 +1,105 @@
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" ->
- {200, %{'Content-Type' => 'text/plain'}, "foo bar"}
+ {200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
- {301, %{'Location' => 'http://example.com/#{next}'}, ""}
+ {301, [{"location", "http://example.com/#{next}"}], ""}
end
%{env | status: status, headers: headers, body: body}
end
end
test "redirects if default max redirects isn't exceeded" do
assert Client.get("http://example.com/5").status == 200
end
test "raise error when redirect default max redirects is exceeded" do
assert_raise(Tesla.Error, "too many redirects", fn -> Client.get("http://example.com/6") end)
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" ->
- {200, %{'Content-Type' => 'text/plain'}, "foo bar"}
+ {200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
- {301, %{'Location' => 'http://example.com/#{next}'}, ""}
+ {301, [{"location", "http://example.com/#{next}"}], ""}
end
%{env | status: status, headers: headers, body: body}
end
end
alias CustomMaxRedirectsClient, as: CMRClient
test "redirects if custom max redirects isn't exceeded" do
assert CMRClient.get("http://example.com/1").status == 200
end
test "raise error when custom max redirects is exceeded" do
assert_raise(Tesla.Error, "too many redirects", fn ->
CMRClient.get("http://example.com/2")
end)
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"}
+ {200, [{"content-type", "text/plain"}], "foo bar"}
"http://example.com" ->
- {301, %{'Location' => 'https://example.com'}, ""}
+ {301, [{"location", "https://example.com"}], ""}
"https://example.com" ->
- {301, %{'Location' => '/pl'}, ""}
+ {301, [{"location", "/pl"}], ""}
"https://example.com/" ->
- {301, %{'Location' => '/pl'}, ""}
+ {301, [{"location", "/pl"}], ""}
"https://example.com/article" ->
- {301, %{'Location' => '/pl'}, ""}
+ {301, [{"location", "/pl"}], ""}
end
%{env | status: status, headers: headers, body: body}
end
end
alias RelativeLocationClient, as: RLClient
test "supports relative address in location header" do
assert RLClient.get("http://example.com").status == 200
end
test "doesn't create double slashes inside new url" do
assert RLClient.get("https://example.com/").url == "https://example.com/pl"
end
test "rewrites URLs to their root" do
assert RLClient.get("https://example.com/article").url == "https://example.com/pl"
end
end
diff --git a/test/tesla/middleware/form_urlencoded_test.exs b/test/tesla/middleware/form_urlencoded_test.exs
index f88b384..fadba6c 100644
--- a/test/tesla/middleware/form_urlencoded_test.exs
+++ b/test/tesla/middleware/form_urlencoded_test.exs
@@ -1,61 +1,61 @@
defmodule Tesla.Middleware.FormUrlencodedTest do
use ExUnit.Case
defmodule Client do
use Tesla
plug Tesla.Middleware.FormUrlencoded
adapter fn env ->
{status, headers, body} =
case env.url do
"/post" ->
- {201, %{'Content-Type' => 'text/html'}, env.body}
+ {201, [{"content-type", "text/html"}], env.body}
"/check_incoming_content_type" ->
- {201, %{'Content-Type' => 'text/html'}, Tesla.get_header(env, "content-type")}
+ {201, [{"content-type", "text/html"}], Tesla.get_header(env, "content-type")}
end
%{env | status: status, headers: headers, body: body}
end
end
test "encode body as application/x-www-form-urlencoded" do
assert URI.decode_query(Client.post("/post", %{"foo" => "%bar "}).body) == %{"foo" => "%bar "}
end
test "leave body alone if binary" do
assert Client.post("/post", "data").body == "data"
end
test "check header is set as application/x-www-form-urlencoded" do
assert Client.post("/check_incoming_content_type", %{"foo" => "%bar "}).body ==
"application/x-www-form-urlencoded"
end
defmodule MultipartClient do
use Tesla
plug Tesla.Middleware.FormUrlencoded
adapter fn %{url: url, body: %Tesla.Multipart{}} = env ->
{status, headers, body} =
case url do
"/upload" ->
- {200, %{'Content-Type' => 'text/html'}, "ok"}
+ {200, [{"content-type", "text/html"}], "ok"}
end
%{env | status: status, headers: headers, body: body}
end
end
test "skips encoding multipart bodies" do
alias Tesla.Multipart
mp =
Multipart.new()
|> Multipart.add_field("param", "foo")
assert MultipartClient.post("/upload", mp).body == "ok"
end
end
diff --git a/test/tesla/middleware/json_test.exs b/test/tesla/middleware/json_test.exs
index 70356a7..eff5aa0 100644
--- a/test/tesla/middleware/json_test.exs
+++ b/test/tesla/middleware/json_test.exs
@@ -1,209 +1,189 @@
defmodule Tesla.Middleware.JsonTest do
use ExUnit.Case
defmodule Client do
use Tesla
plug Tesla.Middleware.JSON
adapter fn env ->
{status, headers, body} =
case env.url do
"/decode" ->
- {200, %{'Content-Type' => 'application/json'}, "{\"value\": 123}"}
+ {200, [{"content-type", "application/json"}], "{\"value\": 123}"}
"/encode" ->
- {
- 200,
- %{'Content-Type' => 'application/json'},
- env.body |> String.replace("foo", "baz")
- }
+ {200, [{"content-type", "application/json"}],
+ env.body |> String.replace("foo", "baz")}
"/empty" ->
- {200, %{'Content-Type' => 'application/json'}, nil}
+ {200, [{"content-type", "application/json"}], nil}
"/empty-string" ->
- {200, %{'Content-Type' => 'application/json'}, ""}
+ {200, [{"content-type", "application/json"}], ""}
"/invalid-content-type" ->
- {200, %{'Content-Type' => 'text/plain'}, "hello"}
+ {200, [{"content-type", "text/plain"}], "hello"}
"/invalid-json-format" ->
- {200, %{'Content-Type' => 'application/json'}, "{\"foo\": bar}"}
+ {200, [{"content-type", "application/json"}], "{\"foo\": bar}"}
"/invalid-json-encoding" ->
- {200, %{'Content-Type' => 'application/json'}, <<
- 123,
- 34,
- 102,
- 111,
- 111,
- 34,
- 58,
- 32,
- 34,
- 98,
- 225,
- 114,
- 34,
- 125
- >>}
+ {200, [{"content-type", "application/json"}],
+ <<123, 34, 102, 111, 111, 34, 58, 32, 34, 98, 225, 114, 34, 125>>}
"/facebook" ->
- {200, %{'Content-Type' => 'text/javascript'}, "{\"friends\": 1000000}"}
+ {200, [{"content-type", "text/javascript"}], "{\"friends\": 1000000}"}
"/raw" ->
- {200, %{}, env.body}
+ {200, [], env.body}
"/stream" ->
list = env.body |> Enum.to_list() |> Enum.join("---")
- {200, %{}, list}
+ {200, [], list}
end
%{env | status: status, headers: headers, body: body}
end
end
test "decode JSON body" do
assert Client.get("/decode").body == %{"value" => 123}
end
test "do not decode empty body" do
assert Client.get("/empty").body == nil
end
test "do not decode empty string body" do
assert Client.get("/empty-string").body == ""
end
test "decode only if Content-Type is application/json or test/json" do
assert Client.get("/invalid-content-type").body == "hello"
end
test "encode body as JSON" do
assert Client.post("/encode", %{"foo" => "bar"}).body == %{"baz" => "bar"}
end
test "do not encode nil body" do
assert Client.post("/raw", nil).body == nil
end
test "do not encode binary body" do
assert Client.post("/raw", "raw-string").body == "raw-string"
end
test "decode if Content-Type is text/javascript" do
assert Client.get("/facebook").body == %{"friends" => 1_000_000}
end
test "post json stream" do
stream = Stream.map(1..3, fn i -> %{id: i} end)
assert env = Client.post("/stream", stream)
assert env.body == ~s|{"id":1}\n---{"id":2}\n---{"id":3}\n|
end
test "raise error when decoding invalid json format" do
assert_raise Tesla.Error, ~r/JSON decode error:/, fn ->
Client.get("/invalid-json-format")
end
end
test "raise error when decoding non-utf8 json" do
assert_raise Tesla.Error, ~r/JSON decode error:/, fn ->
Client.get("/invalid-json-encoding")
end
end
defmodule CustomClient do
use Tesla
plug Tesla.Middleware.DecodeJson, engine: Poison, engine_opts: [keys: :atoms]
adapter fn env ->
{status, headers, body} =
case env.url do
"/decode" ->
- {200, %{'Content-Type' => 'application/json'}, "{\"value\": 123}"}
+ {200, [{"content-type", "application/json"}], "{\"value\": 123}"}
end
%{env | status: status, headers: headers, body: body}
end
end
test "decode with custom engine options" do
assert CustomClient.get("/decode").body == %{value: 123}
end
defmodule CustomContentTypeClient do
use Tesla
plug Tesla.Middleware.JSON, decode_content_types: ["application/x-custom-json"]
adapter fn env ->
{status, headers, body} =
case env.url do
"/decode" ->
- {200, %{'Content-Type' => 'application/x-custom-json'}, "{\"value\": 123}"}
+ {200, [{"content-type", "application/x-custom-json"}], "{\"value\": 123}"}
end
%{env | status: status, headers: headers, body: body}
end
end
test "decode if Content-Type specified in :decode_content_types" do
alias CustomContentTypeClient, as: CCTClient
assert CCTClient.get("/decode").body == %{"value" => 123}
end
defmodule EncodeDecodeJsonClient do
use Tesla
plug Tesla.Middleware.DecodeJson
plug Tesla.Middleware.EncodeJson
adapter fn env ->
{status, headers, body} =
case env.url do
"/foo2baz" ->
- {
- 200,
- %{'Content-Type' => 'application/json'},
- env.body |> String.replace("foo", "baz")
- }
+ {200, [{"content-type", "application/json"}],
+ env.body |> String.replace("foo", "baz")}
end
%{env | status: status, headers: headers, body: body}
end
end
test "EncodeJson / DecodeJson work without options" do
alias EncodeDecodeJsonClient, as: EDJClient
assert EDJClient.post("/foo2baz", %{"foo" => "bar"}).body == %{"baz" => "bar"}
end
defmodule MultipartClient do
use Tesla
plug Tesla.Middleware.JSON
adapter fn %{url: url, body: %Tesla.Multipart{}} = env ->
{status, headers, body} =
case url do
"/upload" ->
- {200, %{'Content-Type' => 'application/json'}, "{\"status\": \"ok\"}"}
+ {200, [{"content-type", "application/json"}], "{\"status\": \"ok\"}"}
end
%{env | status: status, headers: headers, body: body}
end
end
test "skips encoding multipart bodies" do
alias Tesla.Multipart
mp =
Multipart.new()
|> Multipart.add_field("param", "foo")
assert MultipartClient.post("/upload", mp).body == %{"status" => "ok"}
end
end
diff --git a/test/tesla/mock_test.exs b/test/tesla/mock_test.exs
index 4c712ff..f8b5533 100644
--- a/test/tesla/mock_test.exs
+++ b/test/tesla/mock_test.exs
@@ -1,42 +1,42 @@
defmodule Tesla.MockTest do
use ExUnit.Case
defp setup_mock(_) do
Tesla.Mock.mock(fn
%{method: :get, url: "http://example.com/list"} -> %Tesla.Env{status: 200, body: "hello"}
- %{method: :post, url: "http://example.com/create", body: ~s({"some":"data"})} -> {201, %{"Content-Type" => "application/json"}, ~s({"id":42})}
+ %{method: :post, url: "http://example.com/create", body: ~s({"some":"data"})} -> {201, [{"content-type", "application/json"}], ~s"{\"id\":42}"}
end)
:ok
end
describe "with mock" do
setup :setup_mock
test "mock get request" do
assert %Tesla.Env{} = env = MockClient.list()
assert env.status == 200
assert env.body == "hello"
end
test "raise on unmocked request" do
assert_raise Tesla.Mock.Error, fn ->
MockClient.search()
end
end
test "mock post request" do
assert %Tesla.Env{} = env = MockClient.create(%{"some" => "data"})
assert env.status == 201
assert env.body == %{"id" => 42}
end
end
describe "without mock" do
test "raise on unmocked request" do
assert_raise Tesla.Mock.Error, fn ->
MockClient.search()
end
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 7:52 PM (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40041
Default Alt Text
(17 KB)

Event Timeline