forked from Virtomize/confluence-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
389 lines (331 loc) · 9.32 KB
/
request_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package goconfluence
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type testValuesRequest struct {
Endpoint string
Body string
Error error
}
func TestRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
testValues := []testValuesRequest{
{"/test", "\"test\"", nil},
{"/nocontent", "", nil},
{"/noauth", "", fmt.Errorf("authentication failed")},
{"/noservice", "", fmt.Errorf("service is not available: 503 Service Unavailable")},
{"/internalerror", "", fmt.Errorf("internal server error: 500 Internal Server Error")},
{"/unknown", "", fmt.Errorf("unknown response status: 408 Request Timeout")},
}
for _, test := range testValues {
req, err := http.NewRequest("GET", api.endPoint.String()+test.Endpoint, nil)
assert.Nil(t, err)
b, err := api.Request(req)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error())
}
assert.Equal(t, string(b), test.Body)
}
}
type testValuesContentRequest struct {
Content *Content
Method string
Body *Content
Error error
}
func TestSendContentRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getContentEndpoint()
assert.Nil(t, err)
testValues := []testValuesContentRequest{
{nil, "GET", &Content{}, nil},
{&Content{}, "GET", &Content{}, nil},
}
for _, test := range testValues {
b, err := api.SendContentRequest(ep, test.Method, test.Content)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
func TestSendContentRequestToken(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "", "token")
assert.Nil(t, err)
ep, err := api.getContentEndpoint()
assert.Nil(t, err)
testValues := []testValuesContentRequest{
{nil, "GET", &Content{}, nil},
{&Content{}, "GET", &Content{}, nil},
}
for _, test := range testValues {
b, err := api.SendContentRequest(ep, test.Method, test.Content)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
type testValuesAttachmentRequest struct {
AttachmentName string
Attachment io.Reader
Params map[string]string
Error error
}
func TestSendContentAttachmentRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getContentChildEndpoint("43", "attachment")
assert.Nil(t, err)
r1 := strings.NewReader("some test file attachment, normally this would come from a file")
p1 := map[string]string{"comment": "some witty comment"}
r2 := strings.NewReader("an even better attachment\nwith new lines in it")
p2 := map[string]string{"comment": "some other witty comment"}
testValues := []testValuesAttachmentRequest{
{"my awesome attachment", r1, p1, nil},
{"is awesome", r2, p2, nil},
}
for _, test := range testValues {
_, err := api.SendContentAttachmentRequest(ep, test.AttachmentName, test.Attachment, test.Params)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
}
}
type testValuesUserRequest struct {
Method string
Body *User
Error error
}
func TestSendUserRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getUserEndpoint("42")
assert.Nil(t, err)
testValues := []testValuesUserRequest{
{"GET", &User{}, nil},
}
for _, test := range testValues {
b, err := api.SendUserRequest(ep, test.Method)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
type testValuesSearchRequest struct {
Method string
Body *Search
Error error
}
func TestSendSearchRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getSearchEndpoint()
assert.Nil(t, err)
testValues := []testValuesSearchRequest{
{"GET", &Search{}, nil},
}
for _, test := range testValues {
b, err := api.SendSearchRequest(ep, test.Method)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
type testValuesHistoryRequest struct {
Method string
Body *History
Error error
}
func TestSendHistoryRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getContentGenericEndpoint("42", "history")
assert.Nil(t, err)
testValues := []testValuesHistoryRequest{
{"GET", &History{}, nil},
}
for _, test := range testValues {
b, err := api.SendHistoryRequest(ep, test.Method)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
type testValuesLabelRequest struct {
Method string
Body *Labels
Error error
Labels *[]Label
}
func TestSendLabelRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getContentGenericEndpoint("42", "label")
assert.Nil(t, err)
testValues := []testValuesLabelRequest{
{"GET", &Labels{}, nil, &[]Label{}},
}
for _, test := range testValues {
b, err := api.SendLabelRequest(ep, test.Method, test.Labels)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
type testValuesWatcherRequest struct {
Method string
Body *Watchers
Error error
}
func TestSendWatcherRequest(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)
ep, err := api.getContentGenericEndpoint("42", "notification/child-created")
assert.Nil(t, err)
testValues := []testValuesWatcherRequest{
{"GET", &Watchers{}, nil},
}
for _, test := range testValues {
b, err := api.SendWatcherRequest(ep, test.Method)
if test.Error == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, test.Error.Error(), err.Error)
}
assert.Equal(t, test.Body, b)
}
}
func confluenceRestAPIStub() *httptest.Server {
var resp interface{}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/wiki/rest/api/test":
resp = "test"
case "/wiki/rest/api/noauth":
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
case "/wiki/rest/api/nocontent":
http.Error(w, "", http.StatusNoContent)
return
case "/wiki/rest/api/noservice":
http.Error(w, "", http.StatusServiceUnavailable)
return
case "/wiki/rest/api/internalerror":
http.Error(w, "", http.StatusInternalServerError)
return
case "/wiki/rest/api/unknown":
http.Error(w, "", http.StatusRequestTimeout)
return
case "/wiki/rest/api/content/":
resp = Content{}
case "/wiki/rest/api/content/42":
if r.Method == "DELETE" {
resp = ""
w.WriteHeader(http.StatusNoContent)
} else {
resp = Content{}
}
case "/wiki/rest/api/user?username=42":
resp = User{}
case "/wiki/rest/api/user?accountId=42":
resp = User{}
case "/wiki/rest/api/user?key=42":
resp = User{}
case "/wiki/rest/api/user/current":
resp = User{}
case "/wiki/rest/api/user/anonymous":
resp = User{}
case "/wiki/rest/api/search":
resp = Search{}
case "/wiki/rest/api/content/42/history":
resp = History{}
case "/wiki/rest/api/content/42/label":
resp = Labels{}
case "/wiki/rest/api/content/42/label/test":
resp = Labels{}
case "/wiki/rest/api/content/42/notification/child-created":
resp = Watchers{}
case "/wiki/rest/api/content/42/child/page":
resp = Search{}
case "/wiki/rest/api/content/42/child/page?limit=25":
resp = Search{}
case "/wiki/rest/api/content/42/child/attachment":
resp = Search{}
case "/wiki/rest/api/content/43/child/attachment/123/data":
resp = Search{}
case "/wiki/rest/api/content/43/child/attachment":
resp = Content{}
case "/wiki/rest/api/content/42/child/comment":
resp = Search{}
case "/wiki/rest/api/content/42/child/history":
resp = Search{}
case "/wiki/rest/api/content/42/child/label":
resp = Search{}
case "/wiki/rest/api/space":
resp = AllSpaces{}
case "/wiki/rest/api/template/blueprint":
resp = TemplateSearch{}
case "/wiki/rest/api/template/page":
resp = TemplateSearch{}
case "/wiki/api/v2/attachments/2495990589":
resp = Attachment{}
default:
http.Error(w, "not found", http.StatusNotFound)
return
}
b, err := json.Marshal(resp)
if err != nil {
http.Error(w, string(b), http.StatusInternalServerError)
return
}
_, _ = w.Write(b)
}))
}