aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Warski <adam@warski.org>2018-04-24 11:25:45 +0200
committerGitHub <noreply@github.com>2018-04-24 11:25:45 +0200
commitba3879e9ce1a5fcaddec311daff4af73802c2c9a (patch)
tree980088d603d85e491bc1543894ed0fec8ff171f4
parent06df22987e66372281430692351ee622ede570a1 (diff)
parent4e0ab8d83a92a07b40d85ff9ce5a6c9278b88b88 (diff)
downloadsttp-ba3879e9ce1a5fcaddec311daff4af73802c2c9a.tar.gz
sttp-ba3879e9ce1a5fcaddec311daff4af73802c2c9a.tar.bz2
sttp-ba3879e9ce1a5fcaddec311daff4af73802c2c9a.zip
Merge pull request #82 from n4to4/config-max-redirects
Make max redirects configurable
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala2
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/RequestT.scala12
-rw-r--r--tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala14
3 files changed, 26 insertions, 2 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala b/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala
index 96c0213..7004631 100644
--- a/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala
@@ -29,7 +29,7 @@ class FollowRedirectsBackend[R[_], S](delegate: SttpBackend[R, S]) extends SttpB
private def followRedirect[T](request: Request[T, S], response: Response[T], redirects: Int): R[Response[T]] = {
response.header(LocationHeader).fold(responseMonad.unit(response)) { loc =>
- if (redirects >= FollowRedirectsBackend.MaxRedirects) {
+ if (redirects >= request.options.maxRedirects) {
responseMonad.unit(Response(Left("Too many redirects"), 0, "", Nil, Nil))
} else {
followRedirect(request, response, redirects, loc)
diff --git a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala
index 91635e1..082a75a 100644
--- a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala
@@ -229,6 +229,12 @@ case class RequestT[U[_], T, +S](
def followRedirects(fr: Boolean): RequestT[U, T, S] =
this.copy(options = options.copy(followRedirects = fr))
+ def maxRedirects(n: Int): RequestT[U, T, S] =
+ if (n <= 0)
+ this.copy(options = options.copy(followRedirects = false))
+ else
+ this.copy(options = options.copy(followRedirects = true, maxRedirects = n))
+
def tag(k: String, v: Any): RequestT[U, T, S] =
this.copy(tags = tags + (k -> v))
@@ -281,4 +287,8 @@ class SpecifyAuthScheme[U[_], T, +S](hn: String, rt: RequestT[U, T, S]) {
rt.header(hn, s"Bearer $token")
}
-case class RequestOptions(followRedirects: Boolean, readTimeout: Duration)
+case class RequestOptions(
+ followRedirects: Boolean,
+ readTimeout: Duration,
+ maxRedirects: Int = FollowRedirectsBackend.MaxRedirects
+)
diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala
index 1d03154..392f6d8 100644
--- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala
+++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala
@@ -631,6 +631,20 @@ class BasicTests
resp.code should be(0)
resp.history should have size (FollowRedirectsBackend.MaxRedirects)
}
+
+ name should "break redirect loops after user-specified count" in {
+ val maxRedirects = 10
+ val resp = loop.maxRedirects(maxRedirects).send().force()
+ resp.code should be(0)
+ resp.history should have size (maxRedirects)
+ }
+
+ name should "not redirect when maxRedirects is less than or equal to 0" in {
+ val resp = loop.maxRedirects(-1).send().force()
+ resp.code should be(302)
+ resp.body should be('left)
+ resp.history should be('empty)
+ }
}
def timeoutTests(): Unit = {