-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouprouter_test.go
95 lines (83 loc) · 2.84 KB
/
grouprouter_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
package r2router
import (
"github.com/stretchr/testify/assert"
//"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestRouterGroup(t *testing.T) {
router := NewRouter()
router.Group("/user/keys", func(r *GroupRouter) {
r.Get("/", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("GET:/user/keys"))
})
r.Post("/", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("POST:/user/keys"))
})
r.Put("/:id", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("PUT:/user/keys/:id," + p.Get("id")))
})
r.Delete("/:id", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("DELETE:/user/keys/:id," + p.Get("id")))
})
r.Get("/:id", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("GET:/user/keys/:id," + p.Get("id")))
})
r.Head("/:id", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("HEAD:/user/keys/:id," + p.Get("id")))
})
r.Patch("/:id", func(w http.ResponseWriter, r *http.Request, p Params) {
w.Write([]byte("PATCH:/user/keys/:id," + p.Get("id")))
})
})
ts := httptest.NewServer(router)
defer ts.Close()
// get
res, err := http.Get(ts.URL + "/user/keys/testing")
assert.Nil(t, err)
content, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, string(content), "GET:/user/keys/:id,testing")
// Get all
res, err = http.Get(ts.URL + "/user/keys")
assert.Nil(t, err)
content, err = ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, string(content), "GET:/user/keys")
// post
res, err = http.Post(ts.URL+"/user/keys", "", nil)
assert.Nil(t, err)
content, err = ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.Equal(t, string(content), "POST:/user/keys")
// put
client := &http.Client{}
req, err := http.NewRequest("PUT", ts.URL+"/user/keys/testing", nil)
res, err = client.Do(req)
content, err = ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Equal(t, string(content), "PUT:/user/keys/:id,testing")
// delete
req, err = http.NewRequest("DELETE", ts.URL+"/user/keys/testing", nil)
res, err = client.Do(req)
content, err = ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Equal(t, string(content), "DELETE:/user/keys/:id,testing")
req, err = http.NewRequest("HEAD", ts.URL+"/user/keys/testing", nil)
res, err = client.Do(req)
content, err = ioutil.ReadAll(res.Body)
res.Body.Close()
// http seems not sending content
assert.Equal(t, int(res.ContentLength), len([]byte("HEAD:/user/keys/:id,testing")))
req, err = http.NewRequest("PATCH", ts.URL+"/user/keys/testing", nil)
res, err = client.Do(req)
content, err = ioutil.ReadAll(res.Body)
res.Body.Close()
// http seems not sending content
assert.Equal(t, int(res.ContentLength), len([]byte("PATCH:/user/keys/:id,testing")))
}