-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
mocks_test.go
101 lines (80 loc) · 1.84 KB
/
mocks_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
package gorouter
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/valyala/fasthttp"
)
type testLogger struct {
t *testing.T
}
func (t testLogger) Printf(format string, args ...interface{}) {
t.t.Logf(format, args...)
}
type mockHandler struct {
served bool
}
func (mh *mockHandler) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {
mh.served = true
}
func (mh *mockHandler) HandleFastHTTP(_ *fasthttp.RequestCtx) {
mh.served = true
}
type mockFileSystem struct {
opened bool
}
func (mfs *mockFileSystem) Open(_ string) (http.File, error) {
mfs.opened = true
return nil, errors.New("")
}
func mockMiddleware(body string) MiddlewareFunc {
fn := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(body)); err != nil {
panic(err)
}
h.ServeHTTP(w, r)
})
}
return fn
}
func mockServeHTTP(h http.Handler, method, path string) error {
w := httptest.NewRecorder()
req, err := http.NewRequest(method, path, nil)
if err != nil {
return err
}
h.ServeHTTP(w, req)
return nil
}
func mockFastHTTPMiddleware(body string) FastHTTPMiddlewareFunc {
fn := func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
if _, err := fmt.Fprint(ctx, body); err != nil {
panic(err)
}
h(ctx)
}
}
return fn
}
func mockHandleFastHTTP(h fasthttp.RequestHandler, method, path string) error {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(method)
ctx.URI().SetPath(path)
h(ctx)
return nil
}
func checkIfHasRootRoute(t *testing.T, r interface{}, method string) {
switch v := r.(type) {
case *router:
case *fastHTTPRouter:
if rootRoute := v.tree.Find(method); rootRoute == nil {
t.Error("Route not found")
}
default:
t.Error("Unsupported type")
}
}