@@ -7,32 +7,24 @@ How-tos & Examples
77 snippets from this page, be aware that it'll only work if "mock" is
88 installed as a global executable.
99
10- Delaying specific endpoints
11- ---------------------------
12-
13- Making an existing API slow can be easily accomplished combining mock's
14- :ref: `Base APIs <base_api >` and the :ref: `delay option.
15- <cmd_options_reference__delay>`
10+ A "Who is" Lookup Service
11+ -------------------------
1612
1713.. code :: sh
1814
19- $ mock serve -p 8000 --base example.com --delay 2000
15+ mock serve -p 3000 \
16+ --route ' whois' \
17+ --exec ' whois $(mock get-query domain) | mock write'
2018
21- You may want however to make a specific endpoint slow instead of the whole API.
22- This can be achieved using :ref: `middlewares <middlewares >`:
19+ Let's now test it:
2320
2421.. code :: sh
2522
26- $ mock serve -p 8000 --base example.com --middleware '
27- if [ "${MOCK_REQUEST_ENDPOINT}" = "some/endpoint" ]
28- then
29- sleep 2 # wait two seconds
30- fi
31- '
32-
33- With that last example, our API at ``localhost:8000 `` will act as a proxy to
34- ``example.com ``. All requests will be responded immediately except
35- ``some/endpoint `` which will have a delay of 2 seconds.
23+ $ curl localhost:3000/whois? domain=google.com
24+ # Prints out:
25+ # Domain Name: GOOGLE.COM
26+ # Registry Domain ID: 2138514_DOMAIN_COM-VRSN
27+ # ...
3628
3729 An API powered by multiple languages
3830------------------------------------
@@ -184,3 +176,31 @@ Let's now test it:
184176 # {"name":"John Doe","email":"john.doe@example.com"},
185177 # {"name":"Jane Doe","email":"jane.doe@example.com"}
186178 # ]
179+
180+ Delaying specific endpoints
181+ ---------------------------
182+
183+ Making an existing API slow can be easily accomplished combining mock's
184+ :ref:` Base APIs < base_api> ` and the :ref:` delay option.
185+ < cmd_options_reference__delay> `
186+
187+ .. code:: sh
188+
189+ $ mock serve -p 8000 --base example.com --delay 2000
190+
191+ You may want however to make a specific endpoint slow instead of the whole API.
192+ This can be achieved using :ref:` middlewares < middlewares> ` :
193+
194+ .. code:: sh
195+
196+ $ mock serve -p 8000 --base example.com --middleware '
197+ if [ "${MOCK_REQUEST_ENDPOINT} " = "some/endpoint" ]
198+ then
199+ sleep 2 # wait two seconds
200+ fi
201+ '
202+
203+ With that last example, our API at ` ` localhost:8000` ` will act as a proxy to
204+ ` ` example.com` ` . All requests will be responded immediately except
205+ ` ` some/endpoint` ` which will have a delay of 2 seconds.
206+
0 commit comments