$npx -y skills add LambdaTest/agent-skills --skill api-mockingProduces mock server definitions, sandbox environment specs, API stub configurations, and fixture data for any API. Use whenever the user asks about mocking an API, creating a sandbox, "fake server", WireMock stubs, Prism mocks, MSW (Mock Service Worker), "test fixtures", "stub t
| 1 | # API Mock & Sandbox Skill |
| 2 | |
| 3 | Generate mock servers, stubs, fixtures, and sandbox environments for any API. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## Mock Strategy Selection |
| 8 | |
| 9 | | Scenario | Tool / Approach | |
| 10 | |----------|----------------| |
| 11 | | Frontend dev against unbuilt backend | WireMock / Prism / MSW | |
| 12 | | Unit tests (in-process) | In-memory mock functions | |
| 13 | | Contract testing | Pact (consumer-driven contracts) | |
| 14 | | Postman testing | Postman Mock Server | |
| 15 | | Local development | Prism CLI from OpenAPI spec | |
| 16 | | Record & replay real API | VCR (Python/Ruby), nock recordings | |
| 17 | |
| 18 | --- |
| 19 | |
| 20 | ## WireMock Stub Definition |
| 21 | |
| 22 | ```json |
| 23 | { |
| 24 | "request": { |
| 25 | "method": "GET", |
| 26 | "urlPathPattern": "/api/v1/users/([a-z0-9-]+)" |
| 27 | }, |
| 28 | "response": { |
| 29 | "status": 200, |
| 30 | "headers": { "Content-Type": "application/json" }, |
| 31 | "jsonBody": { |
| 32 | "id": "{{request.pathSegments.[3]}}", |
| 33 | "name": "Alice Smith", |
| 34 | "email": "alice@example.com", |
| 35 | "created_at": "2024-01-01T00:00:00Z" |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | ``` |
| 40 | |
| 41 | ### WireMock Stateful Scenario |
| 42 | ```json |
| 43 | [ |
| 44 | { |
| 45 | "scenarioName": "Order flow", |
| 46 | "requiredScenarioState": "Started", |
| 47 | "newScenarioState": "Paid", |
| 48 | "request": { "method": "POST", "url": "/api/v1/orders" }, |
| 49 | "response": { "status": 201, "jsonBody": { "id": "ord_123", "status": "pending" } } |
| 50 | }, |
| 51 | { |
| 52 | "scenarioName": "Order flow", |
| 53 | "requiredScenarioState": "Paid", |
| 54 | "request": { "method": "GET", "url": "/api/v1/orders/ord_123" }, |
| 55 | "response": { "status": 200, "jsonBody": { "id": "ord_123", "status": "paid" } } |
| 56 | } |
| 57 | ] |
| 58 | ``` |
| 59 | |
| 60 | --- |
| 61 | |
| 62 | ## Mock Service Worker (MSW — browser/Node.js) |
| 63 | |
| 64 | ```js |
| 65 | import { http, HttpResponse } from 'msw'; |
| 66 | |
| 67 | export const handlers = [ |
| 68 | http.get('/api/v1/users', () => { |
| 69 | return HttpResponse.json({ |
| 70 | data: [ |
| 71 | { id: 'usr_1', name: 'Alice', email: 'alice@example.com' }, |
| 72 | { id: 'usr_2', name: 'Bob', email: 'bob@example.com' }, |
| 73 | ], |
| 74 | pagination: { total: 2, page: 1, limit: 20 } |
| 75 | }); |
| 76 | }), |
| 77 | |
| 78 | http.post('/api/v1/users', async ({ request }) => { |
| 79 | const body = await request.json(); |
| 80 | return HttpResponse.json( |
| 81 | { id: 'usr_new', ...body, created_at: new Date().toISOString() }, |
| 82 | { status: 201 } |
| 83 | ); |
| 84 | }), |
| 85 | |
| 86 | http.get('/api/v1/users/:id', ({ params }) => { |
| 87 | if (params.id === 'not-found') { |
| 88 | return HttpResponse.json({ error: 'NOT_FOUND' }, { status: 404 }); |
| 89 | } |
| 90 | return HttpResponse.json({ id: params.id, name: 'Alice' }); |
| 91 | }), |
| 92 | ]; |
| 93 | ``` |
| 94 | |
| 95 | --- |
| 96 | |
| 97 | ## Fixture Data Generator |
| 98 | |
| 99 | ```python |
| 100 | from faker import Faker |
| 101 | import uuid |
| 102 | |
| 103 | fake = Faker() |
| 104 | |
| 105 | def generate_user(overrides=None): |
| 106 | user = { |
| 107 | "id": str(uuid.uuid4()), |
| 108 | "name": fake.name(), |
| 109 | "email": fake.email(), |
| 110 | "phone": fake.phone_number(), |
| 111 | "address": { |
| 112 | "street": fake.street_address(), |
| 113 | "city": fake.city(), |
| 114 | "country": fake.country_code() |
| 115 | }, |
| 116 | "created_at": fake.date_time_this_year().isoformat() |
| 117 | } |
| 118 | return {**user, **(overrides or {})} |
| 119 | |
| 120 | def generate_users(count=10): |
| 121 | return [generate_user() for _ in range(count)] |
| 122 | ``` |
| 123 | |
| 124 | --- |
| 125 | |
| 126 | ## Error Scenario Stubs |
| 127 | |
| 128 | Always include these error stubs for every endpoint: |
| 129 | ```json |
| 130 | { "request": { "method": "GET", "url": "/api/v1/users/error-500" }, |
| 131 | "response": { "status": 500, "jsonBody": { "error": "INTERNAL_ERROR" } } } |
| 132 | |
| 133 | { "request": { "method": "GET", "url": "/api/v1/users/error-401" }, |
| 134 | "response": { "status": 401, "jsonBody": { "error": "UNAUTHENTICATED" } } } |
| 135 | |
| 136 | { "request": { "method": "GET", "url": "/api/v1/users/error-429" }, |
| 137 | "response": { "status": 429, |
| 138 | "headers": { "Retry-After": "30" }, |
| 139 | "jsonBody": { "error": "RATE_LIMIT_EXCEEDED" } } } |
| 140 | ``` |
| 141 | |
| 142 | --- |
| 143 | |
| 144 | ## Prism CLI (mock from OpenAPI spec) |
| 145 | |
| 146 | ```bash |
| 147 | # Install |
| 148 | npm install -g @stoplight/prism-cli |
| 149 | |
| 150 | # Mock from local spec |
| 151 | prism mock openapi.yaml --port 4010 |
| 152 | |
| 153 | # Mock from URL |
| 154 | prism mock https://api.example.com/openapi.json |
| 155 | |
| 156 | # Validate requests against spec |
| 157 | prism proxy https://api.example.com openapi.yaml |
| 158 | ``` |
| 159 | |
| 160 | --- |
| 161 | |
| 162 | ## After Completing the API Mocks and Stubs (as requested) |
| 163 | |
| 164 | Once the API mocks output is delivered, ask the user: |
| 165 | |
| 166 | "Would you like me to help in devising rate limiting strategies for these APIs? (yes/no)" |
| 167 | |
| 168 | If the user says **yes**: |
| 169 | - Check if th |