How to use ParameterEncoder class of com.github.kittinunf.fuel.core.interceptors package

Best Fuel code snippet using com.github.kittinunf.fuel.core.interceptors.ParameterEncoder

FuelManager.kt

Source:FuelManager.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.core2import com.github.kittinunf.fuel.core.Client.Hook3import com.github.kittinunf.fuel.core.RequestFactory.PathStringConvertible4import com.github.kittinunf.fuel.core.RequestFactory.RequestConvertible5import com.github.kittinunf.fuel.core.interceptors.ParameterEncoder6import com.github.kittinunf.fuel.core.interceptors.redirectResponseInterceptor7import com.github.kittinunf.fuel.core.requests.DownloadRequest8import com.github.kittinunf.fuel.core.requests.UploadRequest9import com.github.kittinunf.fuel.core.requests.download10import com.github.kittinunf.fuel.core.requests.upload11import com.github.kittinunf.fuel.toolbox.HttpClient12import com.github.kittinunf.fuel.util.readWriteLazy13import java.net.Proxy14import java.security.KeyStore15import java.util.concurrent.Executor16import java.util.concurrent.ExecutorService17import java.util.concurrent.Executors18import javax.net.ssl.HostnameVerifier19import javax.net.ssl.HttpsURLConnection20import javax.net.ssl.SSLContext21import javax.net.ssl.SSLSocketFactory22import javax.net.ssl.TrustManagerFactory23typealias FoldableRequestInterceptor = (RequestTransformer) -> RequestTransformer24typealias FoldableResponseInterceptor = (ResponseTransformer) -> ResponseTransformer25class FuelManager : RequestFactory, RequestFactory.Convenience {26 var client: Client by readWriteLazy { HttpClient(proxy, hook = hook) }27 var proxy: Proxy? = null28 var basePath: String? = null29 var timeoutInMillisecond: Int = 15_00030 var timeoutReadInMillisecond: Int = timeoutInMillisecond31 var progressBufferSize: Int = DEFAULT_BUFFER_SIZE32 var hook: Hook = DefaultHook()33 var baseHeaders: Map<String, String>? = null34 var baseParams: Parameters = emptyList()35 var keystore: KeyStore? = null36 var socketFactory: SSLSocketFactory by readWriteLazy {37 keystore?.let {38 val trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())39 trustFactory.init(it)40 val sslContext = SSLContext.getInstance("SSL")41 sslContext.init(null, trustFactory.trustManagers, null)42 sslContext.socketFactory43 } ?: HttpsURLConnection.getDefaultSSLSocketFactory()44 }45 var hostnameVerifier: HostnameVerifier by readWriteLazy {46 HttpsURLConnection.getDefaultHostnameVerifier()47 }48 // background executionOptions49 var executorService: ExecutorService by readWriteLazy {50 Executors.newCachedThreadPool { command ->51 Thread(command).also { thread ->52 thread.priority = Thread.NORM_PRIORITY53 thread.isDaemon = true54 }55 }56 }57 private val requestInterceptors: MutableList<FoldableRequestInterceptor> =58 mutableListOf(ParameterEncoder)59 private val responseInterceptors: MutableList<FoldableResponseInterceptor> =60 mutableListOf(redirectResponseInterceptor(this))61 // callback executionOptions62 var callbackExecutor: Executor by readWriteLazy { createEnvironment().callbackExecutor }63 var forceMethods: Boolean = false64 /**65 * Make a request using [method] to [path] with [parameters]66 *67 * @see FuelManager.instance68 * @see FuelManager.applyOptions69 *70 * @param method [Method] the HTTP method to make the request with71 * @param path [String] the absolute url or relative to [FuelManager.instance] basePath72 * @param parameters [Parameters?] list of parameters...

Full Screen

Full Screen

ParameterEncoderTest.kt

Source:ParameterEncoderTest.kt Github

copy

Full Screen

...10import org.hamcrest.core.StringContains.containsString11import org.junit.Test12import java.io.ByteArrayInputStream13import java.net.URL14class ParameterEncoderTest {15 @Test16 fun encodeRequestParametersInUrlWhenBodyDisallowed() {17 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)18 methods.forEach { method ->19 val testRequest = DefaultRequest(20 method,21 URL("https://test.fuel.com"),22 parameters = listOf("foo" to "bar")23 )24 var executed = false25 ParameterEncoder { request ->26 assertThat(request.url.toExternalForm(), containsString("?"))27 assertThat(request.url.query, containsString("foo=bar"))28 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))29 executed = true30 request31 }(testRequest)32 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))33 }34 }35 @Test36 fun encodeAppendsRequestParametersInUrl() {37 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)38 methods.forEach { method ->39 val testRequest = DefaultRequest(40 method,41 URL("https://test.fuel.com?a=b"),42 parameters = listOf("foo" to "bar")43 )44 var executed = false45 ParameterEncoder { request ->46 assertThat(request.url.toExternalForm(), containsString("?"))47 assertThat(request.url.query, containsString("&"))48 assertThat(request.url.query, containsString("a=b"))49 assertThat(request.url.query, containsString("foo=bar"))50 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))51 executed = true52 request53 }(testRequest)54 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))55 }56 }57 @Test58 fun encodeRequestParametersInBodyWhenBodyAllowed() {59 val methods = listOf(Method.POST, Method.PATCH, Method.PUT)60 methods.forEach { method ->61 val testRequest = DefaultRequest(62 method,63 URL("https://test.fuel.com"),64 parameters = listOf("foo" to "bar")65 )66 var executed = false67 ParameterEncoder { request ->68 assertThat(request.url.toExternalForm(), not(containsString("?")))69 assertThat(request.url.query, not(containsString("foo=bar")))70 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()?.split(';')?.first()71 assertThat(contentType, equalTo("application/x-www-form-urlencoded"))72 assertThat(request.body.asString(contentType), equalTo("foo=bar"))73 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))74 executed = true75 request76 }(testRequest)77 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))78 }79 }80 @Test81 fun encodeRequestParametersInUrlWhenBodyExists() {82 val methods = listOf(Method.POST, Method.PATCH, Method.PUT)83 methods.forEach { method ->84 val testRequest = DefaultRequest(85 method,86 URL("https://test.fuel.com"),87 parameters = listOf("foo" to "bar")88 ).body("my current body")89 var executed = false90 ParameterEncoder { request ->91 assertThat(request.url.toExternalForm(), containsString("?"))92 assertThat(request.url.query, containsString("foo=bar"))93 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()?.split(';')?.first()94 assertThat(contentType, equalTo("text/plain"))95 assertThat(request.body.asString(contentType), equalTo("my current body"))96 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))97 executed = true98 request99 }(testRequest)100 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))101 }102 }103 @Test104 fun ignoreParameterEncodingForMultipartFormDataRequests() {105 val methods = listOf(Method.POST, Method.PATCH, Method.PUT)106 methods.forEach { method ->107 val testRequest = DefaultRequest(108 method,109 URL("https://test.fuel.com"),110 parameters = listOf("foo" to "bar")111 ).header(Headers.CONTENT_TYPE, "multipart/form-data")112 .upload()113 .add { BlobDataPart(ByteArrayInputStream("12345678".toByteArray()), name = "test", contentLength = 8L) }114 var executed = false115 ParameterEncoder { request ->116 assertThat(request.url.toExternalForm(), not(containsString("?")))117 assertThat(request.url.query, not(containsString("foo=bar")))118 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()?.split(';')?.first()119 assertThat(contentType, equalTo("multipart/form-data"))120 val body = String(request.body.toByteArray())121 assertThat(body, containsString("foo"))122 assertThat(body, containsString("bar"))123 assertThat(body, containsString("test"))124 assertThat(body, containsString("12345678"))125 assertThat("Expected parameters not to be cleared", request.parameters.isEmpty(), equalTo(false))126 executed = true127 request128 }(testRequest)129 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))130 }131 }132 @Test133 fun encodeMultipleParameters() {134 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)135 methods.forEach { method ->136 val testRequest = DefaultRequest(137 method,138 URL("https://test.fuel.com"),139 parameters = listOf("foo" to "bar", "baz" to "q")140 )141 var executed = false142 ParameterEncoder { request ->143 assertThat(request.url.toExternalForm(), containsString("?"))144 assertThat(request.url.query, containsString("foo=bar"))145 assertThat(request.url.query, containsString("baz=q"))146 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))147 executed = true148 request149 }(testRequest)150 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))151 }152 }153 @Test154 fun encodeParameterWithoutValue() {155 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)156 methods.forEach { method ->157 val testRequest = DefaultRequest(158 method,159 URL("https://test.fuel.com"),160 parameters = listOf("foo" to "bar", "baz" to null, "q" to "")161 )162 var executed = false163 ParameterEncoder { request ->164 assertThat(request.url.toExternalForm(), containsString("?"))165 assertThat(request.url.query, containsString("foo=bar"))166 assertThat(request.url.query, not(containsString("baz")))167 assertThat(request.url.query, containsString("q"))168 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))169 executed = true170 request171 }(testRequest)172 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))173 }174 }175 @Test176 fun encodeParametersWithListValues() {177 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)178 methods.forEach { method ->179 val testRequest = DefaultRequest(180 method,181 URL("https://test.fuel.com"),182 parameters = listOf("foo" to "bar", "baz" to listOf("x", "y", "z"))183 )184 var executed = false185 ParameterEncoder { request ->186 assertThat(request.url.toExternalForm(), containsString("?"))187 assertThat(request.url.query, containsString("foo=bar"))188 assertThat(request.url.query, containsString("baz[]=x"))189 assertThat(request.url.query, containsString("baz[]=y"))190 assertThat(request.url.query, containsString("baz[]=z"))191 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))192 executed = true193 request194 }(testRequest)195 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))196 }197 }198 @Test199 fun encodeParametersWithArrayValues() {200 val methods = listOf(Method.GET, Method.DELETE, Method.HEAD, Method.OPTIONS, Method.TRACE)201 methods.forEach { method ->202 val testRequest = DefaultRequest(203 method,204 URL("https://test.fuel.com"),205 parameters = listOf("foo" to "bar", "baz" to arrayOf("x", "y", "z"))206 )207 var executed = false208 ParameterEncoder { request ->209 assertThat(request.url.toExternalForm(), containsString("?"))210 assertThat(request.url.query, containsString("foo=bar"))211 assertThat(request.url.query, containsString("baz[]=x"))212 assertThat(request.url.query, containsString("baz[]=y"))213 assertThat(request.url.query, containsString("baz[]=z"))214 assertThat("Expected parameters to be cleared", request.parameters.isEmpty(), equalTo(true))215 executed = true216 request217 }(testRequest)218 assertThat("Expected encoder \"next\" to be called", executed, equalTo(true))219 }220 }221}...

Full Screen

Full Screen

ParameterEncoder.kt

Source:ParameterEncoder.kt Github

copy

Full Screen

...5import com.github.kittinunf.fuel.core.Parameters6import com.github.kittinunf.fuel.core.RequestTransformer7import java.net.URL8import java.net.URLEncoder9object ParameterEncoder : FoldableRequestInterceptor {10 override fun invoke(next: RequestTransformer): RequestTransformer {11 return inner@{ request ->12 val contentType = request[Headers.CONTENT_TYPE].lastOrNull()13 // Expect the parameters to be already encoded in the body14 if (contentType?.startsWith("multipart/form-data") == true) {15 return@inner next(request)16 }17 // If it can be added to the body18 if (request.body.isEmpty() && allowParametersInBody(request.method)) {19 if (contentType.isNullOrBlank() || contentType.startsWith("application/x-www-form-urlencoded")) {20 return@inner next(21 request22 .header(Headers.CONTENT_TYPE, "application/x-www-form-urlencoded")23 .body(encode(request.parameters))24 .apply { parameters = emptyList() }25 )26 }27 }28 // Has to be added to the URL29 next(30 request31 .apply { url = url.withParameters(parameters) }32 .apply { parameters = emptyList() }33 )34 }35 }36 private fun encode(parameters: Parameters) =37 parameters38 .filterNot { (_, values) -> values == null }39 .flatMap { (key, values) ->40 // Deal with arrays41 ((values as? Iterable<*>)?.toList() ?: (values as? Array<*>)?.toList())?.let {42 val encodedKey = "${URLEncoder.encode(key, "UTF-8")}[]"43 it.map { value -> encodedKey to URLEncoder.encode(value.toString(), "UTF-8") }44 // Deal with regular45 } ?: listOf(URLEncoder.encode(key, "UTF-8") to URLEncoder.encode(values.toString(), "UTF-8"))46 }47 .joinToString("&") { (key, value) -> if (value.isBlank()) key else "$key=$value" }48 private fun allowParametersInBody(method: Method) = when (method) {49 Method.POST, Method.PATCH, Method.PUT -> true50 else -> false51 }52 private fun URL.withParameters(parameters: Parameters): URL {53 val encoded = ParameterEncoder.encode(parameters)54 if (encoded.isEmpty()) {55 return this56 }57 val joiner = if (toExternalForm().contains('?')) {58 // There is already some query59 if (query.isNotEmpty()) "&"60 // There is already a trailing ?61 else ""62 } else "?"63 return URL(toExternalForm() + joiner + encoded)64 }65}...

Full Screen

Full Screen

ParameterEncoder

Using AI Code Generation

copy

Full Screen

1 val parameterEncoder = ParameterEncoder { key, value ->2 listOf(key to value)3 }4 FuelManager.instance.addRequestInterceptor(parameterEncoder)5 val parameterEncoder = ParameterEncoder { key, value ->6 listOf(key to value)7 }8 FuelManager.instance.addRequestInterceptor(parameterEncoder)9 val parameterEncoder = ParameterEncoder { key, value ->10 listOf(key to value)11 }12 FuelManager.instance.addRequestInterceptor(parameterEncoder)13 val parameterEncoder = ParameterEncoder { key, value ->14 listOf(key to value)15 }16 FuelManager.instance.addRequestInterceptor(parameterEncoder)17 val parameterEncoder = ParameterEncoder { key, value ->18 listOf(key to value)19 }20 FuelManager.instance.addRequestInterceptor(parameterEncoder)21 val parameterEncoder = ParameterEncoder { key, value ->22 listOf(key to value)23 }24 FuelManager.instance.addRequestInterceptor(parameterEncoder)25 val parameterEncoder = ParameterEncoder { key, value ->26 listOf(key to value)27 }28 FuelManager.instance.addRequestInterceptor(parameterEncoder)29 val parameterEncoder = ParameterEncoder { key, value ->30 listOf(key to value)31 }32 FuelManager.instance.addRequestInterceptor(parameterEncoder)33 val parameterEncoder = ParameterEncoder { key, value ->34 listOf(key to value)35 }36 FuelManager.instance.addRequestInterceptor(parameterEncoder)37 val parameterEncoder = ParameterEncoder { key, value ->

Full Screen

Full Screen

ParameterEncoder

Using AI Code Generation

copy

Full Screen

1val parameterEncoder = ParameterEncoder { 2val params = it.map { (key, value) -> “$key=$value” }.joinToString(“&”) 3} 4FuelManager.instance.addRequestInterceptor(parameterEncoder)5val parameterDecoder = ParameterDecoder { 6val params = it.split(“&”) 7params.map { 8val (key, value) = it.split(“=”) 9Pair(key, value) 10}.toMap() 11} 12FuelManager.instance.addResponseInterceptor(parameterDecoder)13val parameterEncoder = ParameterEncoder { 14val params = it.map { (key, value) -> “$key=$value” }.joinToString(“&”) 15} 16FuelManager.instance.addRequestInterceptor(parameterEncoder)17val parameterDecoder = ParameterDecoder { 18val params = it.split(“&”) 19params.map { 20val (key, value) = it.split(“=”) 21Pair(key, value) 22}.toMap() 23} 24FuelManager.instance.addResponseInterceptor(parameterDecoder)25val parameterEncoder = ParameterEncoder { 26val params = it.map { (key, value) -> “$key=$value” }.joinToString(“&”) 27} 28FuelManager.instance.addRequestInterceptor(parameterEncoder)29val parameterDecoder = ParameterDecoder { 30val params = it.split(“&”) 31params.map { 32val (key, value) = it.split(“=”) 33Pair(key, value) 34}.toMap() 35} 36FuelManager.instance.addResponseInterceptor(parameterDecoder)37val parameterEncoder = ParameterEncoder { 38val params = it.map { (key, value) -> “$key=$value” }.joinToString(“&”) 39}

Full Screen

Full Screen

ParameterEncoder

Using AI Code Generation

copy

Full Screen

1val json = """{"foo": "bar"}"""2println(response.third.get())3{4"args": {},5"data": "{\"foo\": \"bar\"}",6"files": {},7"form": {},8"headers": {9"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",10},11"json": {12},

Full Screen

Full Screen

ParameterEncoder

Using AI Code Generation

copy

Full Screen

1val json = """{"name": "John"}"""2.header("Content-Type" to "application/json")3.body(json, Charsets.UTF_8)4.response { request, response, result ->5println(response)6}7.header("Content-Type" to "application/xml")8.body(xml, Charsets.UTF_8)9.response { request, response, result ->10println(response)11}12val form = listOf("name" to "John", "age" to 22)13.header("Content-Type" to "application/x-www-form-urlencoded")14.body(form)15.response { request, response, result ->16println(response)17}18val form = listOf("name" to "John", "age" to 22)19.header("Content-Type" to "application/x-www-form-urlencoded")20.body(form)21.response { request, response, result ->22println(response)23}24val form = listOf("name" to "John", "age" to 22)25.header("Content-Type" to "application/x-www-form-urlencoded")26.body(form)27.response { request, response, result ->28println(response)29}30val form = listOf("name" to "John", "age" to

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 ParameterEncoder

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful