|
| 1 | +package com.komanov.jwt.base64.tests |
| 2 | + |
| 3 | +import com.komanov.jwt.base64.Base64Helper.Jdk |
| 4 | +import com.komanov.jwt.base64.Base64Helper.Commons |
| 5 | +import org.specs2.mutable.SpecificationWithJUnit |
| 6 | +import org.specs2.specification.core.Fragments |
| 7 | + |
| 8 | +import java.nio.charset.StandardCharsets |
| 9 | +import scala.util.Random |
| 10 | + |
| 11 | +class Base64HelperTest extends SpecificationWithJUnit { |
| 12 | + Fragments.foreach(Seq( |
| 13 | + "" -> "", |
| 14 | + "a" -> "YQ==", |
| 15 | + "abcd" -> "YWJjZA==", |
| 16 | + "nmmфывыаывпврапопррло" -> "bm1t0YTRi9Cy0YvQsNGL0LLQv9Cy0YDQsNC/0L7Qv9GA0YDQu9C+", |
| 17 | + )) { case (data, expected) => |
| 18 | + s"(jdk) regular encode-decode for input: $data" >> { |
| 19 | + val encoded = Jdk.encode(data.getBytes(StandardCharsets.UTF_8)) |
| 20 | + str(encoded) mustEqual expected |
| 21 | + val bytes = Jdk.decode(encoded) |
| 22 | + str(bytes) mustEqual data |
| 23 | + } |
| 24 | + |
| 25 | + s"(commons) regular encode-decode for input: $data" >> { |
| 26 | + val encoded = Commons.encode(data.getBytes(StandardCharsets.UTF_8)) |
| 27 | + str(encoded) mustEqual expected |
| 28 | + val bytes = Commons.decode(encoded) |
| 29 | + str(bytes) mustEqual data |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + Fragments.foreach(Seq( |
| 34 | + "" -> "", |
| 35 | + "a" -> "YQ", |
| 36 | + "abcd" -> "YWJjZA", |
| 37 | + "nmmфывыаывпврапопррло" -> "bm1t0YTRi9Cy0YvQsNGL0LLQv9Cy0YDQsNC_0L7Qv9GA0YDQu9C-", |
| 38 | + )) { case (data, expected) => |
| 39 | + s"(jdk) regular encode-decode for input: $data" >> { |
| 40 | + val encoded = Jdk.encodeUrlSafe(data.getBytes(StandardCharsets.UTF_8)) |
| 41 | + str(encoded) mustEqual expected |
| 42 | + val bytes = Jdk.decodeUrlSafe(encoded) |
| 43 | + str(bytes) mustEqual data |
| 44 | + } |
| 45 | + |
| 46 | + s"(commons) regular encode-decode for input: $data" >> { |
| 47 | + val encoded = Commons.encodeUrlSafe(data.getBytes(StandardCharsets.UTF_8)) |
| 48 | + str(encoded) mustEqual expected |
| 49 | + val bytes = Commons.decodeUrlSafe(encoded) |
| 50 | + str(bytes) mustEqual data |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private def str(bytes: Array[Byte]): String = |
| 55 | + new String(bytes, StandardCharsets.UTF_8) |
| 56 | +} |
0 commit comments