Skip to content

Core Concepts

Understanding the fundamental concepts of mockd will help you create effective mocks for any scenario.

A mock is a rule that defines:

  1. Request Matcher - Which incoming requests to match
  2. Response - What to send back when matched
{
"matcher": { ... }, // Request matcher
"response": { ... } // Response definition
}

When an HTTP request arrives, mockd checks each mock’s request matcher. The first match wins and its response is returned.

The request matcher defines criteria for matching incoming requests:

{
"matcher": {
"method": "GET",
"path": "/api/users",
"headers": {
"Authorization": "Bearer .*"
},
"query": {
"page": "1"
}
}
}
FieldDescriptionMatching Type
methodHTTP method (GET, POST, etc.)Exact match
pathURL pathExact or pattern
headersHTTP headersExact or regex
queryQuery string parametersExact or regex
bodyRequest bodyJSON matching

Paths can include dynamic segments:

"/api/users/{id}" // Matches /api/users/1, /api/users/abc
"/api/{resource}/{id}" // Matches /api/posts/123
"/api/files/{path:.*}" // Matches /api/files/a/b/c (greedy)

Headers and query params support regex:

{
"headers": {
"Authorization": "Bearer [a-zA-Z0-9]+"
}
}

See Request Matching Guide for complete details.

The response defines what mockd sends back:

{
"response": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"message": "Success"
},
"delayMs": 100
}
}
FieldDescriptionDefault
statusCodeHTTP status code200
-----------------------------
headersResponse headers{}
bodyResponse body (string or JSON)""
delayMsSimulated latency (milliseconds)0

The body can be:

  • JSON object/array - Automatically serialized
  • String - Sent as-is
  • File reference - Load from file
// JSON body
"body": {"users": []}
// String body
"body": "<html>Hello</html>"
// File reference
"body": "@./responses/users.json"

Responses can use templates to include request data:

{
"response": {
"body": {
"received_id": "{{request.pathParam.id}}",
"timestamp": "{{now}}"
}
}
}
VariableDescription
request.methodHTTP method
request.pathRequest path
request.pathParam.{name}Path parameter value
request.query.{name}Query parameter value
request.header.{name}Header value
request.bodyParsed request body
nowCurrent timestamp
uuidRandom UUID

See Response Templating Guide for more.

When multiple mocks could match a request, mockd uses this priority:

  1. More specific paths win - /api/users/1 beats /api/users/{id}
  2. More matchers win - Path + headers beats path only
  3. Order in config - Earlier mocks win if priority is equal

A complete configuration file:

{
"server": {
"port": 4280,
"host": "localhost"
},
"mocks": [
{
"name": "List users",
"matcher": {
"method": "GET",
"path": "/api/users"
},
"response": {
"statusCode": 200,
"body": {"users": []}
}
}
]
}
FieldDescriptionRequired
serverServer configurationNo
mocksArray of mock definitionsYes

mockd can simulate stateful CRUD APIs where:

  • POST creates resources
  • GET retrieves resources
  • PUT/PATCH updates resources
  • DELETE removes resources

State persists across requests during the server session.

{
"stateful": {
"resources": {
"users": {
"collection": "/api/users",
"item": "/api/users/{id}"
}
}
}
}

See Stateful Mocking Guide.

mockd can act as a proxy to record real API traffic:

Terminal window
mockd proxy --target https://api.example.com --record

Recorded requests become mocks automatically.

See Proxy Recording Guide.