-
Notifications
You must be signed in to change notification settings - Fork 24
/
endpoint_test.go
154 lines (131 loc) · 5.05 KB
/
endpoint_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package flagz
import (
"encoding/json"
"net/http"
"net/http/httptest"
"sort"
"testing"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type endpointTestSuite struct {
suite.Suite
flagSet *flag.FlagSet
endpoint *StatusEndpoint
}
func TestEndpointTestSuite(t *testing.T) {
suite.Run(t, &endpointTestSuite{})
}
func (s *endpointTestSuite) SetupTest() {
s.flagSet = flag.NewFlagSet("foobar", flag.ContinueOnError)
s.endpoint = NewStatusEndpoint(s.flagSet)
s.flagSet.String("some_static_string", "trolololo", "Some static string text")
s.flagSet.Float32("some_static_float", 3.14, "Some static int text")
DynStringSlice(s.flagSet, "some_dyn_stringslice", []string{"foo", "bar"}, "Some dynamic slice text")
DynJSON(s.flagSet, "some_dyn_json", &testJSON{SomeString: "foo", SomeInt: 1337}, "Some dynamic JSON text")
// Mark one static and one dynamic flag as changed.
s.flagSet.Set("some_static_string", "yolololo")
s.flagSet.Set("some_dyn_stringslice", "car,star")
}
func (s *endpointTestSuite) TestReturnsAll() {
req, _ := http.NewRequest("GET", "/debug/flagz", nil)
list := s.processFlagSetJSONResponse(req)
s.assertListContainsOnly([]string{"some_static_string", "some_static_float", "some_dyn_stringslice", "some_dyn_json"}, list)
}
func (s *endpointTestSuite) TestReturnsOnlyChanged() {
req, _ := http.NewRequest("GET", "/debug/flagz?only_changed=true", nil)
list := s.processFlagSetJSONResponse(req)
s.assertListContainsOnly([]string{"some_static_string", "some_dyn_stringslice"}, list)
}
func (s *endpointTestSuite) TestReturnsOnlyStatic() {
req, _ := http.NewRequest("GET", "/debug/flagz?type=static", nil)
list := s.processFlagSetJSONResponse(req)
s.assertListContainsOnly([]string{"some_static_string", "some_static_float"}, list)
}
func (s *endpointTestSuite) TestReturnsOnlyDynamic() {
req, _ := http.NewRequest("GET", "/debug/flagz?type=dynamic", nil)
list := s.processFlagSetJSONResponse(req)
s.assertListContainsOnly([]string{"some_dyn_stringslice", "some_dyn_json"}, list)
}
func (s *endpointTestSuite) TestReturnsOnlyDynamicAndChanged() {
req, _ := http.NewRequest("GET", "/debug/flagz?type=dynamic&only_changed=true", nil)
list := s.processFlagSetJSONResponse(req)
s.assertListContainsOnly([]string{"some_dyn_stringslice"}, list)
}
func (s *endpointTestSuite) TestReturnsOnlyStaticAndChanged() {
req, _ := http.NewRequest("GET", "/debug/flagz?type=static&only_changed=true", nil)
list := s.processFlagSetJSONResponse(req)
s.assertListContainsOnly([]string{"some_static_string"}, list)
}
func (s *endpointTestSuite) TestCorrectlyRepresentsResources() {
req, _ := http.NewRequest("GET", "/debug/flagz", nil)
list := s.processFlagSetJSONResponse(req)
assert.Equal(s.T(),
&flagJSON{
Name: "some_static_float",
Description: "Some static int text",
CurrentValue: "3.14",
DefaultValue: "3.14",
IsChanged: false,
IsDynamic: false,
},
findFlagInFlagSetJSON("some_static_float", list),
"must correctly represent a static unchanged flag",
)
assert.Equal(s.T(),
&flagJSON{
Name: "some_dyn_stringslice",
Description: "Some dynamic slice text",
CurrentValue: "[car star]",
DefaultValue: "[foo bar]",
IsChanged: true,
IsDynamic: true,
},
findFlagInFlagSetJSON("some_dyn_stringslice", list),
"must correctly represent a dynamic changed flag",
)
}
func (s *endpointTestSuite) TestServesHTML() {
req, _ := http.NewRequest("GET", "/debug/flagz", nil)
req.Header.Add("Accept", "application/xhtml+xml")
resp := httptest.NewRecorder()
s.endpoint.ListFlags(resp, req)
require.Equal(s.T(), http.StatusOK, resp.Code, "flagz list request must return 200 OK")
require.Contains(s.T(), resp.Header().Get("Content-Type"), "html", "must indicate html in content type")
out := resp.Body.String()
assert.Contains(s.T(), out, "<html>")
assert.Contains(s.T(), out, "some_dyn_stringslice")
}
func (s *endpointTestSuite) processFlagSetJSONResponse(req *http.Request) *flagSetJSON {
resp := httptest.NewRecorder()
s.endpoint.ListFlags(resp, req)
require.Equal(s.T(), http.StatusOK, resp.Code, "flagz list request must return 200 OK")
require.Equal(s.T(), "application/json", resp.Header().Get("Content-Type"), "type must be indicated")
ret := &flagSetJSON{}
require.NoError(s.T(), json.Unmarshal(resp.Body.Bytes(), ret), "unmarshaling JSON response must succeed")
return ret
}
func (s *endpointTestSuite) assertListContainsOnly(flagList []string, list *flagSetJSON) {
existing := []string{}
for _, f := range list.Flags {
existing = append(existing, f.Name)
}
sort.Strings(flagList)
require.EqualValues(s.T(), flagList, existing, "expected set of listed flags must match")
}
func findFlagInFlagSetJSON(flagName string, list *flagSetJSON) *flagJSON {
for _, f := range list.Flags {
if f.Name == flagName {
return f
}
}
return nil
}
type testJSON struct {
SomeString string `json:"string"`
SomeInt int32 `json:"json"`
}