-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_request_test.go
374 lines (323 loc) · 11.8 KB
/
operation_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
package horizonclient
import (
"context"
"fmt"
"testing"
"time"
"github.com/stellar/go/protocols/horizon/operations"
"github.com/stellar/go/support/http/httptest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOperationRequestBuildUrl(t *testing.T) {
op := OperationRequest{endpoint: "operations"}
endpoint, err := op.BuildURL()
// It should return valid all operations endpoint and no errors
require.NoError(t, err)
assert.Equal(t, "operations", endpoint)
op = OperationRequest{ForAccount: "GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU", endpoint: "operations"}
endpoint, err = op.BuildURL()
// It should return valid account operations endpoint and no errors
require.NoError(t, err)
assert.Equal(t, "accounts/GCLWGQPMKXQSPF776IU33AH4PZNOOWNAWGGKVTBQMIC5IMKUNP3E6NVU/operations", endpoint)
op = OperationRequest{ForLedger: 123, endpoint: "operations"}
endpoint, err = op.BuildURL()
// It should return valid ledger operations endpoint and no errors
require.NoError(t, err)
assert.Equal(t, "ledgers/123/operations", endpoint)
op = OperationRequest{forOperationID: "123", endpoint: "operations"}
endpoint, err = op.BuildURL()
// It should return valid operation operations endpoint and no errors
require.NoError(t, err)
assert.Equal(t, "operations/123", endpoint)
op = OperationRequest{ForTransaction: "123", endpoint: "payments"}
endpoint, err = op.BuildURL()
// It should return valid transaction payments endpoint and no errors
require.NoError(t, err)
assert.Equal(t, "transactions/123/payments", endpoint)
op = OperationRequest{ForLedger: 123, forOperationID: "789", endpoint: "operations"}
endpoint, err = op.BuildURL()
// error case: too many parameters for building any operation endpoint
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid request: too many parameters")
}
op = OperationRequest{Cursor: "123456", Limit: 30, Order: OrderAsc, endpoint: "operations"}
endpoint, err = op.BuildURL()
// It should return valid all operations endpoint with query params and no errors
require.NoError(t, err)
assert.Equal(t, "operations?cursor=123456&limit=30&order=asc", endpoint)
op = OperationRequest{Cursor: "123456", Limit: 30, Order: OrderAsc, endpoint: "payments"}
endpoint, err = op.BuildURL()
// It should return valid all operations endpoint with query params and no errors
require.NoError(t, err)
assert.Equal(t, "payments?cursor=123456&limit=30&order=asc", endpoint)
}
func ExampleClient_StreamOperations() {
client := DefaultTestNetClient
// operations for an account
opRequest := OperationRequest{ForAccount: "GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR", Cursor: "760209215489"}
ctx, cancel := context.WithCancel(context.Background())
go func() {
// Stop streaming after 60 seconds.
time.Sleep(60 * time.Second)
cancel()
}()
printHandler := func(op operations.Operation) {
fmt.Println(op)
}
err := client.StreamOperations(ctx, opRequest, printHandler)
if err != nil {
fmt.Println(err)
}
}
func ExampleClient_StreamPayments() {
client := DefaultTestNetClient
// all payments
opRequest := OperationRequest{Cursor: "760209215489"}
ctx, cancel := context.WithCancel(context.Background())
go func() {
// Stop streaming after 60 seconds.
time.Sleep(60 * time.Second)
cancel()
}()
printHandler := func(op operations.Operation) {
fmt.Println(op)
}
err := client.StreamPayments(ctx, opRequest, printHandler)
if err != nil {
fmt.Println(err)
}
}
func ExampleClient_NextOperationsPage() {
client := DefaultPublicNetClient
// all operations
operationRequest := OperationRequest{Limit: 20}
ops, err := client.Operations(operationRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(ops)
// get next pages.
recordsFound := false
if len(ops.Embedded.Records) > 0 {
recordsFound = true
}
page := ops
// get the next page of records if recordsFound is true
for recordsFound {
// next page
nextPage, err := client.NextOperationsPage(page)
if err != nil {
fmt.Println(err)
return
}
page = nextPage
if len(nextPage.Embedded.Records) == 0 {
recordsFound = false
}
fmt.Println(nextPage)
}
}
func ExampleClient_PrevOperationsPage() {
client := DefaultPublicNetClient
// all operations
operationRequest := OperationRequest{Limit: 20}
ops, err := client.Operations(operationRequest)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(ops)
// get prev pages.
recordsFound := false
if len(ops.Embedded.Records) > 0 {
recordsFound = true
}
page := ops
// get the prev page of records if recordsFound is true
for recordsFound {
// prev page
prevPage, err := client.PrevOperationsPage(page)
if err != nil {
fmt.Println(err)
return
}
page = prevPage
if len(prevPage.Embedded.Records) == 0 {
recordsFound = false
}
fmt.Println(prevPage)
}
}
func TestNextOperationsPage(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}
operationRequest := OperationRequest{Limit: 2}
hmock.On(
"GET",
"https://localhost/operations?limit=2",
).ReturnString(200, firstOperationsPage)
ops, err := client.Operations(operationRequest)
if assert.NoError(t, err) {
assert.Equal(t, len(ops.Embedded.Records), 2)
}
hmock.On(
"GET",
"https://horizon-testnet.stellar.org/operations?cursor=661424967682&limit=2&order=asc",
).ReturnString(200, emptyOperationsPage)
nextPage, err := client.NextOperationsPage(ops)
if assert.NoError(t, err) {
assert.Equal(t, len(nextPage.Embedded.Records), 0)
}
}
func TestOperationRequestStreamOperations(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}
// All operations
operationRequest := OperationRequest{}
ctx, cancel := context.WithCancel(context.Background())
hmock.On(
"GET",
"https://localhost/operations?cursor=now",
).ReturnString(200, operationStreamResponse)
operationStream := make([]operations.Operation, 1)
err := client.StreamOperations(ctx, operationRequest, func(op operations.Operation) {
operationStream[0] = op
cancel()
})
if assert.NoError(t, err) {
assert.Equal(t, operationStream[0].GetType(), "create_account")
}
// Account payments
operationRequest = OperationRequest{ForAccount: "GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR"}
ctx, cancel = context.WithCancel(context.Background())
hmock.On(
"GET",
"https://localhost/accounts/GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR/payments?cursor=now",
).ReturnString(200, operationStreamResponse)
err = client.StreamPayments(ctx, operationRequest, func(op operations.Operation) {
operationStream[0] = op
cancel()
})
if assert.NoError(t, err) {
payment, ok := operationStream[0].(operations.CreateAccount)
assert.Equal(t, ok, true)
assert.Equal(t, payment.Funder, "GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR")
}
// test connection error
operationRequest = OperationRequest{}
ctx, cancel = context.WithCancel(context.Background())
hmock.On(
"GET",
"https://localhost/operations?cursor=now",
).ReturnString(500, operationStreamResponse)
err = client.StreamOperations(ctx, operationRequest, func(op operations.Operation) {
operationStream[0] = op
cancel()
})
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "got bad HTTP status code 500")
}
}
var operationStreamResponse = `data: {"_links":{"self":{"href":"https://horizon-testnet.stellar.org/operations/4934917427201"},"transaction":{"href":"https://horizon-testnet.stellar.org/transactions/1c1449106a54cccd8a2ec2094815ad9db30ae54c69c3309dd08d13fdb8c749de"},"effects":{"href":"https://horizon-testnet.stellar.org/operations/4934917427201/effects"},"succeeds":{"href":"https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=4934917427201"},"precedes":{"href":"https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=4934917427201"}},"id":"4934917427201","paging_token":"4934917427201","transaction_successful":true,"source_account":"GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR","type":"create_account","type_i":0,"created_at":"2019-02-27T11:32:39Z","transaction_hash":"1c1449106a54cccd8a2ec2094815ad9db30ae54c69c3309dd08d13fdb8c749de","starting_balance":"10000.0000000","funder":"GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR","account":"GDBLBBDIUULY3HGIKXNK6WVBISY7DCNCDA45EL7NTXWX5R4UZ26HGMGS"}
`
var firstOperationsPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=&limit=2&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=661424967682&limit=2&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=661424967681&limit=2&order=desc"
}
},
"_embedded": {
"records": [
{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations/661424967681"
},
"transaction": {
"href": "https://horizon-testnet.stellar.org/transactions/749e4f8933221b9942ef38a02856803f379789ec8d971f1f60535db70135673e"
},
"effects": {
"href": "https://horizon-testnet.stellar.org/operations/661424967681/effects"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=661424967681"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=661424967681"
}
},
"id": "661424967681",
"paging_token": "661424967681",
"transaction_successful": true,
"source_account": "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
"type": "create_account",
"type_i": 0,
"created_at": "2019-04-24T09:16:14Z",
"transaction_hash": "749e4f8933221b9942ef38a02856803f379789ec8d971f1f60535db70135673e",
"starting_balance": "10000000000.0000000",
"funder": "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
"account": "GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR"
},
{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations/661424967682"
},
"transaction": {
"href": "https://horizon-testnet.stellar.org/transactions/749e4f8933221b9942ef38a02856803f379789ec8d971f1f60535db70135673e"
},
"effects": {
"href": "https://horizon-testnet.stellar.org/operations/661424967682/effects"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=661424967682"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=661424967682"
}
},
"id": "661424967682",
"paging_token": "661424967682",
"transaction_successful": true,
"source_account": "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
"type": "create_account",
"type_i": 0,
"created_at": "2019-04-24T09:16:14Z",
"transaction_hash": "749e4f8933221b9942ef38a02856803f379789ec8d971f1f60535db70135673e",
"starting_balance": "10000.0000000",
"funder": "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
"account": "GDO34SQXVOSNODK7JCTAXLZUPSAF3JIH4ADQELVIKOQJUWQ3U4BMSCSH"
}
]
}
}`
var emptyOperationsPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=661424967682&limit=2&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=661424967684&limit=2&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=661424967683&limit=2&order=desc"
}
},
"_embedded": {
"records": []
}
}`