forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
73 lines (66 loc) · 1.79 KB
/
handlers_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package api
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/linkerd/linkerd2/controller/k8s"
"github.com/sirupsen/logrus"
)
func TestHandleTap(t *testing.T) {
expectations := []struct {
req *http.Request
params httprouter.Params
code int
header http.Header
body string
}{
{
req: &http.Request{
URL: &url.URL{
Path: "/apis",
},
},
code: http.StatusBadRequest,
header: http.Header{"Content-Type": []string{"application/json"}},
body: `{"error":"invalid path: /apis"}`,
},
{
req: &http.Request{
URL: &url.URL{
Path: "/apis/tap.linkerd.io/v1alpha1/watch/namespaces/foo/tap",
},
},
code: http.StatusForbidden,
header: http.Header{"Content-Type": []string{"application/json"}},
body: `{"error":"tap authorization failed (not authorized to access namespaces.tap.linkerd.io), visit https://linkerd.io/tap-rbac for more information"}`,
},
}
for i, exp := range expectations {
exp := exp // pin
t.Run(fmt.Sprintf("%d handle the tap request", i), func(t *testing.T) {
k8sAPI, err := k8s.NewFakeAPI()
if err != nil {
t.Fatalf("NewFakeAPI returned an error: %s", err)
}
h := &handler{
k8sAPI: k8sAPI,
log: logrus.WithField("test", t.Name()),
}
recorder := httptest.NewRecorder()
h.handleTap(recorder, exp.req, exp.params)
if recorder.Code != exp.code {
t.Errorf("Unexpected code: %d, expected: %d", recorder.Code, exp.code)
}
if !reflect.DeepEqual(recorder.Header(), exp.header) {
t.Errorf("Unexpected header: %v, expected: %v", recorder.Header(), exp.header)
}
if recorder.Body.String() != exp.body {
t.Errorf("Unexpected body: %s, expected: %s", recorder.Body.String(), exp.body)
}
})
}
}