Skip to content

Commit

Permalink
Set headers on influxdb_listener ping URL (influxdata#7182)
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda authored Mar 17, 2020
1 parent d14d05c commit b8d18f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions plugins/inputs/influxdb_listener/influxdb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 28 additions & 2 deletions plugins/inputs/influxdb_listener/influxdb_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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()

Expand Down

0 comments on commit b8d18f9

Please sign in to comment.