1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package senddata
import (
"encoding/base64"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
const testPrefix = "test:"
func TestPrefixMatch(t *testing.T) {
tests := []struct {
name string
input string
expectedMatch bool
}{
{"Match with correct prefix", "test:sendData", true},
{"Match with correct prefix and nested data", "test:otherData:nestedData", true},
{"Does not match with wrong prefix", "another:sendData", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := require.New(t)
prefix := Prefix(testPrefix)
name := prefix.Name()
r.Contains(testPrefix, name)
r.Equal(tt.expectedMatch, prefix.Match(tt.input))
})
}
}
func TestPrefixUnpack(t *testing.T) {
tests := []struct {
name string
inputData string
expectedResult string
}{
{"Valid JSON data encoded with base64", "test data", "test data"},
{"Invalid base64 encoded data", "invalid_base64_encoded_data", "invalid_base64_encoded_data"},
{"Invalid JSON data encoded with base64", base64.URLEncoding.EncodeToString([]byte("invalid_json")), "aW52YWxpZF9qc29u"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := require.New(t)
jsonBytes, err := json.Marshal(tt.inputData)
r.NoError(err)
sendData := base64.URLEncoding.EncodeToString(jsonBytes)
prefix := Prefix(testPrefix)
var result string
err = prefix.Unpack(&result, testPrefix+sendData)
r.NoError(err)
r.Equal(tt.expectedResult, result)
})
}
}
|