@@ -2,64 +2,49 @@ package dev.sargunv.pokekotlin.test
22
33import dev.sargunv.pokekotlin.client.PokeApiClient
44import dev.sargunv.pokekotlin.client.PokeApiJson
5+ import io.ktor.client.engine.mock.MockEngine
6+ import io.ktor.client.engine.mock.MockRequestHandleScope
7+ import io.ktor.client.engine.mock.respond
8+ import io.ktor.client.engine.mock.respondError
9+ import io.ktor.client.request.HttpRequestData
10+ import io.ktor.client.request.HttpResponseData
11+ import io.ktor.http.HttpStatusCode
12+ import io.ktor.http.headersOf
513import java.io.File
614import java.io.FileReader
7- import java.nio.charset.Charset
815import java.nio.file.Paths
9- import java.util.logging.Level
10- import java.util.logging.LogManager
1116import kotlinx.serialization.json.JsonObject
1217import kotlinx.serialization.json.JsonPrimitive
1318import kotlinx.serialization.json.buildJsonArray
1419import kotlinx.serialization.json.buildJsonObject
1520import kotlinx.serialization.json.jsonArray
16- import okhttp3.mockwebserver.Dispatcher
17- import okhttp3.mockwebserver.MockResponse
18- import okhttp3.mockwebserver.MockWebServer
19- import okhttp3.mockwebserver.RecordedRequest
20- import okio.Buffer
2121
2222object MockServer {
23+ val mockEngine = MockEngine { request -> dispatch(request) }
24+ val client = PokeApiClient (engine = mockEngine)
25+
26+ private val sampleArchivePath = Paths .get(MockServer ::class .java.getResource(" /data" )!! .toURI())
27+
28+ private fun limit (text : String , limit : Int ): String {
29+ val fullObj = PokeApiJson .decodeFromString<JsonObject >(text)
30+ val fullResults = fullObj[" results" ]!! .jsonArray
31+ val newResults = buildJsonArray { fullResults.take(limit).forEach { add(it) } }
32+ val newObj = buildJsonObject {
33+ fullObj.entries.forEach { (key, value) -> put(key = key, element = value) }
34+ put(key = " results" , element = newResults)
35+ if (fullResults.size > limit) put(key = " next" , element = JsonPrimitive (" DUMMY" ))
36+ }
37+ return PokeApiJson .encodeToString(newObj)
38+ }
2339
24- private val server = MockWebServer ()
25-
26- val url = server.url(" /api/v2/" )!!
27- val client = PokeApiClient (url.url().toString())
28-
29- init {
30- // disable MockWebServer logging
31- LogManager .getLogManager().getLogger(MockWebServer ::class .qualifiedName).level = Level .OFF
32-
33- // get the path to the sample API responses archive
34- val sampleArchivePath = Paths .get(MockServer ::class .java.getResource(" /data" )!! .toURI())
35-
36- // set up the dispatcher to use files in the archive as the mock responses
37- server.dispatcher =
38- object : Dispatcher () {
39- private fun limit (text : String , limit : Int ): String {
40- val fullObj = PokeApiJson .decodeFromString<JsonObject >(text)
41- val fullResults = fullObj[" results" ]!! .jsonArray
42- val newResults = buildJsonArray { fullResults.take(limit).forEach { add(it) } }
43- val newObj = buildJsonObject {
44- fullObj.entries.forEach { (key, value) -> put(key, value) }
45- put(" results" , newResults)
46- if (fullResults.size > limit) put(" next" , JsonPrimitive (" DUMMY" ))
47- }
48- return PokeApiJson .encodeToString(newObj)
49- }
50-
51- override fun dispatch (request : RecordedRequest ): MockResponse {
52- val basePath = request.path.dropLastWhile { it != ' /' }
53- val limit = server.url(request.path).queryParameter(" limit" )?.toInt()
54- val file = File (sampleArchivePath.toString() + basePath + " index.json" )
55- return if (file.exists()) {
56- var text = FileReader (file).use { it.readText() }
57- if (limit != null ) text = limit(text, limit)
58- MockResponse ()
59- .setHeader(" content-type" , " application/json" )
60- .setBody(Buffer ().writeString(text, Charset .defaultCharset()))
61- } else MockResponse ().setResponseCode(404 )
62- }
63- }
40+ private fun MockRequestHandleScope.dispatch (request : HttpRequestData ): HttpResponseData {
41+ val basePath = request.url.encodedPath.dropLastWhile { it != ' /' }
42+ val limit = request.url.parameters[" limit" ]?.toInt()
43+ val file = File (sampleArchivePath.toString() + basePath + " index.json" )
44+ return if (file.exists()) {
45+ val text = FileReader (file).use { it.readText() }
46+ val content = if (limit != null ) limit(text, limit) else text
47+ respond(content = content, headers = headersOf(" content-type" , " application/json" ))
48+ } else respondError(HttpStatusCode .NotFound )
6449 }
6550}
0 commit comments