You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat: Ability to specif function as route response
* feat: Add function support for response creation
fixes#38
* Remove console.log
* Try removing function tests to appease Node 18
* Exclude test
* Downgrade Mocha
* Add --exit to mocha run
Copy file name to clipboardexpand all lines: docs/src/content/docs/mock-servers/extended-response-patterns.mdx
+53-6
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Extended Response Patterns
3
3
description: Learn how to use extended response patterns with a mock server
4
4
---
5
5
6
-
The second argument of any route method in method in a `MockServer` instance is the response pattern. This pattern can be a number (the HTTP status to return) or an object specifying additional data to return with the response. This is helpful when you're making requests that expect a specific response format.
6
+
The second argument of any route method in method in a `MockServer` instance is the response pattern. This pattern can be a number (the HTTP status to return), an object specifying additional data to return with the response, or a function. This is helpful when you're making requests that expect a specific response format.
7
7
8
8
## Extended response pattern keys
9
9
@@ -16,7 +16,7 @@ The following keys can be used in the response pattern object:
16
16
17
17
The response pattern keys are used to create a new `Response` object that is returned when the associated request pattern matches the request.
18
18
19
-
##Responding with a specific status code
19
+
### Respond with a specific status code
20
20
21
21
If you'd like to respond to a request with a specific status code, you can use the `status` key in the response pattern object. Here's an example:
22
22
@@ -36,7 +36,7 @@ This route will respond to any GET request to `/users` with a status code of 200
36
36
server.get("/users", 200);
37
37
```
38
38
39
-
##Responding with specific headers
39
+
### Respond with specific headers
40
40
41
41
If you'd like to respond to a request with specific headers, you can use the `headers` key in the response pattern object. Here's an example that simulates a redirect:
42
42
@@ -55,7 +55,7 @@ server.get("/redirect", {
55
55
56
56
This route will respond to any GET request to `/redirect` with a status code of 301 and a `Location` header that redirects the client to `https://example.com`.
57
57
58
-
##Responding with a specific body
58
+
### Respond with a specific body
59
59
60
60
If you'd like to respond to a request with a specific body, you can use the `body` key in the response pattern object. Here's an example:
61
61
@@ -75,7 +75,7 @@ server.get("/users", {
75
75
76
76
This route will respond to any GET request to `/users` with a status code of 200 and a JSON response body containing an `id` and `name` property. There's no need to call `JSON.stringify` on the object; Mentoss will handle that for you.
77
77
78
-
##Responding with a specific body and headers
78
+
### Respond with a specific body and headers
79
79
80
80
You can combine the `headers` and `body` keys in the response pattern object to respond to a request with specific headers and a specific body. Here's an example:
81
81
@@ -98,7 +98,7 @@ server.get("/users", {
98
98
99
99
This route will respond to any GET request to `/users` with a status code of 200, a `Content-Type` header of `application/json`, and a JSON response body containing an `id` and `name` property.
100
100
101
-
##Delaying the response
101
+
### Delay the response
102
102
103
103
If you'd like to delay the response to a request, you can use the `delay` key in the response pattern object. The `delay` property is the number of milliseconds to wait before returning the response. Here's an example:
104
104
@@ -114,3 +114,50 @@ server.get("/users", {
114
114
```
115
115
116
116
This route will respond to any GET request to `/users` with a status code of 200, but it will wait for 1 second before returning the response.
117
+
118
+
## Use functions for dynamic responses
119
+
120
+
If you'd like to respond to a request with a dynamic value, you can use a function as the response pattern. The function will receive the request object as its only argument and should return a response pattern object. Here's an example:
This route will respond to any GET request to `/users/123` with a status code of 200 and a JSON response body containing an `id` and `name` property. The response is generated dynamically based on the request object. The `request` object contains information about the request, such as the URL, headers, and body, and contains an additional `id` property that is a unique identifier for the request.
142
+
143
+
Response creator functions can also by asynchronous, so you can use `await` to wait for a promise to resolve before returning the response pattern object. Here's an example:
This route will respond to any GET request to `/users/123` with a status code of 200 and a JSON response body containing an `id` and `name` property, but it will wait for 1 second before returning the response. You can use this to simulate network latency or other asynchronous behavior when the `delay` key is not sufficient.
0 commit comments