-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
131 lines (122 loc) · 3.75 KB
/
server_test.go
File metadata and controls
131 lines (122 loc) · 3.75 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package api
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNewServer(t *testing.T) {
s := NewServer()
if s == nil {
t.Fatal("NewServer() returned nil")
}
}
func TestCheckHandler(t *testing.T) {
s := NewServer()
if s == nil {
t.Fatal("NewServer() returned nil")
}
shouldPanic := func(h any) {
defer func() {
if recover() == nil {
t.Errorf("Handle() did not panic when handler's type is %T", h)
}
}()
_ = s.Handler(h)
}
shouldNotPanic := func(h any) {
defer func() {
if x := recover(); x != nil {
t.Errorf("Handle() panic'ed with type %T when it should not have: %v", h, x)
}
}()
_ = s.Handler(h)
}
// The second argument to Handle (the handler) should be a function:
shouldPanic(nil)
shouldPanic(3)
shouldPanic("")
// Function must not be nil:
var f func(*http.Request) (any, error)
shouldPanic(f)
// If not an ordinary HTTP handler, the function should have 1 or 2 input arguments:
shouldPanic(func() {})
shouldPanic(func() (string, error) { return "", nil })
shouldPanic(func(*http.Request, int, int) {})
shouldPanic(func(*http.Request, int, int) (string, error) { return "", nil })
// The first argument must be a *http.Request:
shouldPanic(func(int) (string, error) { return "", nil })
shouldPanic(func(string) (string, error) { return "", nil })
// There must be 2 return values:
shouldPanic(func(*http.Request) int { return 0 })
shouldPanic(func(*http.Request, int) int { return 0 })
shouldPanic(func(*http.Request) (int, int, error) { return 0, 0, nil })
shouldPanic(func(*http.Request, int) (int, int, error) { return 0, 0, nil })
// Second return value must be error:
shouldPanic(func(*http.Request) (int, int) { return 0, 0 })
shouldPanic(func(*http.Request, int) (int, int) { return 0, 0 })
shouldPanic(func(*http.Request) (int, string) { return 0, "" })
shouldPanic(func(*http.Request, int) (int, string) { return 0, "" })
shouldPanic(func(*http.Request) (int, any) { return 0, nil })
shouldPanic(func(*http.Request, int) (int, any) { return 0, nil })
// These should not panic:
shouldNotPanic(http.NotFoundHandler()) // ordinary HTTP handler
shouldNotPanic(func(w http.ResponseWriter, r *http.Request) {}) // ordinary HTTP handler func
shouldNotPanic(func(*http.Request, any) (any, error) { return nil, nil })
shouldNotPanic(func(*http.Request) (any, error) { return nil, nil })
}
func TestInputFields(t *testing.T) {
s := NewServer()
s.Handle("/foo", func(r *http.Request, in struct {
I int
S string
}) (any, error) {
m := InputFields(r)
if in.I != int(m["i"].(float64)) {
t.Fatalf("in.I=%d m[i]=%v\n", in.I, m["i"])
}
if in.S != m["s"] {
t.Fatalf("in.S=%s m[s]=%v\n", in.S, m["s"])
}
return nil, nil
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/foo", bytes.NewReader([]byte(`{"i":23,"s":"string"}`)))
s.ServeHTTP(w, r)
}
func TestServer(t *testing.T) {
s := NewServer()
s.Handle("/foo", func(r *http.Request) (any, error) {
t.Fatal("function called but perm function failed")
return nil, nil
}, func(r *http.Request) bool {
return false
})
bar := false
s.Handle("/bar", func(r *http.Request) (any, error) {
bar = true
return nil, nil
}, func(r *http.Request) bool {
return true
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/foo", nil)
s.ServeHTTP(w, r)
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/bar", nil)
s.ServeHTTP(w, r)
time.Sleep(time.Second)
if !bar {
t.Fatal("function not called when perm function succeeded")
}
s.SetDenyHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bar = false
}))
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/foo", nil)
s.ServeHTTP(w, r)
if bar {
t.Fatal("denyHandler was not called")
}
}