Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ GraphQL queries can also use [Hurl templates].

### Using Dynamic Datas

[Functions] like `newUuid` and `newDate` can be used in templates to create dynamic datas:
[Functions] like `newUuid`, `newDate` or `randomFirstName` can be used in templates to create dynamic datas:


A file that creates a dynamic email (i.e `0531f78f-7f87-44be-a7f2-969a1c4e6d97@test.com`):
Expand All @@ -626,6 +626,19 @@ date: {{newDate}}
HTTP 200
```

A file that creates a random user. Functions parameters are separated with a whitespace, like filters parameters:

```hurl
POST https://example.org/api/users
{
"first_name": "{{randomFirstName}}",
"last_name": "{{randomLastName}}",
"email": "{{randomEmail}}",
"age": {{randomInt 18 99}},
"token": "{{randomString 32}}"
}
```

[Doc](https://hurl.dev/docs/templates.html#functions)

## Testing Response
Expand Down
15 changes: 14 additions & 1 deletion docs/samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ GraphQL queries can also use [Hurl templates].

### Using Dynamic Datas

[Functions] like `newUuid` and `newDate` can be used in templates to create dynamic datas:
[Functions] like `newUuid`, `newDate` or `randomFirstName` can be used in templates to create dynamic datas:


A file that creates a dynamic email (i.e `0531f78f-7f87-44be-a7f2-969a1c4e6d97@test.com`):
Expand All @@ -346,6 +346,19 @@ date: {{newDate}}
HTTP 200
```

A file that creates a random user. Functions parameters are separated with a whitespace, like filters parameters:

```hurl
POST https://example.org/api/users
{
"first_name": "{{randomFirstName}}",
"last_name": "{{randomLastName}}",
"email": "{{randomEmail}}",
"age": {{randomInt 18 99}},
"token": "{{randomString 32}}"
}
```

[Doc](/docs/templates.md#functions)

## Testing Response
Expand Down
24 changes: 24 additions & 0 deletions docs/spec/grammar/hurl.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,37 @@ function:
env-function
| now-function
| uuid-function
| random-bool-function
| random-email-function
| random-first-name-function
| random-full-name-function
| random-int-function
| random-last-name-function
| random-string-function
| random-word-function

env-function: "getEnv"

now-function: "newDate"

uuid-function: "newUuid"

random-bool-function: "randomBool"

random-email-function: "randomEmail"

random-first-name-function: "randomFirstName"

random-full-name-function: "randomFullName"

random-int-function: "randomInt" sp integer sp integer

random-last-name-function: "randomLastName"

random-string-function: "randomString" sp integer

random-word-function: "randomWord"



# Filter
Expand Down
47 changes: 43 additions & 4 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ jsonpath "$.errors[{{index}}].id" == "error"

Besides variables, functions can be used to generate dynamic values. Current functions are:

| Function | Description |
|-----------|--------------------------------------------------------------|
| `newUuid` | Generates an [UUID v4 random string] |
| `newDate` | Generates an [RFC 3339] UTC date string, at the current time |
| Function | Description |
|-------------------------|-------------------------------------------------------------------|
| `newUuid` | Generates an [UUID v4 random string] |
| `newDate` | Generates an [RFC 3339] UTC date string, at the current time |
| `randomBool` | Generates a random boolean |
| `randomEmail` | Generates a random email address |
| `randomFirstName` | Generates a random first name |
| `randomFullName` | Generates a random full name |
| `randomInt <min> <max>` | Generates a random integer between `min` and `max`, both included |
| `randomLastName` | Generates a random last name |
| `randomString <count>` | Generates a random alphanumeric string of `count` characters |
| `randomWord` | Generates a random word |

Like filters, functions parameters are not enclosed in parentheses and are separated with a whitespace.

In the following example, we use `newDate` to generate a dynamic query parameter:

Expand Down Expand Up @@ -77,6 +87,35 @@ When run, the request body will be:
}
```

The `random...` functions generate realistic test data. In this third example, we create a user with a random
name, email and age:

```hurl
POST https://example.org/api/users
{
"first_name": "{{randomFirstName}}",
"last_name": "{{randomLastName}}",
"email": "{{randomEmail}}",
"age": {{randomInt 18 99}},
"token": "{{randomString 32}}"
}
```

When run, the request body will be:

```
{
"first_name": "Vernice",
"last_name": "Watsica",
"email": "kaley.bins@example.com",
"age": 42,
"token": "kZ3xQpL8mNvT2wYc7RbA5sHdEjFgU9iO"
}
```

Note that `randomInt` returns a number and `randomBool` a boolean: used alone in a JSON body, they don't need to be
quoted.


## Types

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Parsing literal
--> tests_error_parser/function_random_int_invalid_range.hurl:3:24
|
3 | value: {{randomInt 100 1}}
| ^ expecting 'an integer greater than or equal to 100'
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET http://localhost:8000
[Query]
value: {{randomInt 100 1}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/function_random_int_invalid_range.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/function_random_int_invalid_range.hurl
11 changes: 11 additions & 0 deletions integration/hurl/tests_ok/function/random_function.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GET http://localhost:8000/random-function
[Query]
bool: {{randomBool}}
email: {{randomEmail}}
first-name: {{randomFirstName}}
full-name: {{randomFullName}}
int: {{randomInt 10 99}}
last-name: {{randomLastName}}
string: {{randomString 32}}
word: {{randomWord}}
HTTP 200
4 changes: 4 additions & 0 deletions integration/hurl/tests_ok/function/random_function.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'

hurl tests_ok/function/random_function.hurl
23 changes: 23 additions & 0 deletions integration/hurl/tests_ok/function/random_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re

from app import app
from flask import request


@app.route("/random-function")
def random_function():
assert request.args.get("bool") in ["true", "false"]

email = request.args.get("email")
assert re.match(r"^[^@\s]+@[^@\s]+$", email)

value = int(request.args.get("int"))
assert 10 <= value <= 99

string = request.args.get("string")
assert re.match(r"^[A-Za-z0-9]{32}$", string)

for name in ["first-name", "last-name", "full-name", "word"]:
assert len(request.args.get(name)) > 0

return ""
4 changes: 4 additions & 0 deletions integration/hurl/tests_ok/function/random_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -Eeuo pipefail

hurl tests_ok/function/random_function.hurl
6 changes: 6 additions & 0 deletions integration/hurlfmt/tests_export/random_function.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<pre><code class="language-hurl"><span class="entry"><span class="request"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span>
<span class="section-header">[Query]</span>
<span class="string">int</span>: <span class="string">{{randomInt 10 99}}</span>
<span class="string">string</span>: <span class="string">{{randomString 32}}</span>
<span class="string">name</span>: <span class="string">{{randomFirstName}}</span>
</span></span></code></pre>
5 changes: 5 additions & 0 deletions integration/hurlfmt/tests_export/random_function.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GET http://localhost:8000/hello
[Query]
int: {{randomInt 10 99}}
string: {{randomString 32}}
name: {{randomFirstName}}
1 change: 1 addition & 0 deletions integration/hurlfmt/tests_export/random_function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/hello","query_string_params":[{"name":"int","value":"{{randomInt 10 99}}"},{"name":"string","value":"{{randomString 32}}"},{"name":"name","value":"{{randomFirstName}}"}]}}]}
5 changes: 5 additions & 0 deletions integration/hurlfmt/tests_export/random_function.lint.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GET http://localhost:8000/hello
[Query]
int: {{randomInt 10 99}}
string: {{randomString 32}}
name: {{randomFirstName}}
4 changes: 4 additions & 0 deletions packages/hurl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ clap = { version = "4.6.5", features = ["string", "wrap_help"] }
curl = "0.4.50"
curl-sys = "0.4.90"
encoding_rs = "0.8.35"
# fake generates the random data of the random... functions. Default features are disabled to keep
# the dependency tree minimal; its rand requirement is already satisfied by the version used by uuid.
fake = { version = "5.1.0", default-features = false }
glob = "0.3.4"
hurl_core = { version = "8.1.0", path = "../hurl_core" }
libflate = "2.3.1"
libxml = "0.3.21"
md5 = "0.8.1"
percent-encoding = "2.3.2"
rand = "0.10.2"
regex = "1.13.1"
serde = { version = "1.0.229", features = ["derive"] }
serde_json = { version = "1.0.151", features = ["arbitrary_precision"] }
Expand Down
15 changes: 14 additions & 1 deletion packages/hurl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ GraphQL queries can also use [Hurl templates].

### Using Dynamic Datas

[Functions] like `newUuid` and `newDate` can be used in templates to create dynamic datas:
[Functions] like `newUuid`, `newDate` or `randomFirstName` can be used in templates to create dynamic datas:


A file that creates a dynamic email (i.e `0531f78f-7f87-44be-a7f2-969a1c4e6d97@test.com`):
Expand All @@ -626,6 +626,19 @@ date: {{newDate}}
HTTP 200
```

A file that creates a random user. Functions parameters are separated with a whitespace, like filters parameters:

```hurl
POST https://example.org/api/users
{
"first_name": "{{randomFirstName}}",
"last_name": "{{randomLastName}}",
"email": "{{randomEmail}}",
"age": {{randomInt 18 99}},
"token": "{{randomString 32}}"
}
```

[Doc](https://hurl.dev/docs/templates.html#functions)

## Testing Response
Expand Down
Loading