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 raw string in predicate value #223

Merged
merged 1 commit into from
Jul 1, 2021
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
1 change: 1 addition & 0 deletions integration/tests/multilines.exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions integration/tests/multilines.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="hurl-file"><div class="hurl-entry"><div class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/multilines</span></span></div><div class="response"><span class="line"><span class="version">HTTP/*</span> <span class="status">200</span></span><span class="line section-header">[Asserts]</span></span><span class="line"><span class="query-type">status</span> <span class="predicate-type">==</span> <span class="string">"line1\nline2\nline3\n"</span></span><span class="line"><span class="query-type">status</span> <span class="predicate-type">==</span> ```line1</span><span class="line">line2</span><span class="line">line3</span><span class="line">```</span></span><span class="line"><span class="query-type">status</span> <span class="predicate-type">==</span> ```</span><span class="line">line1</span><span class="line">line2</span><span class="line">line3</span><span class="line">```</span></span></div></div><span class="line"></span></div>
14 changes: 14 additions & 0 deletions integration/tests/multilines.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET http://localhost:8000/multilines
HTTP/* 200
[Asserts]
body == "line1\nline2\nline3\n"
body == ```line1
line2
line3
```
body == ```
line1
line2
line3
```

1 change: 1 addition & 0 deletions integration/tests/multilines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/multilines"},"response":{"status":200,"asserts":[{"query":{"type":"body"},"predicate":{"type":"equal","value":"line1\nline2\nline3\n"}},{"query":{"type":"body"},"predicate":{"type":"equal","value":"line1\nline2\nline3\n"}},{"query":{"type":"body"},"predicate":{"type":"equal","value":"line1\nline2\nline3\n"}}]}}]}
7 changes: 7 additions & 0 deletions integration/tests/multilines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from tests import app

@app.route("/multilines")
def multilines():
return 'line1\nline2\nline3\n'


2 changes: 1 addition & 1 deletion packages/hurl/src/runner/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn eval_bytes(
) -> Result<http::Body, Error> {
match bytes {
// Body::Text
Bytes::RawString { value, .. } => {
Bytes::RawString(RawString { value, .. }) => {
let value = eval_template(value, variables)?;
Ok(http::Body::Text(value))
}
Expand Down
4 changes: 4 additions & 0 deletions packages/hurl/src/runner/predicate_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub fn eval_predicate_value(
let s = eval_template(template, variables)?;
Ok(Value::String(s))
}
PredicateValue::Raw(value) => {
let s = eval_template(value.value, variables)?;
Ok(Value::String(s))
}
PredicateValue::Integer(value) => Ok(Value::Integer(value)),
PredicateValue::Float(value) => Ok(Value::Float(value.int, value.decimal)),
PredicateValue::Bool(value) => Ok(Value::Bool(value)),
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl/src/runner/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn eval_asserts(
source_info: body.space0.source_info.clone(),
})
}
Bytes::RawString { value, .. } => {
Bytes::RawString(RawString { value, .. }) => {
let expected = match eval_template(value.clone(), variables) {
Ok(s) => Ok(Value::String(s)),
Err(e) => Err(e),
Expand Down
11 changes: 7 additions & 4 deletions packages/hurl_core/src/ast/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ pub struct PredicateFunc {
#[allow(clippy::large_enum_variant)]
pub enum PredicateValue {
String(Template),
Raw(RawString),
Integer(i64),
Float(Float),
Bool(bool),
Expand Down Expand Up @@ -476,6 +477,11 @@ pub enum PredicateFuncValue {

// Unique template type for your string
// quoted and unquoted
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RawString {
pub newline: Whitespace,
pub value: Template,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Template {
Expand Down Expand Up @@ -551,10 +557,7 @@ pub enum Bytes {
Xml {
value: String,
},
RawString {
newline0: Whitespace,
value: Template,
},
RawString(RawString),
Base64 {
space0: Whitespace,
value: Vec<u8>,
Expand Down
74 changes: 41 additions & 33 deletions packages/hurl_core/src/format/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ impl Htmlable for PredicateValue {
PredicateValue::String(value) => {
format!("<span class=\"string\">\"{}\"</span>", value.to_html())
}
PredicateValue::Raw(value) => value.to_html(),
PredicateValue::Integer(value) => format!("<span class=\"number\">{}</span>", value),
PredicateValue::Float(value) => {
format!("<span class=\"number\">{}</span>", value.to_string())
Expand All @@ -612,6 +613,34 @@ impl Htmlable for PredicateValue {
}
}

impl Htmlable for RawString {
fn to_html(&self) -> String {
let mut buffer = String::from("```");
if !self.newline.to_html().as_str().is_empty() {
buffer.push_str("</span><span class=\"line\">");
}

let end_newline = self.value.to_string().ends_with('\n');
let mut lines: Vec<String> = regex::Regex::new(r"\n|\r\n")
.unwrap()
.split(self.value.to_string().trim())
.map(|l| l.to_string())
.collect();

buffer.push_str(xml_escape(lines.remove(0)).as_str());

for line in lines {
buffer.push_str("</span><span class=\"line\">");
buffer.push_str(xml_escape(line).as_str());
}
if end_newline {
buffer.push_str("</span><span class=\"line\">");
}
buffer.push_str("```</span>");
buffer
}
}

impl Htmlable for Body {
fn to_html(&self) -> String {
let mut buffer = String::from("");
Expand Down Expand Up @@ -652,29 +681,8 @@ impl Htmlable for Bytes {
buffer.push(';');
buffer.push_str("</span>");
}
Bytes::RawString { newline0, value } => {
buffer.push_str("```");
if !newline0.to_html().as_str().is_empty() {
buffer.push_str("</span><span class=\"line\">");
}

let end_newline = value.to_string().ends_with('\n');
let mut lines: Vec<String> = regex::Regex::new(r"\n|\r\n")
.unwrap()
.split(value.to_string().trim())
.map(|l| l.to_string())
.collect();

buffer.push_str(xml_escape(lines.remove(0)).as_str());

for line in lines {
buffer.push_str("</span><span class=\"line\">");
buffer.push_str(xml_escape(line).as_str());
}
if end_newline {
buffer.push_str("</span><span class=\"line\">");
}
buffer.push_str("```</span>");
Bytes::RawString(value) => {
buffer.push_str(value.to_html().as_str());
}
Bytes::Json { value } => buffer.push_str(value.to_html().as_str()),
Bytes::Xml { value } => {
Expand Down Expand Up @@ -753,7 +761,7 @@ impl Htmlable for EncodedString {

impl Htmlable for Template {
fn to_html(&self) -> String {
xml_escape(self.to_string())
xml_escape(self.to_string().replace("\n", "\\n"))
}
}

Expand Down Expand Up @@ -785,8 +793,8 @@ mod tests {
#[test]
fn test_raw_string() {
// ``````
let bytes = Bytes::RawString {
newline0: Whitespace {
let raw_string = RawString {
newline: Whitespace {
value: "".to_string(),
source_info: SourceInfo::init(0, 0, 0, 0),
},
Expand All @@ -799,11 +807,11 @@ mod tests {
source_info: SourceInfo::init(0, 0, 0, 0),
},
};
assert_eq!(bytes.to_html(), "``````</span>".to_string());
assert_eq!(raw_string.to_html(), "``````</span>".to_string());

// ```hello```
let bytes = Bytes::RawString {
newline0: Whitespace {
let raw_string = RawString {
newline: Whitespace {
value: "".to_string(),
source_info: SourceInfo::init(0, 0, 0, 0),
},
Expand All @@ -816,14 +824,14 @@ mod tests {
source_info: SourceInfo::init(0, 0, 0, 0),
},
};
assert_eq!(bytes.to_html(), "```hello```</span>".to_string());
assert_eq!(raw_string.to_html(), "```hello```</span>".to_string());

// ```
// line1
// line2
// ```
let bytes = Bytes::RawString {
newline0: Whitespace {
let raw_string = RawString {
newline: Whitespace {
value: "\n".to_string(),
source_info: SourceInfo::init(0, 0, 0, 0),
},
Expand All @@ -836,6 +844,6 @@ mod tests {
source_info: SourceInfo::init(0, 0, 0, 0),
},
};
assert_eq!(bytes.to_html(), "```</span><span class=\"line\">line1</span><span class=\"line\">line2</span><span class=\"line\">```</span>".to_string());
assert_eq!(raw_string.to_html(), "```</span><span class=\"line\">line1</span><span class=\"line\">line2</span><span class=\"line\">```</span>".to_string());
}
}
11 changes: 10 additions & 1 deletion packages/hurl_core/src/parser/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ use super::ParseResult;
pub fn bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
//let start = p.state.clone();
choice(
vec![raw_string, json_bytes, xml_bytes, base64_bytes, file_bytes],
vec![
raw_string_bytes,
json_bytes,
xml_bytes,
base64_bytes,
file_bytes,
],
reader,
)
}
fn raw_string_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
raw_string(reader).map(Bytes::RawString)
}

fn xml_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
match xml::parse(reader) {
Expand Down
6 changes: 3 additions & 3 deletions packages/hurl_core/src/parser/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ mod tests {
value: String::from(""),
source_info: SourceInfo::init(2, 1, 2, 1),
},
value: Bytes::RawString {
newline0: Whitespace {
value: Bytes::RawString(RawString {
newline: Whitespace {
value: "\n".to_string(),
source_info: SourceInfo::init(2, 4, 3, 1),
},
Expand All @@ -498,7 +498,7 @@ mod tests {
quotes: false,
source_info: SourceInfo::init(3, 1, 4, 1),
},
},
}),
line_terminator0: LineTerminator {
space0: Whitespace {
value: "".to_string(),
Expand Down
4 changes: 4 additions & 0 deletions packages/hurl_core/src/parser/predicate_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ pub fn predicate_value(reader: &mut Reader) -> ParseResult<'static, PredicateVal
Ok(value) => Ok(PredicateValue::String(value)),
Err(e) => Err(e),
},
|p1| match raw_string(p1) {
Ok(value) => Ok(PredicateValue::Raw(value)),
Err(e) => Err(e),
},
],
reader,
)
Expand Down
Loading