-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_test.go
48 lines (42 loc) · 1021 Bytes
/
probe_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestProbeHandler(t *testing.T) {
ts := httptest.NewServer(http.NotFoundHandler())
t.Cleanup(ts.Close)
for _, tc := range []struct {
name string
target string
wantStatusCode int
}{
{
name: "no parameters",
target: "/",
wantStatusCode: http.StatusBadRequest,
},
{
name: "bad target",
target: "/?target=://",
wantStatusCode: http.StatusBadRequest,
},
{
name: "unhealthy target",
target: "/?target=" + ts.URL,
wantStatusCode: http.StatusInternalServerError,
},
} {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tc.target, nil)
w := httptest.NewRecorder()
probeHandler(w, req, time.Minute)
if diff := cmp.Diff(tc.wantStatusCode, w.Result().StatusCode); diff != "" {
t.Errorf("Status code diff (-want +got):\n%s", diff)
}
})
}
}