Skip to content

Commit

Permalink
chore(pact-jvm-server): Converted RequestRouter to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Nov 20, 2024
1 parent ff7a287 commit 31287a1
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ abstract class StatefulMockProvider: MockProvider {
}

@Synchronized
fun handleRequest(req: Request): IResponse {
open fun handleRequest(req: Request): IResponse {
logger.debug { "Received request: $req" }
val (response, newSession) = session.receiveRequest(req)
logger.debug { "Generating response: $response" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package au.com.dius.pact.server

import au.com.dius.pact.core.model.IResponse
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.Response
import io.github.oshai.kotlinlogging.KotlinLogging

private val logger = KotlinLogging.logger {}

object RequestRouter {
fun matchPath(request: Request, oldState: ServerState): StatefulMockProvider? {
return oldState.state.entries.firstOrNull { request.path.startsWith(it.key) }?.value
}

fun handlePactRequest(request: Request, oldState: ServerState): IResponse? {
val pact = matchPath(request, oldState)
return pact?.handleRequest(request)
}

fun state404(request: Request, oldState: ServerState) =
(oldState.state.entries.map { it.toPair() } + ("path" to request.path)).joinToString(",\n") { "${it.first} -> ${it.second}" }

fun pactDispatch(request: Request, oldState: ServerState) =
handlePactRequest(request, oldState) ?: Response(404, mutableMapOf(),
OptionalBody.body(state404(request, oldState).toByteArray()))

private val urlPattern = Regex("/(\\w*)\\?{0,1}.*")

@JvmStatic
fun dispatch(request: Request, oldState: ServerState, config: Config): Result {
val matchResult = urlPattern.find(request.path)
val (action) = matchResult!!.destructured
return when (action) {
"create" -> Create.apply(request, oldState, config)
"complete" -> Complete.apply(request, oldState)
"publish" -> Publish.apply(request, oldState, config)
"" -> ListServers.apply(oldState)
else -> Result(pactDispatch(request, oldState), oldState)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package au.com.dius.pact.server
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.pactbroker.IPactBrokerClient
import scala.Option
import scala.collection.JavaConverters
import scala.collection.immutable.List
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties

Expand Down
Loading

0 comments on commit 31287a1

Please sign in to comment.