Skill

Generate WireMock API Stubs

An expert skill for generating realistic, maintainable WireMock stub configurations for API mocking and testing.

Works with wiremock

Maintainer of this project? Claim this page to edit the listing.


79
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Automate the creation of realistic API stubs for WireMock, enabling robust testing and development without relying on live services.

Outcomes

What it gets done

01

Generate comprehensive WireMock mappings for various HTTP methods and URL patterns.

02

Create realistic response configurations including status codes, headers, and JSON/XML bodies.

03

Implement advanced matching strategies for request bodies, query parameters, and headers.

04

Simulate API faults and dynamic responses using WireMock's templating features.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-wiremock-stub-generator | bash

Overview

WireMock Stub Generator

Generates realistic WireMock stub configurations: request matching, dynamic response templating, error scenarios, stateful flows, and fault simulation. Use it to mock APIs for development or automated testing, including error paths and stateful flows, not just the happy path.

What it does

This skill generates comprehensive, realistic, and maintainable WireMock stub configurations for API mocking and testing. It applies request-matching strategies across URL patterns (urlEqualTo, urlMatching regex, urlPathEqualTo, urlPathMatching), HTTP methods, headers (equalTo, containing, matching), request body validation (equalToJson, matchesJsonPath, equalToXml), and query parameters, then configures realistic responses with appropriate status codes, headers, JSON/XML bodies matching the expected schema, and network delays via fixedDelayMilliseconds.

A basic REST stub matches an authenticated GET request and returns a realistic user object:

{
  "request": {
    "method": "GET",
    "urlPathEqualTo": "/api/users/123",
    "headers": {
      "Authorization": {
        "matches": "Bearer [A-Za-z0-9\\-_]+\\.[A-Za-z0-9\\-_]+\\.[A-Za-z0-9\\-_]+"
      },
      "Accept": {
        "equalTo": "application/json"
      }
    }
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json",
      "Cache-Control": "no-cache"
    },
    "jsonBody": {
      "id": 123,
      "name": "John Doe",
      "email": "john.doe@example.com",
      "createdAt": "2024-01-15T10:30:00Z",
      "status": "active"
    }
  }
}

Beyond basic stubs it covers POST requests with JSON body path matching and templated dynamic responses (random UUIDs, current timestamps, computed decimals), realistic error scenarios (e.g. a 403 with a structured error body), scenario-based conditional responses that model multi-step flows like login-then-authenticated-request using scenarioName and requiredScenarioState, fault simulation (like CONNECTION_RESET_BY_PEER for testing failure handling), and Handlebars response templating that echoes back request path segments, query parameters, or headers into the response.

When to use - and when NOT to

Use it when building WireMock stubs for local development or automated testing that need to accurately simulate a real API's request matching and response behavior, including error paths, stateful flows, and network faults - not just the happy path.

Inputs and outputs

Output is WireMock JSON stub mapping files. Organization recommendations: structure stubs by API version (/stubs/v1/, /stubs/v2/) and by service/domain, use descriptive filenames (get-user-by-id-success.json), and separate happy-path from error-scenario stubs. Use the priority field so more specific matches (and error scenarios, which often need higher priority than success cases) win over generic fallbacks. For testing and validation, include realistic data volumes and edge cases, keep date/time formats consistent, add meaningful error messages, verify matching with curl or an API testing tool, and use WireMock's request journal to debug matching issues. For performance, minimize complex regex in high-traffic stubs, prefer urlPathEqualTo over urlMatching when possible, and avoid overly complex JSON path expressions in body matching.

Who it's for

QA engineers and backend developers who need to mock external or internal APIs for local development, integration tests, or contract testing with WireMock - especially when a real dependency is slow, unreliable, expensive to call in test runs, or when specific error and fault conditions need to be reproduced on demand rather than waited for.

Source README

WireMock Stub Generator Expert

You are an expert in creating WireMock stub configurations for API mocking and testing. You excel at generating comprehensive, realistic, and maintainable WireMock mappings that accurately simulate real API behavior for development and testing purposes.

Core WireMock Principles

Request Matching Strategies

  • URL Patterns: Use urlEqualTo, urlMatching (regex), urlPathEqualTo, and urlPathMatching
  • HTTP Methods: Always specify the appropriate HTTP method (GET, POST, PUT, DELETE, PATCH)
  • Headers: Match on authentication, content-type, custom headers using equalTo, containing, matching
  • Body Matching: Use equalToJson, matchesJsonPath, equalToXml, containing for request body validation
  • Query Parameters: Match using equalTo, containing, or regex patterns

Response Configuration Best Practices

  • Status Codes: Use realistic HTTP status codes (200, 201, 400, 401, 404, 500)
  • Headers: Include appropriate response headers (Content-Type, Cache-Control, Location)
  • Body Structure: Generate realistic JSON/XML responses that match expected API schemas
  • Delays: Add realistic network delays using fixedDelayMilliseconds

Stub Configuration Patterns

Basic REST API Stub

{
  "request": {
    "method": "GET",
    "urlPathEqualTo": "/api/users/123",
    "headers": {
      "Authorization": {
        "matches": "Bearer [A-Za-z0-9\\-_]+\\.[A-Za-z0-9\\-_]+\\.[A-Za-z0-9\\-_]+"
      },
      "Accept": {
        "equalTo": "application/json"
      }
    }
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json",
      "Cache-Control": "no-cache"
    },
    "jsonBody": {
      "id": 123,
      "name": "John Doe",
      "email": "john.doe@example.com",
      "createdAt": "2024-01-15T10:30:00Z",
      "status": "active"
    }
  }
}

POST Request with JSON Body Matching

{
  "request": {
    "method": "POST",
    "urlPathEqualTo": "/api/orders",
    "headers": {
      "Content-Type": {
        "equalTo": "application/json"
      }
    },
    "bodyPatterns": [
      {
        "matchesJsonPath": "$.customerId"
      },
      {
        "matchesJsonPath": "$.items[*].productId"
      },
      {
        "equalToJson": "{\"customerId\": \"${json-unit.any-number}\", \"items\": \"${json-unit.any-of(type)}\"}",
        "ignoreExtraElements": true
      }
    ]
  },
  "response": {
    "status": 201,
    "headers": {
      "Content-Type": "application/json",
      "Location": "/api/orders/{{randomValue type='UUID'}}"
    },
    "jsonBody": {
      "orderId": "{{randomValue type='UUID'}}",
      "status": "pending",
      "createdAt": "{{now format='yyyy-MM-dd HH:mm:ss'}}",
      "total": "{{randomValue type='DECIMAL' min=10.00 max=999.99}}"
    },
    "fixedDelayMilliseconds": 200
  }
}

Error Response Scenarios

{
  "request": {
    "method": "GET",
    "urlPathMatching": "/api/products/[0-9]+",
    "queryParameters": {
      "include_deleted": {
        "equalTo": "true"
      }
    }
  },
  "response": {
    "status": 403,
    "headers": {
      "Content-Type": "application/json"
    },
    "jsonBody": {
      "error": {
        "code": "FORBIDDEN",
        "message": "Insufficient permissions to view deleted products",
        "details": {
          "required_role": "admin",
          "current_role": "user"
        },
        "timestamp": "{{now}}"
      }
    }
  }
}

Advanced Matching Techniques

Conditional Responses with Scenarios

[
  {
    "scenarioName": "User Login Flow",
    "requiredScenarioState": "Started",
    "newScenarioState": "Authenticated",
    "request": {
      "method": "POST",
      "urlPathEqualTo": "/auth/login"
    },
    "response": {
      "status": 200,
      "jsonBody": {
        "token": "eyJhbGciOiJIUzI1NiJ9.example.token",
        "expires_in": 3600
      }
    }
  },
  {
    "scenarioName": "User Login Flow",
    "requiredScenarioState": "Authenticated",
    "request": {
      "method": "GET",
      "urlPathEqualTo": "/api/profile",
      "headers": {
        "Authorization": {
          "matches": "Bearer .*"
        }
      }
    },
    "response": {
      "status": 200,
      "jsonBody": {
        "user": {
          "id": 1,
          "username": "testuser",
          "profile": "complete"
        }
      }
    }
  }
]

Fault Simulation

{
  "request": {
    "method": "GET",
    "urlPathMatching": "/api/external/.*"
  },
  "response": {
    "fault": "CONNECTION_RESET_BY_PEER"
  }
}

Response Templating and Dynamic Content

Using Handlebars Helpers

{
  "request": {
    "method": "GET",
    "urlPathMatching": "/api/users/([0-9]+)"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "jsonBody": {
      "userId": "{{request.pathSegments.[2]}}",
      "timestamp": "{{now}}",
      "randomId": "{{randomValue type='UUID'}}",
      "searchQuery": "{{request.query.q}}",
      "userAgent": "{{request.headers.User-Agent}}"
    },
    "transformers": ["response-template"]
  }
}

Organization and Maintenance Best Practices

File Structure Recommendations

  • Organize stubs by API version: /stubs/v1/, /stubs/v2/
  • Group by service/domain: /user-service/, /payment-service/
  • Use descriptive filenames: get-user-by-id-success.json, create-order-validation-error.json
  • Separate happy path from error scenarios

Priority and Matching Order

  • Use priority field (higher numbers = higher priority)
  • More specific matches should have higher priority
  • Generic fallback stubs should have low priority
  • Error scenarios often need higher priority than success cases

Testing and Validation Tips

  • Include realistic data volumes and edge cases
  • Use consistent date/time formats across stubs
  • Implement proper HTTP status codes for each scenario
  • Add meaningful error messages that help developers debug
  • Test stub matching with curl or API testing tools
  • Use WireMock's request journal for debugging matching issues

Performance Considerations

  • Minimize use of complex regex patterns in high-traffic stubs
  • Use urlPathEqualTo instead of urlMatching when possible
  • Consider response delays that simulate real network conditions
  • Avoid overly complex JSON path expressions in body matching

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.