checkDown checks whether the endpoint is down based on resp and the configuration of c. It returns a non-nil error if down. Note that it does not check for degraded response.
(resp *http.Response)
| 177 | // the configuration of c. It returns a non-nil error if down. |
| 178 | // Note that it does not check for degraded response. |
| 179 | func (c Checker) checkDown(resp *http.Response) error { |
| 180 | // Check status code |
| 181 | if resp.StatusCode != c.UpStatus { |
| 182 | return fmt.Errorf("response status %s", resp.Status) |
| 183 | } |
| 184 | |
| 185 | // Check response body |
| 186 | if c.MustContain == "" && c.MustNotContain == "" { |
| 187 | return nil |
| 188 | } |
| 189 | bodyBytes, err := ioutil.ReadAll(resp.Body) |
| 190 | if err != nil { |
| 191 | return fmt.Errorf("reading response body: %v", err) |
| 192 | } |
| 193 | body := string(bodyBytes) |
| 194 | if c.MustContain != "" && !strings.Contains(body, c.MustContain) { |
| 195 | return fmt.Errorf("response does not contain '%s'", c.MustContain) |
| 196 | } |
| 197 | if c.MustNotContain != "" && strings.Contains(body, c.MustNotContain) { |
| 198 | return fmt.Errorf("response contains '%s'", c.MustNotContain) |
| 199 | } |
| 200 | |
| 201 | return nil |
| 202 | } |
| 203 | |
| 204 | // DefaultHTTPClient is used when no other http.Client |
| 205 | // is specified on a Checker. |