hurl
hurl copied to clipboard
Add asserts for Hexadecimal values
Problem to solve
I want to check that a jsonpath value is hexadecimal, and then convert it to decimal and use the regular ==, !=, etc. predicates on the decimal value. In blockchain RPC api, alot of values are in hex format
Proposal
Add new predicate: isHex For other predicates (==, !=, >, < etc.) be able to handle hexadecimal values
Additional context and resources
In blockchain RPC api, a lot of values are in hex format
Tasks to complete
Hi @claytonneal ,
can you give a few concrete examples?
your values are not prefixed with 0x
?
we could have the following filter toInt BASE
it could be called without parameter (base =10 by default)
Sure an example response is json:
{
"jsonrpc": "2.0",
"id": 0,
"result": "0x18e752b"
}
here i want to do asserts:
- result is a hexadecimal string
- result as a decimal is equal to X
- result as a decimal is greater than Y
- can be tested currently with a simple
matches
, and 2. and 3. could be executed with atoInt
filter provided we add a radix, as @fabricereix proposed:
GET https://foo.com
HTTP 200
[Asserts]
jsonpath "$.result" matches /^0x[0-9a-f]+$/
jsonpath "$.result" toInt 16 == 26113323
jsonpath "$.result" toInt 16 > 1000
I quite like that we support it with and without 0x
prefix. Without allowing 0x
as a prefix, we could write also:
GET https://foo.com
HTTP 200
[Asserts]
jsonpath "$.result" matches /^0x[0-9a-f]+$/
jsonpath "$.result" regex /^0x([0-9a-f]+)$/ toInt 16 == 26113323
jsonpath "$.result" regex /^0x([0-9a-f]+)$/ toInt 16 > 1000
or with a temporary variable:
GET https://foo.com
HTTP 200
[Captures]
result: jsonpath "$.result" regex /^0x([0-9a-f]+)$/
[Asserts]
variable "result" matches /^[0-9a-f]+$/
variable "result" toInt 16 == 26113323
variable "result" toInt 16 > 1000
toInt
with a radix (default to 10) seems a good addition.
I also think we should support parsing an hex string starting with 0x
.
There is no ambiguity and this is also the behavior in Python
>>> int('ff', 16)
255
>>> int('0xff', 16)
255