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

Fix query params handling #73

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -85,7 +85,7 @@ final class PublicHttp(handler: PublicApiHandler) {
method: HttpMethod,
path: String,
headers: Map[String, String],
query: Map[String, String],
query: Seq[(String, Seq[String])],
body: RequestBody
): ZIO[WLD, Throwable, (List[Header], StatusCode, HttpStubResponse)] =
handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import ru.tinkoff.tcb.mockingbird.model.HttpMethod
import ru.tinkoff.tcb.mockingbird.model.RequestBody

package object input {
private[api] type ExecInput = (HttpMethod, String, Map[String, String], Map[String, String])
private[api] type ExecInputB = (HttpMethod, String, Map[String, String], Map[String, String], RequestBody)
private[api] type ExecInput = (HttpMethod, String, Map[String, String], Seq[(String, Seq[String])])
private[api] type ExecInputB = (HttpMethod, String, Map[String, String], Seq[(String, Seq[String])], RequestBody)

private[api] val execInput: EndpointInput[ExecInput] =
extractFromRequest(_.method)
Expand All @@ -20,7 +20,5 @@ package object input {
extractFromRequest(_.headers)
.map(_.map(h => h.name -> h.value).to(Map))(_.map { case (name, value) => Header(name, value) }.to(Seq))
)
.and(
queryParams.map(_.toMap)(QueryParams.fromMap)
)
.and(queryParams.map(_.ps)(QueryParams(_)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import eu.timepit.refined.*
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.*
import eu.timepit.refined.numeric.*
import io.circe.Json
import io.circe.parser.parse
import io.scalaland.chimney.dsl.*
import kantan.xpath.*
import kantan.xpath.implicits.*
Expand Down Expand Up @@ -191,10 +189,10 @@ final class AdminApiHandler(
method: HttpMethod,
path: String,
headers: Map[String, String],
query: Map[String, String],
query: Seq[(String, Seq[String])],
body: RequestBody
): RIO[WLD, SID[HttpStub]] = {
val queryObject = Json.fromFields(query.view.mapValues(s => parse(s).getOrElse(Json.fromString(s))))
val queryObject = queryParamsToJsonObject(query)
val f = stubResolver.findStubAndState(method, path, headers, queryObject, body) _

for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.xml.Node
import eu.timepit.refined.*
import eu.timepit.refined.api.Refined
import io.circe.Json
import io.circe.parser.parse
import io.circe.syntax.*
import io.estatico.newtype.ops.*
import mouse.boolean.*
Expand All @@ -16,6 +15,7 @@ import sttp.client4.{Backend as SttpBackend, *}
import sttp.client4.circe.*
import sttp.model.HeaderNames
import sttp.model.Method
import sttp.model.QueryParams
import zio.interop.catz.core.*

import ru.tinkoff.tcb.criteria.*
Expand Down Expand Up @@ -72,10 +72,10 @@ final class PublicApiHandler(
method: HttpMethod,
path: String,
headers: Map[String, String],
query: Map[String, String],
query: Seq[(String, Seq[String])],
body: RequestBody
): RIO[WLD, HttpStubResponse] = {
val queryObject = Json.fromFields(query.view.mapValues(s => parse(s).getOrElse(Json.fromString(s))))
val queryObject = queryParamsToJsonObject(query)
val f = resolver.findStubAndState(method, path, headers, queryObject, body) _

for {
Expand Down Expand Up @@ -166,10 +166,10 @@ final class PublicApiHandler(
private def proxyRequest(
method: HttpMethod,
headers: Map[String, String],
query: Map[String, String],
query: Seq[(String, Seq[String])],
body: RequestBody
)(uri: String, delay: Option[FiniteDuration], timeout: Option[FiniteDuration]): RIO[WLD, HttpStubResponse] = {
val requestUri = uri"$uri".pipe(query.foldLeft(_) { case (u, (key, value)) => u.addParam(key, value) })
val requestUri = uri"$uri".addParams(QueryParams(query))
for {
_ <- log.debug(s"Received headers: ${headers.keys.mkString(", ")}")
// Potentially, we want to pass the request and response as is. If the client wants this
Expand Down Expand Up @@ -218,7 +218,7 @@ final class PublicApiHandler(
private def jsonProxyRequest(
method: HttpMethod,
headers: Map[String, String],
query: Map[String, String],
query: Seq[(String, Seq[String])],
body: RequestBody,
data: Json
)(
Expand All @@ -227,9 +227,7 @@ final class PublicApiHandler(
delay: Option[FiniteDuration],
timeout: Option[FiniteDuration]
): RIO[WLD, HttpStubResponse] = {
val requestUri = uri"$uri".pipe(query.foldLeft(_) { case (u, (key, value)) =>
u.addParam(key, value)
})
val requestUri = uri"$uri".addParams(QueryParams(query))
for {
_ <- log.debug(s"Received headers: ${headers.keys.mkString(", ")}")
req = basicRequest
Expand Down Expand Up @@ -280,7 +278,7 @@ final class PublicApiHandler(
private def xmlProxyRequest(
method: HttpMethod,
headers: Map[String, String],
query: Map[String, String],
query: Seq[(String, Seq[String])],
body: RequestBody,
jData: Json,
xData: Node
Expand All @@ -290,9 +288,7 @@ final class PublicApiHandler(
delay: Option[FiniteDuration],
timeout: Option[FiniteDuration]
): RIO[WLD, HttpStubResponse] = {
val requestUri = uri"$uri".pipe(query.foldLeft(_) { case (u, (key, value)) =>
u.addParam(key, value)
})
val requestUri = uri"$uri".addParams(QueryParams(query))
for {
_ <- log.debug(s"Received headers: ${headers.keys.mkString(", ")}")
req = basicRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.tinkoff.tcb.mockingbird

import io.circe.Json
import io.circe.literal.*
import io.circe.parser.parse

package object api {
/*
Expand All @@ -10,4 +12,15 @@ package object api {

def mkErrorResponse(message: String): String =
json"""{"error": $message}""".noSpaces

def queryParamsToJsonObject(query: Seq[(String, Seq[String])]): Json =
Json.fromFields(
query.map { case (k, vs) =>
val js = vs.map(s => parse(s).getOrElse(Json.fromString(s))) match {
case Seq(x) => x
case xs => Json.arr(xs: _*)
}
k -> js
}
)
}