-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutes_test.go
77 lines (67 loc) · 1.95 KB
/
routes_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
package channelz
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockHandler struct {
}
func (m *mockHandler) WriteTopChannelsPage(w io.Writer) {
// nolint:errcheck
w.Write([]byte("top"))
}
func (m *mockHandler) WriteChannelsPage(w io.Writer, c int64) {
// nolint:errcheck
w.Write([]byte(fmt.Sprintf("channels %d", c)))
}
func (m *mockHandler) WriteChannelPage(w io.Writer, c int64) {
// nolint:errcheck
w.Write([]byte(fmt.Sprintf("channel %d", c)))
}
func (m *mockHandler) WriteSubchannelPage(w io.Writer, c int64) {
// nolint:errcheck
w.Write([]byte(fmt.Sprintf("subchannel %d", c)))
}
func (m *mockHandler) WriteServerPage(w io.Writer, c int64) {
// nolint:errcheck
w.Write([]byte(fmt.Sprintf("server %d", c)))
}
func (m *mockHandler) WriteSocketPage(w io.Writer, c int64) {
// nolint:errcheck
w.Write([]byte(fmt.Sprintf("socket %d", c)))
}
func TestCreateRouter(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
r := createRouter("/channelz", &mockHandler{})
assert.NotNil(r)
ts := httptest.NewServer(r)
defer ts.Close()
expects := map[string]string{
"/channelz": "top",
"/channelz/channel/4": "channel 4",
"/channelz/channels?start=4": "channels 4",
"/channelz/subchannel/5": "subchannel 5",
"/channelz/server/3": "server 3",
"/channelz/socket/3": "socket 3",
// Non matched or errornous paths
"/channelz/channel/x": "",
"/channelz/subchannel/x": "",
"/channelz/server/x": "",
"/channelx": "404 page not found\n",
}
for route, expected := range expects {
res, err := http.Get(ts.URL + route)
require.NoError(err)
response, err := ioutil.ReadAll(res.Body)
require.NoError(err)
res.Body.Close()
assert.Equal([]byte(expected), response, "For path %s the expected result was %q, but instead we got %q",
route, expected, response)
}
}