2018-02-05 16:05:59 -05:00
|
|
|
package httputils // import "github.com/docker/docker/api/server/httputils"
|
2015-04-16 17:26:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoolValue(t *testing.T) {
|
|
|
|
cases := map[string]bool{
|
|
|
|
"": false,
|
|
|
|
"0": false,
|
|
|
|
"no": false,
|
|
|
|
"false": false,
|
|
|
|
"none": false,
|
|
|
|
"1": true,
|
|
|
|
"yes": true,
|
|
|
|
"true": true,
|
|
|
|
"one": true,
|
|
|
|
"100": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, e := range cases {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("test", c)
|
2019-10-12 14:40:19 -04:00
|
|
|
r, _ := http.NewRequest(http.MethodPost, "", nil)
|
2015-04-16 17:26:33 -04:00
|
|
|
r.Form = v
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
a := BoolValue(r, "test")
|
2015-04-16 17:26:33 -04:00
|
|
|
if a != e {
|
|
|
|
t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 10:09:39 -04:00
|
|
|
func TestBoolValueOrDefault(t *testing.T) {
|
2019-10-12 14:40:19 -04:00
|
|
|
r, _ := http.NewRequest(http.MethodGet, "", nil)
|
2015-09-23 19:42:08 -04:00
|
|
|
if !BoolValueOrDefault(r, "queryparam", true) {
|
2015-05-23 10:09:39 -04:00
|
|
|
t.Fatal("Expected to get true default value, got false")
|
|
|
|
}
|
|
|
|
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("param", "")
|
2019-10-12 14:40:19 -04:00
|
|
|
r, _ = http.NewRequest(http.MethodGet, "", nil)
|
2015-05-23 10:09:39 -04:00
|
|
|
r.Form = v
|
2015-09-23 19:42:08 -04:00
|
|
|
if BoolValueOrDefault(r, "param", true) {
|
2015-05-23 10:09:39 -04:00
|
|
|
t.Fatal("Expected not to get true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 03:40:23 -04:00
|
|
|
func TestInt64ValueOrZero(t *testing.T) {
|
2015-04-16 17:26:33 -04:00
|
|
|
cases := map[string]int64{
|
|
|
|
"": 0,
|
|
|
|
"asdf": 0,
|
|
|
|
"0": 0,
|
|
|
|
"1": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, e := range cases {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("test", c)
|
2019-10-12 14:40:19 -04:00
|
|
|
r, _ := http.NewRequest(http.MethodPost, "", nil)
|
2015-04-16 17:26:33 -04:00
|
|
|
r.Form = v
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
a := Int64ValueOrZero(r, "test")
|
2015-04-16 17:26:33 -04:00
|
|
|
if a != e {
|
|
|
|
t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 14:51:43 -04:00
|
|
|
|
|
|
|
func TestInt64ValueOrDefault(t *testing.T) {
|
|
|
|
cases := map[string]int64{
|
|
|
|
"": -1,
|
|
|
|
"-1": -1,
|
|
|
|
"42": 42,
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, e := range cases {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("test", c)
|
2019-10-12 14:40:19 -04:00
|
|
|
r, _ := http.NewRequest(http.MethodPost, "", nil)
|
2015-09-23 14:51:43 -04:00
|
|
|
r.Form = v
|
|
|
|
|
|
|
|
a, err := Int64ValueOrDefault(r, "test", -1)
|
|
|
|
if a != e {
|
|
|
|
t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error should be nil, but received: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInt64ValueOrDefaultWithError(t *testing.T) {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("test", "invalid")
|
2019-10-12 14:40:19 -04:00
|
|
|
r, _ := http.NewRequest(http.MethodPost, "", nil)
|
2015-09-23 14:51:43 -04:00
|
|
|
r.Form = v
|
|
|
|
|
|
|
|
_, err := Int64ValueOrDefault(r, "test", -1)
|
|
|
|
if err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("Expected an error.")
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
}
|