Skip to content

Commit

Permalink
Move serialize's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Domin committed Jul 24, 2023
1 parent f6f2fe4 commit b5f3084
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
}
14 changes: 13 additions & 1 deletion json/circe/src/test/scala/sttp/client4/circe/CirceTests.scala
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 11 additions & 1 deletion json/json4s/src/test/scala/sttp/client4/Json4sTests.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
11 changes: 11 additions & 0 deletions json/play-json/src/test/scala/sttp/client4/PlayJsonTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 12 additions & 0 deletions json/spray-json/src/test/scala/sttp/client4/SprayJsonTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) =>
Expand Down

0 comments on commit b5f3084

Please sign in to comment.