|
| 1 | +package sttp.client4.impl.cats |
| 2 | + |
| 3 | +import cats.effect.IO |
| 4 | +import cats.effect.kernel.Ref |
| 5 | +import cats.effect.std.Semaphore |
| 6 | +import cats.effect.unsafe.implicits.global |
| 7 | +import org.scalatest.flatspec.AnyFlatSpec |
| 8 | +import org.scalatest.matchers.should.Matchers |
| 9 | +import sttp.client4._ |
| 10 | +import sttp.client4.httpclient.cats.HttpClientCatsBackend |
| 11 | +import sttp.client4.listener.ListenerBackend |
| 12 | +import sttp.client4.listener.RequestListener |
| 13 | +import sttp.model.ResponseMetadata |
| 14 | + |
| 15 | +class CatsListenerBackendTest extends AnyFlatSpec with Matchers { |
| 16 | + // #2669 |
| 17 | + it should "notify the listener when a request is interrupted" in { |
| 18 | + for { |
| 19 | + inResponse <- Semaphore[IO](0) |
| 20 | + trail <- Ref.of[IO, List[String]](Nil) |
| 21 | + baseBackend = HttpClientCatsBackend.stub[IO].whenAnyRequest.thenRespondF { |
| 22 | + inResponse.release *> IO.never |
| 23 | + } |
| 24 | + backend = ListenerBackend(baseBackend, spyingListener(trail)) |
| 25 | + // send the request in the background ... |
| 26 | + sendingFiber <- basicRequest.get(uri"http://example.org").send(backend).void.start |
| 27 | + // wait until the request is being processed ... |
| 28 | + _ <- inResponse.acquire |
| 29 | + // the counter should be 1, as it should be incremented in the listener's "before" |
| 30 | + trailBefore <- trail.get |
| 31 | + _ = trailBefore shouldBe List("before") |
| 32 | + // cancel the request, interrupting the IO.never ... |
| 33 | + _ <- sendingFiber.cancel |
| 34 | + // and now the counter should be decreased again |
| 35 | + trailAfter <- trail.get |
| 36 | + _ = trailAfter shouldBe List("before", "exception") |
| 37 | + } yield () |
| 38 | + }.unsafeRunSync() |
| 39 | + |
| 40 | + it should "properly notify listener in case of normal completion" in { |
| 41 | + for { |
| 42 | + inResponse <- Semaphore[IO](0) |
| 43 | + trail <- Ref.of[IO, List[String]](Nil) |
| 44 | + baseBackend = HttpClientCatsBackend.stub[IO].whenAnyRequest.thenRespondOk() |
| 45 | + backend = ListenerBackend(baseBackend, spyingListener(trail)) |
| 46 | + _ <- basicRequest.get(uri"http://example.org").send(backend) |
| 47 | + trailAfter <- trail.get |
| 48 | + _ = trailAfter shouldBe List("before", "response handled") |
| 49 | + } yield () |
| 50 | + }.unsafeRunSync() |
| 51 | + |
| 52 | + it should "properly notify listener in case of an exception" in { |
| 53 | + for { |
| 54 | + inResponse <- Semaphore[IO](0) |
| 55 | + trail <- Ref.of[IO, List[String]](Nil) |
| 56 | + baseBackend = HttpClientCatsBackend.stub[IO].whenAnyRequest.thenThrow(new RuntimeException("test exception")) |
| 57 | + backend = ListenerBackend(baseBackend, spyingListener(trail)) |
| 58 | + _ <- basicRequest.get(uri"http://example.org").send(backend).handleErrorWith { case _ => IO.unit } |
| 59 | + trailAfter <- trail.get |
| 60 | + _ = trailAfter shouldBe List("before", "exception") |
| 61 | + } yield () |
| 62 | + }.unsafeRunSync() |
| 63 | + |
| 64 | + def spyingListener(trail: Ref[IO, List[String]]) = new RequestListener[IO, Unit] { |
| 65 | + override def before(request: GenericRequest[_, _]): IO[Unit] = trail.update(_ :+ "before") |
| 66 | + override def responseBodyReceived(request: GenericRequest[_, _], response: ResponseMetadata, tag: Unit): Unit = () |
| 67 | + override def responseHandled( |
| 68 | + request: GenericRequest[_, _], |
| 69 | + response: ResponseMetadata, |
| 70 | + tag: Unit, |
| 71 | + exception: Option[ResponseException[_]] |
| 72 | + ): IO[Unit] = trail.update(_ :+ "response handled") |
| 73 | + override def exception( |
| 74 | + request: GenericRequest[_, _], |
| 75 | + tag: Unit, |
| 76 | + exception: Throwable, |
| 77 | + responseBodyReceivedCalled: Boolean |
| 78 | + ): IO[Unit] = trail.update(_ :+ s"exception") |
| 79 | + } |
| 80 | +} |
0 commit comments