headers: add negative unit tests

This commit is contained in:
aler9
2021-04-19 22:47:43 +02:00
parent be5df0f7f7
commit 49dfd34ede
2 changed files with 73 additions and 55 deletions

View File

@@ -6,55 +6,53 @@ import (
"github.com/stretchr/testify/require"
)
var casesKeyVal = []struct {
name string
s string
kvs map[string]string
}{
{
"base",
`key1=v1,key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with space",
`key1=v1, key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apices",
`key1="v1", key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apices and comma",
`key1="v,1", key2="v2"`,
map[string]string{
"key1": "v,1",
"key2": "v2",
},
},
{
"with apices and equal",
`key1="v=1", key2="v2"`,
map[string]string{
"key1": "v=1",
"key2": "v2",
},
},
}
func TestKeyValParse(t *testing.T) {
for _, ca := range casesKeyVal {
for _, ca := range []struct {
name string
s string
kvs map[string]string
}{
{
"base",
`key1=v1,key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with space",
`key1=v1, key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apexes",
`key1="v1", key2=v2`,
map[string]string{
"key1": "v1",
"key2": "v2",
},
},
{
"with apexes and comma",
`key1="v,1", key2="v2"`,
map[string]string{
"key1": "v,1",
"key2": "v2",
},
},
{
"with apexes and equal",
`key1="v=1", key2="v2"`,
map[string]string{
"key1": "v=1",
"key2": "v2",
},
},
} {
t.Run(ca.name, func(t *testing.T) {
kvs, err := keyValParse(ca.s, ',')
require.NoError(t, err)
@@ -62,3 +60,27 @@ func TestKeyValParse(t *testing.T) {
})
}
}
func TestKeyValParseError(t *testing.T) {
for _, ca := range []struct {
name string
s string
err string
}{
{
"apexes not closed",
`key1="v,1`,
"apexes not closed (\"v,1)",
},
{
"no key",
`value`,
"unable to find key (value)",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, err := keyValParse(ca.s, ',')
require.Equal(t, ca.err, err.Error())
})
}
}