Skip to content

Commit 9608c2b

Browse files
Merge pull request #3 from michallaskowski/add-stop
Add stop method
2 parents 574f975 + 3a53546 commit 9608c2b

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
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: 13 additions & 2 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
@@ -45,13 +49,20 @@ actual class HttpServer actual constructor() {
4549
val request = Request(gcdRequest.method, path, headers, body)
4650

4751
val response = newRouter.handleRequest(request)
52+
val gcdResponse: GCDWebServerResponse
4853
if (response.body != null) {
49-
GCDWebServerDataResponse(response.body, response.contentType ?: "")
54+
gcdResponse = GCDWebServerDataResponse(response.body, response.contentType ?: "")
5055
} else {
51-
val gcdResponse = GCDWebServerResponse()
56+
gcdResponse = GCDWebServerResponse()
5257
gcdResponse.statusCode = response.status.toLong()
5358
gcdResponse
5459
}
60+
61+
response.headers.forEach {
62+
gcdResponse.setValue(it.value, it.key)
63+
}
64+
65+
gcdResponse
5566
}
5667

5768
httpServer.addHandlerWithMatchBlock(

0 commit comments

Comments
 (0)