From ef2bff291651d71faca396ad671d05969eff66ac Mon Sep 17 00:00:00 2001 From: reimda Date: Tue, 17 Mar 2020 11:03:27 -0600 Subject: [PATCH] Set headers on influxdb_listener ping URL (#7182) --- .../influxdb_listener/influxdb_listener.go | 2 ++ .../influxdb_listener_test.go | 30 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/influxdb_listener/influxdb_listener.go b/plugins/inputs/influxdb_listener/influxdb_listener.go index 60033e050..1eac928af 100644 --- a/plugins/inputs/influxdb_listener/influxdb_listener.go +++ b/plugins/inputs/influxdb_listener/influxdb_listener.go @@ -226,7 +226,9 @@ func (h *InfluxDBListener) handlePing() http.HandlerFunc { verbose := req.URL.Query().Get("verbose") // respond to ping requests + res.Header().Set("X-Influxdb-Version", "1.0") if verbose != "" && verbose != "0" && verbose != "false" { + res.Header().Set("Content-Type", "application/json") res.WriteHeader(http.StatusOK) b, _ := json.Marshal(map[string]string{"version": "1.0"}) // based on header set above res.Write(b) diff --git a/plugins/inputs/influxdb_listener/influxdb_listener_test.go b/plugins/inputs/influxdb_listener/influxdb_listener_test.go index b8ea2014d..6990f6fc6 100644 --- a/plugins/inputs/influxdb_listener/influxdb_listener_test.go +++ b/plugins/inputs/influxdb_listener/influxdb_listener_test.go @@ -477,7 +477,7 @@ func TestWriteEmpty(t *testing.T) { require.EqualValues(t, 204, resp.StatusCode) } -func TestQueryAndPing(t *testing.T) { +func TestQuery(t *testing.T) { listener := newTestListener() acc := &testutil.Accumulator{} @@ -490,14 +490,40 @@ func TestQueryAndPing(t *testing.T) { createURL(listener, "http", "/query", "db=&q=CREATE+DATABASE+IF+NOT+EXISTS+%22mydb%22"), "", nil) require.NoError(t, err) require.EqualValues(t, 200, resp.StatusCode) +} + +func TestPing(t *testing.T) { + listener := newTestListener() + acc := &testutil.Accumulator{} + require.NoError(t, listener.Init()) + require.NoError(t, listener.Start(acc)) + defer listener.Stop() // post ping to listener - resp, err = http.Post(createURL(listener, "http", "/ping", ""), "", nil) + resp, err := http.Post(createURL(listener, "http", "/ping", ""), "", nil) require.NoError(t, err) + require.Equal(t, "1.0", resp.Header["X-Influxdb-Version"][0]) + require.Len(t, resp.Header["Content-Type"], 0) resp.Body.Close() require.EqualValues(t, 204, resp.StatusCode) } +func TestPingVerbose(t *testing.T) { + listener := newTestListener() + acc := &testutil.Accumulator{} + require.NoError(t, listener.Init()) + require.NoError(t, listener.Start(acc)) + defer listener.Stop() + + // post ping to listener + resp, err := http.Post(createURL(listener, "http", "/ping", "verbose=1"), "", nil) + require.NoError(t, err) + require.Equal(t, "1.0", resp.Header["X-Influxdb-Version"][0]) + require.Equal(t, "application/json", resp.Header["Content-Type"][0]) + resp.Body.Close() + require.EqualValues(t, 200, resp.StatusCode) +} + func TestWriteWithPrecision(t *testing.T) { listener := newTestListener()