This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdispatch_test.go
124 lines (98 loc) · 2.63 KB
/
dispatch_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package dispatch
import (
"bytes"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/lunny/log"
"github.com/lunny/tango"
"github.com/unknwon/macaron"
)
func TestDispatch(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
l := log.Std
l.SetOutputLevel(log.Ldebug)
t1 := tango.NewWithLog(l, tango.Logging())
t1.Get("/", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("tango 1"))
})
t2 := tango.NewWithLog(l, tango.Logging())
t2.Get("/", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("tango 2"))
})
dispatch := New(map[string]Handler{
"/": t1,
})
dispatch.Add("/api/", t2)
t3 := tango.NewWithLog(l)
t3.Use(dispatch)
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
t3.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "tango 1")
req, err = http.NewRequest("GET", "http://localhost:8000/api/", nil)
if err != nil {
t.Error(err)
}
buff.Reset()
t3.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "tango 2")
}
func TestDispatch2(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
l := log.Std
l.SetOutputLevel(log.Ldebug)
t1 := tango.NewWithLog(l, tango.Logging())
t1.Get("/", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("tango 1"))
})
m := macaron.Classic()
m.Get("/", func(ctx *macaron.Context) string {
return "macaron"
})
dispatch := New(map[string]Handler{
"/": t1,
})
dispatch.Add("/api/", m)
t3 := tango.NewWithLog(l)
t3.Use(dispatch)
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
t3.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "tango 1")
req, err = http.NewRequest("GET", "http://localhost:8000/api/", nil)
if err != nil {
t.Error(err)
}
buff.Reset()
t3.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "macaron")
}
/* Test Helpers */
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func refute(t *testing.T, a interface{}, b interface{}) {
if a == b {
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}