Loading extract/extract.go +14 −0 Original line number Diff line number Diff line Loading @@ -254,3 +254,17 @@ func Probability(r *http.Request, dp float64) float64 { } return dp } // Inv returns the value of inv param from HTTP request. // Default value is false func Inverse(r *http.Request) (inverse bool) { strInv := r.FormValue("inv") if len(strInv) > 0 { if inv, err := strconv.ParseBool(strInv); err != nil { inverse = false } else { inverse = inv } } return } extract/extract_test.go +24 −0 Original line number Diff line number Diff line Loading @@ -436,3 +436,27 @@ func TestProbability(t *testing.T) { } } } func TestInverse(t *testing.T) { t.Parallel() tests := []struct { title string url string inv bool }{ {"test wrong input", "http://www.tg.c?inv=hello", false}, {"test no input", "http://www.tg.c", false}, {"test good input", "http://www.tg.c?inv=0", false}, {"test good input", "http://www.tg.c?inv=1", true}, } for _, test := range tests { t.Log(test.title) r := &http.Request{Method: "GET"} r.URL, _ = url.Parse(test.url) inv := Inverse(r) if inv != test.inv { t.Errorf("expected %v got %v", test.inv, inv) } } } Loading
extract/extract.go +14 −0 Original line number Diff line number Diff line Loading @@ -254,3 +254,17 @@ func Probability(r *http.Request, dp float64) float64 { } return dp } // Inv returns the value of inv param from HTTP request. // Default value is false func Inverse(r *http.Request) (inverse bool) { strInv := r.FormValue("inv") if len(strInv) > 0 { if inv, err := strconv.ParseBool(strInv); err != nil { inverse = false } else { inverse = inv } } return }
extract/extract_test.go +24 −0 Original line number Diff line number Diff line Loading @@ -436,3 +436,27 @@ func TestProbability(t *testing.T) { } } } func TestInverse(t *testing.T) { t.Parallel() tests := []struct { title string url string inv bool }{ {"test wrong input", "http://www.tg.c?inv=hello", false}, {"test no input", "http://www.tg.c", false}, {"test good input", "http://www.tg.c?inv=0", false}, {"test good input", "http://www.tg.c?inv=1", true}, } for _, test := range tests { t.Log(test.title) r := &http.Request{Method: "GET"} r.URL, _ = url.Parse(test.url) inv := Inverse(r) if inv != test.inv { t.Errorf("expected %v got %v", test.inv, inv) } } }