Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-27 16:30:02 +01:00
Родитель 3fa18636ec
Коммит 42de5cc7f0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 76698F39D527CE8C
2 изменённых файлов: 50 добавлений и 23 удалений

Просмотреть файл

@ -20,16 +20,17 @@ func ParseLogDetails(details string) (map[string]string, error) {
pairs := strings.Split(details, ",")
detailsMap := make(map[string]string, len(pairs))
for _, pair := range pairs {
p := strings.SplitN(pair, "=", 2)
// if there is no equals sign, we will only get 1 part back
if len(p) != 2 {
k, v, ok := strings.Cut(pair, "=")
if !ok || k == "" {
// missing equal sign, or no key.
return nil, errors.New("invalid details format")
}
k, err := url.QueryUnescape(p[0])
var err error
k, err = url.QueryUnescape(k)
if err != nil {
return nil, err
}
v, err := url.QueryUnescape(p[1])
v, err = url.QueryUnescape(v)
if err != nil {
return nil, err
}

Просмотреть файл

@ -3,7 +3,6 @@ package logs
import (
"testing"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
@ -12,24 +11,51 @@ func TestParseLogDetails(t *testing.T) {
testCases := []struct {
line string
expected map[string]string
err error
expectedErr string
}{
{"key=value", map[string]string{"key": "value"}, nil},
{"key1=value1,key2=value2", map[string]string{"key1": "value1", "key2": "value2"}, nil},
{"key+with+spaces=value%3Dequals,asdf%2C=", map[string]string{"key with spaces": "value=equals", "asdf,": ""}, nil},
{"key=,=nothing", map[string]string{"key": "", "": "nothing"}, nil},
{"=", map[string]string{"": ""}, nil},
{"errors", nil, errors.New("invalid details format")},
{
line: "key=value",
expected: map[string]string{"key": "value"},
},
{
line: "key1=value1,key2=value2",
expected: map[string]string{"key1": "value1", "key2": "value2"},
},
{
line: "key+with+spaces=value%3Dequals,asdf%2C=",
expected: map[string]string{"key with spaces": "value=equals", "asdf,": ""},
},
{
line: "key=,key2=",
expected: map[string]string{"key": "", "key2": ""},
},
{
line: "key=,=nothing",
expectedErr: "invalid details format",
},
{
line: "=nothing",
expectedErr: "invalid details format",
},
{
line: "=",
expectedErr: "invalid details format",
},
{
line: "errors",
expectedErr: "invalid details format",
},
}
for _, testcase := range testCases {
testcase := testcase
t.Run(testcase.line, func(t *testing.T) {
actual, err := ParseLogDetails(testcase.line)
if testcase.err != nil {
assert.Error(t, err, testcase.err.Error())
return
for _, tc := range testCases {
tc := tc
t.Run(tc.line, func(t *testing.T) {
actual, err := ParseLogDetails(tc.line)
if tc.expectedErr != "" {
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
} else {
assert.Check(t, err)
}
assert.Check(t, is.DeepEqual(testcase.expected, actual))
assert.Check(t, is.DeepEqual(tc.expected, actual))
})
}
}