How to use String.decodeBase64ToString method of com.github.kittinunf.fuel.util.Base64 class

Best Fuel code snippet using com.github.kittinunf.fuel.util.Base64.String.decodeBase64ToString

RoutingTest.kt

Source:RoutingTest.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel2import com.github.kittinunf.fuel.core.FuelManager3import com.github.kittinunf.fuel.core.HeaderValues4import com.github.kittinunf.fuel.core.Method5import com.github.kittinunf.fuel.core.Parameters6import com.github.kittinunf.fuel.test.MockHttpTestCase7import com.github.kittinunf.fuel.util.FuelRouting8import com.github.kittinunf.fuel.util.decodeBase64ToString9import com.github.kittinunf.fuel.util.encodeBase6410import org.hamcrest.CoreMatchers.containsString11import org.hamcrest.CoreMatchers.notNullValue12import org.hamcrest.CoreMatchers.nullValue13import org.json.JSONObject14import org.junit.Assert.assertThat15import org.junit.Test16import java.net.HttpURLConnection17import org.hamcrest.CoreMatchers.`is` as isEqualTo18class RoutingTest : MockHttpTestCase() {19 private val manager: FuelManager by lazy { FuelManager() }20 sealed class TestApi(private val host: String) : FuelRouting {21 override val basePath = this.host22 class GetTest(host: String) : TestApi(host)23 class GetParamsTest(host: String, val name: String, val value: String) : TestApi(host)24 class PostBodyTest(host: String, val value: String) : TestApi(host)25 class PostBinaryBodyTest(host: String, val value: String) : TestApi(host)26 class PostEmptyBodyTest(host: String) : TestApi(host)27 override val method: Method28 get() {29 return when (this) {30 is GetTest -> Method.GET31 is GetParamsTest -> Method.GET32 is PostBodyTest -> Method.POST33 is PostBinaryBodyTest -> Method.POST34 is PostEmptyBodyTest -> Method.POST35 }36 }37 override val path: String38 get() {39 return when (this) {40 is GetTest -> "/get"41 is GetParamsTest -> "/get"42 is PostBodyTest -> "/post"43 is PostBinaryBodyTest -> "/post"44 is PostEmptyBodyTest -> "/post"45 }46 }47 override val params: Parameters?48 get() {49 return when (this) {50 is GetParamsTest -> listOf(this.name to this.value)51 else -> null52 }53 }54 override val bytes: ByteArray?55 get() {56 return when (this) {57 is PostBinaryBodyTest -> {58 val json = JSONObject()59 json.put("id", this.value)60 json.toString().toByteArray().encodeBase64()61 }62 else -> null63 }64 }65 override val body: String?66 get() {67 return when (this) {68 is PostBodyTest -> {69 val json = JSONObject()70 json.put("id", this.value)71 json.toString()72 }73 else -> null74 }75 }76 override val headers: Map<String, HeaderValues>?77 get() {78 return when (this) {79 is PostBodyTest -> mapOf("Content-Type" to listOf("application/json"))80 is PostBinaryBodyTest -> mapOf("Content-Type" to listOf("application/octet-stream"))81 is PostEmptyBodyTest -> mapOf("Content-Type" to listOf("application/json"))82 else -> null83 }84 }85 }86 @Test87 fun httpRouterGet() {88 mock.chain(89 request = mock.request().withMethod(Method.GET.value),90 response = mock.reflect()91 )92 val (request, response, result) = manager.request(TestApi.GetTest(mock.path(""))).responseString()93 val (data, error) = result94 assertThat(request, notNullValue())95 assertThat(response, notNullValue())96 assertThat(error, nullValue())97 assertThat(data, notNullValue())98 val statusCode = HttpURLConnection.HTTP_OK99 assertThat(response.statusCode, isEqualTo(statusCode))100 }101 @Test102 fun httpRouterGetParams() {103 mock.chain(104 request = mock.request().withMethod(Method.GET.value),105 response = mock.reflect()106 )107 val paramKey = "foo"108 val paramValue = "bar"109 val (request, response, result) = manager.request(TestApi.GetParamsTest(host = mock.path(""), name = paramKey, value = paramValue)).responseString()110 val (data, error) = result111 val string = data as String112 assertThat(request, notNullValue())113 assertThat(response, notNullValue())114 assertThat(error, nullValue())115 assertThat(data, notNullValue())116 val statusCode = HttpURLConnection.HTTP_OK117 assertThat(response.statusCode, isEqualTo(statusCode))118 assertThat(string, containsString(paramKey))119 assertThat(string, containsString(paramValue))120 }121 @Test122 fun httpRouterPostBody() {123 mock.chain(124 request = mock.request().withMethod(Method.POST.value),125 response = mock.reflect()126 )127 val paramValue = "42"128 val (request, response, result) = manager.request(TestApi.PostBodyTest(mock.path(""), paramValue)).responseString()129 val (data, error) = result130 val string = JSONObject(data).getJSONObject("body").getString("string")131 assertThat(request, notNullValue())132 assertThat(response, notNullValue())133 assertThat(error, nullValue())134 assertThat(data, notNullValue())135 val statusCode = HttpURLConnection.HTTP_OK136 assertThat(response.statusCode, isEqualTo(statusCode))137 val res = JSONObject(string)138 assertThat(res.getString("id"), isEqualTo(paramValue))139 }140 @Test141 fun httpRouterPostBinaryBody() {142 mock.chain(143 request = mock.request().withMethod(Method.POST.value),144 response = mock.reflect()145 )146 val paramValue = "42"147 val (request, response, result) = manager.request(TestApi.PostBinaryBodyTest(mock.path(""), paramValue)).responseString()148 val (data, error) = result149 // Binary data is encoded in base64 by mock server150 val string = JSONObject(data).getJSONObject("body").getString("base64Bytes").decodeBase64ToString()151 assertThat(request, notNullValue())152 assertThat(response, notNullValue())153 assertThat(error, nullValue())154 assertThat(data, notNullValue())155 val statusCode = HttpURLConnection.HTTP_OK156 assertThat(response.statusCode, isEqualTo(statusCode))157 val bytes = string!!.decodeBase64ToString()158 assertThat(bytes, containsString(paramValue))159 }160 @Test161 fun httpRouterPostEmptyBody() {162 mock.chain(163 request = mock.request().withMethod(Method.POST.value),164 response = mock.reflect()165 )166 val (request, response, result) = manager.request(TestApi.PostEmptyBodyTest(mock.path(""))).responseString()167 val (data, error) = result168 val string = data as String169 assertThat(request, notNullValue())170 assertThat(response, notNullValue())171 assertThat(error, nullValue())172 assertThat(data, notNullValue())173 val statusCode = HttpURLConnection.HTTP_OK174 assertThat(response.statusCode, isEqualTo(statusCode))175 val res = JSONObject(string)176 assertThat(res.optString("data"), isEqualTo(""))177 }178}...

Full Screen

Full Screen

Base64.kt

Source:Base64.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.util2/**3 * Inspired From https://github.com/square/okio/blob/master/okio/src/main/kotlin/okio/-Base64.kt4 */5import java.lang.System.arraycopy6fun ByteArray.encodeBase64(): ByteArray = encodeBase64ToArray()7fun ByteArray.encodeBase64Url(): ByteArray = encodeBase64ToArray(map = BASE64_URL_SAFE)8fun String.encodeBase64ToString(): String = String(toByteArray().encodeBase64())9fun String.encodeBase64UrlToString(): String = String(toByteArray().encodeBase64Url())10fun String.decodeBase64(): ByteArray? = decodeBase64ToArray()?.let { it }11fun String.decodeBase64ToString(): String? = decodeBase64ToArray()?.let { String(it) }12private val regular = listOf(('A'..'Z'), ('a'..'z'), ('0'..'9'), listOf('+', '/'))13private val urlSafe = listOf(('A'..'Z'), ('a'..'z'), ('0'..'9'), listOf('-', '_'))14private val BASE64 = regular.flatten().map { it.toByte() }.toByteArray()15private val BASE64_URL_SAFE = urlSafe.flatten().map { it.toByte() }.toByteArray()16private fun ByteArray.encodeBase64ToArray(map: ByteArray = BASE64): ByteArray {17 val length = (size + 2) / 3 * 418 val out = ByteArray(length)19 var index = 020 val end = size - size % 321 var i = 022 while (i < end) {23 val b0 = this[i++].toInt()24 val b1 = this[i++].toInt()25 val b2 = this[i++].toInt()26 out[index++] = map[(b0 and 0xff shr 2)]27 out[index++] = map[(b0 and 0x03 shl 4) or (b1 and 0xff shr 4)]28 out[index++] = map[(b1 and 0x0f shl 2) or (b2 and 0xff shr 6)]29 out[index++] = map[(b2 and 0x3f)]30 }31 when (size - end) {32 1 -> {33 val b0 = this[i].toInt()34 out[index++] = map[b0 and 0xff shr 2]35 out[index++] = map[b0 and 0x03 shl 4]36 out[index++] = '='.toByte()37 out[index] = '='.toByte()38 }39 2 -> {40 val b0 = this[i++].toInt()41 val b1 = this[i].toInt()42 out[index++] = map[(b0 and 0xff shr 2)]43 out[index++] = map[(b0 and 0x03 shl 4) or (b1 and 0xff shr 4)]44 out[index++] = map[(b1 and 0x0f shl 2)]45 out[index] = '='.toByte()46 }47 }48 return out49}50private fun String.decodeBase64ToArray(): ByteArray? {51 // Ignore trailing '=' padding and whitespace from the input.52 var limit = length53 while (limit > 0) {54 val c = this[limit - 1]55 if (c != '=' && c != '\n' && c != '\r' && c != ' ' && c != '\t') {56 break57 }58 limit--59 }60 // If the input includes whitespace, this output array will be longer than necessary.61 val out = ByteArray((limit * 6L / 8L).toInt())62 var outCount = 063 var inCount = 064 var word = 065 for (pos in 0 until limit) {66 val c = this[pos]67 val bits: Int68 if (c in 'A'..'Z') {69 // char ASCII value70 // A 65 071 // Z 90 25 (ASCII - 65)72 bits = c.toInt() - 6573 } else if (c in 'a'..'z') {74 // char ASCII value75 // a 97 2676 // z 122 51 (ASCII - 71)77 bits = c.toInt() - 7178 } else if (c in '0'..'9') {79 // char ASCII value80 // 0 48 5281 // 9 57 61 (ASCII + 4)82 bits = c.toInt() + 483 } else if (c == '+' || c == '-') {84 bits = 6285 } else if (c == '/' || c == '_') {86 bits = 6387 } else if (c == '\n' || c == '\r' || c == ' ' || c == '\t') {88 continue89 } else {90 return null91 }92 // Append this char's 6 bits to the word.93 word = word shl 6 or bits94 // For every 4 chars of input, we accumulate 24 bits of output. Emit 3 bytes.95 inCount++96 if (inCount % 4 == 0) {97 out[outCount++] = (word shr 16).toByte()98 out[outCount++] = (word shr 8).toByte()99 out[outCount++] = word.toByte()100 }101 }102 val lastWordChars = inCount % 4103 when (lastWordChars) {104 1 -> {105 // We read 1 char followed by "===". But 6 bits is a truncated byte! Fail.106 return null107 }108 2 -> {109 // We read 2 chars followed by "==". Emit 1 byte with 8 of those 12 bits.110 word = word shl 12111 out[outCount++] = (word shr 16).toByte()112 }113 3 -> {114 // We read 3 chars, followed by "=". Emit 2 bytes for 16 of those 18 bits.115 word = word shl 6116 out[outCount++] = (word shr 16).toByte()117 out[outCount++] = (word shr 8).toByte()118 }119 }120 // If we sized our out array perfectly, we're done.121 if (outCount == out.size) return out122 // Copy the decoded bytes to a new, right-sized array.123 val prefix = ByteArray(outCount)124 arraycopy(out, 0, prefix, 0, outCount)125 return prefix126}...

Full Screen

Full Screen

Base64Test.kt

Source:Base64Test.kt Github

copy

Full Screen

1/**2 * Copied From https://github.com/square/okio/blob/master/okio/src/test/kotlin/okio/ByteStringTest.kt3 */4package com.github.kittinunf.fuel.util5import org.junit.Assert.assertArrayEquals6import org.junit.Assert.assertEquals7import org.junit.Test8class Base64Test {9 @Test10 fun encodeBase64() {11 assertEquals("", "".encodeBase64ToString())12 assertEquals("AA==", "\u0000".encodeBase64ToString())13 assertEquals("AAA=", "\u0000\u0000".encodeBase64ToString())14 assertEquals("AAAA", "\u0000\u0000\u0000".encodeBase64ToString())15 assertEquals("SG93IG1hbnkgbGluZXMgb2YgY29kZSBhcmUgdGhlcmU/ICdib3V0IDIgbWlsbGlvbi4=",16 "How many lines of code are there? 'bout 2 million.".encodeBase64ToString())17 }18 @Test19 fun encodeBase64Url() {20 assertEquals("", "".encodeBase64UrlToString())21 assertEquals("AA==", "\u0000".encodeBase64UrlToString())22 assertEquals("AAA=", "\u0000\u0000".encodeBase64UrlToString())23 assertEquals("AAAA", "\u0000\u0000\u0000".encodeBase64UrlToString())24 assertEquals("SG93IG1hbnkgbGluZXMgb2YgY29kZSBhcmUgdGhlcmU_ICdib3V0IDIgbWlsbGlvbi4=",25 "How many lines of code are there? 'bout 2 million.".encodeBase64UrlToString())26 }27 @Test28 fun ignoreUnnecessaryPadding() {29 assertEquals(null, "\\fgfgff\\".decodeBase64ToString())30 assertEquals("", "====".decodeBase64ToString())31 assertEquals("\u0000\u0000\u0000", "AAAA====".decodeBase64ToString())32 }33 @Test34 fun decodeBase64() {35 assertArrayEquals("".toByteArray(), "".decodeBase64())36 assertEquals("", "".decodeBase64ToString())37 assertEquals(null, "/===".decodeBase64ToString()) // Can't do anything with 6 bits!38 assertEquals("What's to be scared about? It's just a little hiccup in the power...",39 ("V2hhdCdzIHRvIGJlIHNjYXJlZCBhYm91dD8gSXQncyBqdXN0IGEgbGl0dGxlIGhpY2" +40 "N1cCBpbiB0aGUgcG93ZXIuLi4=").decodeBase64ToString())41 assertEquals("How many lines of code are there>", "SG93IG1hbnkgbGluZXMgb2YgY29kZSBhcmUgdGhlcmU+".decodeBase64ToString())42 }43 @Test44 fun decodeBase64WithWhitespace() {45 assertEquals("\u0000\u0000\u0000", " AA AA ".decodeBase64ToString())46 assertEquals("\u0000\u0000\u0000", " AA A\r\nA ".decodeBase64ToString())47 assertEquals("\u0000\u0000\u0000", "AA AA".decodeBase64ToString())48 assertEquals("\u0000\u0000\u0000", " AA AA ".decodeBase64ToString())49 assertEquals("\u0000\u0000\u0000", " AA A\r\nA ".decodeBase64ToString())50 assertEquals("\u0000\u0000\u0000", "A AAA".decodeBase64ToString())51 assertEquals("", " ".decodeBase64ToString())52 }53}...

Full Screen

Full Screen

String.decodeBase64ToString

Using AI Code Generation

copy

Full Screen

1String decodedString = Base64.decodeBase64ToString(base64String);2String encodedString = Base64.encodeBase64ToString(normalString);3}4}5}6}7}8Fuel.post("https

Full Screen

Full Screen

String.decodeBase64ToString

Using AI Code Generation

copy

Full Screen

1val decoded = String.decodeBase64ToString(data)2println(decoded)3val decoded = String.decodeBase64ToByteArray(data)4println(decoded.toString(Charsets.UTF_8))5val decoded = String.decodeBase64ToByteArray(data)6println(decoded.toString(Charsets.UTF_8))7val decoded = String.decodeBase64ToByteArray(data)8println(decoded.toString(Charsets.UTF_8))9val decoded = String.decodeBase64ToByteArray(data)10println(decoded.toString(Charsets.UTF_8))11val decoded = String.decodeBase64ToByteArray(data)12println(decoded.toString(Charsets.UTF_8))13val decoded = String.decodeBase64ToByteArray(data)14println(decoded.toString(Charsets.UTF_8))15val decoded = String.decodeBase64ToByteArray(data)16println(decoded.toString(Charsets.UTF_8))

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful