Skip to content

Commit 49e11a9

Browse files
committed
LinkChecker: Generate response headers when processing a request in tests
1 parent eb76c23 commit 49e11a9

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

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

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

6565
//Check links via: curl --silent -X GET --head 'URL'
6666
val client = MockedHttpClient.fromMeta(
@@ -75,7 +75,6 @@ internal class LinkCheckerTest {
7575
redirect(301, "https://schema.org/"),
7676
),
7777
"https://github.com/androidx/androidx/blob/androidx-main/build.gradle" to listOf(
78-
//todo Calculate header value on building response
7978
//Will wait some time
8079
rateLimitGH(2111),
8180
//Will wait zero time
@@ -154,8 +153,9 @@ internal class LinkCheckerTest {
154153
fun fromMeta(responses: Map<String, List<ResponseMeta>>): HttpClient = fromBuilders(
155154
responses.mapValues {
156155
it.value
157-
.map<ResponseMeta, ResponseBuilder<Any?>> { meta ->
156+
.map<ResponseMeta, ResponseBuilder<Any?>> { metaBuilder ->
158157
{ req ->
158+
val meta = metaBuilder()
159159
MockedHttpResponse.fromRequest(req, meta.first, meta.second.mapValues { h -> listOf(h.value) })
160160
}
161161
}

0 commit comments

Comments
 (0)