refactor: remove logs and reformat
This commit is contained in:
25
http.go
25
http.go
@@ -1,15 +1,15 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Investigating template
|
// Investigating template
|
||||||
@@ -41,27 +41,27 @@ type HTTPMonitor struct {
|
|||||||
ExpectedBody string `mapstructure:"expected_body"`
|
ExpectedBody string `mapstructure:"expected_body"`
|
||||||
bodyRegexp *regexp.Regexp
|
bodyRegexp *regexp.Regexp
|
||||||
|
|
||||||
// data
|
// content check
|
||||||
Data string `mapstructure:"data"`
|
|
||||||
ExpectedMd5Sum string `mapstructure:"expected_md5sum"`
|
ExpectedMd5Sum string `mapstructure:"expected_md5sum"`
|
||||||
ExpectedLength int `mapstructure:"expected_length"`
|
ExpectedLength int `mapstructure:"expected_length"`
|
||||||
|
|
||||||
|
// data
|
||||||
|
Data string `mapstructure:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: test
|
// TODO: test
|
||||||
func (monitor *HTTPMonitor) test() bool {
|
func (monitor *HTTPMonitor) test() bool {
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if monitor.Data != "" {
|
if monitor.Data != "" {
|
||||||
fmt.Println("Data: ", monitor.Data)
|
|
||||||
dataBuffer := strings.NewReader(monitor.Data)
|
dataBuffer := strings.NewReader(monitor.Data)
|
||||||
req, err = http.NewRequest(monitor.Method, monitor.Target, dataBuffer)
|
req, err = http.NewRequest(monitor.Method, monitor.Target, dataBuffer)
|
||||||
fmt.Println("Target: ", dataBuffer)
|
|
||||||
} else {
|
} else {
|
||||||
req, err = http.NewRequest(monitor.Method, monitor.Target, nil)
|
req, err = http.NewRequest(monitor.Method, monitor.Target, nil)
|
||||||
}
|
}
|
||||||
fmt.Println("Target: ", monitor.Target)
|
|
||||||
for k, v := range monitor.Headers {
|
for k, v := range monitor.Headers {
|
||||||
fmt.Println(k, ": ", v)
|
|
||||||
req.Header.Add(k, v)
|
req.Header.Add(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +74,6 @@ func (monitor *HTTPMonitor) test() bool {
|
|||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
|
||||||
monitor.lastFailReason = err.Error()
|
monitor.lastFailReason = err.Error()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -83,18 +82,13 @@ func (monitor *HTTPMonitor) test() bool {
|
|||||||
|
|
||||||
if monitor.ExpectedStatusCode > 0 && resp.StatusCode != monitor.ExpectedStatusCode {
|
if monitor.ExpectedStatusCode > 0 && resp.StatusCode != monitor.ExpectedStatusCode {
|
||||||
monitor.lastFailReason = "Expected HTTP response status: " + strconv.Itoa(monitor.ExpectedStatusCode) + ", got: " + strconv.Itoa(resp.StatusCode)
|
monitor.lastFailReason = "Expected HTTP response status: " + strconv.Itoa(monitor.ExpectedStatusCode) + ", got: " + strconv.Itoa(resp.StatusCode)
|
||||||
fmt.Println(monitor.lastFailReason)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// check response body
|
|
||||||
responseBody, err := ioutil.ReadAll(resp.Body)
|
responseBody, err := ioutil.ReadAll(resp.Body)
|
||||||
responseLength := len(string(responseBody))
|
responseLength := len(string(responseBody))
|
||||||
fmt.Println("Response: ", string(responseBody))
|
|
||||||
fmt.Println("Response len: ", responseLength)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
monitor.lastFailReason = err.Error()
|
monitor.lastFailReason = err.Error()
|
||||||
fmt.Println(err.Error())
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,8 +99,6 @@ func (monitor *HTTPMonitor) test() bool {
|
|||||||
|
|
||||||
if monitor.ExpectedMd5Sum != "" {
|
if monitor.ExpectedMd5Sum != "" {
|
||||||
sum := fmt.Sprintf("%x", (md5.Sum(responseBody)))
|
sum := fmt.Sprintf("%x", (md5.Sum(responseBody)))
|
||||||
fmt.Println("Calculated sum", sum)
|
|
||||||
fmt.Println("Expected sum", monitor.ExpectedMd5Sum)
|
|
||||||
if strings.Compare(sum, monitor.ExpectedMd5Sum) != 0 {
|
if strings.Compare(sum, monitor.ExpectedMd5Sum) != 0 {
|
||||||
monitor.lastFailReason = "Expected respsone body MD5 checksum: " + monitor.ExpectedMd5Sum + ", got: " + sum
|
monitor.lastFailReason = "Expected respsone body MD5 checksum: " + monitor.ExpectedMd5Sum + ", got: " + sum
|
||||||
return false
|
return false
|
||||||
@@ -116,7 +108,6 @@ func (monitor *HTTPMonitor) test() bool {
|
|||||||
if monitor.bodyRegexp != nil {
|
if monitor.bodyRegexp != nil {
|
||||||
if !monitor.bodyRegexp.Match(responseBody) {
|
if !monitor.bodyRegexp.Match(responseBody) {
|
||||||
monitor.lastFailReason = "Unexpected body: " + string(responseBody) + ".\nExpected to match: " + monitor.ExpectedBody
|
monitor.lastFailReason = "Unexpected body: " + string(responseBody) + ".\nExpected to match: " + monitor.ExpectedBody
|
||||||
fmt.Println(monitor.lastFailReason)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user