How to use TestConfiguration class of com.github.kittinunf.fuel.util package

Best Fuel code snippet using com.github.kittinunf.fuel.util.TestConfiguration

Fuel.kt

Source:Fuel.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel2import com.github.kittinunf.fuel.core.FuelManager3import com.github.kittinunf.fuel.core.Method4import com.github.kittinunf.fuel.core.Request5import com.github.kittinunf.fuel.util.TestConfiguration6class Fuel {7 interface PathStringConvertible {8 val path: String9 }10 interface RequestConvertible {11 val request: Request12 }13 companion object {14 internal var testConfiguration = TestConfiguration(timeout = null, blocking = false)15 @JvmStatic @JvmOverloads16 fun testMode(configuration: TestConfiguration.() -> Unit = {}) {17 testConfiguration = TestConfiguration().apply(configuration)18 }19 @JvmStatic20 fun regularMode() = testMode { timeout = null; blocking = false }21 //convenience methods22 //get23 @JvmStatic @JvmOverloads24 fun get(path: String, parameters: List<Pair<String, Any?>>? = null): Request {25 return request(Method.GET, path, parameters)26 }27 @JvmStatic @JvmOverloads28 fun get(convertible: PathStringConvertible, parameters: List<Pair<String, Any?>>? = null): Request {29 return request(Method.GET, convertible, parameters)30 }31 //post...

Full Screen

Full Screen

FuelManager.kt

Source:FuelManager.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core2import com.github.kittinunf.fuel.Fuel3import com.github.kittinunf.fuel.core.interceptors.redirectResponseInterceptor4import com.github.kittinunf.fuel.core.interceptors.validatorResponseInterceptor5import com.github.kittinunf.fuel.toolbox.HttpClient6import com.github.kittinunf.fuel.util.SameThreadExecutorService7import com.github.kittinunf.fuel.util.readWriteLazy8import java.net.Proxy9import java.security.KeyStore10import java.util.concurrent.Executor11import java.util.concurrent.ExecutorService12import java.util.concurrent.Executors13import javax.net.ssl.HostnameVerifier14import javax.net.ssl.HttpsURLConnection15import javax.net.ssl.SSLContext16import javax.net.ssl.SSLSocketFactory17import javax.net.ssl.TrustManagerFactory18class FuelManager {19 var client: Client by readWriteLazy { HttpClient(proxy) }20 var proxy: Proxy? = null21 var basePath: String? = null22 var baseHeaders: Map<String, String>? = null23 var baseParams: List<Pair<String, Any?>> = emptyList()24 var keystore: KeyStore? = null25 var socketFactory: SSLSocketFactory by readWriteLazy {26 keystore?.let {27 val trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())28 trustFactory.init(it)29 val sslContext = SSLContext.getInstance("SSL")30 sslContext.init(null, trustFactory.trustManagers, null)31 sslContext.socketFactory32 } ?: HttpsURLConnection.getDefaultSSLSocketFactory()33 }34 var hostnameVerifier: HostnameVerifier by readWriteLazy {35 HttpsURLConnection.getDefaultHostnameVerifier()36 }37 //background executor38 var executor: ExecutorService by readWriteLazy {39 Executors.newCachedThreadPool { command ->40 Thread {41 Thread.currentThread().priority = Thread.NORM_PRIORITY42 command.run()43 }44 }45 }46 private val requestInterceptors: MutableList<((Request) -> Request) -> ((Request) -> Request)> =47 mutableListOf()48 private val responseInterceptors: MutableList<((Request, Response) -> Response) -> ((Request, Response) -> Response)> =49 mutableListOf(redirectResponseInterceptor(this), validatorResponseInterceptor(200..299))50 fun createExecutor() = if (Fuel.testConfiguration.blocking) SameThreadExecutorService() else executor51 //callback executor52 var callbackExecutor: Executor by readWriteLazy { createEnvironment().callbackExecutor }53 fun request(method: Method, path: String, param: List<Pair<String, Any?>>? = null): Request {54 val request = request(Encoding().apply {55 httpMethod = method56 baseUrlString = basePath57 urlString = path58 parameters = if (param == null) baseParams else baseParams + param59 })60 request.client = client61 request.httpHeaders += baseHeaders.orEmpty()62 request.socketFactory = socketFactory63 request.hostnameVerifier = hostnameVerifier64 request.executor = createExecutor()65 request.callbackExecutor = callbackExecutor66 request.requestInterceptor = requestInterceptors.foldRight({ r: Request -> r }) { f, acc -> f(acc) }67 request.responseInterceptor = responseInterceptors.foldRight({ _: Request, res: Response -> res }) { f, acc -> f(acc) }68 return request69 }70 fun request(method: Method, convertible: Fuel.PathStringConvertible, param: List<Pair<String, Any?>>? = null): Request {71 return request(method, convertible.path, param)72 }73 fun download(path: String, param: List<Pair<String, Any?>>? = null): Request {74 val request = Encoding().apply {75 httpMethod = Method.GET76 baseUrlString = basePath77 urlString = path78 parameters = if (param == null) baseParams else baseParams + param79 requestType = Request.Type.DOWNLOAD80 }.request81 request.client = client82 request.httpHeaders += baseHeaders.orEmpty()83 request.socketFactory = socketFactory84 request.hostnameVerifier = hostnameVerifier85 request.executor = createExecutor()86 request.callbackExecutor = callbackExecutor87 request.requestInterceptor = requestInterceptors.foldRight({ r: Request -> r }) { f, acc -> f(acc) }88 request.responseInterceptor = responseInterceptors.foldRight({ _: Request, res: Response -> res }) { f, acc -> f(acc) }89 return request90 }91 fun upload(path: String, method: Method = Method.POST, param: List<Pair<String, Any?>>? = null): Request {92 val request = Encoding().apply {93 httpMethod = method94 baseUrlString = basePath95 urlString = path96 parameters = if (param == null) baseParams else baseParams + param97 requestType = Request.Type.UPLOAD98 }.request99 request.client = client100 request.httpHeaders += baseHeaders.orEmpty()101 request.socketFactory = socketFactory102 request.hostnameVerifier = hostnameVerifier103 request.executor = createExecutor()104 request.callbackExecutor = callbackExecutor105 request.requestInterceptor = requestInterceptors.foldRight({ r: Request -> r }) { f, acc -> f(acc) }106 request.responseInterceptor = responseInterceptors.foldRight({ _: Request, res: Response -> res }) { f, acc -> f(acc) }107 return request108 }109 fun request(convertible: Fuel.RequestConvertible): Request {110 val request = convertible.request111 request.client = client112 request.httpHeaders += baseHeaders.orEmpty()113 request.socketFactory = socketFactory114 request.hostnameVerifier = hostnameVerifier115 request.executor = createExecutor()116 request.callbackExecutor = callbackExecutor117 request.requestInterceptor = requestInterceptors.foldRight({ r: Request -> r }) { f, acc -> f(acc) }118 request.responseInterceptor = responseInterceptors.foldRight({ _: Request, res: Response -> res }) { f, acc -> f(acc) }119 return request120 }121 fun addRequestInterceptor(interceptor: ((Request) -> Request) -> ((Request) -> Request)) {122 requestInterceptors += interceptor123 }124 fun addResponseInterceptor(interceptor: ((Request, Response) -> Response) -> ((Request, Response) -> Response)) {125 responseInterceptors += interceptor126 }127 fun removeRequestInterceptor(interceptor: ((Request) -> Request) -> ((Request) -> Request)) {128 requestInterceptors -= interceptor129 }130 fun removeResponseInterceptor(interceptor: ((Request, Response) -> Response) -> ((Request, Response) -> Response)) {131 responseInterceptors -= interceptor132 }133 fun removeAllRequestInterceptors() {134 requestInterceptors.clear()135 }136 fun removeAllResponseInterceptors() {137 responseInterceptors.clear()138 }139 companion object {140 //manager141 var instance by readWriteLazy { FuelManager() }142 }143}...

Full Screen

Full Screen

HttpClient.kt

Source:HttpClient.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.toolbox2import com.github.kittinunf.fuel.Fuel3import com.github.kittinunf.fuel.core.Client4import com.github.kittinunf.fuel.core.FuelError5import com.github.kittinunf.fuel.core.Method6import com.github.kittinunf.fuel.core.Request7import com.github.kittinunf.fuel.core.Response8import java.io.BufferedOutputStream9import java.io.ByteArrayInputStream10import java.io.IOException11import java.net.HttpURLConnection12import java.net.Proxy13import java.net.URLConnection14import java.util.zip.GZIPInputStream15import javax.net.ssl.HttpsURLConnection16class HttpClient(val proxy: Proxy? = null) : Client {17 override fun executeRequest(request: Request): Response {18 val response = Response()19 response.url = request.url20 val connection = establishConnection(request) as HttpURLConnection21 try {22 connection.apply {23 val timeout = Fuel.testConfiguration.timeout?.let { if (it == -1) Int.MAX_VALUE else it } ?: request.timeoutInMillisecond24 val timeoutRead = Fuel.testConfiguration.timeoutRead?.let { if (it == -1) Int.MAX_VALUE else it } ?: request.timeoutReadInMillisecond25 connectTimeout = timeout26 readTimeout = timeoutRead27 doInput = true28 useCaches = false29 requestMethod = if (request.httpMethod == Method.PATCH) Method.POST.value else request.httpMethod.value30 instanceFollowRedirects = false31 for ((key, value) in request.httpHeaders) {32 setRequestProperty(key, value)33 }34 if (request.httpMethod == Method.PATCH) setRequestProperty("X-HTTP-Method-Override", "PATCH")35 setDoOutput(connection, request.httpMethod)36 setBodyIfDoOutput(connection, request)37 }38 return response.apply {39 httpResponseHeaders = connection.headerFields ?: emptyMap()40 httpContentLength = connection.contentLength.toLong()41 val contentEncoding = connection.contentEncoding ?: ""42 dataStream = try {43 val stream = connection.errorStream ?: connection.inputStream44 if (contentEncoding.compareTo("gzip", true) == 0) GZIPInputStream(stream) else stream45 } catch (exception: IOException) {46 try {47 connection.errorStream ?: connection.inputStream ?. close()48 } catch (exception: IOException) { }49 ByteArrayInputStream(kotlin.ByteArray(0))50 }51 //try - catch just in case both methods throw52 try {53 httpStatusCode = connection.responseCode54 httpResponseMessage = connection.responseMessage.orEmpty()55 } catch(exception: IOException) {56 throw exception57 }58 }59 } catch(exception: Exception) {60 throw FuelError().apply {61 this.exception = exception62 this.errorData = response.data63 this.response = response64 }65 } finally {66 //As per Android documentation, a connection that is not explicitly disconnected67 //will be pooled and reused! So, don't close it as we need inputStream later!68 //connection.disconnect()69 }70 }71 private fun establishConnection(request: Request): URLConnection {72 val urlConnection = if (proxy != null) request.url.openConnection(proxy) else request.url.openConnection()73 return if (request.url.protocol == "https") {74 val conn = urlConnection as HttpsURLConnection75 conn.apply {76 sslSocketFactory = request.socketFactory77 hostnameVerifier = request.hostnameVerifier78 }79 } else {80 urlConnection as HttpURLConnection81 }82 }83 private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {84 val bodyCallback = request.bodyCallback85 if (bodyCallback != null && connection.doOutput) {86 val contentLength = bodyCallback.invoke(request, null, 0)87 if (request.type == Request.Type.UPLOAD)88 connection.setFixedLengthStreamingMode(contentLength.toInt())89 val outStream = BufferedOutputStream(connection.outputStream)90 outStream.use {91 bodyCallback.invoke(request, outStream, contentLength)92 }93 }94 }95 private fun setDoOutput(connection: HttpURLConnection, method: Method) {96 when (method) {97 Method.GET, Method.DELETE, Method.HEAD -> connection.doOutput = false98 Method.POST, Method.PUT, Method.PATCH -> connection.doOutput = true99 }100 }101}...

Full Screen

Full Screen

TestConfiguration.kt

Source:TestConfiguration.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.util2data class TestConfiguration(3 /**4 * Forced timeout value for every request during test mode is enabled.5 * If set to null, the original timeout will be used.6 * If set to -1, there will be no timeout.7 */8 var timeout: Int? = null,9 /**10 * Forced timeout for read Http operation11 * If set to null, the timeout value will be used.12 * If set to -1, there will be no timeout.13 */14 var timeoutRead: Int? = null15) {16 fun coerceTimeout(timeout: Int) = this.timeout?.let { if (it == -1) Int.MAX_VALUE else it } ?: timeout...

Full Screen

Full Screen

TestConfiguration

Using AI Code Generation

copy

Full Screen

1val (data, error) = result2val (data, error) = result3val (data, error) = result4val (data, error) = result5val (data, error) = result6val (data, error) = result7val (data, error) = result

Full Screen

Full Screen

TestConfiguration

Using AI Code Generation

copy

Full Screen

1val testConfiguration = TestConfiguration()2val testConfiguration = TestConfiguration()3val testConfiguration = TestConfiguration()4val testConfiguration = TestConfiguration()5val testConfiguration = TestConfiguration()6val testConfiguration = TestConfiguration()

Full Screen

Full Screen

TestConfiguration

Using AI Code Generation

copy

Full Screen

1val testConfig = TestConfiguration()2testConfig.assertOk(request, response, result)3val testConfig = TestConfiguration()4testConfig.assertOk(request, response, result)5val testConfig = TestConfiguration()6testConfig.assertOk(request, response, result)7val testConfig = TestConfiguration()8testConfig.assertOk(request, response, result)9val testConfig = TestConfiguration()10testConfig.assertOk(request, response, result)11val testConfig = TestConfiguration()12testConfig.assertOk(request, response, result)13val testConfig = TestConfiguration()14testConfig.assertOk(request, response, result)15val testConfig = TestConfiguration()16testConfig.assertOk(request, response, result)17val testConfig = TestConfiguration()

Full Screen

Full Screen

TestConfiguration

Using AI Code Generation

copy

Full Screen

1val (request, response, result) = Fuel.get("/get", parameters = listOf("foo" to "bar")).responseObject<HttpBin>()2println(response)3println(request)4println(result)5}6}7Success: data: HttpBin(userAgent=Apache-HttpClient/UNAVAILABLE (Java/1.8.0_131), args=map[foo -> bar], origin=

Full Screen

Full Screen

TestConfiguration

Using AI Code Generation

copy

Full Screen

1val testConfiguration = TestConfiguration()2testConfiguration.apply {3stubResponseHeaders = mapOf("Content-Type" to "application/json")4}5FuelManager.instance.apply {6baseHeaders = mapOf("Accept" to "application/json")7baseParams = listOf("foo" to "bar")8basePathParams = listOf("foo" to "bar")9}10FuelManager.instance.baseHeaders = mapOf("Accept" to "application/json")11FuelManager.instance.baseParams = listOf("foo" to "bar")12FuelManager.instance.basePathParams = listOf("foo" to "bar")13val testConfiguration = TestConfiguration()14testConfiguration.apply {15stubResponseHeaders = mapOf("Content-Type" to "application/json")16}17FuelManager.instance.apply {18baseHeaders = mapOf("Accept" to "application/json")19baseParams = listOf("foo" to "bar")20basePathParams = listOf("foo" to "bar")21}22FuelManager.instance.baseHeaders = mapOf("Accept" to "application/json")23FuelManager.instance.baseParams = listOf("foo" to "bar")24FuelManager.instance.basePathParams = listOf("foo" to "bar")25val testConfiguration = TestConfiguration()26testConfiguration.apply {27stubResponseHeaders = mapOf("Content-Type" to "application/json")28}29FuelManager.instance.apply {30baseHeaders = mapOf("Accept" to "application/json")31baseParams = listOf("foo" to "bar")32basePathParams = listOf("foo" to "bar")33}34FuelManager.instance.baseHeaders = mapOf("Accept" to "application/json")35FuelManager.instance.baseParams = listOf("foo" to "bar")36FuelManager.instance.basePathParams = listOf("foo" to "bar")

Full Screen

Full Screen

TestConfiguration

Using AI Code Generation

copy

Full Screen

1val config = TestConfiguration()2val response = Fuel.get("/get").responseObject(Blog.serializer())3val config = TestConfiguration()4val response = Fuel.get("/get").responseObject(Blog.serializer())5val config = TestConfiguration()6val response = Fuel.get("/get").responseObject(Blog.serializer())7val config = TestConfiguration()8val response = Fuel.get("/get").responseObject(Blog.serializer())9val config = TestConfiguration()10val response = Fuel.get("/get").responseObject(Blog.serializer())11val config = TestConfiguration()12val response = Fuel.get("/get").responseObject(Blog.serializer())

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Fuel automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in TestConfiguration

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful