-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
75 lines (55 loc) · 1.56 KB
/
router_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
package goapi_test
import (
"context"
"net/http"
"testing"
"github.com/NaturalSelectionLabs/goapi"
"github.com/NaturalSelectionLabs/goapi/lib/middlewares"
"github.com/ysmood/got"
)
func TestMiddleware(t *testing.T) {
g := got.T(t)
r := goapi.NewRouter()
r.Use(middlewares.Func(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
rq = rq.WithContext(context.WithValue(rq.Context(), "middleware01", "ok")) //nolint: staticcheck
h.ServeHTTP(w, rq)
})
}))
r.Use(middlewares.Func(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
val := rq.Context().Value("middleware01").(string)
g.E(w.Write([]byte(val)))
})
}))
tr := g.Serve()
tr.Mux.Handle("/", r.ServerHandler())
g.Eq(g.Req("", tr.URL("/")).String(), "ok")
}
func TestMiddlewareNotFound(t *testing.T) {
g := got.T(t)
r := goapi.NewRouter()
tr := g.Serve()
tr.Mux.Handle("/", r.ServerHandler())
r.Use(middlewares.Func(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
h.ServeHTTP(w, rq)
})
}))
g.Eq(g.Req("", tr.URL("/x")).StatusCode, http.StatusNotFound)
}
func TestNotFound(t *testing.T) {
g := got.T(t)
r := goapi.New()
tr := g.Serve()
tr.Mux.Handle("/", r.Server())
g.Eq(g.Req("", tr.URL("/test")).StatusCode, http.StatusNotFound)
}
func setupRouter(g got.G, setup func(r *goapi.Group)) *got.Router {
g.Helper()
r := goapi.New()
tr := g.Serve()
setup(r)
tr.Mux.Handle("/", r.Server())
return tr
}