diff --git a/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/impl/LambdaServerFactory.kt b/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/impl/LambdaServerFactory.kt index 479b54e19..979b201a3 100644 --- a/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/impl/LambdaServerFactory.kt +++ b/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/impl/LambdaServerFactory.kt @@ -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 } diff --git a/core/api/src/main/java/io/gatehill/imposter/service/ResponseService.kt b/core/api/src/main/java/io/gatehill/imposter/service/ResponseService.kt index 4889ad6e8..86d6babbe 100644 --- a/core/api/src/main/java/io/gatehill/imposter/service/ResponseService.kt +++ b/core/api/src/main/java/io/gatehill/imposter/service/ResponseService.kt @@ -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) @@ -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) diff --git a/core/engine/src/main/java/io/gatehill/imposter/service/CharacteristicsService.kt b/core/engine/src/main/java/io/gatehill/imposter/service/CharacteristicsService.kt index 54ca00815..3921ca95c 100644 --- a/core/engine/src/main/java/io/gatehill/imposter/service/CharacteristicsService.kt +++ b/core/engine/src/main/java/io/gatehill/imposter/service/CharacteristicsService.kt @@ -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() diff --git a/core/engine/src/main/java/io/gatehill/imposter/service/ResponseServiceImpl.kt b/core/engine/src/main/java/io/gatehill/imposter/service/ResponseServiceImpl.kt index 674724a02..ed8a7cd07 100644 --- a/core/engine/src/main/java/io/gatehill/imposter/service/ResponseServiceImpl.kt +++ b/core/engine/src/main/java/io/gatehill/imposter/service/ResponseServiceImpl.kt @@ -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) @@ -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) @@ -317,7 +317,7 @@ class ResponseServiceImpl @Inject constructor( notFoundMessages += message } - override fun finaliseExchange( + override fun sendThenFinaliseExchange( resourceConfig: ResourceConfig?, httpExchange: HttpExchange, block: () -> Unit, diff --git a/core/engine/src/main/java/io/gatehill/imposter/service/UpstreamService.kt b/core/engine/src/main/java/io/gatehill/imposter/service/UpstreamService.kt index ebf061d7e..fa67c3aab 100644 --- a/core/engine/src/main/java/io/gatehill/imposter/service/UpstreamService.kt +++ b/core/engine/src/main/java/io/gatehill/imposter/service/UpstreamService.kt @@ -153,7 +153,7 @@ class UpstreamService @Inject constructor( } } } - responseService.finaliseExchange(resourceConfig, httpExchange) { + responseService.sendThenFinaliseExchange(resourceConfig, httpExchange) { try { responseService.writeResponseData( resourceConfig, diff --git a/core/engine/src/test/java/io/gatehill/imposter/service/CharacteristicsServiceTest.kt b/core/engine/src/test/java/io/gatehill/imposter/service/CharacteristicsServiceTest.kt index 079309710..bbaa7e688 100644 --- a/core/engine/src/test/java/io/gatehill/imposter/service/CharacteristicsServiceTest.kt +++ b/core/engine/src/test/java/io/gatehill/imposter/service/CharacteristicsServiceTest.kt @@ -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]. @@ -108,7 +103,7 @@ class CharacteristicsServiceTest { private fun sendFailureType(failureType: FailureSimulationType): HttpResponse { val responseService = mock { - on { finaliseExchange(any(), any(), any()) } doAnswer { + on { sendThenFinaliseExchange(any(), any(), any()) } doAnswer { @Suppress("UNCHECKED_CAST") val block = it.arguments[2] as () -> Unit block() } @@ -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 } } diff --git a/core/engine/src/test/java/io/gatehill/imposter/service/ResponseServiceImplTest.kt b/core/engine/src/test/java/io/gatehill/imposter/service/ResponseServiceImplTest.kt index 58a5ca011..4a3a727d0 100644 --- a/core/engine/src/test/java/io/gatehill/imposter/service/ResponseServiceImplTest.kt +++ b/core/engine/src/test/java/io/gatehill/imposter/service/ResponseServiceImplTest.kt @@ -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 @@ -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 /** @@ -222,7 +213,7 @@ class ResponseServiceImplTest { on { response } doReturn httpResponse } var blockExecuted = false - responseService.finaliseExchange(RestResourceConfig(), httpExchange) { + responseService.sendThenFinaliseExchange(RestResourceConfig(), httpExchange) { blockExecuted = true }