-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
68 lines (50 loc) · 1.46 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
65
66
67
68
package dumbdumb
import (
"testing"
)
type MockRequest struct {
Payload string
}
func (r MockRequest) GetPayload() string { return r.Payload }
func (r MockRequest) SendOutput(output string) error { return nil }
type MockHandler struct {
Name string
}
func (h MockHandler) HandleRequest(request Request) error { return nil }
func TestAddHandler(t *testing.T) {
server := NewServer()
server.AddHandler("requesttype1.*", MockHandler{})
server.AddHandler("requesttype2.*", MockHandler{})
if len(server.handlers) != 2 {
t.Error("Expected to have 2 handlers added to server, had",
len(server.handlers))
}
}
func TestRouteRequest(t *testing.T) {
server := NewServer()
h1 := MockHandler{Name: "handler1"}
h2 := MockHandler{Name: "handler2"}
server.AddHandler("requesttype1.*", h1)
server.AddHandler("requesttype2.*", h2)
h, err := server.RouteRequest("requesttype1 blah blah")
if err != nil {
t.Fatal("Unexpected error routing request:", err)
}
if (*h).(MockHandler).Name != h1.Name {
t.Error("Routed to wrong handler")
}
}
type PanickingHandler struct{}
func (h PanickingHandler) HandleRequest(request Request) error {
panic("I fail")
}
func TestPanicRecovery(t *testing.T) {
server := NewServer()
server.AddHandler("requesttype1.*", PanickingHandler{})
defer func() {
if r := recover(); r != nil {
t.Fatal("Server request handling panicked without recovering")
}
}()
server.HandleRequest(MockRequest{Payload: "requesttype1 blah blah"})
}