From 265340181926422a1ba0d1fd7a329d380dd73dbf Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Tue, 1 Nov 2022 19:01:49 +0100 Subject: [PATCH] Fix missing line in HTML output --- integration/tests_failed/assert_newline.html | 19 ++++++ packages/hurl_core/src/format/html.rs | 65 ++++++++++---------- 2 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 integration/tests_failed/assert_newline.html diff --git a/integration/tests_failed/assert_newline.html b/integration/tests_failed/assert_newline.html new file mode 100644 index 000000000..99302c5f9 --- /dev/null +++ b/integration/tests_failed/assert_newline.html @@ -0,0 +1,19 @@ +
# Testing trailing whitespace
+# Response '<p>Hello</p>\n\n'
+
+# Testing with a raw string
+# check exactly the whitespace
+# It should produce an assert error
+GET http://localhost:8000/error-assert-newline
+HTTP/1.0 200
+```<p>Hello</p>
+```
+
+# TODO
+# Testing with XML
+# Ignore trailing whitespace (semantic comparison)
+# It should not produce an error
+# GET http://localhost:8000/error-assert-newline
+# HTTP/1.0 200
+# <p>Hello</p>
+
diff --git a/packages/hurl_core/src/format/html.rs b/packages/hurl_core/src/format/html.rs index 09cf0a85a..bf844be30 100644 --- a/packages/hurl_core/src/format/html.rs +++ b/packages/hurl_core/src/format/html.rs @@ -861,40 +861,11 @@ impl Htmlable for RawString { fn to_html(&self) -> String { let mut buffer = "".to_string(); buffer.push_str(""); - buffer.push_str("```"); - - if !self.newline.value.as_str().is_empty() { - buffer.push_str( - format!( - "{}", - self.newline.value.as_str() - ) - .as_str(), - ); - } - - let mut lines: Vec = regex::Regex::new(r"\n|\r\n") - .unwrap() - .split(self.value.to_string().trim()) - .map(|l| xml_escape(l.to_string())) - .filter(|l| !l.is_empty()) - .collect(); - - if lines.is_empty() { - buffer.push_str("```"); - } else if lines.len() == 1 { - buffer.push_str(encode_html(lines.get(0).unwrap().to_string()).as_str()); - buffer.push_str("```"); - } else { - buffer.push_str(encode_html(lines.remove(0)).as_str()); - buffer.push_str("\n"); - for line in lines { - buffer.push_str(""); - buffer.push_str(encode_html(line).as_str()); - buffer.push_str("\n"); - } - buffer.push_str("```"); - } + let mut s = "```".to_string(); + s.push_str(self.newline.value.as_str()); + s.push_str(self.value.to_string().as_str()); + s.push_str("```"); + buffer.push_str(multilines(s).as_str()); buffer.push_str(""); buffer } @@ -1167,6 +1138,27 @@ mod tests { raw_string.to_html(), "```\nline1\nline2\n```".to_string() ); + + // ```Hello + // ``` + let raw_string = RawString { + newline: Whitespace { + value: "".to_string(), + source_info: SourceInfo::new(0, 0, 0, 0), + }, + value: Template { + quotes: false, + elements: vec![TemplateElement::String { + value: "Hello\n".to_string(), + encoded: "unused".to_string(), + }], + source_info: SourceInfo::new(0, 0, 0, 0), + }, + }; + assert_eq!( + raw_string.to_html(), + "```Hello\n```".to_string() + ); } #[test] @@ -1179,6 +1171,11 @@ mod tests { multilines("\ncafé".to_string()), "<?xml version=\"1.0\"?>\n<drink>café</drink>" ); + + assert_eq!( + multilines("Hello\n".to_string()), + "Hello\n" + ); } #[test]