From 7ef117f5c0f402475bb4bac544217301f378253c Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 7 Sep 2017 17:12:58 -0300 Subject: [PATCH] added tests --- main_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 main_test.go diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..0d16f5e --- /dev/null +++ b/main_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/http/httptest" + "regexp" + "strconv" + "testing" + + "github.com/stretchr/testify/assert" +) + +var srv *httptest.Server + +func TestMain(m *testing.M) { + srv = httptest.NewServer(http.HandlerFunc(probeHandler)) + defer srv.Close() + m.Run() +} + +func TestSuccessQueries(t *testing.T) { + var re = regexp.MustCompile("(?m)^domain_expiry_days ([0-9]+)$") + for _, domain := range []string{ + "carlosbecker.com", + "carinebecker.com", + "watchub.pw", + "google.com", + } { + t.Run(domain, func(t *testing.T) { + var assert = assert.New(t) + var body = req(t, domain, http.StatusOK) + var results = re.FindStringSubmatch(string(body)) + assert.Len(results, 2, "should have returned the metric in the body of the response") + days, err := strconv.Atoi(results[1]) + assert.NoError(err) + assert.True(days > 0, "domain must not be expired") + }) + } +} + +func TestTargetNotProvided(t *testing.T) { + req(t, "", http.StatusBadRequest) +} + +func TestTargetDoesntExist(t *testing.T) { + req(t, "this-domain-should-not-exist.blah", http.StatusBadRequest) +} + +func req(t *testing.T, domain string, expectedStatus int) string { + var assert = assert.New(t) + resp, err := http.Get(fmt.Sprintf("%s?target=%s", srv.URL, domain)) + assert.NoError(err) + assert.Equal(expectedStatus, resp.StatusCode) + body, err := ioutil.ReadAll(resp.Body) + assert.NoError(err) + return string(body) +}