10
10
from io import BytesIO
11
11
import json
12
12
from struct import pack , unpack
13
+ import typing
13
14
import urllib .parse
14
15
15
16
@@ -57,14 +58,21 @@ def set_test_params(self):
57
58
58
59
self .supports_cli = False
59
60
60
- def test_rest_request (self , uri , http_method = 'GET' , req_type = ReqType .JSON , body = '' , status = 200 , ret_type = RetType .JSON ):
61
+ def test_rest_request (
62
+ self ,
63
+ uri : str ,
64
+ http_method : str = 'GET' ,
65
+ req_type : ReqType = ReqType .JSON ,
66
+ body : str = '' ,
67
+ status : int = 200 ,
68
+ ret_type : RetType = RetType .JSON ,
69
+ query_params : typing .Dict [str , typing .Any ] = None ,
70
+ ) -> typing .Union [http .client .HTTPResponse , bytes , str , None ]:
61
71
rest_uri = '/rest' + uri
62
- if req_type == ReqType .JSON :
63
- rest_uri += '.json'
64
- elif req_type == ReqType .BIN :
65
- rest_uri += '.bin'
66
- elif req_type == ReqType .HEX :
67
- rest_uri += '.hex'
72
+ if req_type in ReqType :
73
+ rest_uri += f'.{ req_type .name .lower ()} '
74
+ if query_params :
75
+ rest_uri += f'?{ urllib .parse .urlencode (query_params )} '
68
76
69
77
conn = http .client .HTTPConnection (self .url .hostname , self .url .port )
70
78
self .log .debug (f'{ http_method } { rest_uri } { body } ' )
@@ -83,6 +91,8 @@ def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body=
83
91
elif ret_type == RetType .JSON :
84
92
return json .loads (resp .read ().decode ('utf-8' ), parse_float = Decimal )
85
93
94
+ return None
95
+
86
96
def run_test (self ):
87
97
self .url = urllib .parse .urlparse (self .nodes [0 ].url )
88
98
self .wallet = MiniWallet (self .nodes [0 ])
@@ -213,12 +223,12 @@ def run_test(self):
213
223
bb_hash = self .nodes [0 ].getbestblockhash ()
214
224
215
225
# Check result if block does not exists
216
- assert_equal (self .test_rest_request (f"/headers/1/ { UNKNOWN_PARAM } " ), [])
226
+ assert_equal (self .test_rest_request (f"/headers/{ UNKNOWN_PARAM } " , query_params = { "count" : 1 } ), [])
217
227
self .test_rest_request (f"/block/{ UNKNOWN_PARAM } " , status = 404 , ret_type = RetType .OBJ )
218
228
219
229
# Check result if block is not in the active chain
220
230
self .nodes [0 ].invalidateblock (bb_hash )
221
- assert_equal (self .test_rest_request (f'/headers/1/ { bb_hash } ' ), [])
231
+ assert_equal (self .test_rest_request (f'/headers/{ bb_hash } ' , query_params = { 'count' : 1 } ), [])
222
232
self .test_rest_request (f'/block/{ bb_hash } ' )
223
233
self .nodes [0 ].reconsiderblock (bb_hash )
224
234
@@ -228,7 +238,7 @@ def run_test(self):
228
238
response_bytes = response .read ()
229
239
230
240
# Compare with block header
231
- response_header = self .test_rest_request (f"/headers/1/ { bb_hash } " , req_type = ReqType .BIN , ret_type = RetType .OBJ )
241
+ response_header = self .test_rest_request (f"/headers/{ bb_hash } " , req_type = ReqType .BIN , ret_type = RetType .OBJ , query_params = { "count" : 1 } )
232
242
assert_equal (int (response_header .getheader ('content-length' )), BLOCK_HEADER_SIZE )
233
243
response_header_bytes = response_header .read ()
234
244
assert_equal (response_bytes [:BLOCK_HEADER_SIZE ], response_header_bytes )
@@ -240,7 +250,7 @@ def run_test(self):
240
250
assert_equal (response_bytes .hex ().encode (), response_hex_bytes )
241
251
242
252
# Compare with hex block header
243
- response_header_hex = self .test_rest_request (f"/headers/1/ { bb_hash } " , req_type = ReqType .HEX , ret_type = RetType .OBJ )
253
+ response_header_hex = self .test_rest_request (f"/headers/{ bb_hash } " , req_type = ReqType .HEX , ret_type = RetType .OBJ , query_params = { "count" : 1 } )
244
254
assert_greater_than (int (response_header_hex .getheader ('content-length' )), BLOCK_HEADER_SIZE * 2 )
245
255
response_header_hex_bytes = response_header_hex .read (BLOCK_HEADER_SIZE * 2 )
246
256
assert_equal (response_bytes [:BLOCK_HEADER_SIZE ].hex ().encode (), response_header_hex_bytes )
@@ -267,7 +277,7 @@ def run_test(self):
267
277
self .test_rest_request ("/blockhashbyheight/" , ret_type = RetType .OBJ , status = 400 )
268
278
269
279
# Compare with json block header
270
- json_obj = self .test_rest_request (f"/headers/1/ { bb_hash } " )
280
+ json_obj = self .test_rest_request (f"/headers/{ bb_hash } " , query_params = { "count" : 1 } )
271
281
assert_equal (len (json_obj ), 1 ) # ensure that there is one header in the json response
272
282
assert_equal (json_obj [0 ]['hash' ], bb_hash ) # request/response hash should be the same
273
283
@@ -278,9 +288,9 @@ def run_test(self):
278
288
279
289
# See if we can get 5 headers in one response
280
290
self .generate (self .nodes [1 ], 5 )
281
- json_obj = self .test_rest_request (f"/headers/5/ { bb_hash } " )
291
+ json_obj = self .test_rest_request (f"/headers/{ bb_hash } " , query_params = { "count" : 5 } )
282
292
assert_equal (len (json_obj ), 5 ) # now we should have 5 header objects
283
- json_obj = self .test_rest_request (f"/blockfilterheaders/basic/5/ { bb_hash } " )
293
+ json_obj = self .test_rest_request (f"/blockfilterheaders/basic/{ bb_hash } " , query_params = { "count" : 5 } )
284
294
first_filter_header = json_obj [0 ]
285
295
assert_equal (len (json_obj ), 5 ) # now we should have 5 filter header objects
286
296
json_obj = self .test_rest_request (f"/blockfilter/basic/{ bb_hash } " )
@@ -294,7 +304,7 @@ def run_test(self):
294
304
for num in ['5a' , '-5' , '0' , '2001' , '99999999999999999999999999999999999' ]:
295
305
assert_equal (
296
306
bytes (f'Header count is invalid or out of acceptable range (1-2000): { num } \r \n ' , 'ascii' ),
297
- self .test_rest_request (f"/headers/{ num } / { bb_hash } " , ret_type = RetType .BYTES , status = 400 ),
307
+ self .test_rest_request (f"/headers/{ bb_hash } " , ret_type = RetType .BYTES , status = 400 , query_params = { "count" : num } ),
298
308
)
299
309
300
310
self .log .info ("Test tx inclusion in the /mempool and /block URIs" )
@@ -351,6 +361,11 @@ def run_test(self):
351
361
json_obj = self .test_rest_request ("/chaininfo" )
352
362
assert_equal (json_obj ['bestblockhash' ], bb_hash )
353
363
364
+ # Test compatibility of deprecated and newer endpoints
365
+ self .log .info ("Test compatibility of deprecated and newer endpoints" )
366
+ assert_equal (self .test_rest_request (f"/headers/{ bb_hash } " , query_params = {"count" : 1 }), self .test_rest_request (f"/headers/1/{ bb_hash } " ))
367
+ assert_equal (self .test_rest_request (f"/blockfilterheaders/basic/{ bb_hash } " , query_params = {"count" : 1 }), self .test_rest_request (f"/blockfilterheaders/basic/5/{ bb_hash } " ))
368
+
354
369
355
370
if __name__ == '__main__' :
356
371
RESTTest ().main ()
0 commit comments