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

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

SimpleUploadControllerTest.kt

Source:SimpleUploadControllerTest.kt Github

copy

Full Screen

1package io.mustelidae.seaotter.api.controller2import com.github.kittinunf.fuel.util.encodeBase64UrlToString3import io.kotlintest.matchers.asClue4import io.kotlintest.shouldBe5import io.mustelidae.seaotter.api.IntegrationTestSupport6import io.mustelidae.seaotter.api.resources.EditingUploadResources7import io.mustelidae.seaotter.api.resources.UploadResources8import io.mustelidae.seaotter.common.Replies9import io.mustelidae.seaotter.domain.delivery.Image10import io.mustelidae.seaotter.utils.fromJson11import io.mustelidae.seaotter.utils.fromJsonByContent12import io.mustelidae.seaotter.utils.getTestImageFileAsAbsolutePath13import io.mustelidae.seaotter.utils.toJson14import org.junit.jupiter.api.Test15import org.springframework.hateoas.server.mvc.linkTo16import org.springframework.http.HttpHeaders17import org.springframework.http.MediaType18import org.springframework.mock.web.MockMultipartFile19import org.springframework.test.web.servlet.post20import org.springframework.test.web.servlet.request.MockMvcRequestBuilders21import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status22import org.springframework.util.Base64Utils23import org.springframework.util.LinkedMultiValueMap24import java.io.ByteArrayOutputStream25import java.io.File26import javax.imageio.ImageIO27internal class SimpleUploadControllerTest : IntegrationTestSupport() {28 private val topicCode = "507f191e810c19729de860ea"29 @Test30 fun uploadMultipart() {31 // Given32 val fileName = "snapshot.png"33 val file = File(getTestImageFileAsAbsolutePath(fileName))34 val bufferedImage = Image.from(file).bufferedImage35 // When36 val uri = linkTo<SimpleUploadController> { upload(MockMultipartFile(file.name, file.inputStream()), false, topicCode) }.toUri()37 val replies = mockMvc.perform(38 MockMvcRequestBuilders.multipart(uri)39 .file(MockMultipartFile("multiPartFile", fileName, null, file.inputStream()))40 .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)41 ).andExpect(42 status().is2xxSuccessful43 ).andReturn()44 .response45 .contentAsString46 .fromJsonByContent<List<UploadResources.ReplyOnImage>>()47 // Then48 replies.first().asClue {49 it.width shouldBe bufferedImage.width50 it.height shouldBe bufferedImage.height51 }52 }53 @Test54 fun uploadBase64Form() {55 // Given56 val fileName = "snapshot.png"57 val file = File(getTestImageFileAsAbsolutePath(fileName))58 val bufferedImage = Image.from(file).bufferedImage59 val out = ByteArrayOutputStream()60 ImageIO.write(bufferedImage, "PNG", out)61 val base64 = "data:image/png;base64," + Base64Utils.encodeToString(out.toByteArray())62 val uri = linkTo<SimpleUploadController> { upload("", false, topicCode) }.toUri()63 val parameters = LinkedMultiValueMap<String, String>().apply {64 add("base64", base64)65 add("hasOriginal", "false")66 }67 // When68 val replies = mockMvc.post(uri) {69 params = parameters70 contentType = MediaType.APPLICATION_FORM_URLENCODED71 }.andExpect {72 status { is2xxSuccessful() }73 }.andReturn()74 .response75 .contentAsString76 .fromJsonByContent<List<UploadResources.ReplyOnImage>>()77 // Then78 replies.first().asClue {79 it.width shouldBe bufferedImage.width80 it.height shouldBe bufferedImage.height81 }82 }83 @Test84 fun uploadJson() {85 // Given86 val fileName = "snapshot.png"87 val file = File(getTestImageFileAsAbsolutePath(fileName))88 val bufferedImage = Image.from(file).bufferedImage89 val out = ByteArrayOutputStream()90 ImageIO.write(bufferedImage, "PNG", out)91 val base64 = "data:image/png;base64," + Base64Utils.encodeToString(out.toByteArray())92 val base64SafeUrl = base64.encodeBase64UrlToString()93 val request = UploadResources.Request(94 base64SafeUrl,95 false96 )97 // When98 val replies = mockMvc.post(linkTo<SimpleUploadController> { upload(request) }.toUri()) {99 accept = MediaType.APPLICATION_JSON100 contentType = MediaType.APPLICATION_JSON101 content = request.toJson()102 }.andExpect {103 status { is2xxSuccessful() }104 }.andReturn()105 .response106 .contentAsString107 .fromJson<Replies<EditingUploadResources.ReplyOnImage>>()108 .getContent()109 // Then110 replies.first().asClue {111 it.width shouldBe bufferedImage.width112 it.height shouldBe bufferedImage.height113 }114 }115 116 @Test117 fun uploadUrl() {118 // Given119 val imageUrl = "https://t1.daumcdn.net/daumtop_chanel/op/20200723055344399.png"120 val request = UploadResources.RequestOnUrl(121 imageUrl,122 false123 )124 val replies = mockMvc.post(linkTo<SimpleUploadController> { upload(request) }.toUri()) {125 accept = MediaType.APPLICATION_JSON126 contentType = MediaType.APPLICATION_JSON127 content = request.toJson()128 }.andExpect {129 status { is2xxSuccessful() }130 }.andReturn()131 .response132 .contentAsString133 .fromJson<Replies<EditingUploadResources.ReplyOnImage>>()134 .getContent()135 // Then136 replies.first().asClue {137 it.width shouldBe 360138 it.height shouldBe 188139 }140 } 141}...

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.encodeBase64UrlToString

Using AI Code Generation

copy

Full Screen

1val base64EncodedString = Base64.encodeBase64UrlToString("hello world".toByteArray())2val base64DecodedString = Base64.decodeBase64UrlToString(base64EncodedString)3val base64EncodedString = Base64.encodeBase64ToString("hello world".toByteArray())4val base64DecodedString = Base64.decodeBase64ToString(base64EncodedString)5val base64EncodedByteArray = Base64.encodeBase64ToByteArray("hello world".toByteArray())6val base64DecodedByteArray = Base64.decodeBase64ToByteArray(base64EncodedString)7val base64EncodedByteArray = Base64.encodeBase64UrlToByteArray("hello world".toByteArray())8val base64DecodedByteArray = Base64.decodeBase64UrlToByteArray(base64EncodedString)9val base64EncodedByteArray = Base64.encodeBase64ToByteArray("hello world".toByteArray())10val base64DecodedByteArray = Base64.decodeBase64ToByteArray(base64EncodedString)11val base64EncodedByteArray = Base64.encodeBase64UrlToByteArray("hello world".toByteArray())

Full Screen

Full Screen

String.encodeBase64UrlToString

Using AI Code Generation

copy

Full Screen

1 val encodedString = Base64.encodeBase64UrlToString("Hello World".toByteArray())2 val decodedString = Base64.decodeBase64UrlToString(encodedString)3 val encodedString = Base64.encodeBase64ToString("Hello World".toByteArray())4 val decodedString = Base64.decodeBase64ToString(encodedString)5 val encodedString = Base64.encodeBase64("Hello World".toByteArray())6 val decodedString = Base64.decodeBase64(encodedString)7 val encodedString = Base64.encodeBase64Url("Hello World".toByteArray())8 val decodedString = Base64.decodeBase64Url(encodedString)9 val encodedString = Base64.encodeBase64UrlSafe("Hello World".toByteArray())10 val decodedString = Base64.decodeBase64UrlSafe(encodedString)11 val encodedString = Base64.encodeBase64Chunked("Hello World".toByteArray())12 }13}

Full Screen

Full Screen

String.encodeBase64UrlToString

Using AI Code Generation

copy

Full Screen

1import com.github.kittinunf.fuel.util.Base642val encoded = String.encodeBase64UrlToString("Hello World")3println(encoded)4import com.github.kittinunf.fuel.util.Base645val decoded = String.decodeBase64Url("SGVsbG8gV29ybGQ=")6println(decoded)

Full Screen

Full Screen

String.encodeBase64UrlToString

Using AI Code Generation

copy

Full Screen

1String base64String = Base64.encodeBase64UrlToString(“teststring”.getBytes());2String decodedString = Base64.decodeBase64UrlToString(base64String);3String base64String = Base64.encodeBase64ToString(“teststring”.getBytes());4String decodedString = Base64.decodeBase64ToString(base64String);5String base64String = Base64.encodeBase64ToString(“teststring”.getBytes());6String decodedString = Base64.decodeBase64ToString(base64String);7String base64String = Base64.encodeBase64ToString(“teststring”.getBytes());8String decodedString = Base64.decodeBase64ToString(base64String);9String base64String = Base64.encodeBase64ToString(“teststring”.getBytes());10String decodedString = Base64.decodeBase64ToString(base64String);11String base64String = Base64.encodeBase64ToString(“teststring”.getBytes());12String decodedString = Base64.decodeBase64ToString(base64String);

Full Screen

Full Screen

String.encodeBase64UrlToString

Using AI Code Generation

copy

Full Screen

1String payload = Base64.encodeBase64UrlToString(claims.toString().getBytes("UTF-8"))2String header = Base64.encodeBase64UrlToString(headerJson.toString().getBytes("UTF-8"))3String signature = Base64.encodeBase64UrlToString(signatureBytes)4DecodedJWT jwt = JWT.decode(jwtToken)5Map<String, Claim> claims = jwt.getClaims()6String subject = claims.get("sub").asString()7String name = claims.get("name").asString()8String email = claims.get("email").asString()9String emailVerified = claims.get("email_verified").asString()10String picture = claims.get("picture").asString()11String givenName = claims.get("given_name").asString()12String familyName = claims.get("family_name").asString()13String locale = claims.get("locale").asString()14Date issuedAt = claims.get("iat").asDate()15Date expiresAt = claims.get("exp").asDate()

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