-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_test.go
63 lines (49 loc) · 2.12 KB
/
status_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
package main
import (
"errors"
"net/http"
"testing"
"time"
)
type ResponseMock struct {
WriteCallback func([]byte) (int, error)
}
func (r *ResponseMock) Header() http.Header {
return http.Header{}
}
func (r *ResponseMock) WriteHeader(status int) {}
func (r *ResponseMock) Write(body []byte) (int, error) {
return r.WriteCallback(body)
}
func TestRecordAndServe(t *testing.T) {
called := 0
fixture := `{"slowest":{"a request":[{"latency":123.3,"status":200,"response":{"ok":"yes?"}},{"latency":1.123,"status":200,"response":{"ok":"yes?"}}],"another request":[{"latency":333,"status":200,"response":{"ok":"yes?"}},{"latency":93.1,"status":200,"response":{"ok":"yes?"}},{"latency":12.3,"status":200,"response":{"ok":"yes?"}}]},"errors":{"a request":[],"another request":[{"latency":12.3,"status":200,"response":{"ok":"yes?"},"error":"some error"},{"latency":12.3,"status":200,"response":{"ok":"yes?"},"error":"some other error"}]}}`
res := ResponseMock{
WriteCallback: func(body []byte) (int, error) {
called++
if string(body) != fixture {
t.Errorf("Body didn't match\n%s", string(body))
}
return 0, nil
},
}
status := NewStatus()
status.Record("a request", 1.123, 200, `{"ok":"yes?"}`, nil)
status.Record("a request", 123.3, 200, `{"ok":"yes?"}`, nil)
status.Record("another request", 12.3, 200, `{"ok":"yes?"}`, nil)
status.Record("another request", 93.1, 200, `{"ok":"yes?"}`, nil)
status.Record("another request", 12.3, 200, `{"ok":"yes?"}`, nil)
status.Record("another request", 333.0, 200, `{"ok":"yes?"}`, nil)
status.Record("another request", 12.3, 200, `{"ok":"yes?"}`, errors.New("error gone"))
status.Record("another request", 12.3, 200, `{"ok":"yes?"}`, errors.New("an error"))
status.Record("another request", 12.3, 200, `{"ok":"yes?"}`, errors.New("some other error"))
status.Record("another request", 12.3, 200, `{"ok":"yes?"}`, errors.New("some error"))
time.Sleep(time.Millisecond * 100)
if len(status.Responses) > 0 {
t.Error("Record responses took too long, more than 100 ms")
}
status.ServeHTTP(&res, &http.Request{})
if called != 1 {
t.Error("Response writer Write wasn't called once")
}
}