-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ut): add ut for HttpClient.kt and refactor expect method httpCli…
…entEngine
- Loading branch information
Showing
6 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 31 additions & 2 deletions
33
common/src/jvmTest/kotlin/com/tddworks/common/network/api/ktor/internal/HttpClientTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
package com.tddworks.common.network.api.ktor.internal | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.engine.mock.* | ||
import io.ktor.client.engine.okhttp.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.utils.io.* | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.serialization.json.Json | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
class HttpClientTest { | ||
|
||
@Test | ||
fun `should return correct json response with default settings`() { | ||
runBlocking { | ||
val mockEngine = MockEngine { request -> | ||
respond( | ||
content = ByteReadChannel("""{"ip":"127.0.0.1"}"""), | ||
status = HttpStatusCode.OK, | ||
headers = headersOf(HttpHeaders.ContentType, "application/json") | ||
) | ||
} | ||
val apiClient = createHttpClient( | ||
host = { "some-host" }, | ||
httpClientEngine = mockEngine | ||
) | ||
|
||
val body = apiClient.get("https://some-host:443").body<String>() | ||
assertEquals("""{"ip":"127.0.0.1"}""", body) | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun `should return OkHttp engine`() { | ||
val engineFactory = httpClientEngine() | ||
assertTrue(engineFactory is OkHttp) | ||
val httpClientEngine = httpClientEngine() | ||
assertTrue(httpClientEngine is OkHttpEngine) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters