2021-09-29 15:38:50 +00:00
|
|
|
package checkers_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-11-01 21:13:03 +00:00
|
|
|
"io"
|
2021-09-29 15:38:50 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-11-01 21:13:03 +00:00
|
|
|
|
2023-02-23 17:49:45 +00:00
|
|
|
"gh.tarampamp.am/error-pages/internal/checkers"
|
2021-09-29 15:38:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type httpClientFunc func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (f httpClientFunc) Do(req *http.Request) (*http.Response, error) { return f(req) }
|
|
|
|
|
|
|
|
func TestHealthChecker_CheckSuccess(t *testing.T) {
|
|
|
|
var httpMock httpClientFunc = func(req *http.Request) (*http.Response, error) {
|
|
|
|
assert.Equal(t, http.MethodGet, req.Method)
|
2022-01-27 12:29:49 +00:00
|
|
|
assert.Equal(t, "http://127.0.0.1:123/healthz", req.URL.String())
|
2021-09-29 15:38:50 +00:00
|
|
|
assert.Equal(t, "HealthChecker/internal", req.Header.Get("User-Agent"))
|
|
|
|
|
|
|
|
return &http.Response{
|
2022-11-01 21:13:03 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte{})),
|
2021-09-29 15:38:50 +00:00
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
checker := checkers.NewHealthChecker(context.Background(), httpMock)
|
|
|
|
|
|
|
|
assert.NoError(t, checker.Check(123))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthChecker_CheckFail(t *testing.T) {
|
|
|
|
var httpMock httpClientFunc = func(req *http.Request) (*http.Response, error) {
|
|
|
|
return &http.Response{
|
2022-11-01 21:13:03 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte{})),
|
2021-09-29 15:38:50 +00:00
|
|
|
StatusCode: http.StatusBadGateway,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
checker := checkers.NewHealthChecker(context.Background(), httpMock)
|
|
|
|
|
|
|
|
err := checker.Check(123)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "wrong status code")
|
|
|
|
}
|