1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Example mockd configuration.
#
# yaml-language-server: $schema=https://denislituev.github.io/mockd/schema.json
#
# Start it with:
# mockd serve examples/users.yaml
#
# For browser/SPA testing, enable CORS:
# mockd serve examples/users.yaml --cors
#
# Then try:
# curl http://localhost:8080/health
# curl http://localhost:8080/users/42
# curl "http://localhost:8080/users?role=admin"
# curl -H "X-Tenant-Id: tenant-a" http://localhost:8080/users
# curl -X POST http://localhost:8080/echo -d '{"id":7,"name":"alice"}' -H 'Content-Type: application/json'
# curl http://localhost:8080/fresh # fresh uuid / timestamp each call
# curl http://localhost:8080/flaky # call 3 times: 500, 500, 200
listen: ":8080"
routes:
# Simple health-check endpoint.
- method: GET
path: /health
response:
status: 200
body:
ok: true
# Path parameters + templated response.
- method: GET
path: /users/{id}
response:
status: 200
body:
id: "{{path.id}}"
name: "User {{path.id}}"
# Conditional response based on a query parameter.
- method: GET
path: /users
when:
query:
role: admin
response:
status: 200
body:
users:
- id: 1
name: Admin
headers:
Cache-Control: no-store
# Fallback for the same path.
- method: GET
path: /users
response:
status: 200
body:
users:
- id: 1
name: John
- id: 2
name: Jane
# Conditional response based on a header.
- method: GET
path: /tenants/me
when:
headers:
X-Tenant-Id: tenant-a
response:
status: 200
body:
tenant: "{{header.X-Tenant-Id}}"
# Create user: echoes the request body via templates.
- method: POST
path: /users
response:
status: 201
body:
created: true
id: "{{body.id}}"
name: "{{body.name}}"
# Helper functions for realistic-looking responses.
- method: GET
path: /fresh
response:
status: 200
body:
id: "{{uuid}}"
created_at: "{{now}}"
priority: "{{randomInt(1,5)}}"
# Sequence responses: returns 500 twice, then 200 forever.
# Useful for testing client retry logic.
- method: GET
path: /flaky
response:
sequence:
- status: 500
body:
error: transient
- status: 500
body:
error: transient
- status: 200
body:
ok: true
# Body-based matching for login.
- method: POST
path: /login
when:
body:
username: admin
response:
status: 200
body:
token: admin-token
- method: POST
path: /login
response:
status: 401
body:
error: invalid credentials
# Simulate a failure.
- method: GET
path: /fail
response:
status: 500
body:
error: something went wrong
# Simulate a slow response (useful for timeout testing).
- method: GET
path: /slow
response:
status: 200
delay: 1s
body:
ok: true
# Force the connection to be closed after responding.
- method: GET
path: /drop
response:
status: 200
close_connection: true
body:
dropped: true