|
| 1 | +package io.sentry |
| 2 | + |
| 3 | +import io.ktor.server.request.receive |
| 4 | +import io.ktor.server.request.receiveText |
| 5 | +import io.ktor.server.request.uri |
| 6 | +import io.ktor.server.response.respondText |
| 7 | +import io.ktor.server.routing.get |
| 8 | +import io.ktor.server.routing.post |
| 9 | +import io.ktor.server.routing.routing |
| 10 | +import kotlinx.serialization.json.Json |
| 11 | +import kotlinx.serialization.json.JsonElement |
| 12 | +import kotlinx.serialization.json.jsonArray |
| 13 | +import kotlinx.serialization.json.jsonObject |
| 14 | +import kotlinx.serialization.json.jsonPrimitive |
| 15 | +import java.util.zip.GZIPInputStream |
| 16 | +import io.ktor.server.application.Application |
| 17 | +import io.ktor.server.application.call |
| 18 | + |
| 19 | +fun main(args: Array<String>) { |
| 20 | + io.ktor.server.netty.EngineMain.main(args) |
| 21 | +} |
| 22 | + |
| 23 | +fun Application.module() { |
| 24 | + configureRouting() |
| 25 | +} |
| 26 | + |
| 27 | +fun Application.configureRouting() { |
| 28 | + val receivedEnvelopes = mutableListOf<List<JsonElement>>() |
| 29 | + |
| 30 | + routing { |
| 31 | + post("/{...}") { |
| 32 | + println("Received request: ${call.request.uri}") |
| 33 | + val textBody: String = if (call.request.headers["Content-Encoding"] == "gzip") { |
| 34 | + call.receive<ByteArray>().let { |
| 35 | + GZIPInputStream(it.inputStream()).bufferedReader().use { reader -> |
| 36 | + reader.readText() |
| 37 | + } |
| 38 | + } |
| 39 | + } else { |
| 40 | + call.receiveText() |
| 41 | + } |
| 42 | + |
| 43 | + val jsonItems = textBody.split('\n').mapNotNull { line -> |
| 44 | + try { |
| 45 | + Json.parseToJsonElement(line) |
| 46 | + } catch (e: Exception) { |
| 47 | + null |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + receivedEnvelopes.add(jsonItems) |
| 52 | + |
| 53 | + call.respondText("{}") |
| 54 | + } |
| 55 | + get("/healthCheck") { |
| 56 | + call.respondText("OK") |
| 57 | + } |
| 58 | + get("/assertReceivedAtLeastOneCrashReport") { |
| 59 | + if (receivedEnvelopes.isEmpty()) { |
| 60 | + call.respondText("Mocked Replay have not received any envelopes", status = io.ktor.http.HttpStatusCode.BadRequest) |
| 61 | + } |
| 62 | + |
| 63 | + val hasCrashReport = receivedEnvelopes.any { envelope -> |
| 64 | + envelope.any { item -> |
| 65 | + try { |
| 66 | + if (item.jsonObject.containsKey("exception")) { |
| 67 | + val exception = item.jsonObject["exception"]?.jsonObject |
| 68 | + val values = exception?.get("values")?.jsonArray |
| 69 | + values?.any { value -> |
| 70 | + val message = value.jsonObject["value"]?.jsonPrimitive?.content |
| 71 | + message == "Crash the test app." |
| 72 | + } ?: false |
| 73 | + } else { |
| 74 | + false |
| 75 | + } |
| 76 | + } catch (e: Exception) { |
| 77 | + false |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (hasCrashReport) { |
| 83 | + call.respondText("Received at least one crash report") |
| 84 | + } else { |
| 85 | + call.respondText("No crash report received", status = io.ktor.http.HttpStatusCode.BadRequest) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments