|
| 1 | +package me.proxer.library |
| 2 | + |
| 3 | +import kotlinx.coroutines.CancellationException |
| 4 | +import kotlinx.coroutines.async |
| 5 | +import kotlinx.coroutines.runBlocking |
| 6 | +import okhttp3.mockwebserver.MockResponse |
| 7 | +import org.amshove.kluent.AnyException |
| 8 | +import org.amshove.kluent.coInvoking |
| 9 | +import org.amshove.kluent.shouldBe |
| 10 | +import org.amshove.kluent.shouldBeEqualTo |
| 11 | +import org.amshove.kluent.shouldNotThrow |
| 12 | +import org.amshove.kluent.shouldThrow |
| 13 | +import org.amshove.kluent.with |
| 14 | +import org.junit.jupiter.api.Test |
| 15 | + |
| 16 | +class KotlinExtensionsKtTest : ProxerTest() { |
| 17 | + |
| 18 | + @Test |
| 19 | + fun testAwait(): Unit = runBlocking { |
| 20 | + server.enqueue(MockResponse().setBody(fromResource("news.json"))) |
| 21 | + |
| 22 | + coInvoking { api.notifications.news().build().await() } shouldNotThrow AnyException |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun testAwaitWithError(): Unit = runBlocking { |
| 27 | + server.enqueue(MockResponse().setBody(fromResource("news.json")).setResponseCode(404)) |
| 28 | + |
| 29 | + coInvoking { api.notifications.news().build().await() } shouldThrow ProxerException::class with |
| 30 | + { errorType shouldBe ProxerException.ErrorType.IO } |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun testCancel(): Unit = runBlocking { |
| 35 | + server.enqueue(MockResponse().setBody(fromResource("news.json"))) |
| 36 | + |
| 37 | + with(async { api.notifications.news().build().await() }) { |
| 38 | + cancel() |
| 39 | + |
| 40 | + coInvoking { await() } shouldThrow CancellationException::class |
| 41 | + } |
| 42 | + |
| 43 | + server.requestCount shouldBeEqualTo 0 |
| 44 | + } |
| 45 | +} |
0 commit comments