diff --git a/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt b/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt index 519cb93..2b31c0e 100644 --- a/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt +++ b/http-client/main/no/nav/tilleggsstonader/libs/http/client/AbstractRestClient.kt @@ -34,6 +34,13 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) { uriVariables: Map = emptyMap(), ): T = execute(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables) + inline fun postForEntityNullable( + uri: String, + payload: Any, + httpHeaders: HttpHeaders? = null, + uriVariables: Map = emptyMap(), + ): T? = executeNullable(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables) + inline fun putForEntity( uri: String, payload: Any, @@ -55,15 +62,24 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) { uriVariables: Map = emptyMap(), ): T = execute(uri, HttpMethod.DELETE, HttpEntity(payload, httpHeaders), uriVariables) - inline fun execute( + inline fun execute( urlTemplate: String, method: HttpMethod, entity: HttpEntity<*>, uriVariables: Map = emptyMap(), ): T { + return executeNullable(urlTemplate, method, entity, uriVariables) + ?: error("Mangler body") + } + + inline fun executeNullable( + urlTemplate: String, + method: HttpMethod, + entity: HttpEntity<*>, + uriVariables: Map, + ): T? { try { return restTemplate.exchange(urlTemplate, method, entity, uriVariables).body - ?: error("Mangler body") } catch (e: RestClientResponseException) { val url = expand(urlTemplate, uriVariables) secureLogger.warn("Feil ved kall method=$method mot url=$url", e) diff --git a/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt b/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt index a515bf2..e983c49 100644 --- a/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt +++ b/http-client/test/no/nav/tilleggsstonader/libs/http/client/AbstractRestClientTest.kt @@ -4,6 +4,7 @@ import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.client.WireMock.aResponse import com.github.tomakehurst.wiremock.client.WireMock.anyUrl +import com.github.tomakehurst.wiremock.client.WireMock.created import com.github.tomakehurst.wiremock.client.WireMock.okJson import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo import com.github.tomakehurst.wiremock.common.ConsoleNotifier @@ -46,6 +47,10 @@ internal class AbstractRestClientTest { .toUriString() getForEntity(uri, uriVariables = mapOf("id" to "123", "userId" to "id")) } + + fun postUtenResponseBody(): String? { + return postForEntityNullable(uri.toString(), emptyMap()) + } } companion object { @@ -102,6 +107,16 @@ internal class AbstractRestClientTest { } } + @Test + fun `skal kunne kalle på endepunkt og forvente svar uten body`() { + wireMockServer.stubFor( + WireMock.post(anyUrl()) + .willReturn(created()), + ) + + assertThat(client.postUtenResponseBody()).isNull() + } + @Test fun `query param request skal feile hvis query params ikke mer med`() { wireMockServer.stubFor(