Skip to content

Commit 60833a6

Browse files
committed
LinkChecker: Generate response headers when processing a request in tests
1 parent a23fad6 commit 60833a6

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/test/kotlin/name/valery1707/problem/LinkCheckerTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import javax.net.ssl.SSLSession
2828
import kotlin.io.path.toPath
2929

3030
typealias ResponseBuilder<T> = (HttpRequest) -> HttpResponse<T>
31-
typealias ResponseMeta = Pair<Int, Map<String, String>>
31+
typealias ResponseMeta = () -> Pair<Int, Map<String, String>>
3232

3333
internal class LinkCheckerTest {
3434

@@ -55,10 +55,10 @@ internal class LinkCheckerTest {
5555
val path = javaClass.getResource("/linkChecker/Demo.md")?.toURI()?.toPath()?.parent
5656
assertThat(path).isNotNull.isDirectory.isReadable
5757

58-
fun ok(): ResponseMeta = 200 to mapOf()
59-
fun notFound(): ResponseMeta = 404 to mapOf()
60-
fun redirect(code: Int, target: String): ResponseMeta = code to mapOf("Location" to target)
61-
fun rateLimitGH(awaitMillis: Long): ResponseMeta = 403 to mapOf("x-ratelimit-reset" to Instant.now().plusMillis(awaitMillis).epochSecond.toString())
58+
fun ok(): ResponseMeta = { 200 to mapOf() }
59+
fun notFound(): ResponseMeta = { 404 to mapOf() }
60+
fun redirect(code: Int, target: String): ResponseMeta = { code to mapOf("Location" to target) }
61+
fun rateLimitGH(awaitMillis: Long): ResponseMeta = { 403 to mapOf("x-ratelimit-reset" to Instant.now().plusMillis(awaitMillis).epochSecond.toString()) }
6262

6363
//Check links via: curl --silent -X GET --head 'URL'
6464
val client = MockedHttpClient.fromMeta(
@@ -73,7 +73,6 @@ internal class LinkCheckerTest {
7373
redirect(301, "https://schema.org/"),
7474
),
7575
"https://github.com/androidx/androidx/blob/androidx-main/build.gradle" to listOf(
76-
//todo Calculate header value on building response
7776
//Will wait some time
7877
rateLimitGH(2111),
7978
//Will wait zero time
@@ -152,8 +151,9 @@ internal class LinkCheckerTest {
152151
fun fromMeta(responses: Map<String, List<ResponseMeta>>): HttpClient = fromBuilders(
153152
responses.mapValues {
154153
it.value
155-
.map<ResponseMeta, ResponseBuilder<Any?>> { meta ->
154+
.map<ResponseMeta, ResponseBuilder<Any?>> { metaBuilder ->
156155
{ req ->
156+
val meta = metaBuilder()
157157
MockedHttpResponse.fromRequest(req, meta.first, meta.second.mapValues { h -> listOf(h.value) })
158158
}
159159
}

0 commit comments

Comments
 (0)