Skip to content

Commit

Permalink
Merge pull request #1907 from softwaremill/1862_Bady_serializer_for_J…
Browse files Browse the repository at this point in the history
…SON_AST_types

Add serialize's tests
  • Loading branch information
adamw authored Jul 25, 2023
2 parents a72b953 + 060c640 commit 919aad3
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 25 deletions.
11 changes: 8 additions & 3 deletions json/circe/src/test/scala/sttp/client4/circe/CirceTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ class CirceTests extends AnyFlatSpec with Matchers with EitherValues {
import sttp.model.Uri

val jObject: JsonObject = JsonObject(("location", "hometown".asJson), ("bio", "Scala programmer".asJson))
val result = basicRequest.get(Uri("http://example.org")).body(jObject).body.show
val request: Request[Either[String, String]] = basicRequest.get(Uri("http://example.org")).body(jObject)

val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

case class Inner(a: Int, b: Boolean, c: String)
Expand Down
13 changes: 9 additions & 4 deletions json/json4s/src/test/scala/sttp/client4/Json4sTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sttp.client4

import org.json4s.JsonAST.JString
import org.json4s.ParserUtil.ParseException
import org.json4s.{DefaultFormats, JField, JObject, MappingException, native}
import org.json4s.{native, DefaultFormats, JField, JObject, MappingException}
import org.scalatest._
import sttp.client4.internal._
import sttp.model._
Expand Down Expand Up @@ -83,11 +83,16 @@ class Json4sTests extends AnyFlatSpec with Matchers with EitherValues {

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 request: Request[Either[String, String]] = basicRequest.get(Uri("http://example.org")).body(jObject)

val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

def extractBody[T](request: PartialRequest[T]): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ class JsoniterJsonTests extends AnyFlatSpec with Matchers with EitherValues {

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 request = basicRequest.get(Uri("http://example.org")).body(person)

val expectedResult = "string: {\"name\":\"John\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"name\":\"John\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

def extractBody[T](request: PartialRequest[T]): String =
Expand Down
11 changes: 8 additions & 3 deletions json/play-json/src/test/scala/sttp/client4/PlayJsonTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ class PlayJsonTests extends AnyFlatSpec with Matchers with EitherValues {
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 request: Request[Either[String, String]] = basicRequest.get(Uri("http://example.org")).body(json)

val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

case class Inner(a: Int, b: Boolean, c: String)
Expand Down
11 changes: 8 additions & 3 deletions json/spray-json/src/test/scala/sttp/client4/SprayJsonTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ class SprayJsonTests extends AnyFlatSpec with Matchers with EitherValues {
"location" -> "hometown".toJson,
"bio" -> "Scala programmer".toJson
)
val result = basicRequest.get(Uri("http://example.org")).body(json).body.show
val request: Request[Either[String, String]] = basicRequest.get(Uri("http://example.org")).body(json)

val expectedResult = "string: {\"bio\":\"Scala programmer\",\"location\":\"hometown\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"bio\":\"Scala programmer\",\"location\":\"hometown\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

def extractBody[T](request: PartialRequest[T]): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ class UpickleTests extends AnyFlatSpec with Matchers with EitherValues {
"location" -> "hometown",
"bio" -> "Scala programmer"
)
val result = basicRequest.get(Uri("http://example.org")).body(json).body.show
val request: Request[Either[String, String]] = basicRequest.get(Uri("http://example.org")).body(json)

val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

case class Inner(a: Int, b: Boolean, c: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ class ZioJsonTests extends AnyFlatSpec with Matchers with EitherValues {
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 request = basicRequest.get(Uri("http://example.org")).body(jObject)

val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

def extractBody[T](request: PartialRequest[T]): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ class ZioJsonTests extends AnyFlatSpec with Matchers with EitherValues {
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 request = basicRequest.get(Uri("http://example.org")).body(jObject)

val expectedResult = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val actualBody: String = request.body.show
val actualContentType: Option[String] = request.contentType

result should be(expectedResult)
val expectedBody: String = "string: {\"location\":\"hometown\",\"bio\":\"Scala programmer\"}"
val expectedContentType: Option[String] = Some("application/json; charset=utf-8")

actualBody should be(expectedBody)
actualContentType should be(expectedContentType)
}

def extractBody[T](request: PartialRequest[T]): String =
Expand Down

0 comments on commit 919aad3

Please sign in to comment.