-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test.go
147 lines (131 loc) · 3.41 KB
/
http_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package mecachis
import (
"fmt"
"github.com/sonirico/mecachis/engines"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type state struct {
group string
key string
value string
}
type action struct {
endpoint string
method string
payload string
}
type testCase struct {
name string
action *action
want string
wantStatus int
state *state
}
func prepareHub(t *testing.T, handler http.Handler, actions []action) {
t.Helper()
for _, action := range actions {
req := httptest.NewRequest(action.method, action.endpoint, strings.NewReader(action.payload))
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code < http.StatusOK || recorder.Code > 299 {
t.Errorf("unexpected status code. want ok, have %d", recorder.Code)
}
}
}
func TestHub_ServeHTTP(t *testing.T) {
tests := []testCase{
{
name: "with non-existent group and key",
action: &action{
endpoint: "/mecachis/myapp/key",
method: http.MethodGet,
},
want: "404 page not found",
wantStatus: http.StatusNotFound,
},
{
name: "with existent group and key",
action: &action{
endpoint: "/mecachis/monitoring/sla",
method: http.MethodGet,
},
want: "perfdata: 100%",
wantStatus: http.StatusOK,
state: &state{group: "monitoring", key: "sla", value: "perfdata: 100%"},
},
{
name: "with a wrongly prefixed uri",
action: &action{
endpoint: "/unknownnamespace/g/k",
method: http.MethodGet,
},
want: "404 page not found",
wantStatus: http.StatusNotFound,
},
{
name: "with a wrong number of path params ('key' is missing)",
action: &action{
endpoint: "/mecachis/myapp/",
method: http.MethodGet,
},
want: "404 page not found",
wantStatus: http.StatusNotFound,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
action := test.action
request := httptest.NewRequest(action.method, action.endpoint, strings.NewReader(action.payload))
responseRecorder := httptest.NewRecorder()
hub := NewHub()
if test.state != nil {
g, _ := hub.getOrCreateGroup(test.state.group)
_ = g.Add(test.state.key, MemoryView(test.state.value))
}
hub.ServeHTTP(responseRecorder, request)
if responseRecorder.Code != test.wantStatus {
t.Errorf("unexpected status code. want %d, have %d",
test.wantStatus, responseRecorder.Code)
}
cleanedBody := strings.TrimSpace(responseRecorder.Body.String())
if cleanedBody != test.want {
t.Errorf("unexpected response body. want '%s', have '%s'",
test.want, cleanedBody)
}
})
}
}
func TestHub_ServeHTTP_LRU_engine_eviction(t *testing.T) {
var capacity uint64 = 14
actions := []action{
{
method: http.MethodPost,
endpoint: fmt.Sprintf("/mecachis/metrics/mem?engi=lru&cap=%d", capacity),
payload: "13gb", // +7
},
{
method: http.MethodPost,
endpoint: "/mecachis/metrics/ping",
payload: "10ms", // +8
},
}
hub := NewHub()
prepareHub(t, hub, actions)
group, ok := hub.group("metrics")
if !ok {
t.Errorf("want group, have none")
}
if group.Ct != engines.LRU {
t.Errorf("unexpected engine type. want LRU, have '%v'", group.Ct)
}
if group.Cap != capacity {
t.Errorf("unexpected cache capacity. want %d, have %d", capacity, group.Cap)
}
val, ok := group.Get("mem")
if ok {
t.Errorf("unexpected cache result. expected eviction, have '%s'", val.String())
}
}