From 4e0ab8d83a92a07b40d85ff9ce5a6c9278b88b88 Mon Sep 17 00:00:00 2001 From: n4to4 Date: Tue, 24 Apr 2018 18:19:50 +0900 Subject: Set `followRedirects` explicitly, and disable redirects if `maxRedirects <= 0` --- core/src/main/scala/com/softwaremill/sttp/RequestT.scala | 5 ++++- tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala index 7b3eab9..082a75a 100644 --- a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala +++ b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala @@ -230,7 +230,10 @@ case class RequestT[U[_], T, +S]( this.copy(options = options.copy(followRedirects = fr)) def maxRedirects(n: Int): RequestT[U, T, S] = - this.copy(options = options.copy(maxRedirects = n)) + 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)) diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 4138fe2..392f6d8 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -638,6 +638,13 @@ class BasicTests 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 = { -- cgit v1.2.3