Skip to content

Commit

Permalink
Add MockEngine content test
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Oct 15, 2018
1 parent 9793198 commit a2bde86
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.ktor.client.tests.mock

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.mock.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.*
import kotlinx.coroutines.io.*
import kotlin.test.*

class MockEngineTests {

@Test
fun testClientMock() = runBlocking {
val mockEngine = MockEngine { call ->
if (url.encodedPath == "/") MockHttpResponse(
call,
HttpStatusCode.OK,
ByteReadChannel(byteArrayOf(1, 2, 3)),
headersOf("X-MyHeader", "MyValue")
) else MockHttpResponse(
call, HttpStatusCode.NotFound, ByteReadChannel("Not Found ${url.encodedPath}")
)
}

val client = HttpClient(mockEngine) {
expectSuccess = false
}

assertEquals(byteArrayOf(1, 2, 3).toList(), client.get<ByteArray>("/").toList())
assertEquals("MyValue", client.call("/").response.headers["X-MyHeader"])
assertEquals("Not Found other/path", client.get<String>("/other/path"))
}

}

0 comments on commit a2bde86

Please sign in to comment.