Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing for base64 filters #3628

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration/hurlfmt/tests_export/filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<span class="line"><span class="section-header">[Captures]</span></span>
<span class="line"><span class="string">count</span>: <span class="query-type">jsonpath</span> <span class="string">"$.books"</span> <span class="filter-type">count</span></span>
<span class="line"><span class="section-header">[Asserts]</span></span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.data"</span> <span class="filter-type">base64Decode</span> <span class="predicate-type">==</span> hex,<span class="hex">48656c6c6f</span>;</span> <span class="comment"># base64Decode</span>
<span class="line"><span class="query-type">bytes</span> <span class="filter-type">base64Encode</span> <span class="predicate-type">==</span> <span class="string">"SGVsbG8="</span></span> <span class="comment"># base64Encode</span>
<span class="line"><span class="query-type">jsonpath</span> <span class="string">"$.books"</span> <span class="filter-type">count</span> <span class="predicate-type">==</span> <span class="number">12</span></span> <span class="comment"># count</span>
<span class="line"><span class="query-type">certificate</span> <span class="string">"Expire-Date"</span> <span class="filter-type">daysAfterNow</span> <span class="predicate-type">&gt;</span> <span class="number">15</span></span> <span class="comment"># daysAfterNow</span>
<span class="line"><span class="query-type">certificate</span> <span class="string">"Start-Date"</span> <span class="filter-type">daysBeforeNow</span> <span class="predicate-type">&lt;</span> <span class="number">100</span></span> <span class="comment"># daysBeforeNow</span>
Expand Down
2 changes: 2 additions & 0 deletions integration/hurlfmt/tests_export/filter.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ HTTP 200
[Captures]
count: jsonpath "$.books" count
[Asserts]
jsonpath "$.data" base64Decode == hex,48656c6c6f; # base64Decode
bytes base64Encode == "SGVsbG8=" # base64Encode
jsonpath "$.books" count == 12 # count
certificate "Expire-Date" daysAfterNow > 15 # daysAfterNow
certificate "Start-Date" daysBeforeNow < 100 # daysBeforeNow
Expand Down
2 changes: 1 addition & 1 deletion integration/hurlfmt/tests_export/filter.json
Original file line number Diff line number Diff line change
@@ -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":"$.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"}}]}}]}
{"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":"$.data"},"filters":[{"type":"base64Decode"}],"predicate":{"type":"equal","value":"SGVsbG8=","encoding":"base64"}},{"query":{"type":"bytes"},"filters":[{"type":"base64Encode"}],"predicate":{"type":"equal","value":"SGVsbG8="}},{"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"}}]}}]}
2 changes: 2 additions & 0 deletions integration/hurlfmt/tests_export/filter.lint.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ HTTP 200
[Captures]
count: jsonpath "$.books" count
[Asserts]
jsonpath "$.data" base64Decode == hex,48656c6c6f; # base64Decode
bytes base64Encode == "SGVsbG8=" # base64Encode
jsonpath "$.books" count == 12 # count
certificate "Expire-Date" daysAfterNow > 15 # daysAfterNow
certificate "Start-Date" daysBeforeNow < 100 # daysBeforeNow
Expand Down
2 changes: 2 additions & 0 deletions packages/hurl/src/runner/filter/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub fn eval_filter(
in_assert: bool,
) -> Result<Option<Value>, RunnerError> {
match &filter.value {
FilterValue::Base64Decode => todo!(),
FilterValue::Base64Encode => todo!(),
FilterValue::Count => eval_count(value, filter.source_info, in_assert),
FilterValue::DaysAfterNow => eval_days_after_now(value, filter.source_info, in_assert),
FilterValue::DaysBeforeNow => eval_days_before_now(value, filter.source_info, in_assert),
Expand Down
2 changes: 2 additions & 0 deletions packages/hurl_core/src/ast/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ pub struct Filter {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FilterValue {
Base64Decode,
Base64Encode,
Count,
DaysAfterNow,
DaysBeforeNow,
Expand Down
2 changes: 2 additions & 0 deletions packages/hurl_core/src/format/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,8 @@ impl HtmlFormatter {
fn fmt_filter_value(&mut self, filter_value: &FilterValue) {
let class = "filter-type";
match filter_value {
FilterValue::Base64Decode => self.fmt_span(class, "base64Decode"),
FilterValue::Base64Encode => self.fmt_span(class, "base64Encode"),
FilterValue::Count => self.fmt_span(class, "count"),
FilterValue::DaysAfterNow => self.fmt_span(class, "daysAfterNow"),
FilterValue::DaysBeforeNow => self.fmt_span(class, "daysBeforeNow"),
Expand Down
12 changes: 12 additions & 0 deletions packages/hurl_core/src/parser/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub fn filter(reader: &mut Reader) -> ParseResult<Filter> {
let start = reader.cursor();
let value = choice(
&[
base64_decode_filter,
base64_encode_filter,
count_filter,
days_after_now_filter,
days_before_now_filter,
Expand Down Expand Up @@ -92,6 +94,16 @@ pub fn filter(reader: &mut Reader) -> ParseResult<Filter> {
Ok(Filter { source_info, value })
}

fn base64_decode_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("base64Decode", reader)?;
Ok(FilterValue::Base64Decode)
}

fn base64_encode_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("base64Encode", reader)?;
Ok(FilterValue::Base64Encode)
}

fn count_filter(reader: &mut Reader) -> ParseResult<FilterValue> {
try_literal("count", reader)?;
Ok(FilterValue::Count)
Expand Down
6 changes: 6 additions & 0 deletions packages/hurlfmt/src/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,12 @@ impl ToJson for FilterValue {
let mut attributes = vec![];
let att_name = "type".to_string();
match self {
FilterValue::Base64Decode => {
attributes.push((att_name, JValue::String("base64Decode".to_string())));
}
FilterValue::Base64Encode => {
attributes.push((att_name, JValue::String("base64Encode".to_string())));
}
FilterValue::Count => {
attributes.push((att_name, JValue::String("count".to_string())));
}
Expand Down
2 changes: 2 additions & 0 deletions packages/hurlfmt/src/format/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,8 @@ impl Tokenizable for VariableValue {
impl Tokenizable for Filter {
fn tokenize(&self) -> Vec<Token> {
match self.value.clone() {
FilterValue::Base64Decode => vec![Token::FilterType(String::from("base64Decode"))],
FilterValue::Base64Encode => vec![Token::FilterType(String::from("base64Encode"))],
FilterValue::Count => vec![Token::FilterType(String::from("count"))],
FilterValue::DaysAfterNow => vec![Token::FilterType(String::from("daysAfterNow"))],
FilterValue::DaysBeforeNow => vec![Token::FilterType(String::from("daysBeforeNow"))],
Expand Down
Loading