From 24eb0adbb0e37258ebf9ef96995806433f590e1a Mon Sep 17 00:00:00 2001 From: Xian Chaobo Date: Tue, 19 May 2015 04:01:41 -0400 Subject: [PATCH 1/2] fix #810 bool value Signed-off-by: Xian Chaobo --- api/handlers.go | 6 +++--- api/utils.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 0322d3f50b..b714425e72 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -145,8 +145,8 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { } // Parse flags. - all := r.Form.Get("all") == "1" - limit, _ := strconv.Atoi(r.Form.Get("limit")) + all := boolValue(r, "all") + limit := intValueOrZero(r, "limit") // Parse filters. filters, err := dockerfilters.FromParam(r.Form.Get("filters")) @@ -342,7 +342,7 @@ func deleteContainers(c *context, w http.ResponseWriter, r *http.Request) { } name := mux.Vars(r)["name"] - force := r.Form.Get("force") == "1" + force := boolValue(r, "force") container := c.cluster.Container(name) if container == nil { httpError(w, fmt.Sprintf("Container %s not found", name), http.StatusNotFound) diff --git a/api/utils.go b/api/utils.go index 8041616c80..9b4184860b 100644 --- a/api/utils.go +++ b/api/utils.go @@ -7,6 +7,7 @@ import ( "io" "net" "net/http" + "strconv" "strings" log "github.com/Sirupsen/logrus" @@ -149,3 +150,16 @@ func hijack(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.R return nil } + +func boolValue(r *http.Request, k string) bool { + s := strings.ToLower(strings.TrimSpace(r.FormValue(k))) + return !(s == "" || s == "0" || s == "no" || s == "false" || s == "none") +} + +func intValueOrZero(r *http.Request, k string) int { + val, err := strconv.Atoi(r.FormValue(k)) + if err != nil { + return 0 + } + return val +} From df50eb8c6768bc6b562fff15a1762a817a528199 Mon Sep 17 00:00:00 2001 From: Xian Chaobo Date: Tue, 19 May 2015 21:54:03 -0400 Subject: [PATCH 2/2] add unit test Signed-off-by: Xian Chaobo --- api/utils_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 api/utils_test.go diff --git a/api/utils_test.go b/api/utils_test.go new file mode 100644 index 0000000000..67ebbd25e5 --- /dev/null +++ b/api/utils_test.go @@ -0,0 +1,55 @@ +package api + +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) + r, _ := http.NewRequest("POST", "", nil) + r.Form = v + + a := boolValue(r, "test") + if a != e { + t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a) + } + } +} + +func TestIntValueOrZero(t *testing.T) { + cases := map[string]int{ + "": 0, + "asdf": 0, + "0": 0, + "1": 1, + } + + for c, e := range cases { + v := url.Values{} + v.Set("test", c) + r, _ := http.NewRequest("POST", "", nil) + r.Form = v + + a := intValueOrZero(r, "test") + if a != e { + t.Fatalf("Value: %s, expected: %v, actual: %v", c, e, a) + } + } +}