-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
64 lines (59 loc) · 2.15 KB
/
server_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
package api
import (
"net/http"
"testing"
)
func TestNewServer(t *testing.T) {
s := NewServer()
if s == nil {
t.Fatal("NewServer() returned nil")
}
}
func TestHandler(t *testing.T) {
shouldPanic := func(h any) {
defer func() {
if recover() == nil {
t.Errorf("Handle() did not panic when handler's type is %T", h)
}
}()
_ = 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)
}
}()
_ = Handler(h)
}
// The second argument to Handle (the handler) should be a function:
shouldPanic(3)
shouldPanic("")
// Function must not be nil:
var f func(*http.Request) (any, error)
shouldPanic(f)
// If not an ordinary HYP 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, error, int) { return 0, nil, 0 })
shouldPanic(func(*http.Request, int) (int, error, int) { return 0, nil, 0 })
// 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(func(w http.ResponseWriter, r *http.Request) {}) // ordinary HTTP handler
shouldNotPanic(func(*http.Request, any) (any, error) { return nil, nil })
shouldNotPanic(func(*http.Request) (any, error) { return nil, nil })
}