Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import org.junit.runner.Description
import org.junit.runners.model.Statement

class WebServerRule(
private val lazyMockDispatchers: Lazy<Collection<ResponseBinder>>,
private val lazyMockDispatcher: Lazy<ResponseBinder>,
private val lazyResponseStore: Lazy<ResponseStore>
) : TestRule {
override fun apply(base: Statement, description: Description): Statement =
WebServerInitializationStatement(
lazyMockDispatchers,
lazyMockDispatcher,
lazyResponseStore,
base,
description
)

private class WebServerInitializationStatement(
private val lazyMockDispatchers: Lazy<Collection<ResponseBinder>>,
private val lazyMockDispatcher: Lazy<ResponseBinder>,
private val lazyResponseStore: Lazy<ResponseStore>,
private val base: Statement,
private val description: Description
) : Statement() {
val responseStore by lazy { lazyResponseStore.value }
val mockDispatchers by lazy { lazyMockDispatchers.value }
val mockDispatcher by lazy { lazyMockDispatcher.value }

override fun evaluate() {
val requestResponses = description.requestResponseIds()
Expand All @@ -37,29 +37,24 @@ class WebServerRule(
)
}

mockDispatchers.forEach { dispatcher ->
requestResponses.forEach { requestResponse ->
dispatcher
.bindResponse(requestResponse.request, requestResponse.responseFactory)
}
requestResponses.forEach { requestResponse ->
mockDispatcher.bindResponse(requestResponse)
}
val stubbedResponseKeys = requestResponses
.map { requestResponse -> requestResponse.request.url }
.toSet()

base.evaluate()

val usedResponseKeys = mockDispatchers
.flatMap { dispatcher -> dispatcher.usedEndpoints }
.toSet()
val usedResponseKeys = mockDispatcher.usedEndpoints.toSet()

val unusedResponseKeys = stubbedResponseKeys - usedResponseKeys
check(unusedResponseKeys.isEmpty()) {
"${unusedResponseKeys.size} unused stubbed URLs:\n[" +
unusedResponseKeys.joinToString("]\n[") + "]"
}

mockDispatchers.forEach(ResponseBinder::reset)
mockDispatcher.reset()
}

private fun Description.requestResponseIds() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class MockDispatcher :

override var onWebSocketMessage: (String) -> Unit = {}

override fun bindResponse(request: MockRequest, response: MockResponseFactory) {
responses[request.url] = response
override fun bindResponse(requestResponseFactory: MockRequestResponseFactory) {
responses[requestResponseFactory.request.url] = requestResponseFactory.responseFactory
}

override fun reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.mitteloupe.whoami.test.server

import com.mitteloupe.whoami.test.server.response.MockResponseFactory

interface ResponseBinder {
var onWebSocketMessage: (String) -> Unit
fun bindResponse(requestResponseFactory: MockRequestResponseFactory)

val usedEndpoints: Set<String>

fun bindResponse(request: MockRequest, response: MockResponseFactory)

fun reset()

var onWebSocketMessage: (String) -> Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class BaseTest {
lateinit var composeIdlingResources: @JvmSuppressWildcards Collection<ComposeIdlingResource>

private val webServerRule = WebServerRule(
lazy { listOf(mockDispatcher) },
lazy { mockDispatcher },
lazy { responseStore }
)

Expand Down