Changeset View
Changeset View
Standalone View
Standalone View
test/functional/interface_rest.py
Show All 15 Lines | |||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_greater_than, | assert_greater_than, | ||||
assert_greater_than_or_equal, | assert_greater_than_or_equal, | ||||
hex_str_to_bytes, | hex_str_to_bytes, | ||||
) | ) | ||||
from test_framework.messages import BLOCK_HEADER_SIZE | |||||
class ReqType(Enum): | class ReqType(Enum): | ||||
JSON = 1 | JSON = 1 | ||||
BIN = 2 | BIN = 2 | ||||
HEX = 3 | HEX = 3 | ||||
class RetType(Enum): | class RetType(Enum): | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.sync_all() | self.sync_all() | ||||
self.log.info("Test the /block, /blockhashbyheight and /headers URIs") | self.log.info("Test the /block, /blockhashbyheight and /headers URIs") | ||||
bb_hash = self.nodes[0].getbestblockhash() | bb_hash = self.nodes[0].getbestblockhash() | ||||
# Check binary format | # Check binary format | ||||
response = self.test_rest_request( | response = self.test_rest_request( | ||||
"/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) | "/block/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) | ||||
assert_greater_than(int(response.getheader('content-length')), 80) | assert_greater_than(int(response.getheader( | ||||
'content-length')), BLOCK_HEADER_SIZE) | |||||
response_bytes = response.read() | response_bytes = response.read() | ||||
# Compare with block header | # Compare with block header | ||||
response_header = self.test_rest_request( | response_header = self.test_rest_request( | ||||
"/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) | "/headers/1/{}".format(bb_hash), req_type=ReqType.BIN, ret_type=RetType.OBJ) | ||||
assert_equal(int(response_header.getheader('content-length')), 80) | assert_equal(int(response_header.getheader( | ||||
'content-length')), BLOCK_HEADER_SIZE) | |||||
response_header_bytes = response_header.read() | response_header_bytes = response_header.read() | ||||
assert_equal(response_bytes[0:80], response_header_bytes) | assert_equal( | ||||
response_bytes[0:BLOCK_HEADER_SIZE], response_header_bytes) | |||||
# Check block hex format | # Check block hex format | ||||
response_hex = self.test_rest_request( | response_hex = self.test_rest_request( | ||||
"/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) | "/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) | ||||
assert_greater_than(int(response_hex.getheader('content-length')), 160) | assert_greater_than(int(response_hex.getheader( | ||||
'content-length')), BLOCK_HEADER_SIZE * 2) | |||||
response_hex_bytes = response_hex.read().strip(b'\n') | response_hex_bytes = response_hex.read().strip(b'\n') | ||||
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes) | assert_equal(binascii.hexlify(response_bytes), response_hex_bytes) | ||||
# Compare with hex block header | # Compare with hex block header | ||||
response_header_hex = self.test_rest_request( | response_header_hex = self.test_rest_request( | ||||
"/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) | "/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ) | ||||
assert_greater_than( | assert_greater_than( | ||||
int(response_header_hex.getheader('content-length')), 160) | int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE * 2) | ||||
response_header_hex_bytes = response_header_hex.read(160) | response_header_hex_bytes = response_header_hex.read( | ||||
BLOCK_HEADER_SIZE * 2) | |||||
assert_equal(binascii.hexlify( | assert_equal(binascii.hexlify( | ||||
response_bytes[:80]), response_header_hex_bytes) | response_bytes[:BLOCK_HEADER_SIZE]), response_header_hex_bytes) | ||||
# Check json format | # Check json format | ||||
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash)) | block_json_obj = self.test_rest_request("/block/{}".format(bb_hash)) | ||||
assert_equal(block_json_obj['hash'], bb_hash) | assert_equal(block_json_obj['hash'], bb_hash) | ||||
assert_equal(self.test_rest_request( | assert_equal(self.test_rest_request( | ||||
"/blockhashbyheight/{}".format(block_json_obj['height']))['blockhash'], bb_hash) | "/blockhashbyheight/{}".format(block_json_obj['height']))['blockhash'], bb_hash) | ||||
# Check hex/bin format | # Check hex/bin format | ||||
▲ Show 20 Lines • Show All 110 Lines • Show Last 20 Lines |