You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.9 KiB

9 years ago
  1. ## Mock Responses
  2. Mocks give you full control over the response headers and body returned to the client. They can be used to return anything from a simple html string to a resourceful REST API. Typically, they're used to mock services but can be used for anything.
  3. In the config, define an array called `mocks`. Each mock definition maps a <code>[route](http://expressjs.com/guide/routing.html#route-paths)</code> to a `response`. A simple home page:
  4. ```json
  5. {
  6. "mocks": [
  7. {
  8. "route": "/",
  9. "response": {
  10. "body": "<h1>Welcome to the Mock Responses example</h1>"
  11. }
  12. }
  13. ]
  14. }
  15. ```
  16. Under the hood, the property values from the `response` object are written onto the underlying [koa response object](https://github.com/koajs/koa/blob/master/docs/api/response.md). You can set any valid koa response properies, for example [type](https://github.com/koajs/koa/blob/master/docs/api/response.md#responsetype-1):
  17. ```json
  18. {
  19. "mocks": [
  20. {
  21. "route": "/",
  22. "response": {
  23. "type": "text/plain",
  24. "body": "<h1>Welcome to the Mock Responses example</h1>"
  25. }
  26. }
  27. ]
  28. }
  29. ```
  30. ### Conditional Response
  31. To define a conditional response, set a `request` object on the mock definition. The `request` value acts as a query - the response defined will only be returned if each property of the `request` query matches. For example, return an XML response *only* if the request headers include `accept: application/xml`, else return 404 Not Found.
  32. ```json
  33. {
  34. "mocks": [
  35. {
  36. "route": "/two",
  37. "request": { "accepts": "xml" },
  38. "response": {
  39. "body": "<result id='2' name='whatever' />"
  40. }
  41. }
  42. ]
  43. }
  44. ```
  45. ### Multiple Potential Responses
  46. To specify multiple potential responses, set an array of mock definitions to the `responses` property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:
  47. ```json
  48. {
  49. "mocks": [
  50. {
  51. "route": "/three",
  52. "responses": [
  53. {
  54. "request": { "method": "GET" },
  55. "response": {
  56. "body": "<h1>Mock response for 'GET' request on /three</h1>"
  57. }
  58. },
  59. {
  60. "request": { "method": "POST" },
  61. "response": {
  62. "status": 400,
  63. "body": { "message": "That method is not allowed." }
  64. }
  65. }
  66. ]
  67. }
  68. ]
  69. }
  70. ```
  71. ### Dynamic Response
  72. The examples above all returned static data. To define a dynamic response, create a mock module. Specify its path in the `module` property:
  73. ```json
  74. {
  75. "mocks": [
  76. {
  77. "route": "/four",
  78. "module": "/mocks/stream-self.js"
  79. }
  80. ]
  81. }
  82. ```
  83. Here's what the `stream-self` module looks like. The module should export a mock definition (an object, or array of objects, each with a `response` and optional `request`). In this example, the module simply streams itself to the response but you could set `body` to *any* [valid value](https://github.com/koajs/koa/blob/master/docs/api/response.md#responsebody-1).
  84. ```js
  85. const fs = require('fs')
  86. module.exports = {
  87. response: {
  88. body: fs.createReadStream(__filename)
  89. }
  90. }
  91. ```
  92. ### Response function
  93. For more power, define the response as a function. It will receive the [koa context](https://github.com/koajs/koa/blob/master/docs/api/context.md) as its first argument. Now you have full programmatic control over the response returned.
  94. ```js
  95. module.exports = {
  96. response: function (ctx) {
  97. ctx.body = '<h1>I can do anything i want.</h1>'
  98. }
  99. }
  100. ```
  101. If the route contains tokens, their values are passed to the response. For example, with this mock...
  102. ```json
  103. {
  104. "mocks": [
  105. {
  106. "route": "/players/:id",
  107. "module": "/mocks/players.js"
  108. }
  109. ]
  110. }
  111. ```
  112. ...the `id` value is passed to the `response` function. For example, a path of `/players/10?name=Lionel` would pass `10` to the response function. Additional, the value `Lionel` would be available on `ctx.query.name`:
  113. ```js
  114. module.exports = {
  115. response: function (ctx, id) {
  116. ctx.body = `<h1>id: ${id}, name: ${ctx.query.name}</h1>`
  117. }
  118. }
  119. ```
  120. ### RESTful Resource example
  121. Here's an example of a REST collection (users). We'll create two routes, one for actions on the resource collection, one for individual resource actions.
  122. ```json
  123. {
  124. "mocks": [
  125. { "route": "/users", "module": "/mocks/users.js" },
  126. { "route": "/users/:id", "module": "/mocks/user.js" }
  127. ]
  128. }
  129. ```
  130. Define a module (`users.json`) defining seed data:
  131. ```json
  132. [
  133. { "id": 1, "name": "Lloyd", "age": 40, "nationality": "English" },
  134. { "id": 2, "name": "Mona", "age": 34, "nationality": "Palestinian" },
  135. { "id": 3, "name": "Francesco", "age": 24, "nationality": "Italian" }
  136. ]
  137. ```
  138. The collection module:
  139. ```js
  140. const users = require('./users.json')
  141. /* responses for /users */
  142. const mockResponses = [
  143. /* Respond with 400 Bad Request for PUT and DELETE - inappropriate on a collection */
  144. { request: { method: 'PUT' }, response: { status: 400 } },
  145. { request: { method: 'DELETE' }, response: { status: 400 } },
  146. {
  147. /* for GET requests return a subset of data, optionally filtered on 'minAge' and 'nationality' */
  148. request: { method: 'GET' },
  149. response: function (ctx) {
  150. ctx.body = users.filter(user => {
  151. const meetsMinAge = (user.age || 1000) >= (Number(ctx.query.minAge) || 0)
  152. const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
  153. return meetsMinAge && requiredNationality
  154. })
  155. }
  156. },
  157. {
  158. /* for POST requests, create a new user and return the path to the new resource */
  159. request: { method: 'POST' },
  160. response: function (ctx) {
  161. const newUser = ctx.request.body
  162. users.push(newUser)
  163. newUser.id = users.length
  164. ctx.status = 201
  165. ctx.response.set('Location', `/users/${newUser.id}`)
  166. }
  167. }
  168. ]
  169. module.exports = mockResponses
  170. ```
  171. The individual resource module:
  172. ```js
  173. const users = require('./users.json')
  174. /* responses for /users/:id */
  175. const mockResponses = [
  176. /* don't support POST here */
  177. { request: { method: 'POST' }, response: { status: 400 } },
  178. /* for GET requests, return a particular user */
  179. {
  180. request: { method: 'GET' },
  181. response: function (ctx, id) {
  182. ctx.body = users.find(user => user.id === Number(id))
  183. }
  184. },
  185. /* for PUT requests, update the record */
  186. {
  187. request: { method: 'PUT' },
  188. response: function (ctx, id) {
  189. const updatedUser = ctx.request.body
  190. const existingUserIndex = users.findIndex(user => user.id === Number(id))
  191. users.splice(existingUserIndex, 1, updatedUser)
  192. ctx.status = 200
  193. }
  194. },
  195. /* DELETE request: remove the record */
  196. {
  197. request: { method: 'DELETE' },
  198. response: function (ctx, id) {
  199. const existingUserIndex = users.findIndex(user => user.id === Number(id))
  200. users.splice(existingUserIndex, 1)
  201. ctx.status = 200
  202. }
  203. }
  204. ]
  205. module.exports = mockResponses
  206. ```
  207. [Example](https://github.com/lwsjs/local-web-server/tree/master/example/mock).