skrape{it}
Search…
Pre-configure client
Sometimes you may want to reuse your HTTP Client configuration to avoid code duplication or to archive better maintainability of your code base. Skrape{it} supports shared client configurations by default -- you just need to configure the client properties you want to reuse using the skrape{it} DSL without calling the response function.
Calling the request function will create a Request object which can then be stored to a variable.
The DSL is design to behave like fluent API, which means it will always return the return value of the last thing that was called inside the skrape lambda function.
1
val myPreConfiguredClient = skrape(HttpFetcher) {
2
request {
3
timeout = 10_000
4
headers = mapOf("some-custom-header" to "some-value")
5
followRedirects = true
6
}
7
}
8
9
@Test
10
fun `can use preconfigured client straight away`() {
11
12
myPreConfiguredClient.response {
13
status { code toBe 200 }
14
headers.getValue("some-custom-header") toBe "some-value"
15
}
16
}
17
18
@Test
19
fun `can use preconfigured client but slightly modify`() {
20
21
myPreConfiguredClient.apply {
22
request {
23
followRedirects = false
24
}
25
}.response {
26
status { code toBe 301 }
27
headers.getValue("some-custom-header") toBe "some-value"
28
}
29
}
Copied!
Last modified 1mo ago
Copy link