forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonrpc-test.js
More file actions
172 lines (134 loc) · 4.7 KB
/
jsonrpc-test.js
File metadata and controls
172 lines (134 loc) · 4.7 KB
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
var async = require("async");
var assert = require('assert');
var http = require("http");
var jsonrpc = require("simple-jsonrpc");
var EventEmitter = require('events').EventEmitter;
var Remote = require("ripple-lib").Remote;
var testutils = require("./testutils");
var config = testutils.init_config();
function build_setup(options) {
var setup = testutils.build_setup(options);
return function (done) {
var self = this;
var http_config = config.http_servers["zed"];
self.server_events = new EventEmitter;
self.server = http.createServer(function (req, res) {
// console.log("REQUEST");
var input = "";
req.setEncoding('utf8');
req.on('data', function (buffer) {
// console.log("DATA: %s", buffer);
input = input + buffer;
});
req.on('end', function () {
var request = JSON.parse(input);
// console.log("REQ: %s", JSON.stringify(request, undefined, 2));
self.server_events.emit('request', request, res);
});
req.on('close', function () { });
});
self.server.listen(http_config.port, http_config.ip, void(0), function () {
// console.log("server up: %s %d", http_config.ip, http_config.port);
setup.call(self, done);
});
};
};
function build_teardown() {
var teardown = testutils.build_teardown();
return function (done) {
var self = this;
self.server.close(function () {
// console.log("server closed");
teardown.call(self, done);
});
};
};
suite('JSON-RPC', function() {
var $ = { };
setup(function(done) {
build_setup().call($, done);
});
teardown(function(done) {
build_teardown().call($, done);
});
test('server info', function(done) {
var rippled_config = testutils.get_server_config(config);
var client = jsonrpc.client("http://" + rippled_config.rpc_ip + ":" + rippled_config.rpc_port);
client.call('server_info', [ ], function (result) {
// console.log(JSON.stringify(result, undefined, 2));
assert(typeof result === 'object');
assert('info' in result);
done();
});
});
test('subscribe server', function(done) {
var rippled_config = testutils.get_server_config(config);
var client = jsonrpc.client("http://" + rippled_config.rpc_ip + ":" + rippled_config.rpc_port);
var http_config = config.http_servers["zed"];
client.call('subscribe', [{
'url' : "http://" + http_config.ip + ":" + http_config.port,
'streams' : [ 'server' ],
}], function (result) {
// console.log(JSON.stringify(result, undefined, 2));
assert(typeof result === 'object');
assert('random' in result);
done();
});
});
test('subscribe ledger', function(done) {
var self = this;
var rippled_config = testutils.get_server_config(config);
var client = jsonrpc.client("http://" + rippled_config.rpc_ip + ":" + rippled_config.rpc_port);
var http_config = config.http_servers["zed"];
var steps = [
function (callback) {
self.what = "Subscribe.";
client.call('subscribe', [{
'url' : "http://" + http_config.ip + ":" + http_config.port,
'streams' : [ 'ledger' ],
}], function (result) {
//console.log(JSON.stringify(result, undefined, 2));
assert(typeof result === 'object');
assert('ledger_index' in result);
callback();
});
},
function (callback) {
self.what = "Accept a ledger.";
$.server_events.once('request', function (request, response) {
// console.log("GOT: %s", JSON.stringify(request, undefined, 2));
assert.strictEqual(1, request.params.seq);
assert.strictEqual(3, request.params.ledger_index);
response.statusCode = 200;
response.end(JSON.stringify({
jsonrpc: "2.0",
result: {},
id: request.id
}));
callback();
});
$.remote.ledger_accept();
},
function (callback) {
self.what = "Accept another ledger.";
$.server_events.once('request', function (request, response) {
// console.log("GOT: %s", JSON.stringify(request, undefined, 2));
assert.strictEqual(2, request.params.seq);
assert.strictEqual(4, request.params.ledger_index);
response.statusCode = 200;
response.end(JSON.stringify({
jsonrpc: "2.0",
result: {},
id: request.id
}));
callback();
});
$.remote.ledger_accept();
}
]
async.waterfall(steps, function(error) {
assert(!error, self.what);
done();
});
});
});