Skip to content

Commit

Permalink
Add test for sleeping pill error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissearle committed Feb 11, 2025
1 parent ce8d070 commit 2112bc3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
21 changes: 18 additions & 3 deletions backend/src/test/kotlin/no/java/cupcake/TestExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ fun buildMockEngine(
)
}

fun buildErrorMockEngine(
httpStatusCode: HttpStatusCode,
message: String,
): MockEngine =
MockEngine { request ->
respond(
content = ByteReadChannel(message),
status = httpStatusCode,
headers = headersOf(HttpHeaders.ContentType, "text/plain"),
)
}

fun buildSlackService(
fixture: String,
channel: String,
Expand All @@ -81,20 +93,23 @@ fun buildSlackService(

fun buildSleepingPillService(
fixture: String,
client: HttpClient? = null,
bringService: BringService? = null,
block: (suspend (request: HttpRequestData) -> Unit)? = null,
): SleepingPillService =
SleepingPillService(
client = buildClient(buildMockEngine(fixture, block)),
bringService = buildBringService(fixture = "/postal_codes.json", postalCodeUrl = "/test"),
client = client ?: buildClient(buildMockEngine(fixture, block)),
bringService = bringService ?: buildBringService(fixture = "/postal_codes.json", postalCodeUrl = "/test"),
)

fun buildBringService(
fixture: String,
postalCodeUrl: String,
client: HttpClient? = null,
block: (suspend (request: HttpRequestData) -> Unit)? = null,
): BringService =
BringService(
client = buildClient(buildMockEngine(fixture, block)),
client = client ?: buildClient(buildMockEngine(fixture, block)),
postalCodeUrl = postalCodeUrl,
scheduler = false,
)
39 changes: 38 additions & 1 deletion backend/src/test/kotlin/no/java/cupcake/plugins/RoutingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package no.java.cupcake.plugins
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.HttpRequestData
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
import no.java.cupcake.buildClient
import no.java.cupcake.buildErrorMockEngine
import no.java.cupcake.buildSlackService
import no.java.cupcake.buildSleepingPillService
import no.java.cupcake.buildTestClient
Expand Down Expand Up @@ -108,12 +112,45 @@ class RoutingTest :
}
}
}

test("Fetch sessions - sleeping pill error") {
testApplication {
val id = randomString()

buildTestApplication(
buildService(
"/sessions.json",
client =
buildClient(
buildErrorMockEngine(HttpStatusCode.InternalServerError, "SleepingPill is asleep"),
),
),
)

val client = buildTestClient()

client
.get("/api/conferences/$id/sessions") {
accept(ContentType.Application.Json)
}.apply {
status shouldBe HttpStatusCode.InternalServerError

val response = bodyAsText()

response shouldContain "SleepingPill is asleep"
response shouldContain "500"
response shouldContain "Internal Server Error"
response shouldContain "call to SleepingPill failed"
}
}
}
})

private fun buildService(
fixture: String,
client: HttpClient? = null,
block: (suspend (request: HttpRequestData) -> Unit)? = null,
) = buildSleepingPillService(fixture, block)
) = buildSleepingPillService(fixture = fixture, client = client, block = block)

private fun ApplicationTestBuilder.buildTestApplication(service: SleepingPillService) {
serializedTestApplication {
Expand Down

0 comments on commit 2112bc3

Please sign in to comment.