Skip to content

Commit

Permalink
Fix TestBase64Encode
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtamm committed Mar 2, 2024
1 parent ec3cc7e commit cf5320d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 0 additions & 1 deletion tes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func Base64Encode(t *Task) (string, error) {
if err != nil {
return "", err
}
fmt.Println(str)
str = base64.StdEncoding.EncodeToString([]byte(str))
return str, nil
}
Expand Down
15 changes: 11 additions & 4 deletions tes/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ func TestBase64Encode(t *testing.T) {
},
}

expected := "ewogICJpZCI6ICJ0YXNrMSIsCiAgImV4ZWN1dG9ycyI6IFsKICAgIHsKICAgICAgImltYWdlIjogImFscGluZSIsCiAgICAgICJjb21tYW5kIjogWwogICAgICAgICJlY2hvIiwKICAgICAgICAiaGVsbG8gd29ybGQiCiAgICAgIF0KICAgIH0KICBdCn0="
// Protojson may (and may not) add extra whitespace between labels and values.
// Therefore there are two possible scenarios when the test checks against specific BASE64 value(s).
// More info: https://github.com/golang/protobuf/issues/1082
expected1 := "ewogICJpZCI6ICJ0YXNrMSIsCiAgImV4ZWN1dG9ycyI6IFsKICAgIHsKICAgICAgImltYWdlIjogImFscGluZSIsCiAgICAgICJjb21tYW5kIjogWwogICAgICAgICJlY2hvIiwKICAgICAgICAiaGVsbG8gd29ybGQiCiAgICAgIF0KICAgIH0KICBdCn0="
expected2 := "ewogICJpZCI6ICAidGFzazEiLAogICJleGVjdXRvcnMiOiAgWwogICAgewogICAgICAiaW1hZ2UiOiAgImFscGluZSIsCiAgICAgICJjb21tYW5kIjogIFsKICAgICAgICAiZWNobyIsCiAgICAgICAgImhlbGxvIHdvcmxkIgogICAgICBdCiAgICB9CiAgXQp9"

encoded, err := Base64Encode(task)
if err != nil {
t.Fatal(err)
}

if encoded != expected {
t.Logf("expected: %+v", expected)
t.Logf("actual: %+v", encoded)
if encoded != expected1 && encoded != expected2 {
str, err := MarshalToString(task)
t.Logf("Source JSON-string: %+v (error: %+v)", str, err)
t.Logf("actual: %+v", encoded)
t.Logf("expected1: %+v", expected1)
t.Logf("expected2: %+v", expected2)
t.Fatal("unexpected value returned from Base64Encode")
}

Expand Down

0 comments on commit cf5320d

Please sign in to comment.