Testing functions in CLI

Relay CLI allows creating a test cases for your functions

Relay CLI implements a test command:

relay function test  -f spec.yaml

that can run a test. Example spec file:

version: "v1"
filename: spec_func.lua # Location of the function file
driver: lua
tests:
  - name: standard request # Test case (you can define multiple test cases)
    request:
      method: PUT
      body: |
        {
          "user": "john"
        }
      header:
        foo: bar
    expect:
      request:
        bodyModified: true
        bodyContains: "some"
        bodyEquals: "something new"
        # Header
        headerModified: true
        # Method check
        methodEquals: POST

      response:
        bodyContains: "hello"

And this is an example of the spec_func.lua:

r:SetResponseBody("hello-from-function")
r:SetRequestBody("something new")
r:SetRequestMethod("POST")
r:SetRequestHeader("foo", "bar")

When you execute the test command, relay CLI will:

  1. Create a function
  2. Creates a request based on your specified parameters
  3. Invokes the function
  4. Compares response to the response defined in the spec

Example output:

relay function test -f spec.yaml  
PASS
spec.yaml 0.983s

Failing test cases

Now, let’s modify the function to return a different response body:

r:SetResponseBody("bye-bye")
r:SetRequestBody("something new")
r:SetRequestMethod("POST")
r:SetRequestHeader("foo", "bar")

Running the test will result in a failure:

relay function test -f spec.yaml
--- FAIL:  (0.115s)

    Error:       "bye-bye" does not contain "hello"
    Messages:    response.bodyContains does not match
FAIL
spec.yaml 0.570s

Spec file reference (full example)

version: "v1"
filename: spec_func.lua # Location of the function file
driver: lua
tests:
  - name: standard request # Test case (you can define multiple test cases)
    request:
      method: PUT
      body: |
        {
          "user": "john"
        }
      header:
        foo: bar
    expect:
      request:
        modified: true # Is true when any of the fields get modified
        # Body checks
        bodyModified: true
        bodyContains: "some"
        bodyEquals: "something new"
        # Header
        headerModified: true
        headerEquals:
          Authorization: "Bearer xyz"
        # Method check
        methodModified: true
        methodEquals: POST
        # Request path checks
        pathModified: false
        pathEquals: /some/path
        pathContains: /users
        # Query args (e.g. ?foo=bar)
        queryModified: true
        queryContains: foo=bar


      response:
        bodyContains: "hello"