Skip to content

Commit

Permalink
Merge 5f89b1e into 453d2dd
Browse files Browse the repository at this point in the history
  • Loading branch information
eranharel committed May 3, 2020
2 parents 453d2dd + 5f89b1e commit 4b8f7e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
12 changes: 11 additions & 1 deletion checks/http.go
@@ -1,6 +1,7 @@
package checks

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -42,6 +43,7 @@ type RequestOption func(r *http.Request)

type httpCheck struct {
config *HTTPCheckConfig
payload []byte
successDetails string
}

Expand All @@ -64,6 +66,13 @@ func NewHTTPCheck(config HTTPCheckConfig) (check Check, err error) {
if config.Method == "" {
config.Method = http.MethodGet
}
var payload []byte
if config.Body != nil {
payload, err = ioutil.ReadAll(config.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read body")
}
}
if config.Timeout == 0 {
config.Timeout = time.Second
}
Expand All @@ -74,6 +83,7 @@ func NewHTTPCheck(config HTTPCheckConfig) (check Check, err error) {

check = &httpCheck{
config: &config,
payload: payload,
successDetails: fmt.Sprintf("URL [%s] is accessible", config.URL),
}
return check, nil
Expand Down Expand Up @@ -114,7 +124,7 @@ func (check *httpCheck) Execute() (details interface{}, err error) {
// fetchURL executes the HTTP request to the target URL, and returns a `http.Response`, error.
// It is the callers responsibility to close the response body
func (check *httpCheck) fetchURL() (*http.Response, error) {
req, err := http.NewRequest(check.config.Method, check.config.URL, check.config.Body)
req, err := http.NewRequest(check.config.Method, check.config.URL, bytes.NewBuffer(check.payload))
if err != nil {
return nil, errors.Errorf("unable to create check HTTP request: %v", err)
}
Expand Down
37 changes: 34 additions & 3 deletions checks/http_test.go
Expand Up @@ -2,6 +2,7 @@ package checks

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -89,7 +90,14 @@ func TestNewHttpCheck(t *testing.T) {
}

rw.WriteHeader(200)
_, err := rw.Write([]byte(expectedContent))

reqBody, _ := ioutil.ReadAll(req.Body)
responsePayload := expectedContent
if len(reqBody) != 0 {
responsePayload = string(reqBody)
}

_, err := rw.Write([]byte(responsePayload))
if err != nil {
t.Fatal("Failed to write response: ", err)
}
Expand All @@ -98,8 +106,9 @@ func TestNewHttpCheck(t *testing.T) {
defer server.Close()

t.Run("HttpCheck success call", testHTTPCheckSuccess(server.URL, server.Client()))
t.Run("HttpCheck success call with body check", testHTTPCheckSuccessWithExpectedBody(server.URL, server.Client()))
t.Run("HttpCheck success call with failing body check", testHTTPCheckFailWithUnexpectedBody(server.URL, server.Client()))
t.Run("HttpCheck success call with expected body check", testHTTPCheckSuccessWithExpectedBody(server.URL, server.Client()))
t.Run("HttpCheck success call with POST body payload", testHTTPCheckSuccessWithPostBodyPayload(server.URL, server.Client()))
t.Run("HttpCheck success call with failing expected body check", testHTTPCheckFailWithUnexpectedBody(server.URL, server.Client()))
t.Run("HttpCheck success call with options", testHTTPCheckSuccessWithOptions(server.URL, server.Client(), &receivedDetails))
t.Run("HttpCheck fail on status code", testHTTPCheckFailStatusCode(server.URL, server.Client()))
t.Run("HttpCheck fail on URL", testHTTPCheckFailURL(server.URL, server.Client()))
Expand Down Expand Up @@ -137,6 +146,28 @@ func testHTTPCheckSuccessWithExpectedBody(url string, client *http.Client) func(
}
}

func testHTTPCheckSuccessWithPostBodyPayload(url string, client *http.Client) func(t *testing.T) {
return func(t *testing.T) {
const postPayload = "body-payload"

check, err := NewHTTPCheck(HTTPCheckConfig{
CheckName: "url.check",
URL: url,
Client: client,
ExpectedBody: postPayload,
Body: strings.NewReader(postPayload),
Method: http.MethodPost,
})
assert.Nil(t, err)

for i := 0; i < 5; i++ {
details, err := check.Execute()
assert.Nil(t, err, "check should pass")
assert.Equal(t, fmt.Sprintf("URL [%s] is accessible", url), details, "check should pass")
}
}
}

func testHTTPCheckFailWithUnexpectedBody(url string, client *http.Client) func(t *testing.T) {
return func(t *testing.T) {
check, err := NewHTTPCheck(HTTPCheckConfig{
Expand Down

0 comments on commit 4b8f7e1

Please sign in to comment.