-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
49 lines (37 loc) · 920 Bytes
/
middleware_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
package mid
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
type requestCounterHandler struct{}
func (h requestCounterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// nil
}
func TestRequestCounter(t *testing.T) {
// Block until counter finishes
done := make(chan struct{})
counter := RequestCounter(time.Millisecond*10, func(n uint64, closer chan struct{}) {
if n != 2 {
t.Errorf("Invalid request count")
}
done <- struct{}{}
closer <- struct{}{}
})
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := &requestCounterHandler{}
mux := http.NewServeMux()
mux.Handle("/", counter(handler))
mux.ServeHTTP(rr, req)
mux.ServeHTTP(rr, req)
// router := httprouter.New()
// router.HandlerFunc("GET", "/", counter(handler))
// router.ServeHTTP(rr, req)
// router.ServeHTTP(rr, req)
<-done
}