format code and sort filter in order

This commit is contained in:
Haoyu Cheng 2024-05-14 10:17:44 -04:00 committed by hurl-bot
parent 1ea8936c42
commit 4e588e9bbf
No known key found for this signature in database
GPG Key ID: 1283A2B4A0DCAF8D
15 changed files with 44 additions and 50 deletions

View File

@ -226,6 +226,16 @@ jsonpath "$.published" toDate "%Y-%m-%dT%H:%M:%S%.fZ" format "%A" == "Monday"
jsonpath "$.published" toDate "%+" format "%A" == "Monday" # %+ can be used to parse ISO 8601 / RFC 3339
```
### toFloat
Converts to float number.
```hurl
GET https://example.org/foo
HTTP 200
[Asserts]
jsonpath "$.pi" toFloat == 3.14
```
### toInt
@ -238,17 +248,6 @@ HTTP 200
jsonpath "$.id" toInt == 123
```
### toFloat
Converts to float number.
```hurl
GET https://example.org/foo
HTTP 200
[Asserts]
jsonpath "$.pi" toFloat == 3.14
```
### urlDecode
Replaces %xx escapes with their single-character equivalent.

View File

@ -292,8 +292,8 @@ Short description:
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="replace-filter">replace-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">replace</span>&nbsp;<a href="#sp">sp</a>&nbsp;<span class="grammar-symbol">(</span><a href="#quoted-string">quoted-string</a><span class="grammar-symbol">|</span><a href="#regex">regex</a><span class="grammar-symbol">)</span>&nbsp;<a href="#sp">sp</a>&nbsp;<a href="#quoted-string">quoted-string</a></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="split-filter">split-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">split</span>&nbsp;<a href="#sp">sp</a>&nbsp;<a href="#quoted-string">quoted-string</a></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="to-date-filter">to-date-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">toDate</span></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="to-int-filter">to-int-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">toInt</span></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="to-float-filter">to-float-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">toFloat</span></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="to-int-filter">to-int-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">toInt</span></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="url-decode-filter">url-decode-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">urlDecode</span></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="url-encode-filter">url-encode-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">urlEncode</span></div></div>
<div class="grammar-rule"><div class="grammar-rule-declaration"><span class="grammar-rule-id" id="xpath-filter">xpath-filter</span><span class="grammar-usedby">(used by <a href="#filter">filter</a>)</span></div><div class="grammar-rule-expression"><span class="grammar-literal">xpath</span>&nbsp;<a href="#sp">sp</a>&nbsp;<a href="#quoted-string">quoted-string</a></div></div>

View File

@ -537,10 +537,10 @@ split-filter: "split" sp quoted-string
to-date-filter: "toDate"
to-int-filter: "toInt"
to-float-filter: "toFloat"
to-int-filter: "toInt"
url-decode-filter: "urlDecode"
url-encode-filter: "urlEncode"

View File

@ -17,8 +17,8 @@
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.ips"</span> <span class="filter-type">replace</span> <span class="string">", "</span> <span class="string">"|"</span> <span class="predicate-type">==</span> <span class="string">"192.168.2.1|10.0.0.20|10.0.0.10"</span></span> <span class="comment"># replace</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.ips"</span> <span class="filter-type">split</span> <span class="string">", "</span> <span class="filter-type">count</span> <span class="predicate-type">==</span> <span class="number">3</span></span> <span class="comment"># split</span>
<span class="line"><span class="query-type">header</span> <span class="string">"Expires"</span> <span class="filter-type">toDate</span> <span class="string">"%a, %d %b %Y %H:%M:%S GMT"</span> <span class="filter-type">daysBeforeNow</span> <span class="predicate-type">&gt;</span> <span class="number">1000</span></span> <span class="comment"># toDate</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.id"</span> <span class="filter-type">toInt</span> <span class="predicate-type">==</span> <span class="number">123</span></span> <span class="comment"># toInt</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.pi"</span> <span class="filter-type">toFloat</span> <span class="predicate-type">==</span> <span class="number">3.14</span></span> <span class="comment"># toFloat</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.id"</span> <span class="filter-type">toInt</span> <span class="predicate-type">==</span> <span class="number">123</span></span> <span class="comment"># toInt</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.encoded_url"</span> <span class="filter-type">urlDecode</span> <span class="predicate-type">==</span> <span class="string">"https://mozilla.org/?x=шеллы"</span></span> <span class="comment"># urlDecode</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.url"</span> <span class="filter-type">urlEncode</span> <span class="predicate-type">==</span> <span class="string">"https%3A//mozilla.org/%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"</span></span> <span class="comment"># urlEncode</span>
<span class="line"><span class="query-type">bytes</span> <span class="filter-type">decode</span> <span class="string">"iso-8859-1"</span> <span class="filter-type">xpath</span> <span class="string">"string(//p)"</span> <span class="predicate-type">==</span> <span class="string">"Hello"</span></span> <span class="comment"># xpath</span>

View File

@ -17,8 +17,8 @@ body regex /Hello ([0-9]+)!/ == "Bob"
jsonpath "$.ips" replace ", " "|" == "192.168.2.1|10.0.0.20|10.0.0.10" # replace
jsonpath "$.ips" split ", " count == 3 # split
header "Expires" toDate "%a, %d %b %Y %H:%M:%S GMT" daysBeforeNow > 1000 # toDate
jsonpath "$.id" toInt == 123 # toInt
jsonpath "$.pi" toInt == 3.14 # toFloat
jsonpath "$.id" toInt == 123 # toInt
jsonpath "$.encoded_url" urlDecode == "https://mozilla.org/?x=шеллы" # urlDecode
jsonpath "$.url" urlEncode == "https%3A//mozilla.org/%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B" # urlEncode
bytes decode "iso-8859-1" xpath "string(//p)" == "Hello" # xpath

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/dummy"},"response":{"status":200,"captures":[{"name":"count","query":{"type":"jsonpath","expr":"$.books"},"filters":[{"type":"count"}]}],"asserts":[{"query":{"type":"jsonpath","expr":"$.books"},"filters":[{"type":"count"}],"predicate":{"type":"equal","value":12}},{"query":{"type":"certificate","expr":"Expire-Date"},"filters":[{"type":"daysAfterNow"}],"predicate":{"type":"greater","value":15}},{"query":{"type":"certificate","expr":"Start-Date"},"filters":[{"type":"daysBeforeNow"}],"predicate":{"type":"less","value":100}},{"query":{"type":"bytes"},"filters":[{"type":"decode","encoding":"iso-8859-1"}],"predicate":{"type":"equal","value":"café"}},{"query":{"type":"cookie","expr":"LSID[Expires]"},"filters":[{"type":"format","fmt":"%a, %d %b %Y %H:%M:%S"}],"predicate":{"type":"equal","value":"Wed, 13 Jan 2021 22:23:01"}},{"query":{"type":"jsonpath","expr":"$.text"},"filters":[{"type":"htmlEscape"}],"predicate":{"type":"equal","value":"a &gt; b"}},{"query":{"type":"jsonpath","expr":"$.escaped_html[1]"},"filters":[{"type":"htmlUnescape"}],"predicate":{"type":"equal","value":"<p>Hello</p>"}},{"query":{"type":"variable","name":"books"},"filters":[{"type":"jsonpath","expr":"$[0].name"}],"predicate":{"type":"equal","value":"Dune"}},{"query":{"type":"jsonpath","expr":"$.books"},"filters":[{"type":"nth","n":2}],"predicate":{"type":"equal","value":"Children of Dune"}},{"query":{"type":"body"},"filters":[{"type":"regex","expr":{"type":"regex","value":"Hello ([0-9]+)!"}}],"predicate":{"type":"equal","value":"Bob"}},{"query":{"type":"jsonpath","expr":"$.ips"},"filters":[{"type":"replace","old_value":", ","new_value":"|"}],"predicate":{"type":"equal","value":"192.168.2.1|10.0.0.20|10.0.0.10"}},{"query":{"type":"jsonpath","expr":"$.ips"},"filters":[{"type":"split","sep":", "},{"type":"count"}],"predicate":{"type":"equal","value":3}},{"query":{"type":"header","name":"Expires"},"filters":[{"type":"toDate","fmt":"%a, %d %b %Y %H:%M:%S GMT"},{"type":"daysBeforeNow"}],"predicate":{"type":"greater","value":1000}},{"query":{"type":"jsonpath","expr":"$.id"},"filters":[{"type":"toInt"}],"predicate":{"type":"equal","value":123}},{"query":{"type":"jsonpath","expr":"$.pi"},"filters":[{"type": "toFloat"}],"predicate":{"type":"equal","value":3.14}},{"query":{"type":"jsonpath","expr":"$.encoded_url"},"filters":[{"type":"urlDecode"}],"predicate":{"type":"equal","value":"https://mozilla.org/?x=шеллы"}},{"query":{"type":"jsonpath","expr":"$.url"},"filters":[{"type":"urlEncode"}],"predicate":{"type":"equal","value":"https%3A//mozilla.org/%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"}},{"query":{"type":"bytes"},"filters":[{"type":"decode","encoding":"iso-8859-1"},{"type":"xpath","expr":"string(//p)"}],"predicate":{"type":"equal","value":"Hello"}}]}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/dummy"},"response":{"status":200,"captures":[{"name":"count","query":{"type":"jsonpath","expr":"$.books"},"filters":[{"type":"count"}]}],"asserts":[{"query":{"type":"jsonpath","expr":"$.books"},"filters":[{"type":"count"}],"predicate":{"type":"equal","value":12}},{"query":{"type":"certificate","expr":"Expire-Date"},"filters":[{"type":"daysAfterNow"}],"predicate":{"type":"greater","value":15}},{"query":{"type":"certificate","expr":"Start-Date"},"filters":[{"type":"daysBeforeNow"}],"predicate":{"type":"less","value":100}},{"query":{"type":"bytes"},"filters":[{"type":"decode","encoding":"iso-8859-1"}],"predicate":{"type":"equal","value":"café"}},{"query":{"type":"cookie","expr":"LSID[Expires]"},"filters":[{"type":"format","fmt":"%a, %d %b %Y %H:%M:%S"}],"predicate":{"type":"equal","value":"Wed, 13 Jan 2021 22:23:01"}},{"query":{"type":"jsonpath","expr":"$.text"},"filters":[{"type":"htmlEscape"}],"predicate":{"type":"equal","value":"a &gt; b"}},{"query":{"type":"jsonpath","expr":"$.escaped_html[1]"},"filters":[{"type":"htmlUnescape"}],"predicate":{"type":"equal","value":"<p>Hello</p>"}},{"query":{"type":"variable","name":"books"},"filters":[{"type":"jsonpath","expr":"$[0].name"}],"predicate":{"type":"equal","value":"Dune"}},{"query":{"type":"jsonpath","expr":"$.books"},"filters":[{"type":"nth","n":2}],"predicate":{"type":"equal","value":"Children of Dune"}},{"query":{"type":"body"},"filters":[{"type":"regex","expr":{"type":"regex","value":"Hello ([0-9]+)!"}}],"predicate":{"type":"equal","value":"Bob"}},{"query":{"type":"jsonpath","expr":"$.ips"},"filters":[{"type":"replace","old_value":", ","new_value":"|"}],"predicate":{"type":"equal","value":"192.168.2.1|10.0.0.20|10.0.0.10"}},{"query":{"type":"jsonpath","expr":"$.ips"},"filters":[{"type":"split","sep":", "},{"type":"count"}],"predicate":{"type":"equal","value":3}},{"query":{"type":"header","name":"Expires"},"filters":[{"type":"toDate","fmt":"%a, %d %b %Y %H:%M:%S GMT"},{"type":"daysBeforeNow"}],"predicate":{"type":"greater","value":1000}},{"query":{"type":"jsonpath","expr":"$.pi"},"filters":[{"type": "toFloat"}],"predicate":{"type":"equal","value":3.14}},{"query":{"type":"jsonpath","expr":"$.id"},"filters":[{"type":"toInt"}],"predicate":{"type":"equal","value":123}},{"query":{"type":"jsonpath","expr":"$.encoded_url"},"filters":[{"type":"urlDecode"}],"predicate":{"type":"equal","value":"https://mozilla.org/?x=шеллы"}},{"query":{"type":"jsonpath","expr":"$.url"},"filters":[{"type":"urlEncode"}],"predicate":{"type":"equal","value":"https%3A//mozilla.org/%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"}},{"query":{"type":"bytes"},"filters":[{"type":"decode","encoding":"iso-8859-1"},{"type":"xpath","expr":"string(//p)"}],"predicate":{"type":"equal","value":"Hello"}}]}}]}

View File

@ -17,8 +17,8 @@ body regex /Hello ([0-9]+)!/ == "Bob"
jsonpath "$.ips" replace ", " "|" == "192.168.2.1|10.0.0.20|10.0.0.10" # replace
jsonpath "$.ips" split ", " count == 3 # split
header "Expires" toDate "%a, %d %b %Y %H:%M:%S GMT" daysBeforeNow > 1000 # toDate
jsonpath "$.id" toInt == 123 # toInt
jsonpath "$.pi" toFloat == 3.14 # toFloat
jsonpath "$.id" toInt == 123 # toInt
jsonpath "$.encoded_url" urlDecode == "https://mozilla.org/?x=шеллы" # urlDecode
jsonpath "$.url" urlEncode == "https%3A//mozilla.org/%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B" # urlEncode
bytes decode "iso-8859-1" xpath "string(//p)" == "Hello" # xpath

View File

@ -32,8 +32,8 @@ use crate::runner::filter::regex::eval_regex;
use crate::runner::filter::replace::eval_replace;
use crate::runner::filter::split::eval_split;
use crate::runner::filter::to_date::eval_to_date;
use crate::runner::filter::to_int::eval_to_int;
use crate::runner::filter::to_float::eval_to_float;
use crate::runner::filter::to_int::eval_to_int;
use crate::runner::filter::url_decode::eval_url_decode;
use crate::runner::filter::url_encode::eval_url_encode;
use crate::runner::filter::xpath::eval_xpath;
@ -108,8 +108,8 @@ pub fn eval_filter(
FilterValue::ToDate { fmt, .. } => {
eval_to_date(value, fmt, variables, filter.source_info, in_assert)
}
FilterValue::ToInt => eval_to_int(value, filter.source_info, in_assert),
FilterValue::ToFloat => eval_to_float(value, filter.source_info, in_assert),
FilterValue::ToInt => eval_to_int(value, filter.source_info, in_assert),
FilterValue::UrlDecode => eval_url_decode(value, filter.source_info, in_assert),
FilterValue::UrlEncode => eval_url_encode(value, filter.source_info, in_assert),
FilterValue::XPath { expr, .. } => {

View File

@ -34,8 +34,8 @@ mod regex;
mod replace;
mod split;
mod to_date;
mod to_float;
mod to_int;
mod url_decode;
mod url_encode;
mod xpath;
mod to_float;

View File

@ -19,11 +19,11 @@ use hurl_core::ast::SourceInfo;
use crate::runner::{Error, Number, RunnerError, Value};
pub fn eval_to_float(
pub fn eval_to_float(
value: &Value,
source_info: SourceInfo,
assert: bool,
) -> Result<Option<Value>, Error> {
) -> Result<Option<Value>, Error> {
match value {
Value::Number(Number::Float(v)) => Ok(Some(Value::Number(Number::Float(*v)))),
Value::Number(Number::Integer(v)) => Ok(Some(Value::Number(Number::Float(*v as f64)))),
@ -39,10 +39,10 @@ use crate::runner::{Error, Number, RunnerError, Value};
Err(Error::new(source_info, inner, assert))
}
}
}
}
#[cfg(test)]
pub mod tests {
#[cfg(test)]
pub mod tests {
use crate::runner::filter::eval::eval_filter;
use crate::runner::{Number, RunnerError, Value};
@ -58,9 +58,9 @@ use crate::runner::{Error, Number, RunnerError, Value};
};
assert_eq!(
eval_filter(
&filter,
&Value::String("3.1415".to_string()),
&variable,
&filter,
&Value::String("3.1415".to_string()),
&variable,
false
)
.unwrap()
@ -121,17 +121,12 @@ use crate::runner::{Error, Number, RunnerError, Value};
err.inner,
RunnerError::FilterInvalidInput("string <3x.1415>".to_string())
);
let err = eval_filter(
&filter,
&Value::Bool(true),
&variable,
false
)
.err()
.unwrap();
let err = eval_filter(&filter, &Value::Bool(true), &variable, false)
.err()
.unwrap();
assert_eq!(
err.inner,
RunnerError::FilterInvalidInput("bool <true>".to_string())
);
}
}
}

View File

@ -906,8 +906,8 @@ pub enum FilterValue {
space0: Whitespace,
fmt: Template,
},
ToInt,
ToFloat,
ToInt,
UrlDecode,
UrlEncode,
XPath {

View File

@ -828,8 +828,8 @@ impl HtmlFormatter {
self.fmt_space(space0);
self.fmt_template(fmt);
}
FilterValue::ToInt => self.fmt_span("filter-type", "toInt"),
FilterValue::ToFloat => self.fmt_span("filter-type", "toFloat"),
FilterValue::ToInt => self.fmt_span("filter-type", "toInt"),
FilterValue::UrlDecode => self.fmt_span("filter-type", "urlDecode"),
FilterValue::UrlEncode => self.fmt_span("filter-type", "urlEncode"),
FilterValue::XPath { space0, expr } => {

View File

@ -64,8 +64,8 @@ pub fn filter(reader: &mut Reader) -> ParseResult<Filter> {
regex_filter,
replace_filter,
split_filter,
to_int_filter,
to_float_filter,
to_int_filter,
to_date_filter,
url_decode_filter,
url_encode_filter,
@ -176,16 +176,16 @@ fn to_date_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
Ok(FilterValue::ToDate { space0, fmt })
}
fn to_int_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("toInt", reader)?;
Ok(FilterValue::ToInt)
}
fn to_float_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("toFloat", reader)?;
Ok(FilterValue::ToFloat)
}
fn to_int_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("toInt", reader)?;
Ok(FilterValue::ToInt)
}
fn url_encode_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("urlEncode", reader)?;
Ok(FilterValue::UrlEncode)

View File

@ -718,12 +718,12 @@ impl ToJson for FilterValue {
attributes.push(("type".to_string(), JValue::String("toDate".to_string())));
attributes.push(("fmt".to_string(), JValue::String(fmt.to_string())));
}
FilterValue::ToInt => {
attributes.push(("type".to_string(), JValue::String("toInt".to_string())));
}
FilterValue::ToFloat => {
attributes.push(("type".to_string(), JValue::String("toFloat".to_string())));
}
FilterValue::ToInt => {
attributes.push(("type".to_string(), JValue::String("toInt".to_string())));
}
FilterValue::XPath { expr, .. } => {
attributes.push(("type".to_string(), JValue::String("xpath".to_string())));
attributes.push(("expr".to_string(), JValue::String(expr.to_string())));

View File

@ -1041,8 +1041,8 @@ impl Tokenizable for Filter {
tokens.append(&mut fmt.tokenize());
tokens
}
FilterValue::ToInt => vec![Token::FilterType(String::from("toInt"))],
FilterValue::ToFloat => vec![Token::FilterType(String::from("toFloat"))],
FilterValue::ToInt => vec![Token::FilterType(String::from("toInt"))],
FilterValue::XPath { space0, expr } => {
let mut tokens: Vec<Token> = vec![Token::FilterType(String::from("xpath"))];
tokens.append(&mut space0.tokenize());