Skip to content

Commit

Permalink
add patch support for HttpSystem (#600)
Browse files Browse the repository at this point in the history
* add patch support for HttpSystem

* suppress too many functions on httpsystem

* fix typo on suppress toomanyfunctions on httpsystem

---------

Co-authored-by: Emre Kosen <emre.kosen@trendyol.com>
  • Loading branch information
emrekosen and Emre Kosen authored Oct 14, 2024
1 parent 761654e commit 2486ba1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ suspend fun ValidationDsl.http(
validation: @HttpDsl suspend HttpSystem.() -> Unit
): Unit = validation(this.testSystem.http())

@Suppress("TooManyFunctions")
@HttpDsl
class HttpSystem(
override val testSystem: TestSystem,
Expand Down Expand Up @@ -231,6 +232,50 @@ class HttpSystem(
}.also { expect(StoveHttpResponse.WithBody(it.status.value, it.headers.toMap()) { it.body() }) }
.let { this }

@HttpDsl
suspend fun patchAndExpectBodilessResponse(
uri: String,
body: Option<Any>,
token: Option<String> = None,
headers: Map<String, String> = mapOf(),
expect: suspend (StoveHttpResponse) -> Unit
): HttpSystem = ktorHttpClient.patch(relative(uri)) {
headers.forEach { (key, value) -> header(key, value) }
token.map { header(HeaderConstants.AUTHORIZATION, HeaderConstants.bearer(it)) }
body.map { setBody(it) }
}.also { expect(StoveHttpResponse.Bodiless(it.status.value, it.headers.toMap())) }
.let { this }

@HttpDsl
suspend inline fun <reified TExpected : Any> patchAndExpectJson(
uri: String,
body: Option<Any> = None,
headers: Map<String, String> = mapOf(),
token: Option<String> = None,
expect: (actual: TExpected) -> Unit
): HttpSystem = ktorHttpClient.patch(relative(uri)) {
headers.forEach { (key, value) -> header(key, value) }
token.map { header(HeaderConstants.AUTHORIZATION, HeaderConstants.bearer(it)) }
body.map { setBody(it) }
}.also {
check(it.status.isSuccess()) { "Expected a successful response, but got ${it.status}" }
expect(it.body())
}.let { this }

@HttpDsl
suspend inline fun <reified TExpected : Any> patchAndExpectBody(
uri: String,
body: Option<Any> = None,
headers: Map<String, String> = mapOf(),
token: Option<String> = None,
expect: (actual: StoveHttpResponse.WithBody<TExpected>) -> Unit
): HttpSystem = ktorHttpClient.patch(relative(uri)) {
headers.forEach { (key, value) -> header(key, value) }
token.map { header(HeaderConstants.AUTHORIZATION, HeaderConstants.bearer(it)) }
body.map { setBody(it) }
}.also { expect(StoveHttpResponse.WithBody(it.status.value, it.headers.toMap()) { it.body() }) }
.let { this }

@HttpDsl
suspend fun deleteAndExpectBodilessResponse(
uri: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ class HttpSystemTests : FunSpec({
}
}

test("PATCH and expect bodiless/JSON response") {
val expectedPatchDtoName = UUID.randomUUID().toString()
TestSystem.validate {
wiremock {
mockPatch("/patch-with-response-body", 200, None, responseBody = TestDto(expectedPatchDtoName).some())
mockPatch("/patch-without-response-body", 200, None, responseBody = None)
}

http {

patchAndExpectBodilessResponse("/patch-without-response-body", None, None) { actual ->
actual.status shouldBe 200
}
patchAndExpectJson<TestDto>("/patch-with-response-body") { actual ->
actual.name shouldBe expectedPatchDtoName
}
}
}
}

test("GET and expect JSON response") {
val expectedGetDtoName = UUID.randomUUID().toString()
TestSystem.validate {
Expand Down Expand Up @@ -195,6 +215,21 @@ class HttpSystemTests : FunSpec({
}
}

test("patch and expect body") {
val expectedPatchDtoName = UUID.randomUUID().toString()
TestSystem.validate {
wiremock {
mockPatch("/patch-with-response-body", 200, None, responseBody = TestDto(expectedPatchDtoName).some())
}

http {
patchAndExpectBody<TestDto>("/patch-with-response-body") { actual ->
actual.body().name shouldBe expectedPatchDtoName
}
}
}
}

test("get with query params should work") {
val expectedGetDtoName = UUID.randomUUID().toString()
TestSystem.validate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ class WireMockSystem(
return this
}

@WiremockDsl
fun mockPatch(
url: String,
statusCode: Int,
requestBody: Option<Any> = None,
responseBody: Option<Any> = None,
metadata: Map<String, Any> = mapOf()
): WireMockSystem {
val res =
aResponse()
.withStatus(statusCode)
.withHeader("Content-Type", "application/json; charset=UTF-8")
responseBody.map { res.withBody(json.writeValueAsBytes(it)) }
val req = patch(urlEqualTo(url))
configureBodyAndMetadata(req, metadata, requestBody)
val stub = wireMock.stubFor(req.willReturn(res).withId(UUID.randomUUID()))
stubLog.put(stub.id, stub)
return this
}

@WiremockDsl
fun mockDelete(
url: String,
Expand Down Expand Up @@ -137,6 +157,17 @@ class WireMockSystem(
return this
}

@WiremockDsl
fun mockPatchConfigure(
url: String,
configure: (MappingBuilder, ObjectMapper) -> MappingBuilder
): WireMockSystem {
val req = patch(urlEqualTo(url))
val stub = wireMock.stubFor(configure(req, json).withId(UUID.randomUUID()))
stubLog.put(stub.id, stub)
return this
}

@WiremockDsl
fun mockGetConfigure(
url: String,
Expand Down

0 comments on commit 2486ba1

Please sign in to comment.