Skip to content

Commit

Permalink
Add redact in Capture AST
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix authored and hurl-bot committed Jan 18, 2025
1 parent 75f8dea commit 7513cb7
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/spec/grammar/hurl.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ file-contenttype: [a-zA-Z0-9/+-]+

capture:
lt*
key-string ":" query (sp filter)* lt
key-string ":" query (sp filter)* (sp "redact")? lt

assert:
lt*
Expand Down
7 changes: 7 additions & 0 deletions integration/hurlfmt/tests_export/capture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/dummy</span></span>
</span><span class="response"><span class="line"></span>
<span class="line"><span class="version">HTTP</span> <span class="number">200</span></span>
<span class="line"><span class="section-header">[Captures]</span></span>
<span class="line"><span class="string">name</span>: <span class="query-type">jsonpath</span> <span class="string">"$.name"</span></span>
<span class="line"><span class="string">token</span>: <span class="query-type">jsonpath</span> <span class="string">"$.token"</span> <span class="string">redact</span></span>
</span></span></code></pre>
6 changes: 6 additions & 0 deletions integration/hurlfmt/tests_export/capture.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GET http://localhost:8000/dummy

HTTP 200
[Captures]
name: jsonpath "$.name"
token: jsonpath "$.token" redact
1 change: 1 addition & 0 deletions integration/hurlfmt/tests_export/capture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/dummy"},"response":{"status":200,"captures":[{"name":"name","query":{"type":"jsonpath","expr":"$.name"}},{"name":"token","query":{"type":"jsonpath","expr":"$.token"},"redact":true}]}}]}
6 changes: 6 additions & 0 deletions integration/hurlfmt/tests_export/capture.lint.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GET http://localhost:8000/dummy

HTTP 200
[Captures]
name: jsonpath "$.name"
token: jsonpath "$.token" redact
8 changes: 8 additions & 0 deletions packages/hurl/src/runner/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub mod tests {
// xpath count(//user)
query: query::tests::xpath_count_user_query(),
filters: vec![],
space3: whitespace.clone(),
redact: false,
line_terminator0: LineTerminator {
space0: whitespace.clone(),
comment: None,
Expand Down Expand Up @@ -137,6 +139,8 @@ pub mod tests {
// xpath count(//user)
query: query::tests::jsonpath_duration(),
filters: vec![],
space3: whitespace.clone(),
redact: false,
line_terminator0: LineTerminator {
space0: whitespace.clone(),
comment: None,
Expand Down Expand Up @@ -169,6 +173,8 @@ pub mod tests {
space2: whitespace.clone(),

query: query::tests::xpath_invalid_query(),
space3: whitespace.clone(),
redact: false,
line_terminator0: LineTerminator {
space0: whitespace.clone(),
comment: None,
Expand Down Expand Up @@ -225,6 +231,8 @@ pub mod tests {
},
},
filters: vec![],
space3: whitespace.clone(),
redact: false,
line_terminator0: LineTerminator {
space0: whitespace.clone(),
comment: None,
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 @@ -289,6 +289,8 @@ pub struct Capture {
pub space2: Whitespace,
pub query: Query,
pub filters: Vec<(Whitespace, Filter)>,
pub space3: Whitespace,
pub redact: bool,
pub line_terminator0: LineTerminator,
}

Expand Down
4 changes: 4 additions & 0 deletions packages/hurl_core/src/format/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ impl HtmlFormatter {
self.fmt_space(space);
self.fmt_filter(filter);
}
if capture.redact {
self.fmt_space(&capture.space3);
self.fmt_string("redact");
}
self.fmt_span_close();
self.fmt_lt(&capture.line_terminator0);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/hurl_core/src/parser/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ fn capture(reader: &mut Reader) -> ParseResult<Capture> {
let space2 = zero_or_more_spaces(reader)?;
let q = query(reader)?;
let filters = filters(reader)?;
let space3 = zero_or_more_spaces(reader)?;
let redact = try_literal("redact", reader).is_ok();
let line_terminator0 = line_terminator(reader)?;
Ok(Capture {
line_terminators,
Expand All @@ -301,6 +303,8 @@ fn capture(reader: &mut Reader) -> ParseResult<Capture> {
space2,
query: q,
filters,
space3,
redact,
line_terminator0,
})
}
Expand Down Expand Up @@ -615,6 +619,10 @@ mod tests {
},
}
);

let mut reader = Reader::new("url: header \"Token\" redact");
let capture0 = capture(&mut reader).unwrap();
assert!(capture0.redact);
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions packages/hurlfmt/src/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ impl ToJson for Capture {
let filters = JValue::List(self.filters.iter().map(|(_, f)| f.to_json()).collect());
attributes.push(("filters".to_string(), filters));
}
if self.redact {
attributes.push(("redact".to_string(), JValue::Boolean(true)));
}
JValue::Object(attributes)
}
}
Expand Down Expand Up @@ -976,6 +979,8 @@ pub mod tests {
space2: whitespace(),
query: header_query(),
filters: vec![],
space3: whitespace(),
redact: false,
line_terminator0: line_terminator(),
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/hurlfmt/src/format/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ impl Tokenizable for Capture {
tokens.append(&mut space.tokenize());
tokens.append(&mut filter.tokenize());
}
if self.redact {
tokens.append(&mut self.space3.tokenize());
tokens.push(Token::Keyword(String::from("redact")));
}
tokens.append(&mut self.line_terminator0.tokenize());
tokens
}
Expand Down
7 changes: 7 additions & 0 deletions packages/hurlfmt/src/linter/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ fn lint_capture(capture: &Capture) -> Capture {
.iter()
.map(|(_, f)| (one_whitespace(), lint_filter(f)))
.collect();
let space3 = if capture.redact {
one_whitespace()
} else {
empty_whitespace()
};
Capture {
line_terminators: capture.line_terminators.clone(),
space0: empty_whitespace(),
Expand All @@ -245,6 +250,8 @@ fn lint_capture(capture: &Capture) -> Capture {
space2: one_whitespace(),
query: lint_query(&capture.query),
filters,
space3,
redact: capture.redact,
line_terminator0: lint_line_terminator(&capture.line_terminator0),
}
}
Expand Down

0 comments on commit 7513cb7

Please sign in to comment.