From b5f30847a34361a61a62852ba822565dfc65784b Mon Sep 17 00:00:00 2001 From: Adrian Domin Date: Mon, 24 Jul 2023 08:10:54 +0200 Subject: [PATCH] Move serialize's tests --- .../sttp/client4/circe/BackendStubCirceTests.scala | 12 ------------ .../test/scala/sttp/client4/circe/CirceTests.scala | 14 +++++++++++++- .../sttp/client4/BackendStubJson4sTests.scala | 9 --------- .../src/test/scala/sttp/client4/Json4sTests.scala | 12 +++++++++++- .../jsoniter/BackendStubJsoniterTests.scala | 9 --------- .../sttp/client4/jsoniter/JsoniterJsonTests.scala | 9 +++++++++ .../sttp/client4/BackendStubPlayJsonTests.scala | 11 ----------- .../test/scala/sttp/client4/PlayJsonTests.scala | 11 +++++++++++ .../sttp/client4/BackendStubSprayJsonTests.scala | 12 ------------ .../test/scala/sttp/client4/SprayJsonTests.scala | 12 ++++++++++++ .../upicklejson/BackendStubUpickleTests.scala | 12 ------------ .../sttp/client4/upicklejson/UpickleTests.scala | 14 +++++++++++++- .../client4/ziojson/BackendStubZioJsonTests.scala | 10 ---------- .../scala/sttp/client4/ziojson/ZioJsonTests.scala | 13 ++++++++++++- .../client4/ziojson/BackendStubZioJsonTests.scala | 10 ---------- .../scala/sttp/client4/ziojson/ZioJsonTests.scala | 13 ++++++++++++- 16 files changed, 93 insertions(+), 90 deletions(-) diff --git a/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala b/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala index 858fd6c94f..4406092161 100644 --- a/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala +++ b/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala @@ -6,9 +6,6 @@ import sttp.client4.testing.SyncBackendStub import io.circe.generic.auto._ import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers -import io.circe.syntax.EncoderOps -import io.circe.JsonObject -import sttp.model.Uri class BackendStubCirceTests extends AnyFlatSpec with Matchers with ScalaFutures { @@ -19,14 +16,5 @@ class BackendStubCirceTests extends AnyFlatSpec with Matchers with ScalaFutures r.body should be(Right(Person("John"))) } - it should "serialize from JsonObject using implicit circeBodySerializer" in { - val jObject: JsonObject = JsonObject(("location", "hometown".asJson), ("bio", "Scala programmer".asJson)) - val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show - - val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" - - result should be(expectedResult) - } - case class Person(name: String) } diff --git a/json/circe/src/test/scala/sttp/client4/circe/CirceTests.scala b/json/circe/src/test/scala/sttp/client4/circe/CirceTests.scala index 6c7526d9e8..01288abed9 100644 --- a/json/circe/src/test/scala/sttp/client4/circe/CirceTests.scala +++ b/json/circe/src/test/scala/sttp/client4/circe/CirceTests.scala @@ -1,11 +1,14 @@ package sttp.client4.circe import io.circe._ +import io.circe.syntax.EncoderOps +import io.circe.syntax.EncoderOps +import io.circe.JsonObject +import sttp.model.Uri import org.scalatest._ import sttp.client4.internal._ import sttp.client4._ import sttp.model._ - import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers @@ -105,6 +108,15 @@ class CirceTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some("horses/cats") } + it should "serialize from JsonObject using implicit circeBodySerializer" in { + val jObject: JsonObject = JsonObject(("location", "hometown".asJson), ("bio", "Scala programmer".asJson)) + val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show + + val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" + + result should be(expectedResult) + } + case class Inner(a: Int, b: Boolean, c: String) object Inner { diff --git a/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala b/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala index 465b77df19..b48d54d609 100644 --- a/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala +++ b/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala @@ -24,13 +24,4 @@ class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize from JObject using implicit json4sBodySerializer" in { - val jObject: JObject = JObject(JField("location", JString("hometown")), JField("bio", JString("Scala programmer"))) - val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show - - val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" - - result should be(expectedResult) - } } diff --git a/json/json4s/src/test/scala/sttp/client4/Json4sTests.scala b/json/json4s/src/test/scala/sttp/client4/Json4sTests.scala index bd0254e0fa..a725c5f051 100644 --- a/json/json4s/src/test/scala/sttp/client4/Json4sTests.scala +++ b/json/json4s/src/test/scala/sttp/client4/Json4sTests.scala @@ -1,7 +1,8 @@ package sttp.client4 +import org.json4s.JsonAST.JString import org.json4s.ParserUtil.ParseException -import org.json4s.{native, DefaultFormats, MappingException} +import org.json4s.{DefaultFormats, JField, JObject, MappingException, native} import org.scalatest._ import sttp.client4.internal._ import sttp.model._ @@ -80,6 +81,15 @@ class Json4sTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some(MediaType.ApplicationJson.copy(charset = Some(Utf8)).toString) } + it should "serialize from JObject using implicit json4sBodySerializer" in { + val jObject: JObject = JObject(JField("location", JString("hometown")), JField("bio", JString("Scala programmer"))) + val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show + + val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" + + result should be(expectedResult) + } + def extractBody[T](request: PartialRequest[T]): String = request.body match { case StringBody(body, "utf-8", MediaType.ApplicationJson) => diff --git a/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala b/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala index d5c3296540..7847ac0873 100644 --- a/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala +++ b/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala @@ -26,13 +26,4 @@ class BackendStubJsoniterTests extends AnyFlatSpec with Matchers with ScalaFutur r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize from case class Person using implicit jsoniterBodySerializer" in { - val person = Person("John") - val result = basicRequest.get(Uri("http://example.org")).body(person).body.show - - val expectedResult = "string: {\"name\":\"John\"}" - - result should be(expectedResult) - } } diff --git a/json/jsoniter/src/test/scala/sttp/client4/jsoniter/JsoniterJsonTests.scala b/json/jsoniter/src/test/scala/sttp/client4/jsoniter/JsoniterJsonTests.scala index 248d9a76e4..26b037b65a 100644 --- a/json/jsoniter/src/test/scala/sttp/client4/jsoniter/JsoniterJsonTests.scala +++ b/json/jsoniter/src/test/scala/sttp/client4/jsoniter/JsoniterJsonTests.scala @@ -81,6 +81,15 @@ class JsoniterJsonTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some("horses/cats") } + it should "serialize from case class Person using implicit jsoniterBodySerializer" in { + val person = Person("John") + val result = basicRequest.get(Uri("http://example.org")).body(person).body.show + + val expectedResult = "string: {\"name\":\"John\"}" + + result should be(expectedResult) + } + def extractBody[T](request: PartialRequest[T]): String = request.body match { case StringBody(body, "utf-8", MediaType.ApplicationJson) => diff --git a/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala b/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala index cd16086d06..7dc8dd08ca 100644 --- a/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala +++ b/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala @@ -24,15 +24,4 @@ class BackendStubPlayJsonTests extends AnyFlatSpec with Matchers with ScalaFutur r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize from JsObject using implicit playJsonBodySerializer" in { - val fields: Seq[(String, JsValue)] = - Seq[(String, JsValue)](("location", JsString("hometown")), ("bio", JsString("Scala programmer"))) - val json: JsObject = JsObject(fields) - val result = basicRequest.get(Uri("http://example.org")).body(json).body.show - - val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" - - result should be(expectedResult) - } } diff --git a/json/play-json/src/test/scala/sttp/client4/PlayJsonTests.scala b/json/play-json/src/test/scala/sttp/client4/PlayJsonTests.scala index 9aa359b325..9a3915b1db 100644 --- a/json/play-json/src/test/scala/sttp/client4/PlayJsonTests.scala +++ b/json/play-json/src/test/scala/sttp/client4/PlayJsonTests.scala @@ -100,6 +100,17 @@ class PlayJsonTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some("horses/cats") } + it should "serialize from JsObject using implicit playJsonBodySerializer" in { + val fields: Seq[(String, JsValue)] = + Seq[(String, JsValue)](("location", JsString("hometown")), ("bio", JsString("Scala programmer"))) + val json: JsObject = JsObject(fields) + val result = basicRequest.get(Uri("http://example.org")).body(json).body.show + + val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" + + result should be(expectedResult) + } + case class Inner(a: Int, b: Boolean, c: String) object Inner { diff --git a/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala b/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala index 183a1d1947..89f9cb3dd5 100644 --- a/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala +++ b/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala @@ -25,16 +25,4 @@ class BackendStubSprayJsonTests extends AnyFlatSpec with Matchers with ScalaFutu r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize from JsObject using implicit sprayBodySerializer" in { - val json: JsObject = JsObject( - "location" -> "hometown".toJson, - "bio" -> "Scala programmer".toJson - ) - val result = basicRequest.get(Uri("http://example.org")).body(json).body.show - - val expectedResult = "string: {\"bio\":\"Scala programmer\",\"location\":\"hometown\"}" - - result should be(expectedResult) - } } diff --git a/json/spray-json/src/test/scala/sttp/client4/SprayJsonTests.scala b/json/spray-json/src/test/scala/sttp/client4/SprayJsonTests.scala index c2b71414f9..af7ccf61a4 100644 --- a/json/spray-json/src/test/scala/sttp/client4/SprayJsonTests.scala +++ b/json/spray-json/src/test/scala/sttp/client4/SprayJsonTests.scala @@ -77,6 +77,18 @@ class SprayJsonTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some(MediaType.ApplicationJson.copy(charset = Some(Utf8)).toString) } + it should "serialize from JsObject using implicit sprayBodySerializer" in { + val json: JsObject = JsObject( + "location" -> "hometown".toJson, + "bio" -> "Scala programmer".toJson + ) + val result = basicRequest.get(Uri("http://example.org")).body(json).body.show + + val expectedResult = "string: {\"bio\":\"Scala programmer\",\"location\":\"hometown\"}" + + result should be(expectedResult) + } + def extractBody[T](request: PartialRequest[T]): String = request.body match { case StringBody(body, "utf-8", MediaType.ApplicationJson) => diff --git a/json/upickle/src/test/scala/sttp/client4/upicklejson/BackendStubUpickleTests.scala b/json/upickle/src/test/scala/sttp/client4/upicklejson/BackendStubUpickleTests.scala index 35fc6180ab..2bdacecc87 100644 --- a/json/upickle/src/test/scala/sttp/client4/upicklejson/BackendStubUpickleTests.scala +++ b/json/upickle/src/test/scala/sttp/client4/upicklejson/BackendStubUpickleTests.scala @@ -22,16 +22,4 @@ class BackendStubUpickleTests extends AnyFlatSpec with Matchers with ScalaFuture r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize ujson.Obj using implicit upickleBodySerializer" in { - val json: Obj = ujson.Obj( - "location" -> "hometown", - "bio" -> "Scala programmer" - ) - val result = basicRequest.get(Uri("http://example.org")).body(json).body.show - - val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" - - result should be(expectedResult) - } } diff --git a/json/upickle/src/test/scala/sttp/client4/upicklejson/UpickleTests.scala b/json/upickle/src/test/scala/sttp/client4/upicklejson/UpickleTests.scala index e2638efcf2..e96a12ed9f 100644 --- a/json/upickle/src/test/scala/sttp/client4/upicklejson/UpickleTests.scala +++ b/json/upickle/src/test/scala/sttp/client4/upicklejson/UpickleTests.scala @@ -5,9 +5,9 @@ import org.scalatest._ import sttp.client4.internal._ import sttp.client4._ import sttp.model._ - import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers +import ujson.Obj class UpickleTests extends AnyFlatSpec with Matchers with EitherValues { "The upickle module" should "encode arbitrary bodies given an encoder" in { @@ -88,6 +88,18 @@ class UpickleTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some("horses/cats") } + it should "serialize ujson.Obj using implicit upickleBodySerializer" in { + val json: Obj = ujson.Obj( + "location" -> "hometown", + "bio" -> "Scala programmer" + ) + val result = basicRequest.get(Uri("http://example.org")).body(json).body.show + + val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" + + result should be(expectedResult) + } + case class Inner(a: Int, b: Boolean, c: String) object Inner { diff --git a/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala b/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala index 9d19ff1c37..add05cf885 100644 --- a/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala +++ b/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala @@ -26,14 +26,4 @@ class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize from Json.Obj using implicit zioJsonBodySerializer" in { - val fields: Chunk[(String, Json)] = Chunk(("location", Json.Str("hometown")), ("bio", Json.Str("Scala programmer"))) - val jObject: Json.Obj = Json.Obj(fields) - val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show - - val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" - - result should be(expectedResult) - } } diff --git a/json/zio-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala b/json/zio-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala index f3d735c4e2..2238834191 100644 --- a/json/zio-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala +++ b/json/zio-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala @@ -6,8 +6,9 @@ import org.scalatest.matchers.should.Matchers import zio.json._ import sttp.client4._ import sttp.client4.internal.Utf8 - import sttp.model._ +import zio.Chunk +import zio.json.ast.Json class ZioJsonTests extends AnyFlatSpec with Matchers with EitherValues { @@ -90,6 +91,16 @@ class ZioJsonTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some("horses/cats") } + it should "serialize from Json.Obj using implicit zioJsonBodySerializer" in { + val fields: Chunk[(String, Json)] = Chunk(("location", Json.Str("hometown")), ("bio", Json.Str("Scala programmer"))) + val jObject: Json.Obj = Json.Obj(fields) + val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show + + val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" + + result should be(expectedResult) + } + def extractBody[T](request: PartialRequest[T]): String = request.body match { case StringBody(body, "utf-8", MediaType.ApplicationJson) => diff --git a/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala b/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala index 08ccc016e2..8aa821c4f4 100644 --- a/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala +++ b/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala @@ -26,14 +26,4 @@ class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures r.is200 should be(true) r.body should be(Right(Person("John"))) } - - it should "serialize from Json.Obj using implicit zioJsonBodySerializer" in { - val fields: Chunk[(String, Json)] = Chunk(("location", Json.Str("hometown")), ("bio", Json.Str("Scala programmer"))) - val jObject: Json.Obj = Json.Obj(fields) - val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show - - val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" - - result should be(expectedResult) - } } diff --git a/json/zio1-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala b/json/zio1-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala index f3d735c4e2..2238834191 100644 --- a/json/zio1-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala +++ b/json/zio1-json/src/test/scala/sttp/client4/ziojson/ZioJsonTests.scala @@ -6,8 +6,9 @@ import org.scalatest.matchers.should.Matchers import zio.json._ import sttp.client4._ import sttp.client4.internal.Utf8 - import sttp.model._ +import zio.Chunk +import zio.json.ast.Json class ZioJsonTests extends AnyFlatSpec with Matchers with EitherValues { @@ -90,6 +91,16 @@ class ZioJsonTests extends AnyFlatSpec with Matchers with EitherValues { ct shouldBe Some("horses/cats") } + it should "serialize from Json.Obj using implicit zioJsonBodySerializer" in { + val fields: Chunk[(String, Json)] = Chunk(("location", Json.Str("hometown")), ("bio", Json.Str("Scala programmer"))) + val jObject: Json.Obj = Json.Obj(fields) + val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show + + val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}" + + result should be(expectedResult) + } + def extractBody[T](request: PartialRequest[T]): String = request.body match { case StringBody(body, "utf-8", MediaType.ApplicationJson) =>