これは非常に奇妙な問題です。
base64 文字列をデコードし、の前に最初の部分を取得できる単純なクラスがあります:
。
import scala.util.{Success, Try}
import org.apache.commons.codec.binary.Base64
class IdDecoder {
def decode(token: String): Option[String] = {
if (token.isEmpty)
None
else
Try(new String(Base64.decodeBase64(token.getBytes)).split(":")(0)) match {
case Success(id) => Some(id)
case _ => None
}
}
}
そして、文字列をデコードするメソッドを定義します
object StrangeToken {
def main(args: Array[String]) {
decode()
}
def decode() = {
val token = "InternalServerError"
val Some(id) = (new IdDecoder).decode(token)
println("### StrangeToken's id len:" + id.length)
id.toCharArray.foreach(c => println(c.toInt))
id
}
}
プレーン コードとして実行され、ID の長さは 15 です
sbt のコンソール、IDEA、または本番環境で実行すると、結果は次のようになります。
### StrangeToken's id len:15
34
123
94
65533
118
65533
73
65533
65533
122
65533
43
0
0
0
spec2 テストとして実行、ID の長さは 14
しかし、spec2 で実行すると、次のようになります。
"id decoder" should {
"get decoded string whose length is 15" in {
val id = StrangeToken.decode()
id.length must be equalTo 15
}
}
このテストは失敗し、結果は次のとおりです。
### StrangeToken's id len:14
34
123
94
198
118
8226
73
205
212
122
177
43
198
228
spec2で結果が違う理由がよくわかりません。