-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwsapi_test.gox
144 lines (118 loc) · 3.38 KB
/
wsapi_test.gox
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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// run ``factom-walletd -w /tmp/test_wallet-01 -p 8889`` and ``factomd`` to test the wsapi calls.
package wsapi_test
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
var testnet = "localhost:8889"
func TesthhandleWalletBalances(t *testing.T) {
// Replace the accounts will accounts in your wallet
req := `{"jsonrpc": "2.0", "id": 0, "method": "wallet-balances"}`
resp, err := apiCall(req)
if err != nil {
t.Error(err)
}
t.Log(resp)
}
func TestAllAddresses(t *testing.T) {
req := `{"jsonrpc":"2.0","id":0,"method":"all-addresses"}`
resp, err := apiCall(req)
if err != nil {
t.Error(err)
}
t.Log(resp)
}
func TestGenerateECAddress(t *testing.T) {
req := `{"jsonrpc":"2.0","id":0,"method":"generate-ec-address"}`
resp, err := apiCall(req)
if err != nil {
t.Error(err)
}
t.Log(resp)
}
func TestGenerateFCTAddress(t *testing.T) {
req := `{"jsonrpc":"2.0","id":0,"method":"generate-factoid-address"}`
resp, err := apiCall(req)
if err != nil {
t.Error(err)
}
t.Log(resp)
}
func TestImportAddresses(t *testing.T) {
req := `{"jsonrpc":"2.0","id":0,"method":"import-addresses","params":{"addresses":[{"secret":"Fs3E9gV6DXsYzf7Fqx1fVBQPQXV695eP3k5XbmHEZVRLkMdD9qCK"}]}}`
resp, err := apiCall(req)
if err != nil {
t.Error(err)
}
t.Log(resp)
}
func TestAddress(t *testing.T) {
req := `{"jsonrpc":"2.0","id":0,"method":"address","params":{"address":"FA2jK2HcLnRdS94dEcU27rF3meoJfpUcZPSinpb7AwQvPRY6RL1Q"}}`
resp, err := apiCall(req)
if err != nil {
t.Error(err)
}
t.Log(resp)
}
func TestTransaction(t *testing.T) {
new := `{"jsonrpc":"2.0","id":0,"method":"new-transaction","params":{"tx-name":"a"}}`
if resp, err := apiCall(new); err != nil {
t.Error(err)
} else {
t.Log(resp)
}
addin := `{"jsonrpc":"2.0","id":0,"method":"add-input","params":{"tx-name":"a","address":"FA2jK2HcLnRdS94dEcU27rF3meoJfpUcZPSinpb7AwQvPRY6RL1Q","amount":1000000000}}`
if resp, err := apiCall(addin); err != nil {
t.Error(err)
} else {
t.Log(resp)
}
addout := `{"jsonrpc":"2.0","id":0,"method":"add-output","params":{"tx-name":"a","address":"FA2xWmGckzbACp7LR43bfnViCyN4uNhugBxiaYPtWGVZVSn1y2m6","amount":1000000000}}`
if resp, err := apiCall(addout); err != nil {
t.Error(err)
} else {
t.Log(resp)
}
addfee := `{"jsonrpc":"2.0","id":0,"method":"add-fee","params":{"tx-name":"a","address":"FA2jK2HcLnRdS94dEcU27rF3meoJfpUcZPSinpb7AwQvPRY6RL1Q"}}`
if resp, err := apiCall(addfee); err != nil {
t.Error(err)
} else {
t.Log(resp)
}
sign := `{"jsonrpc":"2.0","id":0,"method":"sign-transaction","params":{"tx-name":"a"}}`
if resp, err := apiCall(sign); err != nil {
t.Error(err)
} else {
t.Log(resp)
}
compose := `{"jsonrpc":"2.0","id":0,"method":"compose-transaction","params":{"tx-name":"a"}}`
if resp, err := apiCall(compose); err != nil {
t.Error(err)
} else {
t.Log(resp)
}
}
func apiCall(req string) (string, error) {
client := &http.Client{}
buf := bytes.NewBuffer([]byte(req))
re, err := http.NewRequest("POST", "http://"+testnet+"/v2", buf)
if err != nil {
return "", err
}
re.SetBasicAuth(RpcUser, RpcPass)
resp, err := client.Do(re)
if err != nil {
return "", err
}
defer resp.Body.Close()
p, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(p), nil
}