Skip to content

Commit d2b90e5

Browse files
Working android test
1 parent df1d9ad commit d2b90e5

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

library/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ kotlin {
8989
dependencies {
9090
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.7'
9191
implementation 'androidx.test.ext:junit:1.1.1'
92+
implementation 'org.robolectric:robolectric:4.3.1'
93+
implementation 'org.jetbrains.kotlin:kotlin-test'
94+
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
95+
implementation 'io.ktor:ktor-client-android:1.3.2'
9296
}
9397
}
9498

library/src/androidMain/kotlin/actual.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ private class DispatcherImpl: Dispatcher() {
3232

3333
if (it.body != null) {
3434
setBody(it.body)
35+
} else {
36+
setHeader("Content-Length", 0)
3537
}
3638
}
3739
} ?: MockResponse()

library/src/commonTest/kotlin/HttpServerTest.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dev.michallaskowski.mokttp
22

33
import io.ktor.client.HttpClient
44
import io.ktor.client.request.get
5+
import io.ktor.client.statement.HttpResponse
6+
import io.ktor.http.HttpStatusCode
57
import kotlin.test.AfterTest
68
import kotlin.test.BeforeTest
79
import kotlin.test.Test
@@ -25,12 +27,7 @@ class HttpServerTest: BaseTest() {
2527
}
2628

2729
@Test
28-
fun someTest() = runTest {
29-
assertEquals(1, 1)
30-
}
31-
32-
@Test
33-
fun returns200OkForProperPath() = runTest {
30+
fun returnsResponsePerPath() = runTest {
3431
server.router = object: Router {
3532
override fun handleRequest(request: Request): Response {
3633
if (request.path != "/test") {
@@ -44,6 +41,8 @@ class HttpServerTest: BaseTest() {
4441

4542
val response = client.get<String>("http://localhost:8080/test")
4643
assertEquals(response, "test")
47-
}
4844

45+
val httpResponse = client.get<HttpResponse>("http://localhost:8080/notTest")
46+
assertEquals(httpResponse.status, HttpStatusCode.NotFound)
47+
}
4948
}

0 commit comments

Comments
 (0)