From 4ea2085c338faf1e51e1e042fe8855d1903867a8 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 23 Feb 2016 10:07:56 -0700 Subject: [PATCH] Fix bad http GET parameter encoding, add unit test --- plugins/inputs/httpjson/httpjson.go | 2 +- plugins/inputs/httpjson/httpjson_test.go | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/httpjson/httpjson.go b/plugins/inputs/httpjson/httpjson.go index fb19a9c1d..727fb4344 100644 --- a/plugins/inputs/httpjson/httpjson.go +++ b/plugins/inputs/httpjson/httpjson.go @@ -188,10 +188,10 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) { switch { case h.Method == "GET": - requestURL.RawQuery = params.Encode() for k, v := range h.Parameters { params.Add(k, v) } + requestURL.RawQuery = params.Encode() case h.Method == "POST": requestURL.RawQuery = "" diff --git a/plugins/inputs/httpjson/httpjson_test.go b/plugins/inputs/httpjson/httpjson_test.go index 972ffb83c..6a98bbad3 100644 --- a/plugins/inputs/httpjson/httpjson_test.go +++ b/plugins/inputs/httpjson/httpjson_test.go @@ -222,17 +222,25 @@ func TestHttpJson200(t *testing.T) { // Test litecoin sample output func TestHttpJsonLiteCoin(t *testing.T) { + params := map[string]string{ + "api_key": "mykey", + } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + assert.NoError(t, err) + key := r.Form.Get("api_key") + assert.Equal(t, "mykey", key) w.WriteHeader(http.StatusOK) fmt.Fprintln(w, validJSON2) })) defer ts.Close() a := HttpJson{ - Servers: []string{ts.URL}, - Name: "", - Method: "GET", - client: RealHTTPClient{client: &http.Client{}}, + Servers: []string{ts.URL}, + Name: "", + Method: "GET", + Parameters: params, + client: RealHTTPClient{client: &http.Client{}}, } var acc testutil.Accumulator