Skip to content

Commit

Permalink
refactor: rename exchange finalisation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Sep 2, 2023
1 parent 71204fe commit 3ac0853
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class LambdaServerFactory @Inject constructor(
path += indexFile
}
logger.debug("Serving static resource: $path")
responseService.finaliseExchange(null, exchange) {
responseService.sendThenFinaliseExchange(null, exchange) {
val responseBehaviour = ReadWriteResponseBehaviourImpl().apply {
responseFile = path
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface ResponseService {
* Set the HTTP status code, headers and body, then calls [HttpResponse.end].
* This should only be called by the error handler. See [failWithNotFoundResponse].
*
* Note: this method calls [finaliseExchange].
* Note: this method calls [sendThenFinaliseExchange].
*/
fun sendNotFoundResponse(httpExchange: HttpExchange)

Expand All @@ -131,9 +131,12 @@ interface ResponseService {
)

/**
* Invoke after the exchange has been handled.
* Invoke the `block`, then finalise the exchange by setting the phase, and
* calling any configured listeners.
* Typically, the block sends the data after the exchange has been handled
* by an appropriate handler.
*/
fun finaliseExchange(resourceConfig: ResourceConfig?, httpExchange: HttpExchange, block: () -> Unit)
fun sendThenFinaliseExchange(resourceConfig: ResourceConfig?, httpExchange: HttpExchange, block: () -> Unit)

fun interface ResponseSender {
@Throws(Exception::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CharacteristicsService @Inject constructor(
failureType,
LogUtil.describeRequestShort(httpExchange),
)
responseService.finaliseExchange(resourceConfig, httpExchange) {
responseService.sendThenFinaliseExchange(resourceConfig, httpExchange) {
try {
when (failureType) {
FailureSimulationType.EmptyResponse -> httpExchange.response.end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ResponseServiceImpl @Inject constructor(
LogUtil.describeRequestShort(httpExchange),
responseBehaviour.statusCode
)
finaliseExchange(resourceConfig, httpExchange) {
sendThenFinaliseExchange(resourceConfig, httpExchange) {
try {
val response = httpExchange.response
response.setStatusCode(responseBehaviour.statusCode)
Expand Down Expand Up @@ -285,7 +285,7 @@ class ResponseServiceImpl @Inject constructor(
httpExchange.fail(HttpUtil.HTTP_NOT_FOUND)
}

override fun sendNotFoundResponse(httpExchange: HttpExchange) = finaliseExchange(null, httpExchange) {
override fun sendNotFoundResponse(httpExchange: HttpExchange) = sendThenFinaliseExchange(null, httpExchange) {
val response = httpExchange.response
response.setStatusCode(HttpUtil.HTTP_NOT_FOUND)

Expand Down Expand Up @@ -317,7 +317,7 @@ class ResponseServiceImpl @Inject constructor(
notFoundMessages += message
}

override fun finaliseExchange(
override fun sendThenFinaliseExchange(
resourceConfig: ResourceConfig?,
httpExchange: HttpExchange,
block: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class UpstreamService @Inject constructor(
}
}
}
responseService.finaliseExchange(resourceConfig, httpExchange) {
responseService.sendThenFinaliseExchange(resourceConfig, httpExchange) {
try {
responseService.writeResponseData(
resourceConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ import io.gatehill.imposter.script.ReadWriteResponseBehaviourImpl
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.*

/**
* Tests for [CharacteristicsService].
Expand Down Expand Up @@ -108,7 +103,7 @@ class CharacteristicsServiceTest {

private fun sendFailureType(failureType: FailureSimulationType): HttpResponse {
val responseService = mock<ResponseService> {
on { finaliseExchange(any(), any(), any()) } doAnswer {
on { sendThenFinaliseExchange(any(), any(), any()) } doAnswer {
@Suppress("UNCHECKED_CAST") val block = it.arguments[2] as () -> Unit
block()
}
Expand All @@ -126,7 +121,7 @@ class CharacteristicsServiceTest {
val service = CharacteristicsService(responseService)
service.sendFailure(mock(), httpExchange, failureType)

verify(responseService).finaliseExchange(any(), eq(httpExchange), any())
verify(responseService).sendThenFinaliseExchange(any(), eq(httpExchange), any())
return httpResponse
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@

package io.gatehill.imposter.service

import io.gatehill.imposter.http.ExchangePhase
import io.gatehill.imposter.http.HttpExchange
import io.gatehill.imposter.http.HttpMethod
import io.gatehill.imposter.http.HttpRequest
import io.gatehill.imposter.http.HttpResponse
import io.gatehill.imposter.http.*
import io.gatehill.imposter.plugin.config.PluginConfigImpl
import io.gatehill.imposter.plugin.config.resource.RestResourceConfig
import io.gatehill.imposter.script.ReadWriteResponseBehaviourImpl
Expand All @@ -56,12 +52,7 @@ import io.vertx.core.buffer.Buffer
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.*
import java.io.File

/**
Expand Down Expand Up @@ -222,7 +213,7 @@ class ResponseServiceImplTest {
on { response } doReturn httpResponse
}
var blockExecuted = false
responseService.finaliseExchange(RestResourceConfig(), httpExchange) {
responseService.sendThenFinaliseExchange(RestResourceConfig(), httpExchange) {
blockExecuted = true
}

Expand Down

0 comments on commit 3ac0853

Please sign in to comment.