Browse Source

tests: GET requests cannot have request body, use POST in rest.py

Sending a request body with GET request is not valid in HTTP spec, and
not compatible with evhttpd.
0.13
Wladimir J. van der Laan 9 years ago
parent
commit
ee2a42b447
  1. 42
      qa/rpc-tests/rest.py

42
qa/rpc-tests/rest.py

@ -32,10 +32,20 @@ def deser_uint256(f):
r += t << (i * 32) r += t << (i * 32)
return r return r
#allows simple http get calls with a request body #allows simple http get calls
def http_get_call(host, port, path, requestdata = '', response_object = 0): def http_get_call(host, port, path, response_object = 0):
conn = httplib.HTTPConnection(host, port) conn = httplib.HTTPConnection(host, port)
conn.request('GET', path, requestdata) conn.request('GET', path)
if response_object:
return conn.getresponse()
return conn.getresponse().read()
#allows simple http post calls with a request body
def http_post_call(host, port, path, requestdata = '', response_object = 0):
conn = httplib.HTTPConnection(host, port)
conn.request('POST', path, requestdata)
if response_object: if response_object:
return conn.getresponse() return conn.getresponse()
@ -137,7 +147,7 @@ class RESTTest (BitcoinTestFramework):
binaryRequest += binascii.unhexlify(vintx); binaryRequest += binascii.unhexlify(vintx);
binaryRequest += pack("i", 0); binaryRequest += pack("i", 0);
bin_response = http_get_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest) bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
output = StringIO.StringIO() output = StringIO.StringIO()
output.write(bin_response) output.write(bin_response)
output.seek(0) output.seek(0)
@ -175,14 +185,14 @@ class RESTTest (BitcoinTestFramework):
#do some invalid requests #do some invalid requests
json_request = '{"checkmempool' json_request = '{"checkmempool'
response = http_get_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'json', json_request, True) response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'json', json_request, True)
assert_equal(response.status, 500) #must be a 500 because we send a invalid json request assert_equal(response.status, 500) #must be a 500 because we send a invalid json request
json_request = '{"checkmempool' json_request = '{"checkmempool'
response = http_get_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', json_request, True) response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', json_request, True)
assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request
response = http_get_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.FORMAT_SEPARATOR+'bin', '', True) response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.FORMAT_SEPARATOR+'bin', '', True)
assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request
#test limits #test limits
@ -190,14 +200,14 @@ class RESTTest (BitcoinTestFramework):
for x in range(0, 20): for x in range(0, 20):
json_request += txid+'-'+str(n)+'/' json_request += txid+'-'+str(n)+'/'
json_request = json_request.rstrip("/") json_request = json_request.rstrip("/")
response = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True) response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
assert_equal(response.status, 500) #must be a 500 because we exceeding the limits assert_equal(response.status, 500) #must be a 500 because we exceeding the limits
json_request = '/checkmempool/' json_request = '/checkmempool/'
for x in range(0, 15): for x in range(0, 15):
json_request += txid+'-'+str(n)+'/' json_request += txid+'-'+str(n)+'/'
json_request = json_request.rstrip("/"); json_request = json_request.rstrip("/");
response = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True) response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
assert_equal(response.status, 200) #must be a 500 because we exceeding the limits assert_equal(response.status, 200) #must be a 500 because we exceeding the limits
self.nodes[0].generate(1) #generate block to not affect upcoming tests self.nodes[0].generate(1) #generate block to not affect upcoming tests
@ -208,27 +218,27 @@ class RESTTest (BitcoinTestFramework):
################ ################
# check binary format # check binary format
response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", "", True) response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
assert_equal(response.status, 200) assert_equal(response.status, 200)
assert_greater_than(int(response.getheader('content-length')), 80) assert_greater_than(int(response.getheader('content-length')), 80)
response_str = response.read() response_str = response.read()
# compare with block header # compare with block header
response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", "", True) response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
assert_equal(response_header.status, 200) assert_equal(response_header.status, 200)
assert_equal(int(response_header.getheader('content-length')), 80) assert_equal(int(response_header.getheader('content-length')), 80)
response_header_str = response_header.read() response_header_str = response_header.read()
assert_equal(response_str[0:80], response_header_str) assert_equal(response_str[0:80], response_header_str)
# check block hex format # check block hex format
response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", "", True) response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
assert_equal(response_hex.status, 200) assert_equal(response_hex.status, 200)
assert_greater_than(int(response_hex.getheader('content-length')), 160) assert_greater_than(int(response_hex.getheader('content-length')), 160)
response_hex_str = response_hex.read() response_hex_str = response_hex.read()
assert_equal(response_str.encode("hex")[0:160], response_hex_str[0:160]) assert_equal(response_str.encode("hex")[0:160], response_hex_str[0:160])
# compare with hex block header # compare with hex block header
response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", "", True) response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
assert_equal(response_header_hex.status, 200) assert_equal(response_header_hex.status, 200)
assert_greater_than(int(response_header_hex.getheader('content-length')), 160) assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
response_header_hex_str = response_header_hex.read() response_header_hex_str = response_header_hex.read()
@ -241,7 +251,7 @@ class RESTTest (BitcoinTestFramework):
assert_equal(block_json_obj['hash'], bb_hash) assert_equal(block_json_obj['hash'], bb_hash)
# compare with json block header # compare with json block header
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", "", True) response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
assert_equal(response_header_json.status, 200) assert_equal(response_header_json.status, 200)
response_header_json_str = response_header_json.read() response_header_json_str = response_header_json.read()
json_obj = json.loads(response_header_json_str, parse_float=decimal.Decimal) json_obj = json.loads(response_header_json_str, parse_float=decimal.Decimal)
@ -265,7 +275,7 @@ class RESTTest (BitcoinTestFramework):
#see if we can get 5 headers in one response #see if we can get 5 headers in one response
self.nodes[1].generate(5) self.nodes[1].generate(5)
self.sync_all() self.sync_all()
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", "", True) response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
assert_equal(response_header_json.status, 200) assert_equal(response_header_json.status, 200)
response_header_json_str = response_header_json.read() response_header_json_str = response_header_json.read()
json_obj = json.loads(response_header_json_str) json_obj = json.loads(response_header_json_str)
@ -278,7 +288,7 @@ class RESTTest (BitcoinTestFramework):
assert_equal(json_obj['txid'], tx_hash) assert_equal(json_obj['txid'], tx_hash)
# check hex format response # check hex format response
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", "", True) hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
assert_equal(hex_string.status, 200) assert_equal(hex_string.status, 200)
assert_greater_than(int(response.getheader('content-length')), 10) assert_greater_than(int(response.getheader('content-length')), 10)

Loading…
Cancel
Save