Skip to content

Commit

Permalink
Add newDate generator
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Nov 23, 2024
1 parent ce9d41c commit ea23052
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions integration/hurl/tests_ok/function.hurl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GET http://localhost:8000/function
[Query]
uuid: {{newUuid}}
now: {{newDate}}
HTTP 200
6 changes: 6 additions & 0 deletions integration/hurl/tests_ok/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
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"))

# check date with at least millisecond precision
# TODO: depends currently on the OS / should be normalized to the same value
date_pattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}"
assert re.match(date_pattern, request.args.get("now"))

return ""
1 change: 1 addition & 0 deletions integration/hurlfmt/tests_export/function.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span></span>
<span class="line"><span class="section-header">[Query]</span></span>
<span class="line"><span class="string">uuid</span>: <span class="string">{{newUuid}}</span></span>
<span class="line"><span class="string">now</span>: <span class="string">{{newDate}}</span></span>
</span></span></code></pre>
1 change: 1 addition & 0 deletions integration/hurlfmt/tests_export/function.hurl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GET http://localhost:8000/hello
[Query]
uuid: {{newUuid}}
now: {{newDate}}
2 changes: 1 addition & 1 deletion integration/hurlfmt/tests_export/function.json
Original file line number Diff line number Diff line change
@@ -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}}"}]}}]}
1 change: 1 addition & 0 deletions integration/hurlfmt/tests_export/function.lint.hurl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GET http://localhost:8000/hello
[Query]
uuid: {{newUuid}}
now: {{newDate}}
5 changes: 5 additions & 0 deletions packages/hurl/src/runner/function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::Utc;
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2024 Orange
Expand All @@ -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<Value, RunnerError> {
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()))
Expand Down
1 change: 1 addition & 0 deletions packages/hurl_core/src/ast/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ pub struct Variable {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Function {
NewDate,
NewUuid,
}

Expand Down
1 change: 1 addition & 0 deletions packages/hurl_core/src/ast/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/hurl_core/src/parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn parse(reader: &mut Reader) -> ParseResult<Function> {
let start = reader.cursor();
let function_name = reader.read_while(|c| c.is_alphanumeric() || c == '_' || c == '-');
match function_name.as_str() {
"newDate" => Ok(Function::NewDate),
"newUuid" => Ok(Function::NewUuid),
_ => Err(ParseError::new(
start.pos,
Expand Down
1 change: 1 addition & 0 deletions packages/hurlfmt/src/format/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ impl Tokenizable for Variable {
impl Tokenizable for Function {
fn tokenize(&self) -> Vec<Token> {
match self {
Function::NewDate => vec![Token::CodeVariable("newDate".to_string())],
Function::NewUuid => vec![Token::CodeVariable("newUuid".to_string())],
}
}
Expand Down

0 comments on commit ea23052

Please sign in to comment.