Merge pull request #229 from Orange-OpenSource/feature/add-hex-body

Add hex body
This commit is contained in:
Fabrice Reix 2021-07-04 19:29:31 +02:00 committed by GitHub
commit 97c7aee595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 101 additions and 31 deletions

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/assert-base64"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"base64","value":"bGluZTEKbGluZTINCmxpbmUzCg=="}}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/assert-base64"},"response":{"version":"HTTP/1.0","status":200,"body":{"encoding":"base64","value":"bGluZTEKbGluZTINCmxpbmUzCg=="}}}]}

View File

@ -2,6 +2,6 @@ error: Assert Body Value
--> tests/error_assert_base64.hurl:12:8
|
12 | base64,bGluZTEKbGluZTIKbGluZTMK;
| ^^^^^^^^^^^^^^^^^^^^^^^^ actual value is <Bytes([6c, 69, 6e, 65, 31, a, 6c, 69, 6e, 65, 32, d, a, 6c, 69, 6e, 65, 33, a])>
| ^^^^^^^^^^^^^^^^^^^^^^^^ actual value is <hex, 6c696e65310a6c696e65320d0a6c696e65330a;>
|

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/assert-base64"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"base64","value":"bGluZTEKbGluZTIKbGluZTMK"}}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/assert-base64"},"response":{"version":"HTTP/1.0","status":200,"body":{"encoding":"base64","value":"bGluZTEKbGluZTIKbGluZTMK"}}}]}

View File

@ -2,6 +2,6 @@ error: Assert Body Value
--> tests/error_assert_file.hurl:8:1
|
8 | file,data.txt;
| ^ actual value is <Bytes([48, 65, 6c, 6c, 6f])>
| ^ actual value is <hex, 48656c6c6f;>
|

View File

@ -1,2 +1,4 @@
curl 'http://localhost:8000/hello'
curl 'http://localhost:8000/hello'
curl 'http://localhost:8000/hello'
curl 'http://localhost:8000/hello'

View File

@ -1 +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/hello</span></span></div><div class="response"><span class="line"></span><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line">```Hello World!```</span></div></div><div class="hurl-entry"><div class="request"><span class="line"></span><span class="line"></span><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span></span></div><div class="response"><span class="line"></span><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line"><span>file, data.txt;</span></span></div></div></div>
<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/hello</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line">```Hello World!```</span></div></div><div class="hurl-entry"><div class="request"><span class="line"></span><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line"><span>file, data.txt;</span></span></div></div><div class="hurl-entry"><div class="request"><span class="line"></span><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line"><span>hex, 48656c6c6f20576f726c6421;</span></span></div></div><div class="hurl-entry"><div class="request"><span class="line"></span><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span></span></div><div class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="status">200</span></span><span class="line"><span>base64, SGVsbG8gV29ybGQh;</span></span></div></div></div>

View File

@ -1,10 +1,15 @@
GET http://localhost:8000/hello
HTTP/1.0 200
```Hello World!```
GET http://localhost:8000/hello
HTTP/1.0 200
file, data.txt;
GET http://localhost:8000/hello
HTTP/1.0 200
hex, 48656c6c6f20576f726c6421;
GET http://localhost:8000/hello
HTTP/1.0 200
base64, SGVsbG8gV29ybGQh;

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/hello"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"Hello World!"}}},{"request":{"method":"GET","url":"http://localhost:8000/hello"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"file","filename":"data.txt"}}}]}
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/hello"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"raw-string","value":"Hello World!"}}},{"request":{"method":"GET","url":"http://localhost:8000/hello"},"response":{"version":"HTTP/1.0","status":200,"body":{"type":"file","filename":"data.txt"}}},{"request":{"method":"GET","url":"http://localhost:8000/hello"},"response":{"version":"HTTP/1.0","status":200,"body":{"encoding":"base64","value":"SGVsbG8gV29ybGQh"}}},{"request":{"method":"GET","url":"http://localhost:8000/hello"},"response":{"version":"HTTP/1.0","status":200,"body":{"encoding":"base64","value":"SGVsbG8gV29ybGQh"}}}]}

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-base64","body":{"type":"base64","value":"SGVsbG8gV29ybGQh"}},"response":{"version":"HTTP/1.0","status":200}}]}
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-base64","body":{"encoding":"base64","value":"SGVsbG8gV29ybGQh"}},"response":{"version":"HTTP/1.0","status":200}}]}

View File

@ -1 +1 @@
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-bytes","headers":[{"name":"Content-Type","value":"application/octet-stream"}],"body":{"type":"base64","value":"AQID"}},"response":{"version":"HTTP/1.0","status":200}}]}
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-bytes","headers":[{"name":"Content-Type","value":"application/octet-stream"}],"body":{"encoding":"base64","value":"AQID"}},"response":{"version":"HTTP/1.0","status":200}}]}

View File

@ -52,10 +52,8 @@ pub fn eval_bytes(
Ok(http::Body::Text(value))
}
// Body:: Binary
Bytes::Base64(Base64 { value, .. }) => Ok(http::Body::Binary(value)),
// Body::File
Bytes::Hex(Hex { value, .. }) => Ok(http::Body::Binary(value)),
Bytes::File(File { filename, .. }) => {
let f = filename.value.as_str();
let path = Path::new(f);

View File

@ -188,6 +188,19 @@ pub fn eval_asserts(
end: space1.source_info.start,
},
}),
Bytes::Hex(Hex {
value,
space0,
space1,
..
}) => asserts.push(AssertResult::Body {
actual: Ok(Value::Bytes(http_response.body.clone())),
expected: Ok(Value::Bytes(value)),
source_info: SourceInfo {
start: space0.source_info.end,
end: space1.source_info.start,
},
}),
Bytes::File { .. } => {
let expected = match eval_body(body.clone(), variables, context_dir) {
Ok(body) => Ok(Value::Bytes(body.bytes())),

View File

@ -54,7 +54,7 @@ impl fmt::Display for Value {
}
Value::Object(_) => "Object()".to_string(),
Value::Nodeset(x) => format!("Nodeset{:?}", x),
Value::Bytes(x) => format!("Bytes({:x?})", x),
Value::Bytes(v) => format!("hex, {};", hex::encode(v)),
Value::Null => "null".to_string(),
Value::Unit => "Unit".to_string(),
};

View File

@ -570,6 +570,7 @@ pub enum Bytes {
RawString(RawString),
Base64(Base64),
File(File),
Hex(Hex),
}
#[derive(Clone, Debug, PartialEq, Eq)]

View File

@ -664,6 +664,9 @@ impl Htmlable for Bytes {
Bytes::Base64(value) => {
buffer.push_str(value.to_html().as_str());
}
Bytes::Hex(value) => {
buffer.push_str(value.to_html().as_str());
}
Bytes::File(value) => {
buffer.push_str(value.to_html().as_str());
}
@ -763,6 +766,18 @@ impl Htmlable for Base64 {
buffer
}
}
impl Htmlable for Hex {
fn to_html(&self) -> String {
let mut buffer = String::from("<span>");
buffer.push_str("hex,");
buffer.push_str(self.space0.to_html().as_str());
buffer.push_str(self.encoded.as_str());
buffer.push_str(self.space1.to_html().as_str());
buffer.push(';');
buffer.push_str("</span>");
buffer
}
}
impl Htmlable for EncodedString {
fn to_html(&self) -> String {

View File

@ -32,6 +32,7 @@ pub fn bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
json_bytes,
xml_bytes,
base64_bytes,
hex_bytes,
file_bytes,
],
reader,
@ -63,6 +64,10 @@ fn base64_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
base64(reader).map(Bytes::Base64)
}
fn hex_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
hex(reader).map(Bytes::Hex)
}
#[cfg(test)]
mod tests {

View File

@ -142,6 +142,7 @@ impl ToJson for Bytes {
fn to_json(&self) -> JValue {
match self {
Bytes::Base64(value) => value.to_json(),
Bytes::Hex(value) => value.to_json(),
Bytes::File(value) => value.to_json(),
Bytes::Json { value } => JValue::Object(vec![
("type".to_string(), JValue::String("json".to_string())),
@ -162,8 +163,22 @@ impl ToJson for Bytes {
impl ToJson for Base64 {
fn to_json(&self) -> JValue {
JValue::Object(vec![
("type".to_string(), JValue::String("base64".to_string())),
("value".to_string(), JValue::String(self.encoded.clone())),
("encoding".to_string(), JValue::String("base64".to_string())),
(
"value".to_string(),
JValue::String(base64::encode(&self.value)),
),
])
}
}
impl ToJson for Hex {
fn to_json(&self) -> JValue {
JValue::Object(vec![
("encoding".to_string(), JValue::String("base64".to_string())),
(
"value".to_string(),
JValue::String(base64::encode(&self.value)),
),
])
}
}

View File

@ -198,6 +198,9 @@ impl Tokenizable for Bytes {
Bytes::Base64(value) => {
tokens.append(&mut value.tokenize());
}
Bytes::Hex(value) => {
tokens.append(&mut value.tokenize());
}
Bytes::File(value) => {
tokens.append(&mut value.tokenize());
}
@ -282,6 +285,17 @@ impl Tokenizable for Base64 {
}
}
impl Tokenizable for Hex {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![Token::Keyword(String::from("hex,"))];
add_tokens(&mut tokens, self.space0.tokenize());
tokens.push(Token::String(self.encoded.to_string()));
add_tokens(&mut tokens, self.space1.tokenize());
tokens.push(Token::Keyword(String::from(";")));
tokens
}
}
impl Tokenizable for File {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![Token::Keyword(String::from("file,"))];

View File

@ -560,6 +560,7 @@ impl Lintable<Bytes> for Bytes {
match self {
Bytes::File(value) => Bytes::File(value.lint()),
Bytes::Base64(value) => Bytes::Base64(value.lint()),
Bytes::Hex(value) => Bytes::Hex(value.lint()),
Bytes::Json { value } => Bytes::Json {
value: value.clone(),
},
@ -586,6 +587,21 @@ impl Lintable<Base64> for Base64 {
}
}
impl Lintable<Hex> for Hex {
fn errors(&self) -> Vec<Error> {
unimplemented!()
}
fn lint(&self) -> Hex {
Hex {
space0: one_whitespace(),
value: self.value.clone(),
encoded: self.encoded.clone(),
space1: empty_whitespace(),
}
}
}
impl Lintable<File> for File {
fn errors(&self) -> Vec<Error> {
unimplemented!()
@ -688,20 +704,6 @@ fn one_whitespace() -> Whitespace {
source_info: SourceInfo::init(0, 0, 0, 0),
}
}
impl Lintable<Hex> for Hex {
fn errors(&self) -> Vec<Error> {
unimplemented!()
}
fn lint(&self) -> Hex {
Hex {
space0: one_whitespace(),
value: self.value.clone(),
encoded: self.encoded.clone(),
space1: empty_whitespace(),
}
}
}
impl Lintable<LineTerminator> for LineTerminator {
fn errors(&self) -> Vec<Error> {