From f4921c1a8626c8463347dc1d7331eef4cd1cae60 Mon Sep 17 00:00:00 2001 From: Sakib Hadziavdic Date: Mon, 9 Oct 2023 19:25:47 +0200 Subject: [PATCH] SharafUtils -> utils --- examples/form/test/src/FormApiSuite.scala | 2 +- examples/json/src/Main.scala | 2 +- examples/json/src/requests.scala | 2 +- examples/json/test/src/JsonApiSuite.scala | 9 ++--- .../oauth2/test/src/IntegrationTest.scala | 4 +- sharaf/src/ba/sake/sharaf/utils.scala | 37 ++++++++++--------- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/examples/form/test/src/FormApiSuite.scala b/examples/form/test/src/FormApiSuite.scala index f2654f9..708b6c5 100644 --- a/examples/form/test/src/FormApiSuite.scala +++ b/examples/form/test/src/FormApiSuite.scala @@ -3,7 +3,7 @@ package demo import ba.sake.formson.* import ba.sake.tupson.* import ba.sake.sharaf.* -import SharafUtils.* +import ba.sake.sharaf.utils.* class FormApiSuite extends munit.FunSuite { diff --git a/examples/json/src/Main.scala b/examples/json/src/Main.scala index 28cf0be..241eca1 100644 --- a/examples/json/src/Main.scala +++ b/examples/json/src/Main.scala @@ -6,7 +6,6 @@ import io.undertow.Undertow import ba.sake.sharaf.* import ba.sake.sharaf.routing.* import ba.sake.sharaf.handlers.* -import ba.sake.tupson.* import ba.sake.validson.* @main def main: Unit = { @@ -21,6 +20,7 @@ class JsonApiModule(port: Int) { val baseUrl = s"http://localhost:${port}" + // don't do this at home! private var db = Seq.empty[CustomerRes] private val routes: Routes = { diff --git a/examples/json/src/requests.scala b/examples/json/src/requests.scala index 4f17602..4e1dfa5 100644 --- a/examples/json/src/requests.scala +++ b/examples/json/src/requests.scala @@ -1,8 +1,8 @@ package demo import ba.sake.tupson.JsonRW -import ba.sake.validson.* import ba.sake.querson.QueryStringRW +import ba.sake.validson.* case class CreateCustomerReq private (name: String, address: CreateAddressReq) derives JsonRW diff --git a/examples/json/test/src/JsonApiSuite.scala b/examples/json/test/src/JsonApiSuite.scala index a6070f8..60bf83c 100644 --- a/examples/json/test/src/JsonApiSuite.scala +++ b/examples/json/test/src/JsonApiSuite.scala @@ -1,10 +1,9 @@ package demo -import ba.sake.sharaf.handlers.ProblemDetails -import ba.sake.sharaf.handlers.ArgumentProblem -import ba.sake.sharaf.SharafUtils import ba.sake.querson.* import ba.sake.tupson.* +import ba.sake.sharaf.handlers.* +import ba.sake.sharaf.utils.* class JsonApiSuite extends munit.FunSuite { @@ -56,7 +55,7 @@ class JsonApiSuite extends munit.FunSuite { // filtering GET locally { - val queryParams = UserQuery(Set("Meho")).toQueryStringMap().map { (k, vs) => k -> vs.head } + val queryParams = UserQuery(Set("Meho")).toQueryStringMap().toRequestsQuery() val res = requests.get(s"$baseUrl/customers", params = queryParams) assertEquals(res.statusCode, 200) assertEquals(res.headers("content-type"), Seq("application/json")) @@ -121,7 +120,7 @@ class JsonApiSuite extends munit.FunSuite { def apply() = module override def beforeEach(context: BeforeEach): Unit = - module = JsonApiModule(SharafUtils.getFreePort()) + module = JsonApiModule(getFreePort()) module.server.start() override def afterEach(context: AfterEach): Unit = diff --git a/examples/oauth2/test/src/IntegrationTest.scala b/examples/oauth2/test/src/IntegrationTest.scala index 9bfcff6..dd31402 100644 --- a/examples/oauth2/test/src/IntegrationTest.scala +++ b/examples/oauth2/test/src/IntegrationTest.scala @@ -8,7 +8,7 @@ import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback import org.pac4j.core.client.Clients import org.pac4j.oauth.client.GenericOAuth20Client import org.pac4j.core.profile.definition.CommonProfileDefinition -import ba.sake.sharaf.SharafUtils +import ba.sake.sharaf.utils.* object TestData { val username = "testUser" @@ -65,7 +65,7 @@ trait IntegrationTest extends munit.FunSuite { client.setTokenUrl(mockOauth2server.tokenEndpointUrl(issuerId).toString()) client.setProfileUrl(mockOauth2server.userInfoUrl(issuerId).toString()) - val port = SharafUtils.getFreePort() + val port = getFreePort() val clients = new Clients(s"http://localhost:${port}/callback", client) // assign fixture diff --git a/sharaf/src/ba/sake/sharaf/utils.scala b/sharaf/src/ba/sake/sharaf/utils.scala index d5e88a2..da30228 100644 --- a/sharaf/src/ba/sake/sharaf/utils.scala +++ b/sharaf/src/ba/sake/sharaf/utils.scala @@ -1,26 +1,29 @@ -package ba.sake.sharaf +package ba.sake.sharaf.utils import java.net.ServerSocket import scala.util.Using import ba.sake.formson._ +import ba.sake.querson.QueryStringMap -object SharafUtils: +def getFreePort(): Int = + Using.resource(new ServerSocket(0)) { ss => + ss.getLocalPort() + } - def getFreePort(): Int = - Using.resource(new ServerSocket(0)) { ss => - ss.getLocalPort() - } - - // requests integration - extension (formDataMap: FormDataMap) - def toRequestsMultipart() = { - val multiItems = formDataMap.flatMap { case (key, values) => - values.map { - case FormValue.Str(value) => requests.MultiItem(key, value) - case FormValue.File(value) => requests.MultiItem(key, value, value.getFileName.toString) - case FormValue.ByteArray(value) => requests.MultiItem(key, value) - } +// requests integration +extension (formDataMap: FormDataMap) + def toRequestsMultipart() = { + val multiItems = formDataMap.flatMap { case (key, values) => + values.map { + case FormValue.Str(value) => requests.MultiItem(key, value) + case FormValue.File(value) => requests.MultiItem(key, value, value.getFileName.toString) + case FormValue.ByteArray(value) => requests.MultiItem(key, value) } - requests.MultiPart(multiItems.toSeq*) } + requests.MultiPart(multiItems.toSeq*) + } + +extension (queryStringMap: QueryStringMap) + def toRequestsQuery(): Map[String, String] = + queryStringMap.map { (k, vs) => k -> vs.head }