Skip to content

Commit

Permalink
Introduced a hook through which plugins for protocols that tunnel req…
Browse files Browse the repository at this point in the history
…uests over http can modify the request before it gets passed on
  • Loading branch information
joelrosario authored and harikrishnan83 committed Jun 17, 2024
1 parent b0a8d46 commit 4adab78
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions core/src/main/kotlin/in/specmatic/stub/HttpStub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ data class HttpStubResponse(
val scenario: Scenario? = null
)

interface RequestInterceptor {
fun interceptRequest(httpRequest: HttpRequest): HttpRequest?
}

class HttpStub(
private val features: List<Feature>,
rawHttpStubs: List<HttpStubData> = emptyList(),
Expand Down Expand Up @@ -156,6 +160,12 @@ class HttpStub(

private val broadcastChannels: Vector<BroadcastChannel<SseEvent>> = Vector(50, 10)

private val requestInterceptors: MutableList<RequestInterceptor> = mutableListOf()

fun registerRequestInterceptor(requestInterceptor: RequestInterceptor) {
requestInterceptors.add(requestInterceptor)
}

private val environment = applicationEngineEnvironment {
module {
install(DoubleReceive)
Expand All @@ -182,8 +192,13 @@ class HttpStub(
val httpLogMessage = HttpLogMessage()

try {
val httpRequest = ktorHttpRequestToHttpRequest(call)
httpLogMessage.addRequest(httpRequest)
val rawHttpRequest = ktorHttpRequestToHttpRequest(call)
httpLogMessage.addRequest(rawHttpRequest)

val httpRequest = requestInterceptors.fold(rawHttpRequest) { request, requestInterceptor ->
requestInterceptor.interceptRequest(request) ?: request
}


val responseFromRequestHandler = requestHandlers.map { it.handleRequest(httpRequest) }.firstOrNull()

Expand Down

0 comments on commit 4adab78

Please sign in to comment.