Skip to content

Commit

Permalink
test(ut): add ut for DefaultHttpRequester
Browse files Browse the repository at this point in the history
 - refactor codes
  • Loading branch information
hanrw committed Jun 3, 2024
1 parent 6da1bbe commit a5e7b10
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import io.ktor.http.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.koin.test.junit5.AutoCloseKoinTest
Expand All @@ -23,6 +25,35 @@ import kotlin.time.Duration.Companion.seconds
class DefaultHttpRequesterTest : AutoCloseKoinTest() {
private lateinit var httpClient: HttpClient

@BeforeEach
fun setUp() {
initKoin()
}

@Test
fun `should throw PermissionException when get 403 in stream`(): Unit = runBlocking {

httpClient = mockHttpClient(
""" {
"error": {
"code": null,
"type": "server_error",
"param": null,
"message": "You are accessing the API from an unsupported country, region, or territory."
}
}""",
mockHttpStatusCode = HttpStatusCode.Forbidden
)

val requester = HttpRequester.default(httpClient)

requester.streamRequest<StreamResponse> { url(path = "/v1/chat/completions") }
.test(timeout = 10.seconds) {
assertTrue(awaitError() is PermissionException)
}

}

@Test
fun `should throw PermissionException when get 403`(): Unit = runBlocking {
val mockResponse = """
Expand Down Expand Up @@ -53,9 +84,6 @@ class DefaultHttpRequesterTest : AutoCloseKoinTest() {

@Test
fun `should return chat stream completion response`() = runTest {

initKoin()

val mockResponse = StreamResponse("some-content")

httpClient = mockHttpClient("""data: {"content": "some-content"}""")
Expand Down

0 comments on commit a5e7b10

Please sign in to comment.