Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test coverage to ~80% #223

Merged
merged 3 commits into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}