-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_middleware_test.go
60 lines (45 loc) · 1.19 KB
/
router_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
50
51
52
53
54
55
56
57
58
59
60
package router
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/donseba/go-router/middleware"
)
func TestRecover(t *testing.T) {
t.Run("Recover from panic", func(t *testing.T) {
mux := http.NewServeMux()
r := New(mux, "Example API", "1.0.0")
r.Use(middleware.Recover)
r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("Panic!")
})
ts := httptest.NewServer(r)
defer ts.Close()
res, err := http.Get(ts.URL + "/panic")
if err != nil {
t.Error(err)
}
if res.StatusCode != http.StatusInternalServerError {
t.Errorf("Expected status code %d, got %d", http.StatusInternalServerError, res.StatusCode)
}
})
}
func TestTimer(t *testing.T) {
t.Run("Timer middleware", func(t *testing.T) {
mux := http.NewServeMux()
r := New(mux, "Example API", "1.0.0")
r.Use(middleware.Timer)
r.Get("/timer", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
ts := httptest.NewServer(r)
defer ts.Close()
res, err := http.Get(ts.URL + "/timer")
if err != nil {
t.Error(err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, res.StatusCode)
}
})
}