Skip to content

Commit

Permalink
Assert with Strikt
Browse files Browse the repository at this point in the history
  • Loading branch information
HarisHoulis committed Feb 12, 2024
1 parent 4aa46a1 commit b215e02
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
testImplementation "org.http4k:http4k-testing-hamkrest"

testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("io.strikt:strikt-core:0.34.1")
}

group = 'com.gildedrose'
Expand Down
63 changes: 37 additions & 26 deletions src/test/kotlin/com/gildedrose/http/ReportHttpTransactionTests.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.gildedrose.http

import com.gildedrose.com.gildedrose.testing.assertAll
import org.http4k.core.Method
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.hasSize
import strikt.assertions.isA
import strikt.assertions.isEqualTo
import strikt.assertions.withElementAt
import strikt.assertions.withFirst
import java.time.Duration
import kotlin.test.assertEquals

internal class ReportHttpTransactionTests {

Expand All @@ -20,33 +24,40 @@ internal class ReportHttpTransactionTests {
@Test
fun `reports transactions to analytics`() {
filter.then {
Response(Status.OK)
}.invoke(Request(Method.GET, "/"))
assertAll(
events.single() as HttpEvent,
{ assertEquals("/", uri) },
{ assertEquals(Method.GET.toString(), method) },
{ assertEquals(Status.OK.code, status) }
)
Response(OK)
}.invoke(Request(GET, "/"))
expectThat(events)
.hasSize(1)
.withFirst {
isA<HttpEvent>().and {
get(HttpEvent::uri).isEqualTo("/")
get(HttpEvent::method).isEqualTo(GET.toString())
get(HttpEvent::status).isEqualTo(OK.code)
}
}
}

@Test
fun `reports slow transactions to analytics with an additional event`() {
filter.then {
Thread.sleep(25)
Response(Status.OK)
}.invoke(Request(Method.GET, "/"))
assertAll(
events.first() as HttpEvent,
{ assertEquals("/", uri) },
{ assertEquals(Method.GET.toString(), method) },
{ assertEquals(Status.OK.code, status) }
)
assertAll(
events[1] as SlowHttpEvent,
{ assertEquals("/", uri) },
{ assertEquals(Method.GET.toString(), method) },
{ assertEquals(Status.OK.code, status) }
)
Response(OK)
}.invoke(Request(GET, "/"))
expectThat(events)
.hasSize(2)
.withFirst {
isA<HttpEvent>().and {
get(HttpEvent::uri).isEqualTo("/")
get(HttpEvent::method).isEqualTo(GET.toString())
get(HttpEvent::status).isEqualTo(OK.code)
}
}
.withElementAt(1) {
isA<SlowHttpEvent>().and {
get(SlowHttpEvent::uri).isEqualTo("/")
get(SlowHttpEvent::method).isEqualTo(GET.toString())
get(SlowHttpEvent::status).isEqualTo(OK.code)
}
}
}
}

0 comments on commit b215e02

Please sign in to comment.