Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140698
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/test/tesla/middleware/follow_redirects_test.exs b/test/tesla/middleware/follow_redirects_test.exs
new file mode 100644
index 0000000..daafa76
--- /dev/null
+++ b/test/tesla/middleware/follow_redirects_test.exs
@@ -0,0 +1,60 @@
+defmodule FollowRedirectsTest do
+ use ExUnit.Case
+
+ use Tesla.Middleware.TestCase, middleware: Tesla.Middleware.FollowRedirects
+
+ defmodule Client do
+ use Tesla
+
+ plug Tesla.Middleware.FollowRedirects
+
+ adapter fn (env) ->
+ {status, headers, body} = case env.url do
+ "/0" ->
+ {200, %{'Content-Type' => 'text/plain'}, "foo bar"}
+ "/" <> n ->
+ next = String.to_integer(n) - 1
+ {301, %{'Location' => '/#{next}'}, ""}
+ end
+
+ %{env | status: status, headers: headers, body: body}
+ end
+ end
+
+ test "redirects if default max redirects isn't exceeded" do
+ assert Client.get("/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("/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
+ "/0" ->
+ {200, %{'Content-Type' => 'text/plain'}, "foo bar"}
+ "/" <> n ->
+ next = String.to_integer(n) - 1
+ {301, %{'Location' => '/#{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("/1").status == 200
+ end
+
+ test "raise error when custom max redirects is exceeded" do
+ assert_raise(Tesla.Error, "too many redirects", fn-> CMRClient.get("/2") end)
+ end
+
+end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jan 21, 4:24 AM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55655
Default Alt Text
(1 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment