Added in test for following redirects. https://github.com/influxdata/telegraf/issues/2191 This depends on code/PR for influxDB https://github.com/influxdata/influxdb/pull/7803

This commit is contained in:
Len Smith 2017-01-07 14:48:29 -06:00
parent fd1feff7b4
commit 93fff5ff2e
2 changed files with 86 additions and 6 deletions

View File

@ -38,6 +38,9 @@ type InfluxDB struct {
// Use SSL but skip chain & host verification
InsecureSkipVerify bool
// Follow Redirects even for puts/posts. This can be insuecure
InsecureFollowRedirect bool
// Precision is only here for legacy support. It will be ignored.
Precision string
@ -122,12 +125,17 @@ func (i *InfluxDB) Connect() error {
Password: i.Password,
UserAgent: i.UserAgent,
Timeout: i.Timeout.Duration,
InsecureFollowRedirect: i.InsecureFollowRedirect,
TLSConfig: tlsCfg,
})
if err != nil {
return err
}
if i.InsecureFollowRedirect {
c.Ping(10)
}
err = createDatabase(c, i.Database)
if err != nil {
log.Println("E! Database creation failed: " + err.Error())

View File

@ -39,3 +39,75 @@ func TestHTTPInflux(t *testing.T) {
err = i.Write(testutil.MockMetrics())
require.NoError(t, err)
}
// TestHTTPInfluxFollow301RedirectFalse verifies that the default behavior is for
// InsecureFollowRedirect is false and that it will generate an error.
func TestHTTPInfluxFollow301RedirectFalse(t *testing.T) {
// The influxDB HTTP server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"results":[{}]}`)
}))
defer ts.Close()
// A intermediate HTTP server which sends a redirect
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "test/html")
w.Header().Set("Location", ts.URL)
w.WriteHeader(http.StatusMovedPermanently)
fmt.Fprintln(w, `<html><head><title>301 Moved Permanently</title></head>`)
fmt.Fprintln(w, `<body bgcolor="white"><center><h1>301 Moved Permanently</h1></center><hr><center>nginx/1.10.0 (Ubuntu)</center></body></html>`)
}))
defer redirectServer.Close()
i := InfluxDB{
URLs: []string{redirectServer.URL},
}
err := i.Connect()
require.NoError(t, err)
// This should be empty because we have one URL, with a redirect. Since we don't
// follow it, the connection is not added.
if len(i.conns) != 0 {
t.Errorf("Did not get an empty list of connections: %s\n", i.conns)
}
}
// TestHTTPInfluxFollow301RedirectTrue verifies that if InsecureFollowRedirect
// is set to true, then Ping() will be called and the CheckRedirect function will
// be enabled and will set URL to the last redirect in the chain. Up to 10
// redirects max will be followed ( This is net/http behavior )
func TestHTTPInfluxFollow301RedirectTrue(t *testing.T) {
// The influxDB HTTP server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"results":[{}]}`)
}))
defer ts.Close()
// A intermediate HTTP server which sends a redirect
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "test/html")
w.Header().Set("Location", ts.URL)
w.WriteHeader(http.StatusMovedPermanently)
fmt.Fprintln(w, `<html><head><title>301 Moved Permanently</title></head>`)
fmt.Fprintln(w, `<body bgcolor="white"><center><h1>301 Moved Permanently</h1></center><hr><center>nginx/1.10.0 (Ubuntu)</center></body></html>`)
}))
defer redirectServer.Close()
i := InfluxDB{
URLs: []string{redirectServer.URL},
InsecureFollowRedirect: true,
}
err := i.Connect()
require.NoError(t, err)
}