Skip to content

Commit

Permalink
Merge pull request #223 from kzys/test-80
Browse files Browse the repository at this point in the history
Increase test coverage to ~80%
  • Loading branch information
youyuanwu committed Oct 28, 2021
2 parents 2cdef49 + 69c0f14 commit dd12a47
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
70 changes: 41 additions & 29 deletions request_test.go
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -152,42 +153,53 @@ func TestJSONRequest(t *testing.T) {
assert.Nil(t, req)
}

//func TestCanHaveBody(t *testing.T) {
//assert.True(t, CanHaveBody("put"))
//assert.True(t, CanHaveBody("post"))
//assert.True(t, CanHaveBody("patch"))
//assert.True(t, CanHaveBody("delete"))
//assert.False(t, CanHaveBody(""))
//assert.False(t, CanHaveBody("get"))
//assert.False(t, CanHaveBody("options"))
//assert.False(t, CanHaveBody("head"))
//assert.False(t, CanHaveBody("invalid"))
//}
func TestHasBody(t *testing.T) {
req, _ := http.NewRequest("GET", "", nil)
assert.False(t, HasBody(req))

req.ContentLength = 123
assert.True(t, HasBody(req))
}

func TestMethod(t *testing.T) {
testcase := []struct {
method string
canHaveBody bool
allowsBody bool
isSafe bool
}{
{"put", true, true, false},
{"post", true, true, false},
{"patch", true, true, false},
{"delete", true, true, false},
{"get", false, true, true},
{"options", false, true, false},
{"head", false, false, true},
{"invalid", false, true, false},
{"", false, true, false},
}

for _, tc := range testcase {
t.Run(tc.method, func(t *testing.T) {
assert.Equal(t, tc.canHaveBody, CanHaveBody(tc.method), "CanHaveBody")

req := http.Request{Method: tc.method}
assert.Equal(t, tc.allowsBody, AllowsBody(&req), "AllowsBody")
assert.Equal(t, tc.isSafe, IsSafe(&req), "IsSafe")
})
}
}

func TestReadSingle(t *testing.T) {
values := url.Values(make(map[string][]string))
values.Add("something", "the thing")
assert.Equal(t, "the thing", ReadSingleValue(tv(values), "something"))
assert.Empty(t, ReadSingleValue(tv(values), "notthere"))
assert.Equal(t, "the thing", ReadSingleValue(Values(values), "something"))
assert.Empty(t, ReadSingleValue(Values(values), "notthere"))
}

func TestReadCollection(t *testing.T) {
values := url.Values(make(map[string][]string))
values.Add("something", "value1,value2")
assert.Equal(t, []string{"value1", "value2"}, ReadCollectionValue(tv(values), "something", "csv"))
assert.Empty(t, ReadCollectionValue(tv(values), "notthere", ""))
}

type tv map[string][]string

func (v tv) GetOK(key string) (value []string, hasKey bool, hasValue bool) {
value, hasKey = v[key]
if !hasKey {
return
}
if len(value) == 0 {
return
}
hasValue = true
return
assert.Equal(t, []string{"value1", "value2"}, ReadCollectionValue(Values(values), "something", "csv"))
assert.Empty(t, ReadCollectionValue(Values(values), "notthere", ""))
}
29 changes: 29 additions & 0 deletions values_test.go
@@ -0,0 +1,29 @@
package runtime

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGetOK(t *testing.T) {
m := make(map[string][]string)
m["key1"] = []string{"value1"}
m["key2"] = []string{}
values := Values(m)

v, hasKey, hasValue := values.GetOK("key1")
require.Equal(t, []string{"value1"}, v)
require.True(t, hasKey)
require.True(t, hasValue)

v, hasKey, hasValue = values.GetOK("key2")
require.Equal(t, []string{}, v)
require.True(t, hasKey)
require.False(t, hasValue)

v, hasKey, hasValue = values.GetOK("key3")
require.Nil(t, v)
require.False(t, hasKey)
require.False(t, hasValue)
}

0 comments on commit dd12a47

Please sign in to comment.