-
Notifications
You must be signed in to change notification settings - Fork 12
/
rest_server_test.go
206 lines (171 loc) · 4.92 KB
/
rest_server_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package aqua
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestDefaultConfiguration(t *testing.T) {
Convey("Given a RestServer", t, func() {
s := NewRestServer()
Convey("Then its embedded Fixture should have proper default values", func() {
So(s.Url, ShouldBeEmpty)
So(s.Root, ShouldBeEmpty)
So(s.Version, ShouldEqual, "*")
})
})
}
func TestGetShouldNotHonourPost(t *testing.T) {
s := NewRestServer()
s.Port = getUniquePortForTestCase()
s.RunAsync()
Convey("Given a RestServer", t, func() {
Convey("A url set for Http GET should return 404 for POST", func() {
postData := make(map[string]string)
postData["abc"] = "def"
url := fmt.Sprintf("http://localhost:%d/aqua/ping", s.Port)
code, _, _ := postUrl(url, postData, nil)
So(code, ShouldEqual, 404)
})
})
}
type AnyService struct {
RestService
honourGet GetApi `url:"a-url"`
honourPost GetApi `url:"a-url"`
}
func (me *AnyService) HonourGet() string { return "" }
func (me *AnyService) HonourPost() string { return "" }
func TestSameUrlWithSameHttpMethods(t *testing.T) {
s := NewRestServer()
Convey("Given a RestServer", t, func() {
Convey("When loading services with same urls and same http methods", func() {
Convey("Then the program should panic", func() {
So(func() {
s.AddService(&AnyService{})
}, ShouldPanic)
})
})
})
}
type AnyServiceA struct {
RestService
honourGet GetApi `url:"a-url"`
honourPost PostApi `url:"a-url"`
}
func (me *AnyServiceA) HonourGet() string { return "" }
func (me *AnyServiceA) HonourPost() string { return "" }
func TestSameUrlWithDifferentHttpMethods(t *testing.T) {
s := NewRestServer()
Convey("Given a RestServer", t, func() {
Convey("When loading services with same urls but different http methods", func() {
Convey("Then the program should NOT panic", func() {
So(func() {
s.AddService(&AnyServiceA{})
}, ShouldNotPanic)
})
})
})
}
func TestAddMethodValidations(t *testing.T) {
Convey("Given a RestServer", t, func() {
s := NewRestServer()
type BasicService struct {
RestService
}
svc := BasicService{}
Convey("When a Service object is directly passed to its add method", func() {
Convey("Then there should be panic", func() {
So(func() {
s.AddService(svc)
}, ShouldPanic)
})
})
Convey("When address of Service object is passed to its add method", func() {
Convey("Then it gets well accepted", func() {
So(func() {
s.AddService(&svc)
}, ShouldNotPanic)
})
})
Convey("When address of an object is passed to it", func() {
Convey("And the object is not composed of RestService struct", func() {
type IsNotComposedOfRestService struct{}
obj := IsNotComposedOfRestService{}
Convey("Then there should be panic", func() {
So(func() {
s.AddService(&obj)
}, ShouldPanic)
})
})
})
Convey("When address an object is passed to it", func() {
Convey("And the object is composed of RestService struct in a named (non-anonymous) field", func() {
type IsComposedOfNamedRestService struct {
field RestService
}
obj := IsComposedOfNamedRestService{}
Convey("Then there should be panic", func() {
So(func() {
s.AddService(&obj)
}, ShouldPanic)
})
})
})
})
}
type UserServiceA struct {
RestService `root:"/A"`
getUser GetApi
}
func (me *UserServiceA) GetUser() string { return "" }
type UserServiceB struct {
RestService `version:"0.3" root:"/B"`
getUser GetApi
}
func (me *UserServiceB) GetUser() string { return "" }
type UserServiceC struct {
RestService `version:"0.3" root:"/C"`
getUser GetApi
}
func (me *UserServiceC) GetUser() string { return "" }
type UserServiceD struct {
RestService `version:"0.3" root:"/D"`
getUser GetApi `version:"0.5"`
}
func (me *UserServiceD) GetUser() string { return "" }
func TestConfigurationHierarchy(t *testing.T) {
Convey("Given a RestServer", t, func() {
s := NewRestServer()
s.Version = "0.2"
Convey("Then the server config is inherited at Fixture", func() {
u := UserServiceA{}
s.AddService(&u)
apiId := getServiceId("GET", "", "0.2", "/A/get-user")
_, found := s.apis[apiId]
So(found, ShouldBeTrue)
})
Convey("Then the service tag overrides server config", func() {
u := UserServiceB{}
s.AddService(&u)
apiId := getServiceId("GET", "", "0.3", "/B/get-user")
_, found := s.apis[apiId]
So(found, ShouldBeTrue)
})
Convey("Then programmatically set service values override service tag", func() {
u := UserServiceC{}
u.Version = "0.4"
s.AddService(&u)
apiId := getServiceId("GET", "", "0.4", "/C/get-user")
_, found := s.apis[apiId]
So(found, ShouldBeTrue)
})
Convey("Then the Fixture tag overrides service values", func() {
u := UserServiceD{}
u.Version = "0.4"
s.AddService(&u)
apiId := getServiceId("GET", "", "0.5", "/D/get-user")
_, found := s.apis[apiId]
So(found, ShouldBeTrue)
})
})
}