-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
main_test.go
59 lines (51 loc) · 1.38 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)
}