From f9d5c8e9f4659ebe8e6084150cfacd1b69812457 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Fri, 22 Nov 2024 18:24:50 +0100 Subject: [PATCH] Add newDate generator --- integration/hurl/tests_ok/function.hurl | 1 + integration/hurl/tests_ok/function.py | 4 ++++ integration/hurlfmt/tests_export/function.html | 1 + integration/hurlfmt/tests_export/function.hurl | 1 + integration/hurlfmt/tests_export/function.json | 2 +- integration/hurlfmt/tests_export/function.lint.hurl | 1 + packages/hurl/src/runner/function.rs | 5 +++++ packages/hurl_core/src/ast/core.rs | 1 + packages/hurl_core/src/ast/display.rs | 1 + packages/hurl_core/src/parser/function.rs | 1 + packages/hurlfmt/src/format/token.rs | 1 + 11 files changed, 18 insertions(+), 1 deletion(-) diff --git a/integration/hurl/tests_ok/function.hurl b/integration/hurl/tests_ok/function.hurl index ca0d9fe5dde..c0603bc9dad 100644 --- a/integration/hurl/tests_ok/function.hurl +++ b/integration/hurl/tests_ok/function.hurl @@ -1,4 +1,5 @@ GET http://localhost:8000/function [Query] uuid: {{newUuid}} +now: {{newDate}} HTTP 200 diff --git a/integration/hurl/tests_ok/function.py b/integration/hurl/tests_ok/function.py index 46a326e5bae..93fe4465a21 100644 --- a/integration/hurl/tests_ok/function.py +++ b/integration/hurl/tests_ok/function.py @@ -8,4 +8,8 @@ def function(): uuid_pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" assert re.match(uuid_pattern, request.args.get("uuid")) + + date_pattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{9}" + assert re.match(date_pattern, request.args.get("now")) + return "" diff --git a/integration/hurlfmt/tests_export/function.html b/integration/hurlfmt/tests_export/function.html index edfabfa2f01..d6624333f98 100644 --- a/integration/hurlfmt/tests_export/function.html +++ b/integration/hurlfmt/tests_export/function.html @@ -1,4 +1,5 @@
GET http://localhost:8000/hello
 [Query]
 uuid: {{newUuid}}
+now: {{newDate}}
 
diff --git a/integration/hurlfmt/tests_export/function.hurl b/integration/hurlfmt/tests_export/function.hurl index 8b4b8f11b59..83026bd4599 100644 --- a/integration/hurlfmt/tests_export/function.hurl +++ b/integration/hurlfmt/tests_export/function.hurl @@ -1,3 +1,4 @@ GET http://localhost:8000/hello [Query] uuid: {{newUuid}} +now: {{newDate}} diff --git a/integration/hurlfmt/tests_export/function.json b/integration/hurlfmt/tests_export/function.json index 3e78810f2c8..a371fd136e0 100644 --- a/integration/hurlfmt/tests_export/function.json +++ b/integration/hurlfmt/tests_export/function.json @@ -1 +1 @@ -{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/hello","query_string_params":[{"name":"uuid","value":"{{newUuid}}"}]}}]} +{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/hello","query_string_params":[{"name":"uuid","value":"{{newUuid}}"},{"name":"now","value":"{{newDate}}"}]}}]} diff --git a/integration/hurlfmt/tests_export/function.lint.hurl b/integration/hurlfmt/tests_export/function.lint.hurl index 8b4b8f11b59..83026bd4599 100644 --- a/integration/hurlfmt/tests_export/function.lint.hurl +++ b/integration/hurlfmt/tests_export/function.lint.hurl @@ -1,3 +1,4 @@ GET http://localhost:8000/hello [Query] uuid: {{newUuid}} +now: {{newDate}} diff --git a/packages/hurl/src/runner/function.rs b/packages/hurl/src/runner/function.rs index f08acdbf701..7a32d7c4d87 100644 --- a/packages/hurl/src/runner/function.rs +++ b/packages/hurl/src/runner/function.rs @@ -1,3 +1,4 @@ +use chrono::Utc; /* * Hurl (https://hurl.dev) * Copyright (C) 2024 Orange @@ -24,6 +25,10 @@ use crate::runner::value::Value; /// Evaluates the function `function`, returns a [`Value`] on success or an [`RunnerError`] . pub fn eval(function: &Function) -> Result { match &function { + Function::NewDate => { + let now = Utc::now(); + Ok(Value::Date(now)) + } Function::NewUuid => { let uuid = Uuid::new_v4(); Ok(Value::String(uuid.to_string())) diff --git a/packages/hurl_core/src/ast/core.rs b/packages/hurl_core/src/ast/core.rs index 0c10e7c1467..a52d7a56deb 100644 --- a/packages/hurl_core/src/ast/core.rs +++ b/packages/hurl_core/src/ast/core.rs @@ -719,6 +719,7 @@ pub struct Variable { #[derive(Clone, Debug, PartialEq, Eq)] pub enum Function { + NewDate, NewUuid, } diff --git a/packages/hurl_core/src/ast/display.rs b/packages/hurl_core/src/ast/display.rs index 776b153b14c..ca0f70768ab 100644 --- a/packages/hurl_core/src/ast/display.rs +++ b/packages/hurl_core/src/ast/display.rs @@ -126,6 +126,7 @@ impl fmt::Display for Variable { impl fmt::Display for Function { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { + Function::NewDate => write!(f, "newDate"), Function::NewUuid => write!(f, "newUuid"), } } diff --git a/packages/hurl_core/src/parser/function.rs b/packages/hurl_core/src/parser/function.rs index ce0484d3191..c0ad3729b09 100644 --- a/packages/hurl_core/src/parser/function.rs +++ b/packages/hurl_core/src/parser/function.rs @@ -28,6 +28,7 @@ pub fn parse(reader: &mut Reader) -> ParseResult { let function_name = reader.read_while(|c| c.is_alphanumeric() || c == '_' || c == '-'); match function_name.as_str() { "newUuid" => Ok(Function::NewUuid), + "newDate" => Ok(Function::NewDate), _ => Err(ParseError::new( start.pos, true, diff --git a/packages/hurlfmt/src/format/token.rs b/packages/hurlfmt/src/format/token.rs index 787f72098f3..08090af6dc5 100644 --- a/packages/hurlfmt/src/format/token.rs +++ b/packages/hurlfmt/src/format/token.rs @@ -796,6 +796,7 @@ impl Tokenizable for Variable { impl Tokenizable for Function { fn tokenize(&self) -> Vec { match self { + Function::NewDate => vec![Token::CodeVariable("newDate".to_string())], Function::NewUuid => vec![Token::CodeVariable("newUuid".to_string())], } }