2016-04-04 02:20:07 +00:00
|
|
|
package http_response
|
|
|
|
|
|
|
|
import (
|
2019-06-19 20:40:53 +00:00
|
|
|
"errors"
|
2016-04-04 02:20:07 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-06-19 20:40:53 +00:00
|
|
|
"net"
|
2016-04-04 02:20:07 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-05-23 12:33:43 +00:00
|
|
|
|
2019-09-10 18:04:24 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-05-23 12:33:43 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2017-05-09 23:21:04 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-05-23 12:33:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-04-04 02:20:07 +00:00
|
|
|
)
|
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
// Receives a list with fields that are expected to be absent
|
2018-04-02 21:13:15 +00:00
|
|
|
func checkAbsentFields(t *testing.T, fields []string, acc *testutil.Accumulator) {
|
2018-03-08 22:55:59 +00:00
|
|
|
for _, field := range fields {
|
|
|
|
ok := acc.HasField("http_response", field)
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receives a list with tags that are expected to be absent
|
2018-04-02 21:13:15 +00:00
|
|
|
func checkAbsentTags(t *testing.T, tags []string, acc *testutil.Accumulator) {
|
2018-03-08 22:55:59 +00:00
|
|
|
for _, tag := range tags {
|
|
|
|
ok := acc.HasTag("http_response", tag)
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receives a dictionary and with expected fields and their values. If a value is nil, it will only check
|
|
|
|
// that the field exists, but not its contents
|
2018-04-02 21:13:15 +00:00
|
|
|
func checkFields(t *testing.T, fields map[string]interface{}, acc *testutil.Accumulator) {
|
2019-09-10 18:04:24 +00:00
|
|
|
t.Helper()
|
2018-03-08 22:55:59 +00:00
|
|
|
for key, field := range fields {
|
|
|
|
switch v := field.(type) {
|
|
|
|
case int:
|
|
|
|
value, ok := acc.IntField("http_response", key)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, field, value)
|
|
|
|
case float64:
|
|
|
|
value, ok := acc.FloatField("http_response", key)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, field, value)
|
|
|
|
case string:
|
|
|
|
value, ok := acc.StringField("http_response", key)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, field, value)
|
|
|
|
case nil:
|
|
|
|
ok := acc.HasField("http_response", key)
|
|
|
|
require.True(t, ok)
|
|
|
|
default:
|
|
|
|
t.Log("Unsupported type for field: ", v)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receives a dictionary and with expected tags and their values. If a value is nil, it will only check
|
|
|
|
// that the tag exists, but not its contents
|
2018-04-02 21:13:15 +00:00
|
|
|
func checkTags(t *testing.T, tags map[string]interface{}, acc *testutil.Accumulator) {
|
2018-03-08 22:55:59 +00:00
|
|
|
for key, tag := range tags {
|
|
|
|
switch v := tag.(type) {
|
|
|
|
case string:
|
|
|
|
ok := acc.HasTag("http_response", key)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, tag, acc.TagValue("http_response", key))
|
|
|
|
case nil:
|
|
|
|
ok := acc.HasTag("http_response", key)
|
|
|
|
require.True(t, ok)
|
|
|
|
default:
|
|
|
|
t.Log("Unsupported type for tag: ", v)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
func setUpTestMux() http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/redirect", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Redirect(w, req, "/good", http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/good", func(w http.ResponseWriter, req *http.Request) {
|
2020-05-26 22:52:13 +00:00
|
|
|
w.Header().Set("Server", "MyTestServer")
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
fmt.Fprintf(w, "hit the good page!")
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/noheader", func(w http.ResponseWriter, req *http.Request) {
|
2016-04-04 02:20:07 +00:00
|
|
|
fmt.Fprintf(w, "hit the good page!")
|
|
|
|
})
|
2017-02-01 14:21:08 +00:00
|
|
|
mux.HandleFunc("/jsonresponse", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
fmt.Fprintf(w, "\"service_status\": \"up\", \"healthy\" : \"true\"")
|
|
|
|
})
|
2016-04-04 02:20:07 +00:00
|
|
|
mux.HandleFunc("/badredirect", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Redirect(w, req, "/badredirect", http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/mustbepostmethod", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != "POST" {
|
|
|
|
http.Error(w, "method wasn't post", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "used post correctly!")
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/musthaveabody", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
|
|
req.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "couldn't read request body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if string(body) == "" {
|
|
|
|
http.Error(w, "body was empty", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "sent a body!")
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/twosecondnap", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:13:15 +00:00
|
|
|
func checkOutput(t *testing.T, acc *testutil.Accumulator, presentFields map[string]interface{}, presentTags map[string]interface{}, absentFields []string, absentTags []string) {
|
2019-09-10 18:04:24 +00:00
|
|
|
t.Helper()
|
2018-03-08 22:55:59 +00:00
|
|
|
if presentFields != nil {
|
|
|
|
checkFields(t, presentFields, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if presentTags != nil {
|
|
|
|
checkTags(t, presentTags, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if absentFields != nil {
|
|
|
|
checkAbsentFields(t, absentFields, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if absentTags != nil {
|
|
|
|
checkAbsentTags(t, absentTags, acc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:08:25 +00:00
|
|
|
func TestHeaders(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cHeader := r.Header.Get("Content-Type")
|
|
|
|
assert.Equal(t, "Hello", r.Host)
|
|
|
|
assert.Equal(t, "application/json", cHeader)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-05-19 11:08:25 +00:00
|
|
|
Address: ts.URL,
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 2},
|
2016-05-19 11:08:25 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Host": "Hello",
|
|
|
|
},
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2016-05-19 11:08:25 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-05-19 11:08:25 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
func TestFields(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 22:52:13 +00:00
|
|
|
func TestHTTPHeaderTags(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
HTTPHeaderTags: map[string]string{"Server": "my_server", "Content-Type": "content_type"},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
|
|
|
"content_length": nil,
|
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
"my_server": "MyTestServer",
|
|
|
|
"content_type": "application/json; charset=utf-8",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
|
|
|
|
|
|
|
h = &HTTPResponse{
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
Address: ts.URL + "/noheader",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
HTTPHeaderTags: map[string]string{"Server": "my_server", "Content-Type": "content_type"},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:40:53 +00:00
|
|
|
func findInterface() (net.Interface, error) {
|
|
|
|
potential, _ := net.Interfaces()
|
|
|
|
|
|
|
|
for _, i := range potential {
|
|
|
|
// we are only interest in loopback interfaces which are up
|
|
|
|
if (i.Flags&net.FlagUp == 0) || (i.Flags&net.FlagLoopback == 0) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if addrs, _ := i.Addrs(); len(addrs) > 0 {
|
|
|
|
// return interface if it has at least one unicast address
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.Interface{}, errors.New("cannot find suitable loopback interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterface(t *testing.T) {
|
|
|
|
var (
|
|
|
|
mux = setUpTestMux()
|
|
|
|
ts = httptest.NewServer(mux)
|
|
|
|
)
|
|
|
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
intf, err := findInterface()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-06-19 20:40:53 +00:00
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
Interface: intf.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err = h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2019-06-19 20:40:53 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
func TestRedirects(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/redirect",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
|
|
|
|
h = &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/badredirect",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
2017-06-06 20:39:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"result_type": "connection_failed",
|
|
|
|
"result_code": 3,
|
|
|
|
}
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"result": "connection_failed",
|
|
|
|
}
|
|
|
|
absentFields = []string{"http_response_code", "response_time", "response_string_match"}
|
|
|
|
absentTags := []string{"status_code"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, nil, nil)
|
2018-03-08 22:55:59 +00:00
|
|
|
|
|
|
|
expectedFields = map[string]interface{}{"result_type": "connection_failed"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethod(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/mustbepostmethod",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "POST",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "POST",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
|
|
|
|
h = &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/mustbepostmethod",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusMethodNotAllowed,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "405",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields = []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
|
|
|
|
//check that lowercase methods work correctly
|
|
|
|
h = &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/mustbepostmethod",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "head",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusMethodNotAllowed,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "head",
|
|
|
|
"status_code": "405",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields = []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBody(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/musthaveabody",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
|
|
|
|
h = &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/musthaveabody",
|
|
|
|
Method: "GET",
|
2016-05-23 12:33:43 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
2016-04-04 02:20:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusBadRequest,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
}
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "400",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields = []string{"response_string_match"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 14:21:08 +00:00
|
|
|
func TestStringMatch(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2017-02-01 14:21:08 +00:00
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseStringMatch: "hit the good page",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2017-02-01 14:21:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"response_string_match": 1,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, nil, nil)
|
2017-02-01 14:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringMatchJson(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2017-02-01 14:21:08 +00:00
|
|
|
Address: ts.URL + "/jsonresponse",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseStringMatch: "\"service_status\": \"up\"",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2017-02-01 14:21:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"response_string_match": 1,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, nil, nil)
|
2017-02-01 14:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringMatchFail(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2017-02-01 14:21:08 +00:00
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseStringMatch: "hit the bad page",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2017-02-01 14:21:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"response_string_match": 0,
|
|
|
|
"result_type": "response_string_mismatch",
|
|
|
|
"result_code": 1,
|
|
|
|
"response_time": nil,
|
2019-08-16 22:45:20 +00:00
|
|
|
"content_length": nil,
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "response_string_mismatch",
|
|
|
|
}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, nil, nil)
|
2017-02-01 14:21:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
func TestTimeout(t *testing.T) {
|
2017-05-09 23:21:04 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping test with sleep in short mode.")
|
|
|
|
}
|
|
|
|
|
2016-04-04 02:20:07 +00:00
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2016-04-04 02:20:07 +00:00
|
|
|
Address: ts.URL + "/twosecondnap",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
2017-05-16 22:25:30 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second},
|
2016-04-07 01:57:49 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-04-04 02:20:07 +00:00
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
2017-05-09 23:21:04 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
2017-06-06 20:39:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-09 23:21:04 +00:00
|
|
|
|
2018-03-08 22:55:59 +00:00
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"result_type": "timeout",
|
|
|
|
"result_code": 4,
|
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"result": "timeout",
|
|
|
|
}
|
2019-08-16 22:45:20 +00:00
|
|
|
absentFields := []string{"http_response_code", "response_time", "content_length", "response_string_match"}
|
2018-03-08 22:55:59 +00:00
|
|
|
absentTags := []string{"status_code"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 18:04:24 +00:00
|
|
|
func TestBadRegex(t *testing.T) {
|
2018-03-08 22:55:59 +00:00
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2018-03-08 22:55:59 +00:00
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseStringMatch: "bad regex:[[",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
2019-08-16 22:45:20 +00:00
|
|
|
absentFields := []string{"http_response_code", "response_time", "content_length", "response_string_match", "result_type", "result_code"}
|
2018-03-08 22:55:59 +00:00
|
|
|
absentTags := []string{"status_code", "result", "server", "method"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, nil, nil, absentFields, absentTags)
|
2018-03-08 22:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkErrors(t *testing.T) {
|
|
|
|
// DNS error
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2018-03-08 22:55:59 +00:00
|
|
|
Address: "https://nonexistent.nonexistent", // Any non-resolvable URL works here
|
|
|
|
Body: "",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
FollowRedirects: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"result_type": "dns_error",
|
|
|
|
"result_code": 5,
|
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"result": "dns_error",
|
|
|
|
}
|
2019-08-16 22:45:20 +00:00
|
|
|
absentFields := []string{"http_response_code", "response_time", "content_length", "response_string_match"}
|
2018-03-08 22:55:59 +00:00
|
|
|
absentTags := []string{"status_code"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)
|
2018-03-08 22:55:59 +00:00
|
|
|
|
2020-05-14 07:41:58 +00:00
|
|
|
// Connection failed
|
2018-03-08 22:55:59 +00:00
|
|
|
h = &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2018-07-28 01:29:54 +00:00
|
|
|
Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here
|
2018-03-08 22:55:59 +00:00
|
|
|
Body: "",
|
|
|
|
Method: "GET",
|
2018-07-28 01:29:54 +00:00
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 5},
|
2018-03-08 22:55:59 +00:00
|
|
|
FollowRedirects: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"result_type": "connection_failed",
|
|
|
|
"result_code": 3,
|
|
|
|
}
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"result": "connection_failed",
|
|
|
|
}
|
2019-08-16 22:45:20 +00:00
|
|
|
absentFields = []string{"http_response_code", "response_time", "content_length", "response_string_match"}
|
2018-03-08 22:55:59 +00:00
|
|
|
absentTags = []string{"status_code"}
|
2018-04-02 21:13:15 +00:00
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)
|
2016-04-04 02:20:07 +00:00
|
|
|
}
|
2019-08-16 22:45:20 +00:00
|
|
|
|
|
|
|
func TestContentLength(t *testing.T) {
|
|
|
|
mux := setUpTestMux()
|
|
|
|
ts := httptest.NewServer(mux)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-08-16 22:45:20 +00:00
|
|
|
URLs: []string{ts.URL + "/good"},
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
|
|
|
"content_length": len([]byte("hit the good page!")),
|
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
|
|
|
|
|
|
|
h = &HTTPResponse{
|
2019-09-23 22:39:50 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-08-16 22:45:20 +00:00
|
|
|
URLs: []string{ts.URL + "/musthaveabody"},
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
FollowRedirects: true,
|
|
|
|
}
|
|
|
|
acc = testutil.Accumulator{}
|
|
|
|
err = h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields = map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
|
|
|
"content_length": len([]byte("sent a body!")),
|
|
|
|
}
|
|
|
|
expectedTags = map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields = []string{"response_string_match"}
|
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
|
|
|
}
|
2019-09-10 18:04:24 +00:00
|
|
|
|
|
|
|
func TestRedirect(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Location", "http://example.org")
|
|
|
|
w.WriteHeader(http.StatusMovedPermanently)
|
|
|
|
w.Write([]byte("test"))
|
|
|
|
})
|
|
|
|
|
|
|
|
plugin := &HTTPResponse{
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
ResponseStringMatch: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := plugin.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"http_response",
|
|
|
|
map[string]string{
|
|
|
|
"server": ts.URL,
|
|
|
|
"method": "GET",
|
|
|
|
"result": "success",
|
|
|
|
"status_code": "301",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"result_code": 0,
|
|
|
|
"result_type": "success",
|
|
|
|
"http_response_code": 301,
|
|
|
|
"response_string_match": 1,
|
|
|
|
"content_length": 4,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := acc.GetTelegrafMetrics()
|
|
|
|
for _, m := range actual {
|
|
|
|
m.RemoveField("response_time")
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
|
|
|
|
}
|
2020-05-12 21:01:17 +00:00
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
aHeader := r.Header.Get("Authorization")
|
|
|
|
assert.Equal(t, "Basic bWU6bXlwYXNzd29yZA==", aHeader)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
h := &HTTPResponse{
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
Address: ts.URL + "/good",
|
|
|
|
Body: "{ 'test': 'data'}",
|
|
|
|
Method: "GET",
|
|
|
|
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
|
|
|
|
Username: "me",
|
|
|
|
Password: "mypassword",
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := h.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFields := map[string]interface{}{
|
|
|
|
"http_response_code": http.StatusOK,
|
|
|
|
"result_type": "success",
|
|
|
|
"result_code": 0,
|
|
|
|
"response_time": nil,
|
|
|
|
"content_length": nil,
|
|
|
|
}
|
|
|
|
expectedTags := map[string]interface{}{
|
|
|
|
"server": nil,
|
|
|
|
"method": "GET",
|
|
|
|
"status_code": "200",
|
|
|
|
"result": "success",
|
|
|
|
}
|
|
|
|
absentFields := []string{"response_string_match"}
|
|
|
|
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
|
|
|
|
}
|