Skip to content

Commit

Permalink
update to 5 second default and string map for headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Swithenbank committed Apr 7, 2016
1 parent 6feef3f commit bf09b4f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 67 deletions.
9 changes: 4 additions & 5 deletions plugins/inputs/http_response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ This input plugin will test HTTP/HTTPS connections.
[[inputs.http_response]]
## Server address (default http://localhost)
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
## Set response_timeout (default 5 seconds)
response_timeout = 5
## HTTP Request Method
method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
[inputs.http_response.headers]
Host = github.com
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
## Optional HTTP Request Body
Expand Down
40 changes: 16 additions & 24 deletions plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package http_response

import (
"bufio"
"errors"
"io"
"net/http"
"net/textproto"
"net/url"
"strings"
"time"
Expand All @@ -20,7 +18,7 @@ type HTTPResponse struct {
Body string
Method string
ResponseTimeout int
Headers string
Headers map[string]string
FollowRedirects bool
}

Expand All @@ -32,14 +30,13 @@ func (h *HTTPResponse) Description() string {
var sampleConfig = `
## Server address (default http://localhost)
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
## Set response_timeout (default 5 seconds)
response_timeout = 5
## HTTP Request Method
method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
## HTTP Request Headers (all values must be strings)
[inputs.http_response.headers]
# Host = "github.com"
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
## Optional HTTP Request Body
Expand Down Expand Up @@ -71,17 +68,14 @@ func CreateHttpClient(followRedirects bool, ResponseTimeout time.Duration) *http
return client
}

// ParseHeaders takes a string of newline seperated http headers and returns a
// http.Header object. An error is returned if the headers cannot be parsed.
func ParseHeaders(headers string) (http.Header, error) {
headers = strings.TrimSpace(headers) + "\n\n"
reader := bufio.NewReader(strings.NewReader(headers))
tp := textproto.NewReader(reader)
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
return nil, err
// CreateHeaders takes a map of header strings and puts them
// into a http.Header Object
func CreateHeaders(headers map[string]string) http.Header {
httpHeaders := make(http.Header)
for key := range headers {
httpHeaders.Add(key, headers[key])
}
return http.Header(mimeHeader), nil
return httpHeaders
}

// HTTPGather gathers all fields and returns any errors it encounters
Expand All @@ -99,10 +93,8 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
if err != nil {
return nil, err
}
request.Header, err = ParseHeaders(h.Headers)
if err != nil {
return nil, err
}
request.Header = CreateHeaders(h.Headers)

// Start Timer
start := time.Now()
resp, err := client.Do(request)
Expand All @@ -126,7 +118,7 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
// Set default values
if h.ResponseTimeout < 1 {
h.ResponseTimeout = 10
h.ResponseTimeout = 5
}
// Check send and expected string
if h.Method == "" {
Expand Down
72 changes: 34 additions & 38 deletions plugins/inputs/http_response/http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ import (
"time"
)

func TestParseHeaders(t *testing.T) {
fakeHeaders := `
Accept: text/plain
Content-Type: application/json
Cache-Control: no-cache
`
headers, err := ParseHeaders(fakeHeaders)
require.NoError(t, err)
func TestCreateHeaders(t *testing.T) {
fakeHeaders := map[string]string{
"Accept": "text/plain",
"Content-Type": "application/json",
"Cache-Control": "no-cache",
}
headers := CreateHeaders(fakeHeaders)
testHeaders := make(http.Header)
testHeaders.Add("Accept", "text/plain")
testHeaders.Add("Content-Type", "application/json")
testHeaders.Add("Cache-Control", "no-cache")
assert.Equal(t, testHeaders, headers)

headers, err = ParseHeaders("Accept text/plain")
require.Error(t, err)
}

func setUpTestMux() http.Handler {
Expand Down Expand Up @@ -77,9 +73,9 @@ func TestFields(t *testing.T) {
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err := h.HTTPGather()
Expand All @@ -102,9 +98,9 @@ func TestRedirects(t *testing.T) {
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err := h.HTTPGather()
Expand All @@ -119,9 +115,9 @@ Content-Type: application/json
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err = h.HTTPGather()
Expand All @@ -138,9 +134,9 @@ func TestMethod(t *testing.T) {
Body: "{ 'test': 'data'}",
Method: "POST",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err := h.HTTPGather()
Expand All @@ -155,9 +151,9 @@ Content-Type: application/json
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err = h.HTTPGather()
Expand All @@ -173,9 +169,9 @@ Content-Type: application/json
Body: "{ 'test': 'data'}",
Method: "head",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err = h.HTTPGather()
Expand All @@ -196,9 +192,9 @@ func TestBody(t *testing.T) {
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err := h.HTTPGather()
Expand All @@ -212,9 +208,9 @@ Content-Type: application/json
Address: ts.URL + "/musthaveabody",
Method: "GET",
ResponseTimeout: 20,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
fields, err = h.HTTPGather()
Expand All @@ -235,9 +231,9 @@ func TestTimeout(t *testing.T) {
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: 1,
Headers: `
Content-Type: application/json
`,
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
_, err := h.HTTPGather()
Expand Down

0 comments on commit bf09b4f

Please sign in to comment.