generated from alexejk/go-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
client_test.go
280 lines (235 loc) · 7 KB
/
client_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package xmlrpc
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestNewClient(t *testing.T) {
c, err := NewClient(":8080/rpc")
require.Error(t, err)
require.Nil(t, c)
c, err = NewClient("http://localhost")
require.NoError(t, err)
require.NotNil(t, c)
}
func TestClient_Call(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := &struct {
Name string `xml:"methodName"`
Params []*ResponseParam `xml:"params>param"`
}{}
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "test server: read body")
err = xml.Unmarshal(body, m)
require.NoError(t, err, "test server: unmarshal body")
nameParts := strings.Split(m.Name, ".")
require.Equal(t, 2, len(nameParts))
require.Equal(t, "my", nameParts[0], "test server: method should start with 'my.'")
require.Equal(t, 1, len(m.Params))
require.Equal(t, "12345", *m.Params[0].Value.Int)
file := nameParts[1]
_, _ = fmt.Fprintln(w, string(loadTestFile(t, fmt.Sprintf("response_%s.xml", file))))
}))
defer ts.Close()
c, err := NewClient(ts.URL)
require.NoError(t, err)
require.NotNil(t, c)
req := &struct {
Index int
}{
Index: 12345,
}
resp := &struct {
Area string
Index int
}{}
err = c.Call("my.simple", req, resp)
require.NoError(t, err)
require.Equal(t, "South Dakota", resp.Area)
require.Equal(t, 12345, resp.Index)
}
func TestClient_Github_86(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := &struct {
Name string `xml:"methodName"`
Params []*ResponseParam `xml:"params>param"`
}{}
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "test server: read body")
err = xml.Unmarshal(body, m)
require.NoError(t, err, "test server: unmarshal body")
require.Equal(t, "di", m.Name)
require.Equal(t, 3, len(m.Params))
expected := []string{"abc", "def", "hij"}
for i, p := range m.Params {
require.Equal(t, expected[i], *p.Value.String)
}
respBody := `<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><string>OK</string></value>
</param>
<param>
<value><int>200</int></value>
</param>
<param>
<value>
<array>
<data>
<value><i4>200</i4></value>
<value><array><data><value>Some String</value></data></array></value>
<value>Server: Sip Express Media Server (5.1.0 (x86_64/Linux)) calls: 0 active/0 total/0 connect/0 min</value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>`
_, _ = fmt.Fprintln(w, respBody)
}))
defer ts.Close()
c, err := NewClient(ts.URL)
require.NoError(t, err)
require.NotNil(t, c)
type DiRequest struct {
First string
Second string
Third string
}
type DiResponse struct {
Status string
Code int
Data []any
}
req := &DiRequest{
First: "abc",
Second: "def",
Third: "hij",
}
resp := &DiResponse{}
err = c.Call("di", req, resp)
require.NoError(t, err)
require.Equal(t, "OK", resp.Status)
require.Equal(t, 200, resp.Code)
// Data array decoding validation
require.NotEmpty(t, resp.Data)
require.Len(t, resp.Data, 3)
require.Equal(t, 200, resp.Data[0].(int))
require.Equal(t, []any{"Some String"}, resp.Data[1].([]any))
require.Equal(t, "Server: Sip Express Media Server (5.1.0 (x86_64/Linux)) calls: 0 active/0 total/0 connect/0 min", resp.Data[2].(string))
}
func TestClient_Fault(t *testing.T) {
ts := mockupServer(t, "response_fault.xml")
defer ts.Close()
c, err := NewClient(ts.URL)
require.NoError(t, err)
require.NotNil(t, c)
req := &struct{}{}
resp := &struct{}{}
err = c.Call("my.fault", req, resp)
require.Error(t, err)
}
func TestClient_Bugzilla(t *testing.T) {
ts := mockupServer(t, "response_bugzilla_version.xml")
defer ts.Close()
c, err := NewClient(ts.URL)
require.NoError(t, err)
require.NotNil(t, c)
resp := &struct {
BugzillaVersion struct {
Version string
}
}{}
err = c.Call("Bugzilla.version", nil, resp)
require.NoError(t, err)
require.NotEmpty(t, resp.BugzillaVersion.Version)
require.Equal(t, "20220802.1", resp.BugzillaVersion.Version)
}
// Checks Issue 52 (https://github.com/alexejk/go-xmlrpc/issues/52)
// Makes several calls to ensure there is no request-response confusion caused by the changes
// Test must have a small delay in order to compare go-routines (due to defers)
func TestClient_GoRoutineLeak_Issue52(t *testing.T) {
ts := mockupBugzillaVersionServer(t)
var request = func() {
client, err := NewClient(ts.URL)
if err != nil {
require.FailNow(t, "panic when generating clients")
}
defer client.Close()
result := struct {
BugzillaVersion struct {
Version string
}
}{}
// Make several calls to the server
for i := 1; i < 5; i++ {
err = client.Call(fmt.Sprintf("Bugzilla.version.%d", i), nil, &result)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("20220802.%d", i), result.BugzillaVersion.Version)
}
}
// Baseline (BEFORE)
preTestRoutines := runtime.NumGoroutine()
for i := 0; i < 5; i++ {
fmt.Printf("...Request %d\n", i)
request()
}
// Allow for all things to close down properly
time.Sleep(1 * time.Second)
// Result (AFTER)
postTestRoutines := runtime.NumGoroutine()
// Ensure that the amount of go routines AFTER <= BEFORE (meaning - we did not leak any)
require.LessOrEqual(t, postTestRoutines, preTestRoutines)
}
func mockupServer(t *testing.T, respFile string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "text/xml", r.Header.Get("Content-Type"))
w.WriteHeader(200)
_, _ = fmt.Fprint(w, string(loadTestFile(t, respFile)))
}))
}
// mockupBugzillaVersionServer returns a test server that is able to parse the method name and inject the last part of
// the method name into response version. This follows Bugzilla response format, however does some modifications to make
// requests more dynamic and comparable
func mockupBugzillaVersionServer(t *testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "text/xml", r.Header.Get("Content-Type"))
m := &struct {
Name string `xml:"methodName"`
}{}
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "test server: read body")
err = xml.Unmarshal(body, m)
require.NoError(t, err, "test server: unmarshal body")
nameParts := strings.Split(m.Name, ".")
num := nameParts[2] // Bugzilla.Version.%d
w.WriteHeader(200)
_, _ = fmt.Fprintf(w, `
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>version</name>
<value>
<string>20220802.%s</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
`, num)
}))
}