-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to check serialization and deserialization
- Loading branch information
Adrian Domin
committed
Jul 18, 2023
1 parent
dea0475
commit 97b37e1
Showing
7 changed files
with
239 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala
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,37 @@ | ||
package sttp.client4 | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.client4.testing.SyncBackendStub | ||
import sttp.model.Uri | ||
import org.json4s.{native, DefaultFormats, JField, JObject} | ||
import org.json4s.JsonAST.JString | ||
|
||
case class Person(name: String) | ||
|
||
class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures { | ||
|
||
implicit val serialization = native.Serialization | ||
implicit val formats = DefaultFormats | ||
|
||
import json4s._ | ||
|
||
it should "deserialize to json using a string stub" in { | ||
val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") | ||
val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(Right(Person("John"))) | ||
} | ||
|
||
it should "serialize from JsObject using implicit upickleBodySerializer" in { | ||
val jObject: JObject = JObject(JField("location", JString("hometown")), JField("bio", JString("Scala programmer"))) | ||
|
||
val backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) | ||
val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(jObject) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala
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,27 @@ | ||
package sttp.client4.jsoniter | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.client4.testing.SyncBackendStub | ||
import sttp.model.Uri | ||
import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec | ||
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker | ||
import sttp.client4.basicRequest | ||
|
||
case class Person(name: String) | ||
|
||
object Person { | ||
implicit val personJsonValueCodec: JsonValueCodec[Person] = JsonCodecMaker.make | ||
} | ||
|
||
class BackendStubJsoniterTests extends AnyFlatSpec with Matchers with ScalaFutures { | ||
|
||
it should "deserialize to json using a string stub" in { | ||
val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") | ||
val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(Right(Person("John"))) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala
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,39 @@ | ||
package sttp.client4 | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.client4.testing.SyncBackendStub | ||
import sttp.model.Uri | ||
import play.api.libs.json.{JsObject, JsValue, Json} | ||
import play.api.libs.json.{JsString, OFormat} | ||
import playJson._ | ||
|
||
case class Person(name: String) | ||
|
||
object Person { | ||
implicit val personFormat: OFormat[Person] = Json.format[Person] | ||
} | ||
|
||
class BackendStubSprayJsonTests extends AnyFlatSpec with Matchers with ScalaFutures { | ||
|
||
it should "deserialize to json using a string stub" in { | ||
val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") | ||
val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(Right(Person("John"))) | ||
} | ||
|
||
it should "serialize from JsObject using implicit upickleBodySerializer" in { | ||
val fields: Seq[(String, JsValue)] = | ||
Seq[(String, JsValue)](("location", JsString("hometown")), ("bio", JsString("Scala programmer"))) | ||
val json: JsObject = JsObject(fields) | ||
|
||
val backend = SyncBackendStub.whenAnyRequest.thenRespond(json) | ||
val r = basicRequest.get(Uri("http://example.org")).body(json).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(json) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala
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,41 @@ | ||
package sttp.client4 | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.client4.testing.SyncBackendStub | ||
import sttp.model.Uri | ||
import spray.json.{enrichAny, JsObject} | ||
import spray.json.DefaultJsonProtocol.{jsonFormat1, RootJsObjectFormat, StringJsonFormat} | ||
import spray.json.RootJsonFormat | ||
import sprayJson._ | ||
|
||
case class Person(name: String) | ||
|
||
object Person { | ||
implicit val personRootJsonFormat: RootJsonFormat[Person] = jsonFormat1(Person.apply) | ||
} | ||
|
||
class BackendStubSprayJsonTests extends AnyFlatSpec with Matchers with ScalaFutures { | ||
|
||
it should "deserialize to json using a string stub" in { | ||
val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") | ||
val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(Right(Person("John"))) | ||
} | ||
|
||
it should "serialize from JsObject using implicit upickleBodySerializer" in { | ||
val json: JsObject = JsObject( | ||
"location" -> "hometown".toJson, | ||
"bio" -> "Scala programmer".toJson | ||
) | ||
|
||
val backend = SyncBackendStub.whenAnyRequest.thenRespond(json) | ||
val r = basicRequest.get(Uri("http://example.org")).body(json).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(json) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala
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,40 @@ | ||
package sttp.client4.ziojson | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.client4.basicRequest | ||
import sttp.client4.testing.SyncBackendStub | ||
import sttp.model.Uri | ||
import zio.json.ast.Json | ||
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} | ||
import zio.Chunk | ||
|
||
case class Person(name: String) | ||
|
||
object Person { | ||
implicit val encoder: JsonEncoder[Person] = DeriveJsonEncoder.gen[Person] | ||
implicit val codec: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person] | ||
} | ||
|
||
class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures { | ||
|
||
it should "deserialize to json using a string stub" in { | ||
val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") | ||
val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) | ||
|
||
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 backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) | ||
val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(jObject) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala
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,40 @@ | ||
package sttp.client4.ziojson | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.client4.testing.SyncBackendStub | ||
import sttp.model.Uri | ||
import sttp.client4.basicRequest | ||
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} | ||
import zio.json.ast.Json | ||
import zio.Chunk | ||
|
||
case class Person(name: String) | ||
|
||
object Person { | ||
implicit val encoder: JsonEncoder[Person] = DeriveJsonEncoder.gen[Person] | ||
implicit val codec: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person] | ||
} | ||
|
||
class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures { | ||
|
||
it should "deserialize to json using a string stub" in { | ||
val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") | ||
val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) | ||
|
||
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 backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) | ||
val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) | ||
|
||
r.is200 should be(true) | ||
r.body should be(jObject) | ||
} | ||
} |