github.com/getong/docker@v1.13.1/api/server/httputils/form_test.go (about)

     1  package httputils
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  	"testing"
     7  )
     8  
     9  func TestBoolValue(t *testing.T) {
    10  	cases := map[string]bool{
    11  		"":      false,
    12  		"0":     false,
    13  		"no":    false,
    14  		"false": false,
    15  		"none":  false,
    16  		"1":     true,
    17  		"yes":   true,
    18  		"true":  true,
    19  		"one":   true,
    20  		"100":   true,
    21  	}
    22  
    23  	for c, e := range cases {
    24  		v := url.Values{}
    25  		v.Set("test", c)
    26  		r, _ := http.NewRequest("POST", "", nil)
    27  		r.Form = v
    28  
    29  		a := BoolValue(r, "test")
    30  		if a != e {
    31  			t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
    32  		}
    33  	}
    34  }
    35  
    36  func TestBoolValueOrDefault(t *testing.T) {
    37  	r, _ := http.NewRequest("GET", "", nil)
    38  	if !BoolValueOrDefault(r, "queryparam", true) {
    39  		t.Fatal("Expected to get true default value, got false")
    40  	}
    41  
    42  	v := url.Values{}
    43  	v.Set("param", "")
    44  	r, _ = http.NewRequest("GET", "", nil)
    45  	r.Form = v
    46  	if BoolValueOrDefault(r, "param", true) {
    47  		t.Fatal("Expected not to get true")
    48  	}
    49  }
    50  
    51  func TestInt64ValueOrZero(t *testing.T) {
    52  	cases := map[string]int64{
    53  		"":     0,
    54  		"asdf": 0,
    55  		"0":    0,
    56  		"1":    1,
    57  	}
    58  
    59  	for c, e := range cases {
    60  		v := url.Values{}
    61  		v.Set("test", c)
    62  		r, _ := http.NewRequest("POST", "", nil)
    63  		r.Form = v
    64  
    65  		a := Int64ValueOrZero(r, "test")
    66  		if a != e {
    67  			t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
    68  		}
    69  	}
    70  }
    71  
    72  func TestInt64ValueOrDefault(t *testing.T) {
    73  	cases := map[string]int64{
    74  		"":   -1,
    75  		"-1": -1,
    76  		"42": 42,
    77  	}
    78  
    79  	for c, e := range cases {
    80  		v := url.Values{}
    81  		v.Set("test", c)
    82  		r, _ := http.NewRequest("POST", "", nil)
    83  		r.Form = v
    84  
    85  		a, err := Int64ValueOrDefault(r, "test", -1)
    86  		if a != e {
    87  			t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a)
    88  		}
    89  		if err != nil {
    90  			t.Fatalf("Error should be nil, but received: %s", err)
    91  		}
    92  	}
    93  }
    94  
    95  func TestInt64ValueOrDefaultWithError(t *testing.T) {
    96  	v := url.Values{}
    97  	v.Set("test", "invalid")
    98  	r, _ := http.NewRequest("POST", "", nil)
    99  	r.Form = v
   100  
   101  	_, err := Int64ValueOrDefault(r, "test", -1)
   102  	if err == nil {
   103  		t.Fatalf("Expected an error.")
   104  	}
   105  }