-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_test.go
55 lines (46 loc) · 1.07 KB
/
route_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
package router_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gostalt/router"
)
func TestRouteInference(t *testing.T) {
rtr := router.New()
rtr.Get("/handler", testHandler)
rtr.Get("func", testFunc)
server := httptest.NewServer(rtr)
defer server.Close()
resp, err := http.Get(server.URL + "/handler")
if err != nil {
t.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
expected := "handler"
if string(body) != expected {
t.Errorf("Got %s, wanted %s.", string(body), expected)
}
resp, err = http.Get(server.URL + "/func")
if err != nil {
t.Fatal(err)
}
body, _ = ioutil.ReadAll(resp.Body)
expected = "func"
if string(body) != expected {
t.Errorf("Got %s, wanted %s.", string(body), expected)
}
}
type stringHandler struct{}
func (stringHandler) String() string {
return "Test"
}
var (
testStringer = stringHandler{}
testHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("handler"))
})
testFunc = func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("func"))
}
)