Stoobly Docs
  • Introduction
  • Use Cases
    • Generate Mock APIs
      • Empower Development
      • Scale API Testing
    • Enable E2E Testing
  • FAQ
    • Recording
    • Mocking
    • Terminology
  • Getting Started
    • Installing the Agent
      • Installation with pipx
      • Installation with Docker
  • Core Concepts
    • Agent
      • Intercept Modes
        • Mocking
        • Recording
      • Lifecycle Hooks
      • Proxy Settings
        • Data Rules
        • Firewall Rules
        • Rewrite Rules
        • Match Rules
    • Context
    • Mock API
      • Request
        • Response
        • Replay History
      • Scenarios
      • Snapshots
      • Fixtures
      • Public Folder
    • Scaffold
      • Service
      • Validation
      • Workflow
  • Guides
    • How to Run the Agent
      • Run with CLI
      • Run with Docker
    • How to Configure the Agent
      • Forward Proxy
        • Enable HTTPS Traffic
      • Reverse Proxy
    • How to Record Requests
      • Recording from the UI
      • Recording from the CLI
      • How to Create Contexts
      • How to Create Scenarios
        • Creating from the UI
        • Creating from the CLI
      • How to Create Requests
      • How to Customize Recordings
        • Customizing with Lifecycle Hooks
    • How to Update Requests
      • Editing from the UI
      • Editing with Snapshots
      • How to Update Scenarios
        • Updating from the UI
        • Updating from the CLI
      • Updating with Replay
        • Replaying from the UI
        • Replaying from the CLI
        • How to Customize Replays
          • Customizing with Lifecycle Hooks
      • Updating with Open API
    • How to Mock APIs
      • How to Enable Mocking
        • Enabling from the UI
        • Enabling from the CLI
      • How to Snapshot Requests
        • Deleting Snapshots
        • Sharing Snapshots
      • How to Use Fixtures
      • How to Customize Mocking
        • Customizing with Lifecycle Hooks
        • Customizing with Request Headers
      • Troubleshooting
    • How to Replay Requests
      • Replay with the UI
      • Replay with the CLI
    • How to Integrate E2E Testing
      • How to Scaffold an App
        • Scaffolding a Service
        • Customizing a Workflow
          • Customizing Container Services
          • Customizing Lifecycle Hooks
          • Customizing Init Scripts
          • Customizing Configure Scripts
          • Customizing Makefile
        • Troubleshooting
      • How to Run a Workflow
        • Running with CLI command
        • Running with Make
        • Troubleshooting
          • Validating
      • How to Stop a Workflow
        • Stopping with CLI command
        • Stopping with Make
      • How to Update a Scaffold
        • Deleting a Service
      • FAQ
  • Developer Guide
    • Installation from Source
    • Submitting Change Requests
    • Releases
  • Experimental
    • Experimental Features
      • Aliases
      • Endpoints
      • API Testing
        • Getting Started
        • Configuration
          • Assign
          • Lifecycle Hooks
          • Trace
      • Optional Components
Powered by GitBook
On this page
  • What are Lifecycle Hooks?
  • Why use Lifecycle Hooks?
  • What are the Different Lifecycle Hook Events Supported?
  • Types of Contexts
  • Enabling Lifecycle Hook Scripts Use
  • Next Steps

Was this helpful?

  1. Core Concepts
  2. Agent

Lifecycle Hooks

We currently only support lifecycle hook scripts written in Python.

What are Lifecycle Hooks?

A lifecycle hook is a way to add custom functionality during specific events in the life of a Stoobly resource such as a request or a response.

Stoobly's Lifecycle Hooks can be written as Python functions which use the Stoobly Python library to hook into these events and do addtional tasks.

Why use Lifecycle Hooks?

These hooks enable you to add custom functionality with your own code at different stages.

Some examples include:

  • Rewriting a header's value before or after its request gets sent to not store sensitive data

  • Adding, removing or modifying data such as in headers, query parameters, bodies, etc.

  • Adding extra debug information or publishing metrics

What are the Different Lifecycle Hook Events Supported?

handle_before_request
from stoobly_agent.app.proxy.context import InterceptContext

def handle_before_request(context: InterceptContext):
    pass
handle_before_record
from stoobly_agent.app.proxy.record.context import RecordContext

def handle_before_record(context: RecordContext):
    pass
handle_before_mock
from stoobly_agent.app.proxy.mock.context import MockContext

def handle_before_mock(context: MockContext):
    pass
handle_before_test
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_mock(context: TestContext):
    pass
handle_after_record
from stoobly_agent.app.proxy.record.context import RecordContext

def handle_after_record(context: RecordContext):
    pass
handle_after_mock
from stoobly_agent.app.proxy.mock.context import MockContext

def handle_after_mock(context: MockContext):
    pass
handle_after_test
from stoobly_agent.app.proxy.test.context import TestContext

def handle_after_test(context: TestContext):
    pass
handle_before_replay
from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_before_replay(context: ReplayContext):
    pass
handle_after_replay
from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_after_replay(context: ReplayContext):
    pass
handle_before_response
from stoobly_agent.app.proxy.context import InterceptContext

def handle_before_response(context: InterceptContext):
    pass
handle_before_request_component_create
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_request_component_create(context: TestContext):
    pass
handle_before_request_component_delete
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_request_component_delete(context: TestContext):
    pass

Types of Contexts

There are several types of contexts available for your scripts. The parameter type will change depending on what type of hook you are using. To see the fields and methods available, view the full definition as well as any parent classes they might be inherited from.

RecordContext

To import it, add this to the top of your script:

from stoobly_agent.app.proxy.record.context import RecordContext

ReplayContext

To import it, add this to the top of your script:

from stoobly_agent.app.proxy.replay.context import ReplayContext

MockContext

To import it, add this to the top of your script:

from stoobly_agent.app.proxy.mock.context import MockContext

Enabling Lifecycle Hook Scripts Use

Once your script is written, it can now be used by the Agent. Lifecycle hooks are supported for the following commands:

  • stoobly-agent run

  • stoobly-agent request replay

  • stoobly-agent scenario replay

  • stoobly-agent endpoint import

Next Steps

Next see our guide on how to create your own lifecycle hooks and how to use them:

PreviousRecordingNextProxy Settings

Last updated 1 month ago

Was this helpful?

For the full list, see to the Stoobly Python definitions of the events available. All of the strings on the right side of the variable assignment are also the function names available in lifecycle hook scripts.

For record events use the RecordContext. The full definition can .

For replay events use the ReplayContext. The full definition can .

For mock events use the MockContext. The full definition can .

this link
be found here
be found here
be found here
How to Customize Recordings