protodec/spec/protodec_spec.cr

30 lines
974 B
Crystal
Raw Permalink Normal View History

2019-10-26 15:16:05 +00:00
require "./spec_helper"
describe Protodec do
it "decodes Base64 data" do
2019-10-26 17:57:32 +00:00
input = "4qmFsgIsEhhVQ0NqOTU2SUY2MkZiVDdHb3VzemFqOXcaEEVnbGpiMjF0ZFc1cGRIaz0"
2019-10-26 15:16:05 +00:00
output = input.strip
.try { |i| URI.decode_www_form(i) }
.try { |i| URI.decode_www_form(i) }
.try { |i| Base64.decode(i) }
.try { |i| IO::Memory.new(i) }
.try { |i| Protodec::Any.parse(i) }
output["80226972:0:embedded"]["2:0:string"].should eq("UCCj956IF62FbT7Gouszaj9w")
output["80226972:0:embedded"]["3:1:base64"]["2:0:string"].should eq("community")
end
it "encodes JSON object" do
object = Protodec::Any.cast_json({
"80226972:0:embedded" => {
"2:0:string" => "UCCj956IF62FbT7Gouszaj9w",
"3:1:base64" => {
"2:0:string" => "community",
},
},
})
2019-10-26 17:57:32 +00:00
Base64.urlsafe_encode(Protodec::Any.from_json(object), padding: false).should eq("4qmFsgIsEhhVQ0NqOTU2SUY2MkZiVDdHb3VzemFqOXcaEEVnbGpiMjF0ZFc1cGRIaz0")
2019-10-26 15:16:05 +00:00
end
end