Commit f7cb7258 authored by santiaago's avatar santiaago
Browse files

add inv parameter extraction to extract pkg

parent 65282dfd
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -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
}
+24 −0
Original line number Diff line number Diff line
@@ -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)
		}
	}
}