Skip to content

Commit

Permalink
Fix flaky test for sitemapIndexRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram-- committed Feb 25, 2024
1 parent b3f2229 commit 7df0596
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/test/kotlin/org/audux/bgg/request/SitemapIndexRequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
package org.audux.bgg.request

import com.google.common.truth.Truth.assertThat
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.MockEngineConfig
import io.ktor.client.engine.mock.MockRequestHandleScope
import io.ktor.client.engine.mock.respondBadRequest
import io.ktor.client.engine.mock.respondOk
import io.ktor.client.request.HttpRequestData
import io.ktor.http.Headers
import io.ktor.http.HttpMethod
import io.ktor.http.Url
Expand All @@ -23,6 +29,7 @@ import org.audux.bgg.common.Domain
import org.audux.bgg.common.SitemapLocationType
import org.audux.bgg.response.SitemapLocation
import org.audux.bgg.util.TestUtils
import org.audux.bgg.util.TestUtils.xml
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -199,12 +206,7 @@ class SitemapIndexRequestTest {
@Test
fun `Automatically requests all pages in the sitemap index`() = runBlocking {
val engine =
TestUtils.setupMockEngine(
"sitemapindex.diffuse",
"sitemap_boardgame_page1",
"sitemap_boardgameversion_page1",
"sitemap_files_page1"
)
MockEngine(MockEngineConfig().apply { addHandler { setupSitemapResponses(it) } })
BggClient.engine = { engine }

val response = BggClient.sitemapIndex().diffuse().call()
Expand All @@ -223,5 +225,18 @@ class SitemapIndexRequestTest {
assertThat(response.data!![SitemapLocationType.BOARD_GAME_VERSIONS]).hasSize(9)
assertThat(response.data!![SitemapLocationType.FILES]).hasSize(11)
}

private fun MockRequestHandleScope.setupSitemapResponses(requestData: HttpRequestData) =
if (requestData.url.toString().endsWith("sitemapindex")) {
respondOk(String(xml("sitemapindex.diffuse").readAllBytes()))
} else if (requestData.url.toString().endsWith("boardgame_page_1")) {
respondOk(String(xml("sitemap_boardgame_page1").readAllBytes()))
} else if (requestData.url.toString().endsWith("boardgameversion_page_1")) {
respondOk(String(xml("sitemap_boardgameversion_page1").readAllBytes()))
} else if (requestData.url.toString().endsWith("files_page_1")) {
respondOk(String(xml("sitemap_files_page1").readAllBytes()))
} else {
respondBadRequest()
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/org/audux/bgg/util/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.audux.bgg.BggClient.InternalBggClient

object TestUtils {
/**
* Sets up a HttpEnginecusing a [MockEngine] and [respondOk] responses with the given xml files
* Sets up a HttpEngine using a [MockEngine] and [respondOk] responses with the given xml files
* as the actual response.
*/
@JvmStatic
Expand Down

0 comments on commit 7df0596

Please sign in to comment.