Orange-OpenSource / hurl

Hurl, run and test HTTP requests with plain text.
https://hurl.dev
Apache License 2.0
12.89k stars 482 forks source link

Add asserts for Hexadecimal values #1320

Open claytonneal opened 1 year ago

claytonneal commented 1 year ago

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

fabricereix commented 1 year ago

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)

claytonneal commented 1 year ago

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

jcamiel commented 1 year ago
  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.

fabricereix commented 1 year ago

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