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,15 +6,11 @@ import (
)
func findValue(str string, separator byte) (string, string, error) {
if str == "" {
return "", "", nil
}
if str[0] == '"' {
if len(str) > 0 && str[0] == '"' {
i := 1
for {
if i >= len(str) {
return "", "", fmt.Errorf("apices not closed (%v)", str)
return "", "", fmt.Errorf("apexes not closed (%v)", str)
}
if str[i] == '"' {
@@ -41,7 +37,7 @@ func keyValParse(str string, separator byte) (map[string]string, error) {
for len(str) > 0 {
i := strings.IndexByte(str, '=')
if i < 0 {
return nil, fmt.Errorf("unable to find key '%v'", str)
return nil, fmt.Errorf("unable to find key (%v)", str)
}
var k string
k, str = str[:i], str[i+1:]

View File

@@ -6,7 +6,8 @@ import (
"github.com/stretchr/testify/require"
)
var casesKeyVal = []struct {
func TestKeyValParse(t *testing.T) {
for _, ca := range []struct {
name string
s string
kvs map[string]string
@@ -28,7 +29,7 @@ var casesKeyVal = []struct {
},
},
{
"with apices",
"with apexes",
`key1="v1", key2=v2`,
map[string]string{
"key1": "v1",
@@ -36,7 +37,7 @@ var casesKeyVal = []struct {
},
},
{
"with apices and comma",
"with apexes and comma",
`key1="v,1", key2="v2"`,
map[string]string{
"key1": "v,1",
@@ -44,17 +45,14 @@ var casesKeyVal = []struct {
},
},
{
"with apices and equal",
"with apexes and equal",
`key1="v=1", key2="v2"`,
map[string]string{
"key1": "v=1",
"key2": "v2",
},
},
}
func TestKeyValParse(t *testing.T) {
for _, ca := range casesKeyVal {
} {
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())
})
}
}