github.com/rentongzhang/docker@v1.8.2-rc1/api/server/form_test.go (about)

     1  package server
     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  }