mirror of https://github.com/docker/docs.git
Merge pull request #814 from jimmyxian/fix-bool-value
Fix #810 bool value
This commit is contained in:
commit
89d3c2a4cb
|
@ -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)
|
||||
|
|
14
api/utils.go
14
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue