2018-02-16 08:00:10 +08:00
|
|
|
package http_test
|
|
|
|
|
|
|
|
|
|
import (
|
2018-12-12 11:12:00 +08:00
|
|
|
"compress/gzip"
|
|
|
|
|
"fmt"
|
2021-09-29 05:16:32 +08:00
|
|
|
"io"
|
2018-02-16 08:00:10 +08:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2021-04-23 21:37:27 +08:00
|
|
|
"net/url"
|
2018-02-16 08:00:10 +08:00
|
|
|
"testing"
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2021-04-23 21:37:27 +08:00
|
|
|
httpconfig "github.com/influxdata/telegraf/plugins/common/http"
|
2021-10-26 21:45:03 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/oauth"
|
|
|
|
|
httpplugin "github.com/influxdata/telegraf/plugins/inputs/http"
|
2018-02-16 08:00:10 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
func TestHTTPWithJSONFormat(t *testing.T) {
|
2018-02-16 08:00:10 +08:00
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/endpoint" {
|
|
|
|
|
_, _ = w.Write([]byte(simpleJSON))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
|
|
|
|
metricName := "metricName"
|
2018-08-23 10:26:48 +08:00
|
|
|
|
|
|
|
|
p, _ := parsers.NewParser(&parsers.Config{
|
|
|
|
|
DataFormat: "json",
|
|
|
|
|
MetricName: "metricName",
|
|
|
|
|
})
|
2018-02-16 08:00:10 +08:00
|
|
|
plugin.SetParser(p)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 08:00:10 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
|
|
|
|
|
require.Len(t, acc.Metrics, 1)
|
|
|
|
|
|
|
|
|
|
// basic check to see if we got the right field, value and tag
|
|
|
|
|
var metric = acc.Metrics[0]
|
|
|
|
|
require.Equal(t, metric.Measurement, metricName)
|
|
|
|
|
require.Len(t, acc.Metrics[0].Fields, 1)
|
|
|
|
|
require.Equal(t, acc.Metrics[0].Fields["a"], 1.2)
|
2021-10-26 21:45:03 +08:00
|
|
|
require.Equal(t, acc.Metrics[0].Tags["url"], address)
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHTTPHeaders(t *testing.T) {
|
|
|
|
|
header := "X-Special-Header"
|
|
|
|
|
headerValue := "Special-Value"
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/endpoint" {
|
|
|
|
|
if r.Header.Get(header) == headerValue {
|
|
|
|
|
_, _ = w.Write([]byte(simpleJSON))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-02-16 08:00:10 +08:00
|
|
|
Headers: map[string]string{header: headerValue},
|
|
|
|
|
}
|
2018-08-23 10:26:48 +08:00
|
|
|
|
|
|
|
|
p, _ := parsers.NewParser(&parsers.Config{
|
|
|
|
|
DataFormat: "json",
|
|
|
|
|
MetricName: "metricName",
|
|
|
|
|
})
|
2018-02-16 08:00:10 +08:00
|
|
|
plugin.SetParser(p)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 08:00:10 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInvalidStatusCode(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricName := "metricName"
|
2018-08-23 10:26:48 +08:00
|
|
|
p, _ := parsers.NewParser(&parsers.Config{
|
|
|
|
|
DataFormat: "json",
|
|
|
|
|
MetricName: metricName,
|
|
|
|
|
})
|
2018-02-16 08:00:10 +08:00
|
|
|
plugin.SetParser(p)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 08:00:10 +08:00
|
|
|
require.Error(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 05:23:36 +08:00
|
|
|
func TestSuccessStatusCodes(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2019-10-22 05:23:36 +08:00
|
|
|
SuccessStatusCodes: []int{200, 202},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricName := "metricName"
|
|
|
|
|
p, _ := parsers.NewParser(&parsers.Config{
|
|
|
|
|
DataFormat: "json",
|
|
|
|
|
MetricName: metricName,
|
|
|
|
|
})
|
|
|
|
|
plugin.SetParser(p)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2019-10-22 05:23:36 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 10:36:01 +08:00
|
|
|
func TestMethod(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin := &httpplugin.HTTP{
|
2018-02-16 10:36:01 +08:00
|
|
|
URLs: []string{fakeServer.URL},
|
|
|
|
|
Method: "POST",
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 10:26:48 +08:00
|
|
|
p, _ := parsers.NewParser(&parsers.Config{
|
|
|
|
|
DataFormat: "json",
|
|
|
|
|
MetricName: "metricName",
|
|
|
|
|
})
|
2018-02-16 10:36:01 +08:00
|
|
|
plugin.SetParser(p)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 10:36:01 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 08:00:10 +08:00
|
|
|
const simpleJSON = `
|
|
|
|
|
{
|
|
|
|
|
"a": 1.2
|
|
|
|
|
}
|
|
|
|
|
`
|
2018-12-12 11:12:00 +08:00
|
|
|
|
|
|
|
|
func TestBodyAndContentEncoding(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fmt.Sprintf("http://%s", ts.Listener.Addr().String())
|
2018-12-12 11:12:00 +08:00
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin *httpplugin.HTTP
|
2018-12-12 11:12:00 +08:00
|
|
|
queryHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no body",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "POST",
|
2021-10-26 21:45:03 +08:00
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte(""), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "post body",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "POST",
|
|
|
|
|
Body: "test",
|
|
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte("test"), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "get method body is sent",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "GET",
|
|
|
|
|
Body: "test",
|
|
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte("test"), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "gzip encoding",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "GET",
|
|
|
|
|
Body: "test",
|
|
|
|
|
ContentEncoding: "gzip",
|
|
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
require.Equal(t, r.Header.Get("Content-Encoding"), "gzip")
|
|
|
|
|
|
|
|
|
|
gr, err := gzip.NewReader(r.Body)
|
|
|
|
|
require.NoError(t, err)
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(gr)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte("test"), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
tt.queryHandlerFunc(t, w, r)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
parser, err := parsers.NewParser(&parsers.Config{DataFormat: "influx"})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tt.plugin.SetParser(parser)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, tt.plugin.Init())
|
2018-12-12 11:12:00 +08:00
|
|
|
err = tt.plugin.Gather(&acc)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-23 21:37:27 +08:00
|
|
|
|
|
|
|
|
type TestHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
|
|
|
|
|
|
|
|
|
|
func TestOAuthClientCredentialsGrant(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
var token = "2YotnFZFEjr1zCsicMWpAA"
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(fmt.Sprintf("http://%s", ts.Listener.Addr().String()))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin *httpplugin.HTTP
|
2021-04-23 21:37:27 +08:00
|
|
|
tokenHandler TestHandlerFunc
|
|
|
|
|
handler TestHandlerFunc
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no credentials",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
2021-04-23 21:37:27 +08:00
|
|
|
URLs: []string{u.String()},
|
|
|
|
|
},
|
|
|
|
|
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
require.Len(t, r.Header["Authorization"], 0)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "success",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
2021-04-23 21:37:27 +08:00
|
|
|
URLs: []string{u.String() + "/write"},
|
|
|
|
|
HTTPClientConfig: httpconfig.HTTPClientConfig{
|
|
|
|
|
OAuth2Config: oauth.OAuth2Config{
|
|
|
|
|
ClientID: "howdy",
|
|
|
|
|
ClientSecret: "secret",
|
|
|
|
|
TokenURL: u.String() + "/token",
|
|
|
|
|
Scopes: []string{"urn:opc:idm:__myscopes__"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
values := url.Values{}
|
|
|
|
|
values.Add("access_token", token)
|
|
|
|
|
values.Add("token_type", "bearer")
|
|
|
|
|
values.Add("expires_in", "3600")
|
|
|
|
|
_, err := w.Write([]byte(values.Encode()))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
},
|
|
|
|
|
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
require.Equal(t, []string{"Bearer " + token}, r.Header["Authorization"])
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
case "/write":
|
|
|
|
|
tt.handler(t, w, r)
|
|
|
|
|
case "/token":
|
|
|
|
|
tt.tokenHandler(t, w, r)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
parser, _ := parsers.NewValueParser("metric", "string", "", nil)
|
|
|
|
|
tt.plugin.SetParser(parser)
|
|
|
|
|
err = tt.plugin.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
err = tt.plugin.Gather(&acc)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|