Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to check serialization and deserialization #1898

Merged
merged 9 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@ 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 {

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 JsonObject using implicit circeBodySerializer" in {

val jObject: JsonObject = JsonObject(("location", "hometown".asJson), ("bio", "Scala programmer".asJson))
PanHNE marked this conversation as resolved.
Show resolved Hide resolved

val backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject)
PanHNE marked this conversation as resolved.
Show resolved Hide resolved
val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend)

r.is200 should be(true)
r.body should be(jObject)
}

case class Person(name: String)
}
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 JObject using implicit json4sBodySerializer" 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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 {

import sttp.client4.basicRequest

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 case class Person using implicit jsoniterBodySerializer" in {
val person = Person("John")

val backend = SyncBackendStub.whenAnyRequest.thenRespond(person)
val r = basicRequest.get(Uri("http://example.org")).body(person).send(backend)

r.is200 should be(true)
r.body should be(person)
}
}
PanHNE marked this conversation as resolved.
Show resolved Hide resolved
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 BackendStubPlayJsonTests 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 playJsonBodySerializer" 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)
}
}
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 sprayBodySerializer" 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import sttp.client4.testing.SyncBackendStub
import sttp.model.Uri
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import ujson.Obj

case class Person(name: String)
object Person {
Expand All @@ -22,4 +23,16 @@ class BackendStubUpickleTests extends AnyFlatSpec with Matchers with ScalaFuture
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 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)
}
}
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)
}
}
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)
}
}