Skip to content

Commit 7a3181a

Browse files
jnewberyromanz
authored andcommitted
[tests] Make json request building more consistent in interface_rest.py
1 parent 3fd4490 commit 7a3181a

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

test/functional/interface_rest.py

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ def http_post_call(host, port, path, requestdata='', response_object=0):
4242
return conn.getresponse().read()
4343

4444
class RESTTest (BitcoinTestFramework):
45-
FORMAT_SEPARATOR = "."
46-
4745
def set_test_params(self):
4846
self.setup_clean_chain = True
4947
self.num_nodes = 3
@@ -75,7 +73,8 @@ def run_test(self):
7573

7674
self.log.info("Load the transaction using the /tx URI")
7775

78-
json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json")
76+
json_request = "/rest/tx/{}.json".format(txid)
77+
json_string = http_get_call(url.hostname, url.port, json_request)
7978
json_obj = json.loads(json_string)
8079
vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then)
8180
# get n of 0.1 outpoint
@@ -86,8 +85,8 @@ def run_test(self):
8685

8786
self.log.info("Query an unspent TXO using the /getutxos URI")
8887

89-
json_request = '/' + txid + '-' + str(n)
90-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
88+
json_request = "/rest/getutxos/{}-{}.json".format(txid, str(n))
89+
json_string = http_get_call(url.hostname, url.port, json_request)
9190
json_obj = json.loads(json_string)
9291

9392
# Check chainTip response
@@ -99,8 +98,8 @@ def run_test(self):
9998

10099
self.log.info("Query a spent TXO using the /getutxos URI")
101100

102-
json_request = '/' + vintx + '-0'
103-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
101+
json_request = "/rest/getutxos/{}-0.json".format(vintx)
102+
json_string = http_get_call(url.hostname, url.port, json_request)
104103
json_obj = json.loads(json_string)
105104

106105
# Check chainTip response
@@ -114,8 +113,8 @@ def run_test(self):
114113

115114
self.log.info("Query two TXOs using the /getutxos URI")
116115

117-
json_request = '/' + txid + '-' + str(n) + '/' + vintx + '-0'
118-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
116+
json_request = "/rest/getutxos/{}-{}/{}-0.json".format(txid, str(n), vintx)
117+
json_string = http_get_call(url.hostname, url.port, json_request)
119118
json_obj = json.loads(json_string)
120119
assert_equal(len(json_obj['utxos']), 1)
121120
assert_equal(json_obj['bitmap'], "10")
@@ -130,7 +129,7 @@ def run_test(self):
130129
bin_request += hex_str_to_bytes(vintx)
131130
bin_request += pack("i", 0)
132131

133-
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', bin_request)
132+
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', bin_request)
134133
output = BytesIO()
135134
output.write(bin_response)
136135
output.seek(0)
@@ -147,7 +146,8 @@ def run_test(self):
147146

148147
# do a tx and don't sync
149148
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
150-
json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json")
149+
json_request = "/rest/tx/{}.json".format(txid)
150+
json_string = http_get_call(url.hostname, url.port, json_request)
151151
json_obj = json.loads(json_string)
152152
# get the spent output to later check for utxo (should be spent by then)
153153
spent = '{}-{}'.format(json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout'])
@@ -158,64 +158,58 @@ def run_test(self):
158158
n = vout['n']
159159
spending = '{}-{}'.format(txid, n)
160160

161-
json_request = '/' + spending
162-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
161+
json_request = '/rest/getutxos/{}.json'.format(spending)
162+
json_string = http_get_call(url.hostname, url.port, json_request)
163163
json_obj = json.loads(json_string)
164164
assert_equal(len(json_obj['utxos']), 0)
165165

166-
json_request = '/checkmempool/' + spending
167-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
166+
json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending)
167+
json_string = http_get_call(url.hostname, url.port, json_request)
168168
json_obj = json.loads(json_string)
169169
assert_equal(len(json_obj['utxos']), 1)
170170

171-
json_request = '/' + spent
172-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
171+
json_request = '/rest/getutxos/{}.json'.format(spent)
172+
json_string = http_get_call(url.hostname, url.port, json_request)
173173
json_obj = json.loads(json_string)
174174
assert_equal(len(json_obj['utxos']), 1)
175175

176-
json_request = '/checkmempool/' + spent
177-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
176+
json_request = '/rest/getutxos/checkmempool/{}.json'.format(spent)
177+
json_string = http_get_call(url.hostname, url.port, json_request)
178178
json_obj = json.loads(json_string)
179179
assert_equal(len(json_obj['utxos']), 0)
180180

181181
self.nodes[0].generate(1)
182182
self.sync_all()
183183

184-
json_request = '/' + spending
185-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
184+
json_request = '/rest/getutxos/{}.json'.format(spending)
185+
json_string = http_get_call(url.hostname, url.port, json_request)
186186
json_obj = json.loads(json_string)
187187
assert_equal(len(json_obj['utxos']), 1)
188188

189-
json_request = '/checkmempool/' + spending
190-
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
189+
json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending)
190+
json_string = http_get_call(url.hostname, url.port, json_request)
191191
json_obj = json.loads(json_string)
192192
assert_equal(len(json_obj['utxos']), 1)
193193

194194
# Do some invalid requests
195195
json_request = '{"checkmempool'
196-
response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'json', json_request, True)
196+
response = http_post_call(url.hostname, url.port, '/rest/getutxos.json', json_request, True)
197197
assert_equal(response.status, 400) # must be a 400 because we send an invalid json request
198198

199199
json_request = '{"checkmempool'
200-
response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', json_request, True)
200+
response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', json_request, True)
201201
assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request
202202

203-
response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool' + self.FORMAT_SEPARATOR + 'bin', '', True)
203+
response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool.json', '', True)
204204
assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request
205205

206206
# Test limits
207-
json_request = '/checkmempool/'
208-
for x in range(0, 20):
209-
json_request += txid + '-' + str(n) + '/'
210-
json_request = json_request.rstrip("/")
211-
response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True)
207+
json_request = '/rest/getutxos/checkmempool/' + '/'.join(["{}-{}".format(txid, n) for n in range(20)]) + '.json'
208+
response = http_post_call(url.hostname, url.port, json_request, '', True)
212209
assert_equal(response.status, 400) # must be a 400 because we exceeding the limits
213210

214-
json_request = '/checkmempool/'
215-
for x in range(0, 15):
216-
json_request += txid + '-' + str(n) + '/'
217-
json_request = json_request.rstrip("/")
218-
response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True)
211+
json_request = '/rest/getutxos/checkmempool/' + '/'.join(['{}-{}'.format(txid, n) for n in range(15)]) + '.json'
212+
response = http_post_call(url.hostname, url.port, json_request, '', True)
219213
assert_equal(response.status, 200) # must be a 200 because we are within the limits
220214

221215
self.nodes[0].generate(1) # generate block to not affect upcoming tests
@@ -224,40 +218,40 @@ def run_test(self):
224218
self.log.info("Test the /block and /headers URIs")
225219

226220
# Check binary format
227-
response = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True)
221+
response = http_get_call(url.hostname, url.port, '/rest/block/{}.bin'.format(bb_hash), True)
228222
assert_equal(response.status, 200)
229223
assert_greater_than(int(response.getheader('content-length')), 80)
230224
response_str = response.read()
231225

232226
# Compare with block header
233-
response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True)
227+
response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.bin'.format(bb_hash), True)
234228
assert_equal(response_header.status, 200)
235229
assert_equal(int(response_header.getheader('content-length')), 80)
236230
response_header_str = response_header.read()
237231
assert_equal(response_str[0:80], response_header_str)
238232

239233
# Check block hex format
240-
response_hex = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True)
234+
response_hex = http_get_call(url.hostname, url.port, '/rest/block/{}.hex'.format(bb_hash), True)
241235
assert_equal(response_hex.status, 200)
242236
assert_greater_than(int(response_hex.getheader('content-length')), 160)
243237
response_hex_str = response_hex.read()
244238
assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160])
245239

246240
# Compare with hex block header
247-
response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True)
241+
response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.hex'.format(bb_hash), True)
248242
assert_equal(response_header_hex.status, 200)
249243
assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
250244
response_header_hex_str = response_header_hex.read()
251245
assert_equal(response_hex_str[0:160], response_header_hex_str[0:160])
252246
assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160])
253247

254248
# Check json format
255-
block_json_string = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + 'json')
249+
block_json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(bb_hash))
256250
block_json_obj = json.loads(block_json_string)
257251
assert_equal(block_json_obj['hash'], bb_hash)
258252

259253
# Compare with json block header
260-
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "json", True)
254+
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.json'.format(bb_hash), True)
261255
assert_equal(response_header_json.status, 200)
262256
response_header_json_str = response_header_json.read().decode('utf-8')
263257
json_obj = json.loads(response_header_json_str, parse_float=Decimal)
@@ -272,7 +266,7 @@ def run_test(self):
272266
# See if we can get 5 headers in one response
273267
self.nodes[1].generate(5)
274268
self.sync_all()
275-
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/' + bb_hash + self.FORMAT_SEPARATOR + "json", True)
269+
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/{}.json'.format(bb_hash), True)
276270
assert_equal(response_header_json.status, 200)
277271
response_header_json_str = response_header_json.read().decode('utf-8')
278272
json_obj = json.loads(response_header_json_str)
@@ -281,12 +275,12 @@ def run_test(self):
281275
self.log.info("Test the /tx URI")
282276

283277
tx_hash = block_json_obj['tx'][0]['txid']
284-
json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "json")
278+
json_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.json'.format(tx_hash))
285279
json_obj = json.loads(json_string)
286280
assert_equal(json_obj['txid'], tx_hash)
287281

288282
# Check hex format response
289-
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "hex", True)
283+
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.hex'.format(tx_hash), True)
290284
assert_equal(hex_string.status, 200)
291285
assert_greater_than(int(response.getheader('content-length')), 10)
292286

@@ -300,14 +294,14 @@ def run_test(self):
300294
self.sync_all()
301295

302296
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
303-
json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info' + self.FORMAT_SEPARATOR + 'json')
297+
json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info.json')
304298
json_obj = json.loads(json_string)
305299
assert_equal(json_obj['size'], 3)
306300
# the size of the memory pool should be greater than 3x ~100 bytes
307301
assert_greater_than(json_obj['bytes'], 300)
308302

309303
# Check that there are our submitted transactions in the TX memory pool
310-
json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents' + self.FORMAT_SEPARATOR + 'json')
304+
json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents.json')
311305
json_obj = json.loads(json_string)
312306
for i, tx in enumerate(txs):
313307
assert_equal(tx in json_obj, True)
@@ -319,14 +313,14 @@ def run_test(self):
319313
self.sync_all()
320314

321315
# Check if the 3 tx show up in the new block
322-
json_string = http_get_call(url.hostname, url.port, '/rest/block/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json')
316+
json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(newblockhash[0]))
323317
json_obj = json.loads(json_string)
324318
for tx in json_obj['tx']:
325319
if 'coinbase' not in tx['vin'][0]: # exclude coinbase
326320
assert_equal(tx['txid'] in txs, True)
327321

328322
# Check the same but without tx details
329-
json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json')
323+
json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/{}.json'.format(newblockhash[0]))
330324
json_obj = json.loads(json_string)
331325
for tx in txs:
332326
assert_equal(tx in json_obj['tx'], True)

0 commit comments

Comments
 (0)