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

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

ParameterEncoderTest.kt

Source:ParameterEncoderTest.kt Github

copy

Full Screen

...12import 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

...9object 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

encode

Using AI Code Generation

copy

Full Screen

1fun main(args: Array<String>) {2 println(request)3 println(response)4 println(result.get())5}6fun main(args: Array<String>) {7 println(request)8 println(response)9 println(result.get())10}11fun main(args: Array<String>) {12 println(request)13 println(response)14 println(result.get())15}16fun main(args: Array<String>) {17 println(request)18 println(response)19 println(result.get())20}21fun main(args: Array<String>) {22 println(request)23 println(response)24 println(result.get())25}26fun main(args: Array<String>) {27 println(request)28 println(response)29 println(result.get())30}31fun main(args: Array<String>) {

Full Screen

Full Screen

encode

Using AI Code Generation

copy

Full Screen

1val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )2val encodedParameters = ParameterEncoder . encode (parameters)3println (encodedParameters)4val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )5val encodedParameters = ParameterEncoder . encode (parameters)6println (encodedParameters)7val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )8val encodedParameters = ParameterEncoder . encode (parameters)9println (encodedParameters)10val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )11val encodedParameters = ParameterEncoder . encode (parameters)12println (encodedParameters)13val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )14val encodedParameters = ParameterEncoder . encode (parameters)15println (encodedParameters)16val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )17val encodedParameters = ParameterEncoder . encode (parameters)18println (encodedParameters)19val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )20val encodedParameters = ParameterEncoder . encode (parameters)21println (encodedParameters)22val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )23val encodedParameters = ParameterEncoder . encode (parameters)24println (encodedParameters)25val parameters = listOf ( "key1" to "value1" , "key2" to "value2" )26val encodedParameters = ParameterEncoder . encode (parameters)27println (encodedParameters

Full Screen

Full Screen

encode

Using AI Code Generation

copy

Full Screen

1fun encodeParameters ( parameters : List < Pair < String , Any ? > > ) : String { val encoder = ParameterEncoder () val encoded = encoder . encode ( parameters ) return encoded }2fun decodeParameters ( queryString : String ) : List < Pair < String , Any ? > > { val encoder = ParameterEncoder () val decoded = encoder . decode ( queryString ) return decoded }3fun encodeParameters ( parameters : List < Pair < String , Any ? > > ) : String { val encoder = ParameterEncoder () val encoded = encoder . encode ( parameters ) return encoded }4fun decodeParameters ( queryString : String ) : List < Pair < String , Any ? > > { val encoder = ParameterEncoder () val decoded = encoder . decode ( queryString ) return decoded }5fun encodeParameters ( parameters : List < Pair < String , Any ? > > ) : String { val encoder = ParameterEncoder () val encoded = encoder . encode ( parameters ) return encoded }6fun decodeParameters ( queryString : String ) : List < Pair < String , Any ? > > { val encoder = ParameterEncoder () val decoded = encoder . decode ( queryString ) return decoded }7fun encodeParameters ( parameters : List < Pair < String , Any ? > > ) : String { val encoder = ParameterEncoder () val encoded = encoder . encode ( parameters ) return encoded }8fun decodeParameters ( queryString : String ) : List < Pair < String , Any ? > > { val encoder = ParameterEncoder () val decoded = encoder . decode ( queryString ) return decoded }9fun encodeParameters ( parameters : List < Pair <

Full Screen

Full Screen

encode

Using AI Code Generation

copy

Full Screen

1val interceptor = RequestInterceptor { next ->2val encoder = ParameterEncoder()3val encoded = encoder.encode(parameters)4next(encoded)5}6FuelManager.instance.addRequestInterceptor(interceptor)7FuelManager.instance.addResponseInterceptor(interceptor)8FuelManager.instance.addRequestInterceptors(interceptor)

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 method 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