feat: check content length
This commit is contained in:
16
http.go
16
http.go
@@ -44,6 +44,7 @@ type HTTPMonitor struct {
|
|||||||
// data
|
// data
|
||||||
Data string `mapstructure:"data"`
|
Data string `mapstructure:"data"`
|
||||||
ExpectedMd5Sum string `mapstructure:"expected_md5sum"`
|
ExpectedMd5Sum string `mapstructure:"expected_md5sum"`
|
||||||
|
ExpectedLength int `mapstructure:"expected_length"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: test
|
// TODO: test
|
||||||
@@ -88,21 +89,28 @@ func (monitor *HTTPMonitor) test() bool {
|
|||||||
|
|
||||||
// check response body
|
// check response body
|
||||||
responseBody, err := ioutil.ReadAll(resp.Body)
|
responseBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
responseLength := len(string(responseBody))
|
||||||
fmt.Println("Response: ", 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())
|
fmt.Println(err.Error())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if monitor.ExpectedLength > 0 && responseLength != monitor.ExpectedLength {
|
||||||
|
monitor.lastFailReason = "Expected response body length: " + strconv.Itoa(monitor.ExpectedLength) + ", got: " + strconv.Itoa(responseLength)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
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("Calculated sum", sum)
|
||||||
fmt.Println("Expected sum", monitor.ExpectedMd5Sum)
|
fmt.Println("Expected sum", monitor.ExpectedMd5Sum)
|
||||||
equalMd5 := strings.Compare(sum, monitor.ExpectedMd5Sum) == 0
|
if strings.Compare(sum, monitor.ExpectedMd5Sum) != 0 {
|
||||||
if !equalMd5 {
|
monitor.lastFailReason = "Expected respsone body MD5 checksum: " + monitor.ExpectedMd5Sum + ", got: " + sum
|
||||||
monitor.lastFailReason = "Expected Md5 sum: " + monitor.ExpectedMd5Sum + ", got: " + sum
|
return false
|
||||||
}
|
}
|
||||||
return equalMd5
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if monitor.bodyRegexp != nil {
|
if monitor.bodyRegexp != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user