Webhook Relay
PricingLogin
  • Introduction
  • Quick Start - Forwarding
  • Quick Start - Tunnels
  • 🛠️Installation
    • Relay CLI
      • Install
      • Auto-start
      • Run config.yaml reference
    • Containerized
      • Kubernetes Installation
      • Podman
      • Docker
      • Docker Compose
  • Products
    • 🛰️Webhook Forwarding
      • Glossary
      • WebSocket Server
      • Authentication
      • Custom Domains
    • ⚡Functions
      • Managing functions
      • Edit request/response
      • Working with JSON
      • 🦾Advanced
        • Working with time
        • Testing functions in CLI
        • Making HTTP Requests
        • Multipart Form Data
        • URLEncoded Form Data
        • GCP BigQuery
        • Sending Emails
        • JWT authentication
        • Base64, Hashes, Encryption
      • 🤖Integrating into CI/CD
    • 🔃Tunnels
      • Using tunnels
      • Custom Domains
      • Encryption (HTTPS)
      • Regions
  • 📝Examples
    • Intro to examples
    • Webhooks
      • Receiving webhooks on localhost
      • Receive webhooks inside your JavaScript app
      • Execute shell scripts on remote machines
    • Functions
      • Enrich webhooks from 3rd party APIs
      • Convert DockerHub webhook to Slack notification
      • Allowing only POST requests through
      • Manipulate webhook request body
    • Tunnels
      • Ingress for any Kubernetes environment
      • Demoing your website
    • 🏠Home Automation
      • Home Assistant
      • Node-RED
      • Raspberry Pi
  • Platform
    • CLI Basics
    • Using CLI Behind Proxy
    • Self-hosting Server
      • Server deployment
      • Client configuration
    • Security & Tech
Powered by GitBook
On this page
  • Failing test cases
  • Spec file reference (full example)

Was this helpful?

  1. Products
  2. Functions
  3. Advanced

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"
PreviousWorking with timeNextMaking HTTP Requests

Last updated 1 year ago

Was this helpful?

⚡
🦾