forked from goji/goji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
71 lines (60 loc) · 1.22 KB
/
util_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
package goji
import (
"net/http"
"net/http/httptest"
"strings"
"goji.io/internal"
"golang.org/x/net/context"
)
type boolPattern bool
func (b boolPattern) Match(ctx context.Context, _ *http.Request) context.Context {
if b {
return ctx
}
return nil
}
type testPattern struct {
index int
mark *int
methods []string
prefix string
}
func (t testPattern) Match(ctx context.Context, r *http.Request) context.Context {
if t.index < *t.mark {
return nil
}
path := ctx.Value(internal.Path).(string)
if !strings.HasPrefix(path, t.prefix) {
return nil
}
if t.methods != nil {
if _, ok := t.HTTPMethods()[r.Method]; !ok {
return nil
}
}
return ctx
}
func (t testPattern) PathPrefix() string {
return t.prefix
}
func (t testPattern) HTTPMethods() map[string]struct{} {
if t.methods == nil {
return nil
}
m := make(map[string]struct{})
for _, method := range t.methods {
m[method] = struct{}{}
}
return m
}
type intHandler int
func (i intHandler) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) {
}
func wr() (*httptest.ResponseRecorder, *http.Request) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
panic(err)
}
return w, r
}