hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Add asserts for Hexadecimal values

Open claytonneal opened this issue 1 year ago • 4 comments

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

claytonneal avatar Mar 01 '23 13:03 claytonneal

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)

fabricereix avatar Mar 01 '23 14:03 fabricereix

Sure an example response is json:

{
    "jsonrpc": "2.0",
    "id": 0,
    "result": "0x18e752b"
}

here i want to do asserts:

  1. result is a hexadecimal string
  2. result as a decimal is equal to X
  3. result as a decimal is greater than Y

claytonneal avatar Mar 02 '23 10:03 claytonneal

  1. can be tested currently with a simple matches, and 2. and 3. could be executed with a toInt 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.

jcamiel avatar Mar 02 '23 21:03 jcamiel

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

fabricereix avatar Mar 03 '23 07:03 fabricereix