Skip to content

Commit

Permalink
fixed network test (influxdata#8498)
Browse files Browse the repository at this point in the history
  • Loading branch information
helenosheaa authored Dec 4, 2020
1 parent bc301dd commit 9eacc88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ type HTTPResponse struct {
Log telegraf.Logger

compiledStringMatch *regexp.Regexp
client *http.Client
client httpClient
}

type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}

// Description returns the plugin Description
Expand Down
13 changes: 13 additions & 0 deletions plugins/inputs/http_response/http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -905,6 +906,17 @@ func TestBadRegex(t *testing.T) {
checkOutput(t, &acc, nil, nil, absentFields, absentTags)
}

type fakeClient struct {
statusCode int
err error
}

func (f *fakeClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: f.statusCode,
}, f.err
}

func TestNetworkErrors(t *testing.T) {
// DNS error
h := &HTTPResponse{
Expand All @@ -914,6 +926,7 @@ func TestNetworkErrors(t *testing.T) {
Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
FollowRedirects: false,
client: &fakeClient{err: &url.Error{Err: &net.OpError{Err: &net.DNSError{Err: "DNS error"}}}},
}

var acc testutil.Accumulator
Expand Down

0 comments on commit 9eacc88

Please sign in to comment.