Skip to content

Commit d4ea363

Browse files
Add stop method
1 parent 574f975 commit d4ea363

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

library/src/androidMain/kotlin/actual.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ actual class HttpServer actual constructor() {
4949
mockWebServer.start(port)
5050
}
5151

52+
actual fun stop() {
53+
mockWebServer.shutdown()
54+
}
55+
5256
actual var router: Router?
5357
get() = dispatcher.router
5458
set(newRouter) {

library/src/commonMain/kotlin/HttpServer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ interface Router {
2020
expect class HttpServer() {
2121
var router: Router?
2222
fun start(port: Int)
23+
fun stop()
2324
}

library/src/commonTest/kotlin/HttpServerTest.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class HttpServerTest: BaseTest() {
2222

2323
@AfterTest
2424
fun teardown() = runTest {
25-
// todo: server close
25+
server.stop()
2626
client.close()
2727
}
2828

@@ -40,9 +40,29 @@ class HttpServerTest: BaseTest() {
4040
server.start(8080)
4141

4242
val response = client.get<String>("http://localhost:8080/test")
43-
assertEquals(response, "test")
43+
assertEquals("test", response)
4444

4545
val httpResponse = client.get<HttpResponse>("http://localhost:8080/notTest")
46-
assertEquals(httpResponse.status, HttpStatusCode.NotFound)
46+
assertEquals(HttpStatusCode.NotFound, httpResponse.status)
47+
}
48+
49+
@Test
50+
fun returnsHeaders() = runTest {
51+
server.router = object: Router {
52+
override fun handleRequest(request: Request): Response {
53+
return Response(
54+
200,
55+
mapOf("customHeader" to "customHeaderValue"),
56+
Data("test"),
57+
"text/plain"
58+
)
59+
}
60+
}
61+
62+
server.start(8080)
63+
64+
val httpResponse = client.get<HttpResponse>("http://localhost:8080/anything")
65+
assertEquals( HttpStatusCode.OK, httpResponse.status)
66+
assertEquals("customHeaderValue", httpResponse.headers["customHeader"])
4767
}
4868
}

library/src/iOSMain/kotlin/actual.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ actual class HttpServer actual constructor() {
2525
httpServer.startWithPort(port.toULong(), null)
2626
}
2727

28+
actual fun stop() {
29+
httpServer.stop()
30+
}
31+
2832
actual var router: Router? = null
2933
set(newRouter) {
3034
field = newRouter

0 commit comments

Comments
 (0)