Skip to content

Commit

Permalink
SharafUtils -> utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Oct 9, 2023
1 parent d69c7f7 commit f4921c1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/form/test/src/FormApiSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion examples/json/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion examples/json/src/requests.scala
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 4 additions & 5 deletions examples/json/test/src/JsonApiSuite.scala
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions examples/oauth2/test/src/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
37 changes: 20 additions & 17 deletions sharaf/src/ba/sake/sharaf/utils.scala
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit f4921c1

Please sign in to comment.