-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_test.go
65 lines (51 loc) · 1.36 KB
/
api_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
package sapi
import (
"github.com/gorilla/mux"
"net/http"
"testing"
)
func foo(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("foo"))
}
var newRequestHost = func(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
return req
}
func TestMakeEndpoints(t *testing.T) {
Regist(MakeApiEndpoints("/foo", "GET", http.HandlerFunc(foo)))
req := newRequestHost("GET", "http://example.com/foo")
var match mux.RouteMatch
ok := gRouter.Path("/foo").Match(req, &match)
if !ok {
t.Errorf("Expected url match http://example.com/foo, but false")
}
Regist(MakeApiEndpoints("/bar", "POST", http.HandlerFunc(foo)))
req = newRequestHost("GET", "http://example.com/bar0")
ok = gRouter.Path("/bar").Match(req, &match)
if ok {
t.Errorf("Unxpected url match http://example.com/bar, but true")
}
}
type Foo struct {
s []byte
}
func (f *Foo) Get(w http.ResponseWriter, r *http.Request) {
w.Write(f.s)
}
func (f *Foo) Mapper() ApiEndpoints {
e := make(ApiEndpoints)
e["GET"] = map[string]http.Handler{"/foo": http.HandlerFunc(f.Get)}
return e
}
func TestApiInterface(t *testing.T) {
Regist(new(Foo))
req := newRequestHost("GET", "http://example.com/foo")
var match mux.RouteMatch
ok := gRouter.Path("/foo").Match(req, &match)
if !ok {
t.Errorf("Expected url match http://example.com/foo, but false")
}
}