mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
headers: add negative unit tests
This commit is contained in:
@@ -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:]
|
||||
|
@@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user