-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_test.go
163 lines (134 loc) · 4.37 KB
/
rest_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
155
156
157
158
159
160
161
162
163
package main
import (
"io/ioutil"
"net/http/httptest"
"net/url"
"testing"
"github.com/franela/goreq"
. "github.com/smartystreets/goconvey/convey"
)
func getTestURL(ts *httptest.Server, url string) string {
return ts.URL + url
}
func Test_RestGet(t *testing.T) {
Convey("Given a rest server is launched", t, func() {
fc, _ := NewFlagCache()
driver, _ := NewMemoryDriver()
driver.fc = fc
ts := httptest.NewServer(pennantRouter(fc, driver))
defer ts.Close()
Convey("flag list returns an empty list", func() {
res, err := goreq.Request{Uri: getTestURL(ts, "/flags")}.Do()
So(err, ShouldBeNil)
var flagList FlagListResponse
res.Body.FromJsonTo(&flagList)
So(flagList.Status, ShouldEqual, 200)
So(len(flagList.Flags), ShouldEqual, 0)
})
Convey("A flag can be saved", func() {
flagfile, err := ioutil.ReadFile("tests/data/flag1.json")
res, err := goreq.Request{
Uri: getTestURL(ts, "/flags"),
Method: "POST",
Body: string(flagfile)}.Do()
So(err, ShouldBeNil)
var flagItem FlagItemResponse
res.Body.FromJsonTo(&flagItem)
So(flagItem.Status, ShouldEqual, 200)
So(flagItem.Flag.Name, ShouldEqual, "red_button")
Convey("and then fetched", func() {
res, err := goreq.Request{
Uri: getTestURL(ts, "/flags/red_button")}.Do()
So(err, ShouldBeNil)
var flagItem FlagItemResponse
res.Body.FromJsonTo(&flagItem)
So(flagItem.Status, ShouldEqual, 200)
So(flagItem.Flag.Name, ShouldEqual, "red_button")
})
Convey("and then listed", func() {
res, err := goreq.Request{
Uri: getTestURL(ts, "/flags")}.Do()
So(err, ShouldBeNil)
var flagList FlagListResponse
res.Body.FromJsonTo(&flagList)
So(flagList.Status, ShouldEqual, 200)
So(flagList.Flags[0], ShouldEqual, "red_button")
})
Convey("and then deleted", func() {
res, err := goreq.Request{
Method: "DELETE",
Uri: getTestURL(ts, "/flags/red_button")}.Do()
So(err, ShouldBeNil)
var flagVal FlagValueResponse
res.Body.FromJsonTo(&flagVal)
So(flagVal.Status, ShouldEqual, 200)
Convey("and then not listed", func() {
res, err := goreq.Request{
Uri: getTestURL(ts, "/flags")}.Do()
So(err, ShouldBeNil)
var flagList FlagListResponse
res.Body.FromJsonTo(&flagList)
So(flagList.Status, ShouldEqual, 200)
So(len(flagList.Flags), ShouldEqual, 0)
})
})
Convey("FlagValue returns 404 for missing flag", func() {
res, err := goreq.Request{
Method: "POST",
Body: "{\"user_username\":\"foobar\"}",
Uri: getTestURL(ts, "/flagValue/not_a_real_flag")}.Do()
So(err, ShouldBeNil)
var flagVal FlagValueResponse
res.Body.FromJsonTo(&flagVal)
So(flagVal.Status, ShouldEqual, 404)
So(flagVal.Enabled, ShouldEqual, false)
})
Convey("And return a false value for post", func() {
res, err := goreq.Request{
Method: "POST",
Body: "{\"user_id\":1}",
Uri: getTestURL(ts, "/flagValue/red_button")}.Do()
So(err, ShouldBeNil)
var flagVal FlagValueResponse
res.Body.FromJsonTo(&flagVal)
So(flagVal.Status, ShouldEqual, 200)
So(flagVal.Enabled, ShouldEqual, false)
})
Convey("And return a true value for post", func() {
res, err := goreq.Request{
Method: "POST",
Body: "{\"user_id\":10}",
Uri: getTestURL(ts, "/flagValue/red_button")}.Do()
So(err, ShouldBeNil)
var flagVal FlagValueResponse
res.Body.FromJsonTo(&flagVal)
So(flagVal.Status, ShouldEqual, 200)
So(flagVal.Enabled, ShouldEqual, true)
})
Convey("And return a false value for get", func() {
qstring := url.Values{}
qstring.Set("user_username", "nobody")
res, err := goreq.Request{
QueryString: qstring,
Uri: getTestURL(ts, "/flagValue/red_button")}.Do()
So(err, ShouldBeNil)
var flagVal FlagValueResponse
res.Body.FromJsonTo(&flagVal)
So(flagVal.Status, ShouldEqual, 200)
So(flagVal.Enabled, ShouldEqual, false)
})
Convey("And return a true value for get", func() {
qstring := url.Values{}
qstring.Set("user_username", "foobar")
res, err := goreq.Request{
QueryString: qstring,
Uri: getTestURL(ts, "/flagValue/red_button")}.Do()
So(err, ShouldBeNil)
var flagVal FlagValueResponse
res.Body.FromJsonTo(&flagVal)
So(flagVal.Status, ShouldEqual, 200)
So(flagVal.Enabled, ShouldEqual, true)
})
})
})
}