Skip to content

Commit

Permalink
add: added testcases for GetFileExtension utils
Browse files Browse the repository at this point in the history
  • Loading branch information
namwoam committed Jun 24, 2024
1 parent d7ffb50 commit 550d1a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
26 changes: 26 additions & 0 deletions base/component_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package base

import (
"bufio"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"testing"

qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -48,3 +53,24 @@ func TestComponent_ListConnectorDefinitions(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Check(gotJSON, qt.JSONEquals, wantConnectorDefinitionStruct)
}

func TestUtil_GetFileExtension(t *testing.T) {
c := qt.New(t)

file, err := os.Open("./testdata/test_image.png")
c.Assert(err, qt.IsNil)
defer file.Close()
wantFileExtension := "png"

reader := bufio.NewReader(file)
content, err := io.ReadAll(reader)
c.Assert(err, qt.IsNil)

fileBase64 := base64.StdEncoding.EncodeToString(content)
fileBase64 = "data:image/png;base64," + fileBase64
fmt.Println(fileBase64)
gotFileExtension := GetBase64FileExtensionSlow(fileBase64)
c.Check(gotFileExtension, qt.Equals, wantFileExtension)
gotFileExtension = GetBase64FileExtensionFast(fileBase64)
c.Check(gotFileExtension, qt.Equals, wantFileExtension)
}
2 changes: 0 additions & 2 deletions base/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package base

import (
"encoding/base64"
"fmt"
"mime"
"strings"

Expand Down Expand Up @@ -218,7 +217,6 @@ func GetBase64FileExtensionFast(b64 string) string {
header := splitB64[0]
header = strings.TrimPrefix(header, "data:")
header = strings.TrimSuffix(header, ";base64")
fmt.Println("### header:", header)
mtype, _, err := mime.ParseMediaType(header)
if err != nil {
return err.Error()
Expand Down
Binary file added base/testdata/test_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 550d1a5

Please sign in to comment.