Webhook Relay
Search…
Testing functions in CLI
Relay CLI allows creating a test cases for your functions
Relay CLI implements a test command:
1
relay function test -f spec.yaml
Copied!
that can run a test. Example spec file:
1
version: "v1"
2
filename: spec_func.lua # Location of the function file
3
driver: lua
4
tests:
5
- name: standard request # Test case (you can define multiple test cases)
6
request:
7
method: PUT
8
body: |
9
{
10
"user": "john"
11
}
12
header:
13
foo: bar
14
expect:
15
request:
16
bodyModified: true
17
bodyContains: "some"
18
bodyEquals: "something new"
19
# Header
20
headerModified: true
21
# Method check
22
methodEquals: POST
23
​
24
response:
25
bodyContains: "hello"
26
​
Copied!
And this is an example of the spec_func.lua:
1
r:SetResponseBody("hello-from-function")
2
r:SetRequestBody("something new")
3
r:SetRequestMethod("POST")
4
r:SetRequestHeader("foo", "bar")
Copied!
When you execute the test command, relay CLI will:
  1. 1.
    Create a function
  2. 2.
    Creates a request based on your specified parameters
  3. 3.
    Invokes the function
  4. 4.
    Compares response to the response defined in the spec
Example output:
1
relay function test -f spec.yaml
2
PASS
3
spec.yaml 0.983s
Copied!

Failing test cases

Now, let's modify the function to return a different response body:
1
r:SetResponseBody("bye-bye")
2
r:SetRequestBody("something new")
3
r:SetRequestMethod("POST")
4
r:SetRequestHeader("foo", "bar")
Copied!
Running the test will result in a failure:
1
relay function test -f spec.yaml
2
--- FAIL: (0.115s)
3
​
4
Error: "bye-bye" does not contain "hello"
5
Messages: response.bodyContains does not match
6
FAIL
7
spec.yaml 0.570s
8
​
Copied!

Spec file reference (full example)

1
version: "v1"
2
filename: spec_func.lua # Location of the function file
3
driver: lua
4
tests:
5
- name: standard request # Test case (you can define multiple test cases)
6
request:
7
method: PUT
8
body: |
9
{
10
"user": "john"
11
}
12
header:
13
foo: bar
14
expect:
15
request:
16
modified: true # Is true when any of the fields get modified
17
# Body checks
18
bodyModified: true
19
bodyContains: "some"
20
bodyEquals: "something new"
21
# Header
22
headerModified: true
23
headerEquals:
24
Authorization: "Bearer xyz"
25
# Method check
26
methodModified: true
27
methodEquals: POST
28
# Request path checks
29
pathModified: false
30
pathEquals: /some/path
31
pathContains: /users
32
# Query args (e.g. ?foo=bar)
33
queryModified: true
34
queryContains: foo=bar
35
36
​
37
response:
38
bodyContains: "hello"
Copied!