Skip to content

Commit

Permalink
Add client.get operator for features
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Aug 17, 2020
1 parent b54f0eb commit 68d6014
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@ internal val FEATURE_INSTALLED_LIST = AttributeKey<Attributes>("ApplicationFeatu
/**
* Base interface representing a [HttpClient] feature.
*/
interface HttpClientFeature<out TConfig : Any, TFeature : Any> {
public interface HttpClientFeature<out TConfig : Any, TFeature : Any> {
/**
* The [AttributeKey] for this feature.
*/
val key: AttributeKey<TFeature>
public val key: AttributeKey<TFeature>

/**
* Builds a [TFeature] by calling the [block] with a [TConfig] config instance as receiver.
*/
fun prepare(block: TConfig.() -> Unit = {}): TFeature
public fun prepare(block: TConfig.() -> Unit = {}): TFeature

/**
* Installs the [feature] class for a [HttpClient] defined at [scope].
*/
fun install(feature: TFeature, scope: HttpClient)
public fun install(feature: TFeature, scope: HttpClient)
}

/**
* Try to get a [feature] installed in this client. Returns `null` if the feature was not previously installed.
* Try to get the [feature] installed in this client. Returns `null` if the feature was not previously installed.
*/
fun <B : Any, F : Any> HttpClient.feature(feature: HttpClientFeature<B, F>): F? =
public fun <B : Any, F : Any> HttpClient.feature(feature: HttpClientFeature<B, F>): F? =
attributes.getOrNull(FEATURE_INSTALLED_LIST)?.getOrNull(feature.key)

/**
* Find the [feature] installed in [HttpClient].
*
* @throws [IllegalStateException] if [feature] is not installed.
*/
public operator fun <B : Any, F: Any> HttpClient.get(feature: HttpClientFeature<B, F>): F {
val message = "Feature $feature is not installed. Consider using `install(${feature.key})` in client config first."
return feature(feature) ?: error(message)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("NO_EXPLICIT_RETURN_TYPE_IN_API_MODE_WARNING")
@file:Suppress("NO_EXPLICIT_RETURN_TYPE_IN_API_MODE_WARNING", "KDocMissingDocumentation")

package io.ktor.client.tests.utils

Expand All @@ -16,22 +16,22 @@ import kotlinx.coroutines.*
/**
* Web url for tests.
*/
const val TEST_SERVER: String = "http://127.0.0.1:8080"
public const val TEST_SERVER: String = "http://127.0.0.1:8080"

/**
* Websocket server url for tests.
*/
const val TEST_WEBSOCKET_SERVER: String = "ws://127.0.0.1:8080"
public const val TEST_WEBSOCKET_SERVER: String = "ws://127.0.0.1:8080"

/**
* Proxy server url for tests.
*/
const val HTTP_PROXY_SERVER: String = "http://127.0.0.1:8082"
public const val HTTP_PROXY_SERVER: String = "http://127.0.0.1:8082"

/**
* Perform test with selected client [engine].
*/
fun testWithEngine(
public fun testWithEngine(
engine: HttpClientEngine,
block: suspend TestClientBuilder<*>.() -> Unit
) = testWithClient(HttpClient(engine), block)
Expand Down Expand Up @@ -59,7 +59,7 @@ private fun testWithClient(
/**
* Perform test with selected client engine [factory].
*/
fun <T : HttpClientEngineConfig> testWithEngine(
public fun <T : HttpClientEngineConfig> testWithEngine(
factory: HttpClientEngineFactory<T>,
loader: ClientLoader? = null,
block: suspend TestClientBuilder<T>.() -> Unit
Expand Down Expand Up @@ -106,30 +106,26 @@ private suspend fun concurrency(level: Int, block: suspend (Int) -> Unit) {
}

@InternalAPI
@Suppress("KDocMissingDocumentation")
class TestClientBuilder<T : HttpClientEngineConfig>(
var config: HttpClientConfig<T>.() -> Unit = {},
var test: suspend TestInfo.(client: HttpClient) -> Unit = {},
var after: suspend (client: HttpClient) -> Unit = {},
var repeatCount: Int = 1,
public class TestClientBuilder<T : HttpClientEngineConfig>(
public var config: HttpClientConfig<T>.() -> Unit = {},
public var test: suspend TestInfo.(client: HttpClient) -> Unit = {},
public var after: suspend (client: HttpClient) -> Unit = {},
public var repeatCount: Int = 1,
var dumpAfterDelay: Long = -1,
var concurrency: Int = 1
)

@InternalAPI
@Suppress("KDocMissingDocumentation")
fun <T : HttpClientEngineConfig> TestClientBuilder<T>.config(block: HttpClientConfig<T>.() -> Unit) {
public fun <T : HttpClientEngineConfig> TestClientBuilder<T>.config(block: HttpClientConfig<T>.() -> Unit) {
config = block
}

@InternalAPI
@Suppress("KDocMissingDocumentation")
fun TestClientBuilder<*>.test(block: suspend TestInfo.(client: HttpClient) -> Unit) {
public fun TestClientBuilder<*>.test(block: suspend TestInfo.(client: HttpClient) -> Unit) {
test = block
}

@InternalAPI
@Suppress("KDocMissingDocumentation")
fun TestClientBuilder<*>.after(block: suspend (client: HttpClient) -> Unit): Unit {
public fun TestClientBuilder<*>.after(block: suspend (client: HttpClient) -> Unit): Unit {
after = block
}

0 comments on commit 68d6014

Please sign in to comment.