Orange-OpenSource / hurl

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

Leading zeros are trimmed from variables #1314

Open tnicolaisen-snabble opened 1 year ago

tnicolaisen-snabble commented 1 year ago

Summary

We're calling a service that requires day-of-year as a 3 digit number, "001" being Jan 1st, and so on.

When running a hurl script with a variable that contains leading zeros, they get trimmed.

Steps to reproduce

Hurl file:

POST http://foo
{
    "dayOfYear": "{{dayOfYear}}"
}
HTTP 200
hurl --very-verbose --variable dayOfYear="003" sandbox.hurl

What is the current bug behavior?

The output is:

* Options:
*     fail fast: true
*     follow redirect: false
*     insecure: false
*     max redirect: 50
*     retry: false
*     retry max count: 10
* Variables:
*     dayOfYear: 3
* ------------------------------------------------------------------------------
* Executing entry 1
*
* Cookie store:
*
* Request:
* POST http://foo
*
* Implicit content-type=application/json
*
* Request can be run with the following curl command:
* curl -H 'Content-Type: application/json' --data $'{\n    "dayOfYear": "3"\n}' 'http://foo'

What is the expected correct behavior?

I expected the resulting "dayOfYear" to have the value "003".

Output of checks

hurl --version:

hurl 2.0.1 libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libssh/0.9.3/openssl/zlib nghttp2/1.40.0
Features (libcurl):  AsynchDNS brotli HTTP2 IDN IPv6 Largefile libz NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets
Features (built-in): brotli

Thanks for having a look!

jcamiel commented 1 year ago

Hi, thanks for the detailed report.

Variables are typed, but we haven't well documented what are the conversion rules. For instance ,

$ hurl --very-verbose --variable enable="true" /tmp/test.hurl

The variable enable will be considered by Hurl as a boolean value. If you really want to use true as a string, you can write:

hurl --very-verbose --variable enable='"true"' /tmp/test.hurl

For your case, you can use:

hurl --very-verbose --variable dayOfYear='"003"' /tmp/test.hurl

And dayOfYear will be considered as the string 003.

We definitively should document it, and see if we need an exception for input like 000123456.

tnicolaisen-snabble commented 1 year ago

@jcamiel Got it! Thanks for the swift response, on a Sunday no less :)

tnicolaisen-snabble commented 1 year ago

For reference, it took a bit more fiddling to pass the parameter in through a shell script. This worked:

dayOfYear=\"$(date '+%j')\"
hurl --variable dayOfYear="${dayOfYear}" sandbox.hurl
jcamiel commented 1 year ago

Thanks, we're still going to add an exception for the int case, i.e considering 0000123456789 as a string! See @fabricereix comment

fabricereix commented 1 year ago

A few examples in the bash

a=001          integer <1>      # the leading zeros are removed by Hurl
a="001"        integer <1>      # the quote are removed by the shell
a=\"001\"      string  <001>    # the quotes are passed to Hurl that can treat the value as a String
a=" 001"       string  < 001>   # Hurl treats the value as String because it contains a space
a=\"\"001\"\"  string  <"001">  # one pair of quotes is used to type the value as string, one is part of the value  

idem for boolean

a=true         boolean <true>     
a="true"       boolean <true>   # the quote are removed by the shell
a=\"true\"     string  <true>   # the quotes are passed to Hurl that can treat the value as a String
a=" true"      string  < true>  # Hurl treats the value as String because it contains a space
a=\"\"true\"\" string  <"true"> # one pair of quotes is used to type the value as string, one is part of the value  
fabricereix commented 1 year ago

We will still parse 0000123456789 as an integer (like Rust). We will add theses examples to the documentation.