Skip to content

Commit

Permalink
Skal kunne kalle endepunkter som returnerer 201 uten body
Browse files Browse the repository at this point in the history
  • Loading branch information
blommish committed Dec 15, 2023
1 parent a6ecafd commit ade0276
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) {
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T = execute(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables)

inline fun <reified T : Any> postForEntityNullable(
uri: String,
payload: Any,
httpHeaders: HttpHeaders? = null,
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T? = executeNullable(uri, HttpMethod.POST, HttpEntity(payload, httpHeaders), uriVariables)

inline fun <reified T : Any> putForEntity(
uri: String,
payload: Any,
Expand All @@ -55,15 +62,24 @@ abstract class AbstractRestClient(val restTemplate: RestTemplate) {
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T = execute(uri, HttpMethod.DELETE, HttpEntity(payload, httpHeaders), uriVariables)

inline fun <reified T> execute(
inline fun <reified T : Any> execute(
urlTemplate: String,
method: HttpMethod,
entity: HttpEntity<*>,
uriVariables: Map<String, *> = emptyMap<String, String>(),
): T {
return executeNullable<T>(urlTemplate, method, entity, uriVariables)
?: error("Mangler body")
}

inline fun <reified T : Any> executeNullable(
urlTemplate: String,
method: HttpMethod,
entity: HttpEntity<*>,
uriVariables: Map<String, *>
): T? {
try {
return restTemplate.exchange<T>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +47,10 @@ internal class AbstractRestClientTest {
.toUriString()
getForEntity<Any>(uri, uriVariables = mapOf("id" to "123", "userId" to "id"))
}

fun postUtenResponseBody(): String? {
return postForEntityNullable<String>(uri.toString(), emptyMap<String, String>())
}
}

companion object {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ade0276

Please sign in to comment.