Skip to content

Commit 2009056

Browse files
committed
DefaultHttpSystem to HttpSystem, add missing extensions, remove duplicated extensions
1 parent 6e81fa3 commit 2009056

File tree

4 files changed

+80
-140
lines changed

4 files changed

+80
-140
lines changed

examples/ktor-example/src/test/kotlin/com/stove/ktor/example/e2e/ExampleTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.stove.ktor.example.e2e
22

33
import arrow.core.some
44
import com.trendol.stove.testing.e2e.rdbms.postgres.postgresql
5-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.postAndExpectBodilessResponse
5+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.postAndExpectBodilessResponse
66
import com.trendyol.stove.testing.e2e.http.http
77
import com.trendyol.stove.testing.e2e.rdbms.RelationalDatabaseSystem.Companion.shouldQuery
88
import com.trendyol.stove.testing.e2e.system.TestSystem

examples/spring-example/src/test/kotlin/com/stove/spring/example/e2e/ExampleTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.stove.spring.example.e2e
33
import arrow.core.some
44
import com.trendyol.stove.testing.e2e.couchbase.CouchbaseSystem.Companion.shouldGet
55
import com.trendyol.stove.testing.e2e.couchbase.couchbase
6-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.get
7-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.postAndExpectJson
6+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.get
7+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.postAndExpectJson
88
import com.trendyol.stove.testing.e2e.http.http
99
import com.trendyol.stove.testing.e2e.kafka.KafkaSystem.Companion.shouldBeConsumedOnCondition
1010
import com.trendyol.stove.testing.e2e.kafka.KafkaSystem.Companion.shouldBeFailedOnCondition

lib/stove-testing-e2e-http/src/main/kotlin/com/trendyol/stove/testing/e2e/http/DefaultHttpSystem.kt renamed to lib/stove-testing-e2e-http/src/main/kotlin/com/trendyol/stove/testing/e2e/http/HttpSystem.kt

Lines changed: 70 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ data class HttpClientSystemOptions(val objectMapper: ObjectMapper = StoveObjectM
3434
replaceWith = ReplaceWith("withHttpClient()", "com.trendyol.stove.testing.e2e.http.TestSystem")
3535
)
3636
fun TestSystem.withDefaultHttp(objectMapper: ObjectMapper = StoveObjectMapper.Default): TestSystem {
37-
this.getOrRegister(DefaultHttpSystem(this, objectMapper))
37+
this.getOrRegister(HttpSystem(this, objectMapper))
3838
return this
3939
}
4040

4141
fun TestSystem.withHttpClient(options: HttpClientSystemOptions = HttpClientSystemOptions()): TestSystem {
42-
this.getOrRegister(DefaultHttpSystem(this, options.objectMapper))
42+
this.getOrRegister(HttpSystem(this, options.objectMapper))
4343
return this
4444
}
4545

@@ -51,52 +51,34 @@ fun WithDsl.httpClient(configure: () -> HttpClientSystemOptions = { HttpClientSy
5151
"This method is deprecated, going to be removed",
5252
replaceWith = ReplaceWith("http()", "com.trendyol.stove.testing.e2e.http.TestSystem")
5353
)
54-
fun TestSystem.defaultHttp(): DefaultHttpSystem =
55-
getOrNone<DefaultHttpSystem>().getOrElse {
56-
throw SystemNotRegisteredException(DefaultHttpSystem::class)
54+
fun TestSystem.defaultHttp(): HttpSystem =
55+
getOrNone<HttpSystem>().getOrElse {
56+
throw SystemNotRegisteredException(HttpSystem::class)
5757
}
5858

59-
fun TestSystem.http(): DefaultHttpSystem =
60-
getOrNone<DefaultHttpSystem>().getOrElse {
61-
throw SystemNotRegisteredException(DefaultHttpSystem::class)
59+
fun TestSystem.http(): HttpSystem =
60+
getOrNone<HttpSystem>().getOrElse {
61+
throw SystemNotRegisteredException(HttpSystem::class)
6262
}
6363

64-
suspend fun ValidationDsl.http(validation: suspend DefaultHttpSystem.() -> Unit): Unit =
64+
suspend fun ValidationDsl.http(validation: suspend HttpSystem.() -> Unit): Unit =
6565
validation(this.testSystem.http())
6666

67-
class DefaultHttpSystem(
67+
class HttpSystem(
6868
override val testSystem: TestSystem,
6969
private val objectMapper: ObjectMapper
7070
) : PluggedSystem {
7171
private val httpClient: HttpClient = httpClient()
7272

73-
@PublishedApi
74-
internal suspend fun <TExpected : Any> postAndExpectJson(
73+
@Suppress("unused")
74+
suspend fun getResponse(
7575
uri: String,
76-
body: Option<Any>,
77-
clazz: KClass<TExpected>,
78-
token: Option<String>,
79-
expect: suspend (actual: TExpected) -> Unit
80-
): DefaultHttpSystem = httpClient.send(uri) { request ->
81-
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
82-
body.fold(
83-
ifEmpty = { request.POST(BodyPublishers.noBody()) },
84-
ifSome = { request.POST(BodyPublishers.ofString(objectMapper.writeValueAsString(it))) }
85-
)
86-
}.let { expect(deserialize(it, clazz)); this }
87-
88-
@PublishedApi
89-
internal suspend fun postAndExpectBodilessResponse(
90-
uri: String,
91-
token: Option<String>,
92-
body: Option<Any>,
76+
queryParams: Map<String, String> = mapOf(),
77+
token: Option<String> = None,
9378
expect: suspend (StoveHttpResponse) -> Unit
94-
): DefaultHttpSystem = httpClient.send(uri) { request ->
79+
): HttpSystem = httpClient.send(uri, queryParams) { request ->
9580
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
96-
body.fold(
97-
ifEmpty = { request.POST(BodyPublishers.noBody()) },
98-
ifSome = { request.POST(BodyPublishers.ofString(objectMapper.writeValueAsString(it))) }
99-
)
81+
request
10082
}.let { expect(StoveHttpResponse(it.statusCode(), it.headers().map())); this }
10183

10284
@PublishedApi
@@ -106,7 +88,7 @@ class DefaultHttpSystem(
10688
clazz: KClass<TExpected>,
10789
token: Option<String>,
10890
expect: suspend (TExpected) -> Unit
109-
): DefaultHttpSystem = httpClient.send(uri, queryParams) { request ->
91+
): HttpSystem = httpClient.send(uri, queryParams) { request ->
11092
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
11193
request.GET()
11294
}.let { expect(deserialize(it, clazz)); this }
@@ -118,7 +100,7 @@ class DefaultHttpSystem(
118100
clazz: KClass<TExpected>,
119101
token: Option<String>,
120102
expect: suspend (List<TExpected>) -> Unit
121-
): DefaultHttpSystem = httpClient.send(uri, queryParams) { request ->
103+
): HttpSystem = httpClient.send(uri, queryParams) { request ->
122104
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
123105
request.GET()
124106
}.let {
@@ -131,22 +113,40 @@ class DefaultHttpSystem(
131113
}
132114

133115
@PublishedApi
134-
internal suspend fun getResponse(
116+
internal suspend fun <TExpected : Any> postAndExpectJson(
135117
uri: String,
136-
queryParams: Map<String, String>,
118+
body: Option<Any>,
119+
clazz: KClass<TExpected>,
137120
token: Option<String>,
121+
expect: suspend (actual: TExpected) -> Unit
122+
): HttpSystem = httpClient.send(uri) { request ->
123+
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
124+
body.fold(
125+
ifEmpty = { request.POST(BodyPublishers.noBody()) },
126+
ifSome = { request.POST(BodyPublishers.ofString(objectMapper.writeValueAsString(it))) }
127+
)
128+
}.let { expect(deserialize(it, clazz)); this }
129+
130+
@PublishedApi
131+
internal suspend fun postAndExpectBodilessResponse(
132+
uri: String,
133+
token: Option<String>,
134+
body: Option<Any>,
138135
expect: suspend (StoveHttpResponse) -> Unit
139-
): DefaultHttpSystem = httpClient.send(uri, queryParams) { request ->
136+
): HttpSystem = httpClient.send(uri) { request ->
140137
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
141-
request
138+
body.fold(
139+
ifEmpty = { request.POST(BodyPublishers.noBody()) },
140+
ifSome = { request.POST(BodyPublishers.ofString(objectMapper.writeValueAsString(it))) }
141+
)
142142
}.let { expect(StoveHttpResponse(it.statusCode(), it.headers().map())); this }
143143

144144
@PublishedApi
145145
internal suspend fun deleteAndExpectBodilessResponse(
146146
uri: String,
147147
token: Option<String>,
148148
expect: suspend (StoveHttpResponse) -> Unit
149-
): DefaultHttpSystem = httpClient.send(uri) { request ->
149+
): HttpSystem = httpClient.send(uri) { request ->
150150
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
151151
request.DELETE()
152152
}.let { expect(StoveHttpResponse(it.statusCode(), it.headers().map())); this }
@@ -157,7 +157,7 @@ class DefaultHttpSystem(
157157
token: Option<String>,
158158
body: Option<Any>,
159159
expect: suspend (StoveHttpResponse) -> Unit
160-
): DefaultHttpSystem = httpClient.send(uri) { request ->
160+
): HttpSystem = httpClient.send(uri) { request ->
161161
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
162162
body.fold(
163163
ifEmpty = { request.PUT(BodyPublishers.noBody()) },
@@ -172,7 +172,7 @@ class DefaultHttpSystem(
172172
clazz: KClass<TExpected>,
173173
token: Option<String>,
174174
expect: suspend (actual: TExpected) -> Unit
175-
): DefaultHttpSystem = httpClient.send(uri) { request ->
175+
): HttpSystem = httpClient.send(uri) { request ->
176176
token.map { request.setHeader(Headers.Authorization, Headers.bearer(it)) }
177177
body.fold(
178178
ifEmpty = { request.PUT(BodyPublishers.noBody()) },
@@ -234,136 +234,76 @@ class DefaultHttpSystem(
234234
fun bearer(token: String) = "Bearer $token"
235235
}
236236

237-
fun DefaultHttpSystem.client(): HttpClient = this.httpClient
237+
@Suppress("unused")
238+
fun HttpSystem.client(): HttpClient = this.httpClient
238239

239240
/**
240-
* Extension for: [DefaultHttpSystem.get]
241+
* Extension for: [HttpSystem.get]
241242
* */
242-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.get(
243+
suspend inline fun <reified TExpected : Any> HttpSystem.get(
243244
uri: String,
244245
queryParams: Map<String, String> = mapOf(),
245246
token: Option<String> = None,
246247
noinline expect: suspend (TExpected) -> Unit
247-
): DefaultHttpSystem = this.get(uri, queryParams, TExpected::class, token, expect)
248+
): HttpSystem = this.get(uri, queryParams, TExpected::class, token, expect)
248249

249250
/**
250-
* Extension for: [DefaultHttpSystem.get]
251+
* Extension for: [HttpSystem.getMany]
251252
* */
252-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.get(
253-
uri: String,
254-
queryParams: Map<String, String> = mapOf(),
255-
noinline expect: suspend (TExpected) -> Unit
256-
): DefaultHttpSystem = this.get(uri, queryParams, TExpected::class, None, expect)
257-
258-
/**
259-
* Extension for: [DefaultHttpSystem.getResponse]
260-
* */
261-
suspend fun DefaultHttpSystem.getResponse(
262-
uri: String,
263-
queryParams: Map<String, String> = mapOf(),
264-
expect: suspend (StoveHttpResponse) -> Unit
265-
): DefaultHttpSystem = this.getResponse(uri, queryParams, None, expect)
266-
267-
/**
268-
* Extension for: [DefaultHttpSystem.getMany]
269-
* */
270-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.getMany(
253+
suspend inline fun <reified TExpected : Any> HttpSystem.getMany(
271254
uri: String,
272255
queryParams: Map<String, String> = mapOf(),
273256
token: Option<String> = None,
274257
noinline expect: suspend (List<TExpected>) -> Unit
275-
): DefaultHttpSystem = this.getMany(uri, queryParams, TExpected::class, token, expect)
258+
): HttpSystem = this.getMany(uri, queryParams, TExpected::class, token, expect)
276259

277260
/**
278-
* Extension for: [DefaultHttpSystem.getMany]
261+
* Extension for: [HttpSystem.postAndExpectJson]
279262
* */
280-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.getMany(
281-
uri: String,
282-
queryParams: Map<String, String> = mapOf(),
283-
noinline expect: suspend (List<TExpected>) -> Unit
284-
): DefaultHttpSystem = this.getMany(uri, queryParams, TExpected::class, None, expect)
285-
286-
/**
287-
* Extension for: [DefaultHttpSystem.postAndExpectJson]
288-
* */
289-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.postAndExpectJson(
263+
suspend inline fun <reified TExpected : Any> HttpSystem.postAndExpectJson(
290264
uri: String,
291265
body: Option<Any> = None,
292266
token: Option<String> = None,
293267
noinline expect: suspend (actual: TExpected) -> Unit
294-
): DefaultHttpSystem = this.postAndExpectJson(uri, body, TExpected::class, token, expect)
295-
296-
/**
297-
* Extension for: [DefaultHttpSystem.postAndExpectJson]
298-
* */
299-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.postAndExpectJson(
300-
uri: String,
301-
body: Option<Any> = None,
302-
noinline expect: suspend (actual: TExpected) -> Unit
303-
): DefaultHttpSystem = this.postAndExpectJson(uri, body, TExpected::class, None, expect)
268+
): HttpSystem = this.postAndExpectJson(uri, body, TExpected::class, token, expect)
304269

305270
/**
306-
* Extension for: [DefaultHttpSystem.postAndExpectJson]
271+
* Extension for: [HttpSystem.postAndExpectBodilessResponse]
307272
* */
308-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.postAndExpectJson(
309-
uri: String,
310-
noinline expect: suspend (actual: TExpected) -> Unit
311-
): DefaultHttpSystem = this.postAndExpectJson(uri, None, TExpected::class, None, expect)
312-
313-
/**
314-
* Extension for: [DefaultHttpSystem.postAndExpectBodilessResponse]
315-
* */
316-
suspend inline fun DefaultHttpSystem.postAndExpectBodilessResponse(
273+
suspend inline fun HttpSystem.postAndExpectBodilessResponse(
317274
uri: String,
275+
token: Option<String> = None,
318276
body: Option<Any> = None,
319277
noinline expect: suspend (actual: StoveHttpResponse) -> Unit
320-
): DefaultHttpSystem = this.postAndExpectBodilessResponse(uri, None, body, expect)
278+
): HttpSystem = this.postAndExpectBodilessResponse(uri, token, body, expect)
321279

322280
/**
323-
* Extension for: [DefaultHttpSystem.postAndExpectBodilessResponse]
281+
* Extension for: [HttpSystem.deleteAndExpectBodilessResponse]
324282
* */
325-
suspend inline fun DefaultHttpSystem.postAndExpectBodilessResponse(
283+
suspend inline fun HttpSystem.deleteAndExpectBodilessResponse(
326284
uri: String,
285+
token: Option<String> = None,
327286
noinline expect: suspend (actual: StoveHttpResponse) -> Unit
328-
): DefaultHttpSystem = this.postAndExpectBodilessResponse(uri, None, None, expect)
287+
): HttpSystem = this.deleteAndExpectBodilessResponse(uri, token, expect)
329288

330289
/**
331-
* Extension for: [DefaultHttpSystem.deleteAndExpectBodilessResponse]
290+
* Extension for: [HttpSystem.putAndExpectJson]
332291
* */
333-
suspend inline fun DefaultHttpSystem.deleteAndExpectBodilessResponse(
334-
uri: String,
335-
noinline expect: suspend (actual: StoveHttpResponse) -> Unit
336-
): DefaultHttpSystem = this.deleteAndExpectBodilessResponse(uri, None, expect)
337-
338-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.putAndExpectJson(
292+
suspend inline fun <reified TExpected : Any> HttpSystem.putAndExpectJson(
339293
uri: String,
294+
token: Option<String> = None,
340295
body: Option<Any> = None,
341296
noinline expect: suspend (actual: TExpected) -> Unit
342-
): DefaultHttpSystem = this.putAndExpectJson(uri, body, TExpected::class, None, expect)
297+
): HttpSystem = this.putAndExpectJson(uri, body, TExpected::class, token, expect)
343298

344299
/**
345-
* Extension for: [DefaultHttpSystem.putAndExpectJson]
300+
* Extension for: [HttpSystem.putAndExpectBodilessResponse]
346301
* */
347-
suspend inline fun <reified TExpected : Any> DefaultHttpSystem.putAndExpectJson(
348-
uri: String,
349-
noinline expect: suspend (actual: TExpected) -> Unit
350-
): DefaultHttpSystem = this.putAndExpectJson(uri, None, TExpected::class, None, expect)
351-
352-
/**
353-
* Extension for: [DefaultHttpSystem.putAndExpectBodilessResponse]
354-
* */
355-
suspend inline fun DefaultHttpSystem.putAndExpectBodilessResponse(
302+
suspend inline fun HttpSystem.putAndExpectBodilessResponse(
356303
uri: String,
304+
token: Option<String> = None,
357305
body: Option<Any> = None,
358306
noinline expect: suspend (actual: StoveHttpResponse) -> Unit
359-
): DefaultHttpSystem = this.putAndExpectBodilessResponse(uri, None, body, expect)
360-
361-
/**
362-
* Extension for: [DefaultHttpSystem.putAndExpectBodilessResponse]
363-
* */
364-
suspend inline fun DefaultHttpSystem.putAndExpectBodilessResponse(
365-
uri: String,
366-
noinline expect: suspend (actual: StoveHttpResponse) -> Unit
367-
): DefaultHttpSystem = this.putAndExpectBodilessResponse(uri, None, None, expect)
307+
): HttpSystem = this.putAndExpectBodilessResponse(uri, token, body, expect)
368308
}
369309
}

lib/stove-testing-e2e-http/src/test/kotlin/com/trendyol/stove/testing/e2e/http/DefaultHttpSystemTests.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package com.trendyol.stove.testing.e2e.http
22

33
import arrow.core.None
44
import arrow.core.some
5-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.deleteAndExpectBodilessResponse
6-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.get
7-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.getMany
8-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.postAndExpectBodilessResponse
9-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.postAndExpectJson
10-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.putAndExpectBodilessResponse
11-
import com.trendyol.stove.testing.e2e.http.DefaultHttpSystem.Companion.putAndExpectJson
5+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.deleteAndExpectBodilessResponse
6+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.get
7+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.getMany
8+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.postAndExpectBodilessResponse
9+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.postAndExpectJson
10+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.putAndExpectBodilessResponse
11+
import com.trendyol.stove.testing.e2e.http.HttpSystem.Companion.putAndExpectJson
1212
import com.trendyol.stove.testing.e2e.system.TestSystem
1313
import com.trendyol.stove.testing.e2e.system.abstractions.ApplicationUnderTest
1414
import com.trendyol.stove.testing.e2e.system.abstractions.ExperimentalStoveDsl

0 commit comments

Comments
 (0)