@@ -10,9 +10,9 @@ import io.ktor.client.request.HttpRequestData
1010import io.ktor.client.request.HttpResponseData
1111import io.ktor.http.HttpStatusCode
1212import io.ktor.http.headersOf
13- import java .io.File
14- import java .io.FileReader
15- import java.nio.file.Paths
13+ import kotlin .io.path.Path
14+ import kotlin .io.path.exists
15+ import kotlin.io.path.readText
1616import kotlinx.serialization.json.JsonObject
1717import kotlinx.serialization.json.JsonPrimitive
1818import kotlinx.serialization.json.buildJsonArray
@@ -23,8 +23,6 @@ object MockServer {
2323 val mockEngine = MockEngine { request -> dispatch(request) }
2424 val client = PokeApiClient (engine = mockEngine)
2525
26- private val sampleArchivePath = Paths .get(MockServer ::class .java.getResource(" /data" )!! .toURI())
27-
2826 private fun limit (text : String , limit : Int ): String {
2927 val fullObj = PokeApiJson .decodeFromString<JsonObject >(text)
3028 val fullResults = fullObj[" results" ]!! .jsonArray
@@ -38,11 +36,11 @@ object MockServer {
3836 }
3937
4038 private fun MockRequestHandleScope.dispatch (request : HttpRequestData ): HttpResponseData {
41- val basePath = request.url.encodedPath.dropLastWhile { it != ' / ' }
39+ val basePath = request.url.encodedPath
4240 val limit = request.url.parameters[" limit" ]?.toInt()
43- val file = File (sampleArchivePath.toString() + basePath + " index.json" )
41+ val file = Path ( " src/jvmTest/resources/data " + basePath + " index.json" )
4442 return if (file.exists()) {
45- val text = FileReader ( file).use { it. readText() }
43+ val text = file. readText()
4644 val content = if (limit != null ) limit(text, limit) else text
4745 respond(content = content, headers = headersOf(" content-type" , " application/json" ))
4846 } else respondError(HttpStatusCode .NotFound )
0 commit comments