From 3a25d516abc42ffe75743f9f81ce0f053a6701b5 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Wed, 14 Jul 2021 21:04:28 +0200 Subject: [PATCH] Add md5 query --- Cargo.lock | 7 +++++++ integration/tests/bytes.html | 2 +- integration/tests/bytes.hurl | 1 + integration/tests/bytes.json | 2 +- packages/hurl/Cargo.toml | 1 + packages/hurl/src/runner/query.rs | 4 ++++ packages/hurl_core/src/ast/core.rs | 1 + packages/hurl_core/src/format/html.rs | 3 +++ packages/hurl_core/src/parser/query.rs | 6 ++++++ packages/hurlfmt/src/format/json.rs | 3 +++ packages/hurlfmt/src/format/token.rs | 1 + packages/hurlfmt/src/linter/rules.rs | 1 + 12 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11748892e..54cf4a7aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -396,6 +396,7 @@ dependencies = [ "hurl_core", "libflate", "libxml", + "md5", "percent-encoding", "regex", "serde", @@ -505,6 +506,12 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" + [[package]] name = "memchr" version = "2.4.0" diff --git a/integration/tests/bytes.html b/integration/tests/bytes.html index fec5a4fda..96be2c583 100644 --- a/integration/tests/bytes.html +++ b/integration/tests/bytes.html @@ -1 +1 @@ -
GET http://localhost:8000/bytes
HTTP/1.0 200Content-Type: application/octet-stream[Asserts]bytes equals hex,010203;bytes equals base64,AQID;bytes count == 3bytes startsWith hex,01;bytes contains hex,02;sha256 equals hex,039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81;
\ No newline at end of file +
GET http://localhost:8000/bytes
HTTP/1.0 200Content-Type: application/octet-stream[Asserts]bytes equals hex,010203;bytes equals base64,AQID;bytes count == 3bytes startsWith hex,01;bytes contains hex,02;sha256 equals hex,039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81;md5 equals hex,5289df737df57326fcdd22597afb1fac;
\ No newline at end of file diff --git a/integration/tests/bytes.hurl b/integration/tests/bytes.hurl index dd8f9ab90..765189314 100644 --- a/integration/tests/bytes.hurl +++ b/integration/tests/bytes.hurl @@ -8,3 +8,4 @@ bytes count == 3 bytes startsWith hex,01; bytes contains hex,02; sha256 equals hex,039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81; +md5 equals hex,5289df737df57326fcdd22597afb1fac; diff --git a/integration/tests/bytes.json b/integration/tests/bytes.json index a40c3917f..50481a74a 100644 --- a/integration/tests/bytes.json +++ b/integration/tests/bytes.json @@ -1 +1 @@ -{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/bytes"},"response":{"version":"HTTP/1.0","status":200,"headers":[{"name":"Content-Type","value":"application/octet-stream"}],"asserts":[{"query":{"type":"bytes"},"predicate":{"type":"equal","value":"AQID","encoding":"base64"}},{"query":{"type":"bytes"},"predicate":{"type":"equal","value":"AQID","encoding":"base64"}},{"query":{"type":"bytes","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}},{"query":{"type":"bytes"},"predicate":{"type":"start-with","value":"AQ==","encoding":"base64"}},{"query":{"type":"bytes"},"predicate":{"type":"contain","value":"Ag==","encoding":"base64"}},{"query":{"type":"sha256"},"predicate":{"type":"equal","value":"A5BYxvLAy0ksUzsKTRTvd8wPeKvMztUofYShogEc+4E=","encoding":"base64"}}]}}]} \ No newline at end of file +{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/bytes"},"response":{"version":"HTTP/1.0","status":200,"headers":[{"name":"Content-Type","value":"application/octet-stream"}],"asserts":[{"query":{"type":"bytes"},"predicate":{"type":"equal","value":"AQID","encoding":"base64"}},{"query":{"type":"bytes"},"predicate":{"type":"equal","value":"AQID","encoding":"base64"}},{"query":{"type":"bytes","subquery":{"type":"count"}},"predicate":{"type":"equal","value":3}},{"query":{"type":"bytes"},"predicate":{"type":"start-with","value":"AQ==","encoding":"base64"}},{"query":{"type":"bytes"},"predicate":{"type":"contain","value":"Ag==","encoding":"base64"}},{"query":{"type":"sha256"},"predicate":{"type":"equal","value":"A5BYxvLAy0ksUzsKTRTvd8wPeKvMztUofYShogEc+4E=","encoding":"base64"}},{"query":{"type":"md5"},"predicate":{"type":"equal","value":"Uonfc331cyb83SJZevsfrA==","encoding":"base64"}}]}}]} \ No newline at end of file diff --git a/packages/hurl/Cargo.toml b/packages/hurl/Cargo.toml index bf67d51c1..e6e3e86b2 100644 --- a/packages/hurl/Cargo.toml +++ b/packages/hurl/Cargo.toml @@ -31,6 +31,7 @@ hex-literal = "0.3.2" hurl_core = { version = "1.1.0", path = "../hurl_core" } libflate = "1.1.0" libxml = "0.3.0" +md5 = "0.7.0" percent-encoding = "2.1.0" regex = "1.5.4" serde = "1.0.126" diff --git a/packages/hurl/src/runner/query.rs b/packages/hurl/src/runner/query.rs index bd9006fc4..1f2da53fb 100644 --- a/packages/hurl/src/runner/query.rs +++ b/packages/hurl/src/runner/query.rs @@ -232,6 +232,10 @@ pub fn eval_query_value( let bytes = Value::Bytes(result[..].to_vec()); Ok(Some(bytes)) } + QueryValue::Md5 {} => { + let bytes = md5::compute(http_response.body).to_vec(); + Ok(Some(Value::Bytes(bytes))) + } } } diff --git a/packages/hurl_core/src/ast/core.rs b/packages/hurl_core/src/ast/core.rs index 1710e6bbe..828c4cb8c 100644 --- a/packages/hurl_core/src/ast/core.rs +++ b/packages/hurl_core/src/ast/core.rs @@ -324,6 +324,7 @@ pub enum QueryValue { Duration {}, Bytes {}, Sha256 {}, + Md5 {}, } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/packages/hurl_core/src/format/html.rs b/packages/hurl_core/src/format/html.rs index dba33cf64..b814bf979 100644 --- a/packages/hurl_core/src/format/html.rs +++ b/packages/hurl_core/src/format/html.rs @@ -385,6 +385,9 @@ impl Htmlable for QueryValue { QueryValue::Sha256 {} => { buffer.push_str("sha256"); } + QueryValue::Md5 {} => { + buffer.push_str("md5"); + } } buffer } diff --git a/packages/hurl_core/src/parser/query.rs b/packages/hurl_core/src/parser/query.rs index 1e03f05a2..5c75d614a 100644 --- a/packages/hurl_core/src/parser/query.rs +++ b/packages/hurl_core/src/parser/query.rs @@ -69,6 +69,7 @@ fn query_value(reader: &mut Reader) -> ParseResult<'static, QueryValue> { duration_query, bytes_query, sha256_query, + md5_query, ], reader, ) @@ -166,6 +167,11 @@ fn sha256_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> { Ok(QueryValue::Sha256 {}) } +fn md5_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> { + try_literal("md5", reader)?; + Ok(QueryValue::Md5 {}) +} + #[cfg(test)] mod tests { use super::*; diff --git a/packages/hurlfmt/src/format/json.rs b/packages/hurlfmt/src/format/json.rs index 77b18307b..7f1628f05 100644 --- a/packages/hurlfmt/src/format/json.rs +++ b/packages/hurlfmt/src/format/json.rs @@ -324,6 +324,9 @@ fn query_value_attributes(query_value: &QueryValue) -> Vec<(String, JValue)> { QueryValue::Sha256 {} => { attributes.push(("type".to_string(), JValue::String("sha256".to_string()))); } + QueryValue::Md5 {} => { + attributes.push(("type".to_string(), JValue::String("md5".to_string()))); + } }; attributes } diff --git a/packages/hurlfmt/src/format/token.rs b/packages/hurlfmt/src/format/token.rs index 35e1c41fe..eeef3fe51 100644 --- a/packages/hurlfmt/src/format/token.rs +++ b/packages/hurlfmt/src/format/token.rs @@ -487,6 +487,7 @@ impl Tokenizable for QueryValue { QueryValue::Duration {} => tokens.push(Token::QueryType(String::from("duration"))), QueryValue::Bytes {} => tokens.push(Token::QueryType(String::from("bytes"))), QueryValue::Sha256 {} => tokens.push(Token::QueryType(String::from("sha256"))), + QueryValue::Md5 {} => tokens.push(Token::QueryType(String::from("md5"))), } tokens } diff --git a/packages/hurlfmt/src/linter/rules.rs b/packages/hurlfmt/src/linter/rules.rs index 3b326f109..0ebc0601c 100644 --- a/packages/hurlfmt/src/linter/rules.rs +++ b/packages/hurlfmt/src/linter/rules.rs @@ -300,6 +300,7 @@ impl Lintable for QueryValue { QueryValue::Duration {} => QueryValue::Duration {}, QueryValue::Bytes {} => QueryValue::Bytes {}, QueryValue::Sha256 {} => QueryValue::Sha256 {}, + QueryValue::Md5 {} => QueryValue::Md5 {}, } } }