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
  • Background
  • Writing a Lifecycle Hooks Script
  • Enabling Lifecycle Hook Script Use
  • Example

Was this helpful?

  1. Guides
  2. How to Mock APIs
  3. How to Customize Mocking

Customizing with Lifecycle Hooks

PreviousHow to Customize MockingNextCustomizing with Request Headers

Last updated 1 month ago

Was this helpful?

Background

Writing a Lifecycle Hooks Script

  1. Write a Python script (e.g. lifecycle_hooks.py)

  2. In this script, define the lifecycle events to hook into

  3. Define what kind of behavior to execute for each of those events

Below is a sample script that prints some strings during two lifecycle events.

Manipulating the context object will affect the behaviour of the request interception.

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

def handle_before_mock(context: MockContext):
    print('Before mock!')

def handle_after_mock(context: MockContext):
    print('After mock!')
  • handle_before_mock is for before a request gets mocked

  • handle_after_mock is for after a request gets mocked

Enabling Lifecycle Hook Script Use

Pass the path to the lifecycle hooks scripts using the --lifecycle-hooks-path option:

stoobly-agent run --lifecycle-hooks-path ~/path-to-file/lifecycle_hooks.py

Example

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

def handle_after_mock(context: MockContext):
    response = context.flow.response
    response.status_code = 401 # Test unauthorized
Lifecycle Hooks