-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from amatalai/follow-redirects-improvement
Follow redirects improvement
- Loading branch information
Showing
2 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |