update to 5 second default and string map for headers

This commit is contained in:
Luke Swithenbank 2016-04-07 11:57:49 +10:00 committed by Michele Fadda
parent 3fcc81866a
commit 42e4cba6bd
3 changed files with 54 additions and 67 deletions

View File

@ -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

View File

@ -1,11 +1,9 @@
package http_response
import (
"bufio"
"errors"
"io"
"net/http"
"net/textproto"
"net/url"
"strings"
"time"
@ -20,7 +18,7 @@ type HTTPResponse struct {
Body string
Method string
ResponseTimeout int
Headers string
Headers map[string]string
FollowRedirects bool
}
@ -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
@ -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
@ -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)
@ -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 == "" {

View File

@ -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 {
@ -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()
@ -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()
@ -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()
@ -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()
@ -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()
@ -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()
@ -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()
@ -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()
@ -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()