45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package probe
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/tls"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"tea.karlcom.de/karl/bucketeer/internal/model"
|
||
|
|
)
|
||
|
|
|
||
|
|
func newProber(t *testing.T, status int) (*Prober, string) {
|
||
|
|
t.Helper()
|
||
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
w.WriteHeader(status)
|
||
|
|
}))
|
||
|
|
t.Cleanup(srv.Close)
|
||
|
|
p := New(Config{Workers: 1, Timeout: time.Second})
|
||
|
|
// httptest uses a self-signed cert; the prober already skips verification.
|
||
|
|
_ = tls.Config{}
|
||
|
|
host := strings.TrimPrefix(srv.URL, "https://")
|
||
|
|
return p, host
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestProbeClassification(t *testing.T) {
|
||
|
|
cases := []struct {
|
||
|
|
status int
|
||
|
|
want model.ExposureState
|
||
|
|
}{
|
||
|
|
{http.StatusOK, model.ExposurePublicRead},
|
||
|
|
{http.StatusForbidden, model.ExposureAuthRequired},
|
||
|
|
{http.StatusUnauthorized, model.ExposureAuthRequired},
|
||
|
|
{http.StatusNotFound, model.ExposureNotFound},
|
||
|
|
{http.StatusInternalServerError, model.ExposureReachable},
|
||
|
|
}
|
||
|
|
for _, c := range cases {
|
||
|
|
p, host := newProber(t, c.status)
|
||
|
|
got := p.probe(t.Context(), model.Candidate{Hostname: host})
|
||
|
|
if got != c.want {
|
||
|
|
t.Errorf("status %d: got %s, want %s", c.status, got, c.want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|