2828 MiniWallet ,
2929 getnewdestination ,
3030)
31- from typing import Optional
3231
3332
3433INVALID_PARAM = "abc"
@@ -66,13 +65,16 @@ def test_rest_request(
6665 body : str = '' ,
6766 status : int = 200 ,
6867 ret_type : RetType = RetType .JSON ,
69- query_params : Optional [dict [str , typing .Any ]] = None ,
68+ query_params : typing . Union [dict [str , typing .Any ], str , None ] = None ,
7069 ) -> typing .Union [http .client .HTTPResponse , bytes , str , None ]:
7170 rest_uri = '/rest' + uri
7271 if req_type in ReqType :
7372 rest_uri += f'.{ req_type .name .lower ()} '
7473 if query_params :
75- rest_uri += f'?{ urllib .parse .urlencode (query_params )} '
74+ if isinstance (query_params , str ):
75+ rest_uri += f'?{ query_params } '
76+ else :
77+ rest_uri += f'?{ urllib .parse .urlencode (query_params )} '
7678
7779 conn = http .client .HTTPConnection (self .url .hostname , self .url .port )
7880 self .log .debug (f'{ http_method } { rest_uri } { body } ' )
@@ -82,7 +84,7 @@ def test_rest_request(
8284 conn .request ('POST' , rest_uri , body )
8385 resp = conn .getresponse ()
8486
85- assert_equal ( resp .status , status )
87+ assert resp .status == status , f"Expected: { status } , Got: { resp . status } - Response: { str ( resp . read ()) } "
8688
8789 if ret_type == RetType .OBJ :
8890 return resp
@@ -455,6 +457,52 @@ def run_test(self):
455457 expected = [(p ["scriptPubKey" ], p ["value" ]) for p in prevouts ]
456458 assert_equal (expected , actual )
457459
460+ self .log .info ("Test the /blockpart URI" )
461+
462+ blockhash = self .nodes [0 ].getbestblockhash ()
463+ block_bin = self .test_rest_request (f"/block/{ blockhash } " , req_type = ReqType .BIN , ret_type = RetType .BYTES )
464+ for req_type in (ReqType .BIN , ReqType .HEX ):
465+ def get_block_part (status : int = 200 , ** kwargs ):
466+ resp = self .test_rest_request (f"/blockpart/{ blockhash } " , status = status ,
467+ req_type = req_type , ret_type = RetType .BYTES , ** kwargs )
468+ assert isinstance (resp , bytes )
469+ if req_type is ReqType .HEX and status == 200 :
470+ resp = bytes .fromhex (resp .decode ().strip ())
471+ return resp
472+
473+ assert_equal (block_bin , get_block_part (query_params = {"offset" : 0 , "size" : len (block_bin )}))
474+
475+ assert len (block_bin ) >= 500
476+ assert_equal (block_bin [20 :320 ], get_block_part (query_params = {"offset" : 20 , "size" : 300 }))
477+ assert_equal (block_bin [- 5 :], get_block_part (query_params = {"offset" : len (block_bin ) - 5 , "size" : 5 }))
478+
479+ get_block_part (status = 400 , query_params = {"offset" : 10 })
480+ get_block_part (status = 400 , query_params = {"size" : 100 })
481+ get_block_part (status = 400 , query_params = {"offset" : "x" })
482+ get_block_part (status = 400 , query_params = {"size" : "y" })
483+ get_block_part (status = 400 , query_params = {"offset" : "x" , "size" : "y" })
484+ assert get_block_part (status = 400 , query_params = "%XY" ).decode ("utf-8" ).startswith ("URI parsing failed" )
485+
486+ get_block_part (status = 400 , query_params = {"offset" : 0 , "size" : 0 })
487+ get_block_part (status = 400 , query_params = {"offset" : len (block_bin ), "size" : 0 })
488+ get_block_part (status = 400 , query_params = {"offset" : len (block_bin ) + 1 , "size" : 1 })
489+ get_block_part (status = 400 , query_params = {"offset" : len (block_bin ), "size" : 1 })
490+ get_block_part (status = 400 , query_params = {"offset" : len (block_bin ) + 1 , "size" : 1 })
491+ get_block_part (status = 400 , query_params = {"offset" : 0 , "size" : len (block_bin ) + 1 })
492+
493+ self .test_rest_request (f"/blockpart/{ blockhash } " , status = 400 , req_type = ReqType .JSON , ret_type = RetType .OBJ )
494+
495+ self .log .info ("Missing block data should cause REST API to fail" )
496+
497+ self .test_rest_request (f"/block/{ blockhash } " , status = 200 , req_type = ReqType .BIN , ret_type = RetType .OBJ )
498+ self .test_rest_request (f"/blockpart/{ blockhash } " , query_params = {"offset" : 0 , "size" : 1 }, status = 200 , req_type = ReqType .BIN , ret_type = RetType .OBJ )
499+ blk_files = list (self .nodes [0 ].blocks_path .glob ("blk*.dat" ))
500+ for blk_file in blk_files :
501+ blk_file .rename (blk_file .with_suffix ('.bkp' ))
502+ self .test_rest_request (f"/block/{ blockhash } " , status = 500 , req_type = ReqType .BIN , ret_type = RetType .OBJ )
503+ self .test_rest_request (f"/blockpart/{ blockhash } " , query_params = {"offset" : 0 , "size" : 1 }, status = 500 , req_type = ReqType .BIN , ret_type = RetType .OBJ )
504+ for blk_file in blk_files :
505+ blk_file .with_suffix ('.bkp' ).rename (blk_file )
458506
459507 self .log .info ("Test the /deploymentinfo URI" )
460508
0 commit comments