Skip to content

Commit

Permalink
Support templates in response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Mar 24, 2024
1 parent f045ad2 commit 9fa3332
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import ru.tinkoff.tcb.mockingbird.model.XmlProxyResponse
import ru.tinkoff.tcb.mockingbird.scenario.CallbackEngine
import ru.tinkoff.tcb.mockingbird.scenario.ScenarioEngine
import ru.tinkoff.tcb.protocol.log.*
import ru.tinkoff.tcb.utils.any.*
import ru.tinkoff.tcb.utils.circe.optics.JsonOptic
import ru.tinkoff.tcb.utils.regex.*
import ru.tinkoff.tcb.utils.sandboxing.GraalJsSandbox
Expand Down Expand Up @@ -128,11 +129,15 @@ final class PublicApiHandler(
)
case _ =>
ZIO.succeed(
if (stub.response.isTemplate) {
HttpStubResponse.jsonBody
.updateF(_.substitute(data).substitute(xdata))
.andThen(HttpStubResponse.xmlBody.updateF(_.substitute(data).substitute(xdata)))(stub.response)
} else stub.response
stub.response
.applyIf(_.isTemplate)(
HttpStubResponse.jsonBody
.updateF(_.substitute(data).substitute(xdata))
.andThen(HttpStubResponse.xmlBody.updateF(_.substitute(data).substitute(xdata)))
)
.applyIf(HttpStubResponse.headers.getOption(_).exists(_.values.exists(_.isTemplate)))(
HttpStubResponse.headers.updateF(_.view.mapValues(_.substitute(data, xdata)).toMap)
)
)
}
_ <- ZIO.when(stub.scope == Scope.Countdown)(stubDAO.updateById(stub.id, prop[HttpStub](_.times).inc(-1)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package ru.tinkoff.tcb.utils
import scala.util.matching.Regex

package object transformation {
val SubstRx: Regex = """\$\{(.*?)\}""".r
val CodeRx: Regex = """%\{(.*?)\}""".r
val SubstRx: Regex = """\$\{(.+?)\}""".r
val CodeRx: Regex = """%\{(.+?)\}""".r
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import ru.tinkoff.tcb.utils.transformation.json.*

package object string {
implicit final class StringTransformations(private val s: String) extends AnyVal {
def isTemplate: Boolean =
CodeRx.findFirstIn(s).isDefined || SubstRx.findFirstIn(s).isDefined

def substitute(jvalues: Json, xvalues: Node)(implicit sandbox: GraalJsSandbox): String =
if (SubstRx.findFirstIn(s).isDefined || CodeRx.findFirstIn(s).isDefined)
Json.fromString(s).substitute(jvalues).substitute(xvalues).asString.getOrElse(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ class StringTransformationsSpec extends AnyFunSuite with Matchers with TryValues

"${/a}".substitute(Json.Null, xml("<a>test</a>")) shouldBe "test"
}

test("isTemplate test") {
"".isTemplate shouldBe false
"{}".isTemplate shouldBe false
"${}".isTemplate shouldBe false
"${a}".isTemplate shouldBe true
"${a.b}".isTemplate shouldBe true
"${a.[0]}".isTemplate shouldBe true
"${a.[0].b}".isTemplate shouldBe true

"%{}".isTemplate shouldBe false
"%{var a = 1}".isTemplate shouldBe true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.tinkoff.tcb.utils

package object any {
implicit class AnyExtensionOps[T](private val t: T) extends AnyVal {
@inline def applyIf(condition: T => Boolean)(fun: T => T): T =
if (condition(t)) fun(t) else t
}
}

0 comments on commit 9fa3332

Please sign in to comment.